use super::*; struct Fixture(PathBuf); impl Fixture { fn new() -> Self { let root = std::env::temp_dir().join(format!("runestone-git-sync-{}", uuid::Uuid::new_v4())); std::fs::create_dir_all(&root).unwrap(); let fixture = Self(dunce::canonicalize(root).unwrap()); fixture.git( ".", &["init", "--bare", "--initial-branch=main", "remote.git"], ); fixture.git(".", &["clone", "remote.git", "local"]); for directory in ["local"] { fixture.git(directory, &["config", "user.name", "RuneStone Test"]); fixture.git(directory, &["config", "user.email", "test@example.invalid"]); } fixture } fn path(&self, directory: &str) -> PathBuf { self.0.join(directory) } fn git(&self, directory: &str, args: &[&str]) -> String { checked(&self.path(directory), args).unwrap() } fn commit(&self, directory: &str, name: &str, contents: &str) { std::fs::write(self.path(directory).join(name), contents).unwrap(); self.git(directory, &["add", "--", name]); self.git( directory, &[ "-c", "user.name=RuneStone Test", "-c", "user.email=test@example.invalid", "commit", "-m", contents, ], ); } fn seed(&self) { self.commit("local", "first.txt", "Initial commit"); synchronize(&self.path("local"), true).unwrap(); self.git(".", &["clone", "remote.git", "peer"]); } } impl Drop for Fixture { fn drop(&mut self) { let _ = std::fs::remove_dir_all(&self.0); } } #[test] fn push_and_fast_forward_pull_use_only_the_upstream_branch() { let fixture = Fixture::new(); fixture.seed(); fixture.git("local", &["branch", "unpublished"]); fixture.git("local", &["tag", "unpublished-tag"]); fixture.git("local", &["config", "push.default", "matching"]); fixture.git("local", &["config", "remote.origin.mirror", "true"]); fixture.commit("local", "second.txt", "Local change"); synchronize(&fixture.path("local"), true).unwrap(); let refs = fixture.git("remote.git", &["for-each-ref", "--format=%(refname)"]); assert_eq!(refs.trim(), "refs/heads/main"); fixture.git("peer", &["pull", "--ff-only"]); fixture.commit("peer", "peer.txt", "Peer change"); fixture.git("peer", &["push"]); fixture.git("local", &["config", "pull.rebase", "true"]); synchronize(&fixture.path("local"), false).unwrap(); assert_eq!( fixture.git("local", &["rev-parse", "HEAD"]), fixture.git("peer", &["rev-parse", "HEAD"]) ); assert_eq!( std::fs::read_to_string(fixture.path("local").join("peer.txt")).unwrap(), "Peer change" ); } #[test] fn divergent_push_and_pull_preserve_local_and_remote_commits() { let fixture = Fixture::new(); fixture.seed(); fixture.commit("peer", "peer.txt", "Peer change"); fixture.git("peer", &["push"]); fixture.commit("local", "local.txt", "Local change"); fixture.git( "local", &[ "config", "remote.origin.push", "+refs/heads/main:refs/heads/main", ], ); fixture.git("local", &["config", "pull.rebase", "true"]); fixture.git("local", &["config", "pull.ff", "false"]); let local = fixture.git("local", &["rev-parse", "HEAD"]); let remote = fixture.git("remote.git", &["rev-parse", "HEAD"]); assert!(synchronize(&fixture.path("local"), true).is_err()); assert!(synchronize(&fixture.path("local"), false).is_err()); assert_eq!(fixture.git("local", &["rev-parse", "HEAD"]), local); assert_eq!(fixture.git("remote.git", &["rev-parse", "HEAD"]), remote); assert!(!fixture.path("local/.git/MERGE_HEAD").exists()); assert!(!fixture.path("local/.git/rebase-merge").exists()); } #[test] fn dirty_missing_upstream_detached_and_remote_errors_are_clear() { let fixture = Fixture::new(); fixture.seed(); std::fs::write(fixture.path("local/first.txt"), "Unsaved change").unwrap(); fixture.git("local", &["config", "merge.autoStash", "true"]); assert!(synchronize(&fixture.path("local"), false) .unwrap_err() .to_string() .contains("clean working tree")); assert_eq!( std::fs::read_to_string(fixture.path("local/first.txt")).unwrap(), "Unsaved change" ); fixture.git("local", &["branch", "--unset-upstream"]); assert!(synchronize(&fixture.path("local"), true) .unwrap_err() .to_string() .contains("no remote upstream")); fixture.git("local", &["checkout", "--detach"]); assert!(synchronize(&fixture.path("local"), true) .unwrap_err() .to_string() .contains("Select a branch")); fixture.git("local", &["checkout", "main"]); fixture.git("local", &["branch", "--set-upstream-to=origin/main"]); fixture.git( "local", &["remote", "set-url", "origin", "../missing-remote.git"], ); assert!(synchronize(&fixture.path("local"), true) .unwrap_err() .to_string() .contains("Push failed")); } #[test] fn graph_shows_branch_forks_merges_tags_and_detached_history() { let fixture = Fixture::new(); assert!(graph(&fixture.path("local")).unwrap().commits.is_empty()); fixture.seed(); fixture.git("local", &["checkout", "-b", "feature"]); fixture.commit("local", "feature.txt", "Feature commit"); fixture.git("local", &["checkout", "main"]); fixture.commit("local", "main.txt", "Main commit"); fixture.git( "local", &["merge", "--no-ff", "feature", "-m", "Merge feature"], ); fixture.git("local", &["tag", "test-tag"]); let history = graph(&fixture.path("local")).unwrap(); for expected in [ "HEAD -> main", "feature", "test-tag", "Merge feature", "RuneStone Test", ] { assert!(format!("{history:?}").contains(expected), "Missing {expected}: {history:?}"); } let merge = history.commits.iter().find(|commit| commit.summary == "Merge feature").unwrap(); assert_eq!(merge.parents.len(), 2); let details = commit_details(&fixture.path("local"), &merge.id).unwrap(); assert_eq!(details.files.len(), 1); assert_eq!(details.files[0].path, "feature.txt"); assert!(commit_diff(&fixture.path("local"), &merge.id, "feature.txt").unwrap().contains("+Feature commit")); for parent in &merge.parents { assert!(history.commits.iter().any(|commit| &commit.id == parent)); } fixture.git("local", &["checkout", "--detach"]); fixture.commit("local", "detached.txt", "Detached commit"); assert!(graph(&fixture.path("local")) .unwrap() .commits.iter().any(|commit| commit.summary == "Detached commit")); }