repo: Simplify Git integration tests

5bea479dd9e98c0306a6e6aa492faca234e3e336
Use a purpose-built repository fixture so each integration test expresses
only the repository history and Git configuration that it needs.

Assisted-by: Codex:gpt-5.6-sol
Alexis Sellier committed ago 1 parent a5567d2e
crates/repo/src/git.rs +153 -207
1049 1049
1050 1050
#[cfg(test)]
1051 1051
mod tests {
1052 1052
    use super::*;
1053 1053
1054 -
    /// Runs a Git subcommand in `repository` and requires success.
1055 -
    fn git(repository: &Repository, subcommand: &str, args: &[&str]) {
1056 -
        let output = repository
1057 -
            .run(
1058 -
                subcommand,
1059 -
                args,
1060 -
                Opts {
1061 -
                    env: &[
1062 -
                        ("GIT_AUTHOR_NAME", "Rustik Test"),
1063 -
                        ("GIT_AUTHOR_EMAIL", "rustik@example.com"),
1064 -
                        ("GIT_COMMITTER_NAME", "Rustik Test"),
1065 -
                        ("GIT_COMMITTER_EMAIL", "rustik@example.com"),
1066 -
                    ],
1067 -
                    ..Opts::default()
1068 -
                },
1069 -
            )
1070 -
            .unwrap();
1071 -
        assert!(output.status.success());
1054 +
    /// Temporary Git repository removed when its test finishes.
1055 +
    struct TestRepository {
1056 +
        /// Repository working tree.
1057 +
        path: PathBuf,
1058 +
        /// Repository API under test.
1059 +
        repository: Repository,
1060 +
    }
1061 +
1062 +
    impl TestRepository {
1063 +
        /// Creates a repository with Git's default initial branch.
1064 +
        fn new() -> Self {
1065 +
            Self::init(&[])
1066 +
        }
1067 +
1068 +
        /// Creates a repository with `main` as its initial branch.
1069 +
        fn with_main_branch() -> Self {
1070 +
            Self::init(&["-b", "main"])
1071 +
        }
1072 +
1073 +
        /// Creates a repository with the requested object format.
1074 +
        fn with_object_format(object_format: &str) -> Self {
1075 +
            let format = format!("--object-format={object_format}");
1076 +
            Self::init(&[&format])
1077 +
        }
1078 +
1079 +
        /// Initializes a repository with additional `git init` arguments.
1080 +
        fn init(args: &[&str]) -> Self {
1081 +
            let nonce = SystemTime::now()
1082 +
                .duration_since(UNIX_EPOCH)
1083 +
                .unwrap()
1084 +
                .as_nanos();
1085 +
            let path = std::env::temp_dir()
1086 +
                .join(format!("rustik-repo-test-{}-{nonce}", std::process::id()));
1087 +
            let status = Command::new("git")
1088 +
                .args(["init", "-q"])
1089 +
                .args(args)
1090 +
                .arg(&path)
1091 +
                .status()
1092 +
                .unwrap();
1093 +
            assert!(status.success());
1094 +
            let repository = Repository::discover(path.clone()).unwrap();
1095 +
1096 +
            Self { path, repository }
1097 +
        }
1098 +
1099 +
        /// Returns the repository working tree.
1100 +
        fn path(&self) -> &Path {
1101 +
            &self.path
1102 +
        }
1103 +
1104 +
        /// Writes a working-tree file.
1105 +
        fn write(&self, path: &str, contents: &str) {
1106 +
            fs::write(self.path.join(path), contents).unwrap();
1107 +
        }
1108 +
1109 +
        /// Creates an empty commit.
1110 +
        fn commit_empty(&self, message: &str) {
1111 +
            self.git("commit", &["-q", "--allow-empty", "-m", message]);
1112 +
        }
1113 +
1114 +
        /// Commits the selected working-tree files.
1115 +
        fn commit(&self, paths: &[&str], message: &str) {
1116 +
            self.git("add", paths);
1117 +
            self.git("commit", &["-q", "-m", message]);
1118 +
        }
1119 +
1120 +
        /// Writes and commits one file.
1121 +
        fn commit_file(&self, path: &str, contents: &str, message: &str) {
1122 +
            self.write(path, contents);
1123 +
            self.commit(&[path], message);
1124 +
        }
1125 +
1126 +
        /// Runs a Git subcommand and requires success.
1127 +
        fn git(&self, subcommand: &str, args: &[&str]) {
1128 +
            let output = self
1129 +
                .repository
1130 +
                .run(
1131 +
                    subcommand,
1132 +
                    args,
1133 +
                    Opts {
1134 +
                        env: &[
1135 +
                            ("GIT_AUTHOR_NAME", "Rustik Test"),
1136 +
                            ("GIT_AUTHOR_EMAIL", "rustik@example.com"),
1137 +
                            ("GIT_COMMITTER_NAME", "Rustik Test"),
1138 +
                            ("GIT_COMMITTER_EMAIL", "rustik@example.com"),
1139 +
                        ],
1140 +
                        ..Opts::default()
1141 +
                    },
1142 +
                )
1143 +
                .unwrap();
1144 +
            assert!(output.status.success());
1145 +
        }
1146 +
    }
1147 +
1148 +
    impl std::ops::Deref for TestRepository {
1149 +
        type Target = Repository;
1150 +
1151 +
        fn deref(&self) -> &Self::Target {
1152 +
            &self.repository
1153 +
        }
1154 +
    }
1155 +
1156 +
    impl Drop for TestRepository {
1157 +
        fn drop(&mut self) {
1158 +
            fs::remove_dir_all(&self.path).unwrap();
1159 +
        }
1072 1160
    }
1073 1161
1074 1162
    #[test]
1075 1163
    fn branch_base_prefers_non_trunk_upstream() {
1076 -
        let directory = TempIndex::new().unwrap();
1077 -
        let repository_path = directory.path().parent().unwrap();
1078 -
        let status = Command::new("git")
1079 -
            .args(["init", "-q", "-b", "main"])
1080 -
            .arg(repository_path)
1081 -
            .status()
1082 -
            .unwrap();
1083 -
        assert!(status.success());
1084 -
        let repository = Repository::discover(repository_path.to_owned()).unwrap();
1085 -
        git(
1086 -
            &repository,
1087 -
            "commit",
1088 -
            &["-q", "--allow-empty", "-m", "main"],
1089 -
        );
1090 -
        git(&repository, "switch", &["-q", "-c", "integration"]);
1091 -
        git(
1092 -
            &repository,
1093 -
            "commit",
1094 -
            &["-q", "--allow-empty", "-m", "integration"],
1095 -
        );
1096 -
        git(&repository, "switch", &["-q", "-c", "feature"]);
1097 -
        git(
1098 -
            &repository,
1099 -
            "branch",
1100 -
            &["--set-upstream-to=integration", "feature"],
1101 -
        );
1102 -
        git(
1103 -
            &repository,
1104 -
            "commit",
1105 -
            &["-q", "--allow-empty", "-m", "feature"],
1106 -
        );
1107 -
        git(&repository, "branch", &["aaa"]);
1164 +
        let repository = TestRepository::with_main_branch();
1165 +
        repository.commit_empty("main");
1166 +
        repository.git("switch", &["-q", "-c", "integration"]);
1167 +
        repository.commit_empty("integration");
1168 +
        repository.git("switch", &["-q", "-c", "feature"]);
1169 +
        repository.git("branch", &["--set-upstream-to=integration", "feature"]);
1170 +
        repository.commit_empty("feature");
1171 +
        repository.git("branch", &["aaa"]);
1108 1172
        let feature = repository
1109 1173
            .commit_id(&Rev::try_from("feature").unwrap())
1110 1174
            .unwrap();
1111 1175
        let target = Rev::from(feature);
1112 1176
        let integration = Rev::try_from("integration").unwrap();
1117 1181
        );
1118 1182
    }
1119 1183
1120 1184
    #[test]
1121 1185
    fn branch_base_ignores_missing_upstream() {
1122 -
        let directory = TempIndex::new().unwrap();
1123 -
        let repository_path = directory.path().parent().unwrap();
1124 -
        let status = Command::new("git")
1125 -
            .args(["init", "-q", "-b", "main"])
1126 -
            .arg(repository_path)
1127 -
            .status()
1128 -
            .unwrap();
1129 -
        assert!(status.success());
1130 -
        let repository = Repository::discover(repository_path.to_owned()).unwrap();
1131 -
        git(
1132 -
            &repository,
1133 -
            "commit",
1134 -
            &["-q", "--allow-empty", "-m", "main"],
1135 -
        );
1136 -
        git(&repository, "switch", &["-q", "-c", "feature"]);
1137 -
        git(
1138 -
            &repository,
1139 -
            "commit",
1140 -
            &["-q", "--allow-empty", "-m", "feature"],
1141 -
        );
1142 -
        git(&repository, "remote", &["add", "origin", "."]);
1143 -
        git(
1144 -
            &repository,
1145 -
            "update-ref",
1146 -
            &["refs/remotes/origin/feature", "feature"],
1147 -
        );
1148 -
        git(
1149 -
            &repository,
1150 -
            "branch",
1151 -
            &["--set-upstream-to=origin/feature", "feature"],
1152 -
        );
1153 -
        git(
1154 -
            &repository,
1155 -
            "update-ref",
1156 -
            &["-d", "refs/remotes/origin/feature"],
1157 -
        );
1186 +
        let repository = TestRepository::with_main_branch();
1187 +
        repository.commit_empty("main");
1188 +
        repository.git("switch", &["-q", "-c", "feature"]);
1189 +
        repository.commit_empty("feature");
1190 +
        repository.git("remote", &["add", "origin", "."]);
1191 +
        repository.git("update-ref", &["refs/remotes/origin/feature", "feature"]);
1192 +
        repository.git("branch", &["--set-upstream-to=origin/feature", "feature"]);
1193 +
        repository.git("update-ref", &["-d", "refs/remotes/origin/feature"]);
1158 1194
        assert_eq!(
1159 1195
            repository.config("branch.feature.remote").unwrap(),
1160 1196
            Some("origin".to_owned())
1161 1197
        );
1162 1198
        assert_eq!(
1175 1211
        );
1176 1212
    }
1177 1213
1178 1214
    #[test]
1179 1215
    fn branch_base_missing_exact_upstream_falls_back_to_trunk() {
1180 -
        let directory = TempIndex::new().unwrap();
1181 -
        let repository_path = directory.path().parent().unwrap();
1182 -
        let status = Command::new("git")
1183 -
            .args(["init", "-q", "-b", "main"])
1184 -
            .arg(repository_path)
1185 -
            .status()
1186 -
            .unwrap();
1187 -
        assert!(status.success());
1188 -
        let repository = Repository::discover(repository_path.to_owned()).unwrap();
1189 -
        git(
1190 -
            &repository,
1191 -
            "commit",
1192 -
            &["-q", "--allow-empty", "-m", "main"],
1193 -
        );
1194 -
        git(&repository, "switch", &["-q", "-c", "integration"]);
1195 -
        git(
1196 -
            &repository,
1197 -
            "commit",
1198 -
            &["-q", "--allow-empty", "-m", "integration"],
1199 -
        );
1200 -
        git(&repository, "switch", &["-q", "-c", "feature"]);
1201 -
        git(
1202 -
            &repository,
1203 -
            "commit",
1204 -
            &["-q", "--allow-empty", "-m", "feature"],
1205 -
        );
1206 -
        git(&repository, "remote", &["add", "origin", "."]);
1207 -
        git(
1208 -
            &repository,
1209 -
            "update-ref",
1210 -
            &["refs/remotes/origin/feature", "feature"],
1211 -
        );
1212 -
        git(
1213 -
            &repository,
1214 -
            "branch",
1215 -
            &["--set-upstream-to=origin/feature", "feature"],
1216 -
        );
1217 -
        git(
1218 -
            &repository,
1219 -
            "update-ref",
1220 -
            &["-d", "refs/remotes/origin/feature"],
1221 -
        );
1222 -
        git(&repository, "switch", &["-q", "-c", "container"]);
1223 -
        git(
1224 -
            &repository,
1225 -
            "commit",
1226 -
            &["-q", "--allow-empty", "-m", "container"],
1227 -
        );
1228 -
        git(
1229 -
            &repository,
1230 -
            "branch",
1231 -
            &["--set-upstream-to=integration", "container"],
1232 -
        );
1216 +
        let repository = TestRepository::with_main_branch();
1217 +
        repository.commit_empty("main");
1218 +
        repository.git("switch", &["-q", "-c", "integration"]);
1219 +
        repository.commit_empty("integration");
1220 +
        repository.git("switch", &["-q", "-c", "feature"]);
1221 +
        repository.commit_empty("feature");
1222 +
        repository.git("remote", &["add", "origin", "."]);
1223 +
        repository.git("update-ref", &["refs/remotes/origin/feature", "feature"]);
1224 +
        repository.git("branch", &["--set-upstream-to=origin/feature", "feature"]);
1225 +
        repository.git("update-ref", &["-d", "refs/remotes/origin/feature"]);
1226 +
        repository.git("switch", &["-q", "-c", "container"]);
1227 +
        repository.commit_empty("container");
1228 +
        repository.git("branch", &["--set-upstream-to=integration", "container"]);
1233 1229
        let feature = repository
1234 1230
            .commit_id(&Rev::try_from("feature").unwrap())
1235 1231
            .unwrap();
1236 1232
        let target = Rev::from(feature);
1237 1233
        let main = Rev::try_from("main").unwrap();
1242 1238
        );
1243 1239
    }
1244 1240
1245 1241
    #[test]
1246 1242
    fn branch_base_exact_branch_without_upstream_falls_back_to_trunk() {
1247 -
        let directory = TempIndex::new().unwrap();
1248 -
        let repository_path = directory.path().parent().unwrap();
1249 -
        let status = Command::new("git")
1250 -
            .args(["init", "-q", "-b", "main"])
1251 -
            .arg(repository_path)
1252 -
            .status()
1253 -
            .unwrap();
1254 -
        assert!(status.success());
1255 -
        let repository = Repository::discover(repository_path.to_owned()).unwrap();
1256 -
        git(
1257 -
            &repository,
1258 -
            "commit",
1259 -
            &["-q", "--allow-empty", "-m", "main"],
1260 -
        );
1261 -
        git(&repository, "switch", &["-q", "-c", "feature"]);
1262 -
        git(
1263 -
            &repository,
1264 -
            "commit",
1265 -
            &["-q", "--allow-empty", "-m", "feature"],
1266 -
        );
1267 -
        git(&repository, "switch", &["-q", "-c", "container"]);
1268 -
        git(
1269 -
            &repository,
1270 -
            "commit",
1271 -
            &["-q", "--allow-empty", "-m", "container"],
1272 -
        );
1273 -
        git(&repository, "remote", &["add", "origin", "."]);
1274 -
        git(
1275 -
            &repository,
1243 +
        let repository = TestRepository::with_main_branch();
1244 +
        repository.commit_empty("main");
1245 +
        repository.git("switch", &["-q", "-c", "feature"]);
1246 +
        repository.commit_empty("feature");
1247 +
        repository.git("switch", &["-q", "-c", "container"]);
1248 +
        repository.commit_empty("container");
1249 +
        repository.git("remote", &["add", "origin", "."]);
1250 +
        repository.git(
1276 1251
            "update-ref",
1277 1252
            &["refs/remotes/origin/container", "container"],
1278 1253
        );
1279 -
        git(
1280 -
            &repository,
1254 +
        repository.git(
1281 1255
            "branch",
1282 1256
            &["--set-upstream-to=origin/container", "container"],
1283 1257
        );
1284 1258
        let feature = repository
1285 1259
            .commit_id(&Rev::try_from("feature").unwrap())
1293 1267
        );
1294 1268
    }
1295 1269
1296 1270
    #[test]
1297 1271
    fn commit_stats_uses_merge_first_parent() {
1298 -
        let directory = TempIndex::new().unwrap();
1299 -
        let repository_path = directory.path().parent().unwrap();
1300 -
        let status = Command::new("git")
1301 -
            .args(["init", "-q", "-b", "main"])
1302 -
            .arg(repository_path)
1303 -
            .status()
1304 -
            .unwrap();
1305 -
        assert!(status.success());
1306 -
        let repository = Repository::discover(repository_path.to_owned()).unwrap();
1307 -
        fs::write(repository_path.join("base"), "base\n").unwrap();
1308 -
        git(&repository, "add", &["base"]);
1309 -
        git(&repository, "commit", &["-q", "-m", "base"]);
1310 -
        git(&repository, "switch", &["-q", "-c", "feature"]);
1311 -
        fs::write(repository_path.join("feature"), "first\nsecond\n").unwrap();
1312 -
        git(&repository, "add", &["feature"]);
1313 -
        git(&repository, "commit", &["-q", "-m", "feature"]);
1314 -
        git(&repository, "switch", &["-q", "main"]);
1315 -
        fs::write(repository_path.join("main"), "main\n").unwrap();
1316 -
        git(&repository, "add", &["main"]);
1317 -
        git(&repository, "commit", &["-q", "-m", "main"]);
1318 -
        git(
1319 -
            &repository,
1320 -
            "merge",
1321 -
            &["-q", "--no-ff", "feature", "-m", "merge"],
1322 -
        );
1272 +
        let repository = TestRepository::with_main_branch();
1273 +
        repository.commit_file("base", "base\n", "base");
1274 +
        repository.git("switch", &["-q", "-c", "feature"]);
1275 +
        repository.commit_file("feature", "first\nsecond\n", "feature");
1276 +
        repository.git("switch", &["-q", "main"]);
1277 +
        repository.commit_file("main", "main\n", "main");
1278 +
        repository.git("merge", &["-q", "--no-ff", "feature", "-m", "merge"]);
1323 1279
        let merge = repository
1324 1280
            .commit_id(&Rev::try_from("HEAD").unwrap())
1325 1281
            .unwrap();
1326 1282
1327 1283
        assert_eq!(
1334 1290
        );
1335 1291
    }
1336 1292
1337 1293
    #[test]
1338 1294
    fn commit_diff_returns_root_commit_file() {
1339 -
        let directory = TempIndex::new().unwrap();
1340 -
        let repository_path = directory.path().parent().unwrap();
1341 -
        let status = Command::new("git")
1342 -
            .args(["init", "-q"])
1343 -
            .arg(repository_path)
1344 -
            .status()
1345 -
            .unwrap();
1346 -
        assert!(status.success());
1347 -
        let repository = Repository::discover(repository_path.to_owned()).unwrap();
1348 -
        fs::write(repository_path.join("root"), "root\n").unwrap();
1349 -
        git(&repository, "add", &["root"]);
1350 -
        git(&repository, "commit", &["-q", "-m", "root"]);
1295 +
        let repository = TestRepository::new();
1296 +
        repository.commit_file("root", "root\n", "root");
1351 1297
        let root = repository
1352 1298
            .commit_id(&Rev::try_from("HEAD").unwrap())
1353 1299
            .unwrap();
1354 1300
1355 1301
        let diff = utf8(repository.commit_diff(root).unwrap()).unwrap();
root\nnew file mode")); +0 -0
file\n")); +0 -0
file\n"), +0 -0
file\n"), +0 -0
a\n--- a/a\n+++ b/a\n@@ -1 +1 @@\n-old\n+new\n".to_owned(), +0 -0
b\n--- a/b\n+++ b/b\n@@ -0,0 +1 @@\n+line\n".to_owned(), +0 -0