diff --git a/tests/basic.rs b/tests/basic.rs index 304f0dd..f1e7d7f 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -7,7 +7,7 @@ use itertools::Itertools; #[test] fn test_can_compile() { let td = assert_fs::TempDir::new().unwrap(); - let mut cmd = fixup(&td); + let mut cmd = fixup(&td).unwrap(); let ex = cmd.arg("--help").output().unwrap(); let out = String::from_utf8(ex.stdout).unwrap(); let err = String::from_utf8(ex.stderr).unwrap(); @@ -22,16 +22,16 @@ fn test_can_compile() { #[test] fn straightforward() { let td = assert_fs::TempDir::new().unwrap(); - git_init(&td); + git_init(&td).unwrap(); - git_file_commit("a", &td); - git_file_commit("b", &td); - git(&["checkout", "-b", "changes", "HEAD~"], &td); + git_file_commit("a", &td).unwrap(); + git_file_commit("b", &td).unwrap(); + git(&["checkout", "-b", "changes", "HEAD~"], &td).unwrap(); for n in &["c", "d", "e"] { - git_file_commit(n, &td); + git_file_commit(n, &td).unwrap(); } - let out = git_log(&td); + let out = git_log(&td).unwrap(); assert_eq!( out, "\ @@ -47,16 +47,18 @@ fn straightforward() { ); td.child("new").touch().unwrap(); - git(&["add", "new"], &td); + git(&["add", "new"], &td).unwrap(); - fixup(&td).args(["-P", "d"]).output().unwrap(); + fixup(&td).unwrap().args(["-P", "d"]).output().unwrap(); let shown = git_out( &["diff-tree", "--no-commit-id", "--name-only", "-r", ":/d"], &td, - ); - let files = string(shown.stdout); - let err = string(shown.stderr); + ) + .unwrap(); + + let files = String::from_utf8(shown.stdout).unwrap(); + let err = String::from_utf8(shown.stderr).unwrap(); assert_eq!( files, @@ -75,11 +77,11 @@ fn uses_merge_base_for_all_defaults() { for branch in ["main", "develop", "trunk", "master"] { eprintln!("testing branch {branch}"); let td = assert_fs::TempDir::new().unwrap(); - git_init_default_branch_name(branch, &td); + git_init_default_branch_name(branch, &td).unwrap(); - git_commits(&["a", "b", "c", "d"], &td); - git(&["checkout", "-b", "changes", ":/c"], &td); - git_commits(&["f", "g"], &td); + git_commits(&["a", "b", "c", "d"], &td).unwrap(); + git(&["checkout", "-b", "changes", ":/c"], &td).unwrap(); + git_commits(&["f", "g"], &td).unwrap(); let expected = format!( "\ @@ -92,7 +94,7 @@ fn uses_merge_base_for_all_defaults() { * a " ); - let actual = git_log(&td); + let actual = git_log(&td).unwrap(); assert_eq!( expected, actual, "expected:\n{}\nactual:\n{}", @@ -102,26 +104,26 @@ fn uses_merge_base_for_all_defaults() { // commits *before* the merge base of a default branch don't get found by // default td.child("new").touch().unwrap(); - git(&["add", "new"], &td); - fixup(&td).args(["-P", "b"]).assert().failure(); + git(&["add", "new"], &td).unwrap(); + fixup(&td).unwrap().args(["-P", "b"]).assert().failure(); // commits *after* the merge base of a default branch *do* get found by default - git(&["reset", "HEAD~"], &td); - git(&["add", "new"], &td); - fixup(&td).args(["-P", "f"]).assert().success(); + git(&["reset", "HEAD~"], &td).unwrap(); + git(&["add", "new"], &td).unwrap(); + fixup(&td).unwrap().args(["-P", "f"]).assert().success(); } } #[test] fn simple_straightline_commits() { let td = assert_fs::TempDir::new().unwrap(); - git_init(&td); + git_init(&td).unwrap(); - git_commits(&["a", "b"], &td); - git(&["checkout", "-b", "changes"], &td); - git(&["branch", "-u", "main"], &td); - git_commits(&["target", "d"], &td); + git_commits(&["a", "b"], &td).unwrap(); + git(&["checkout", "-b", "changes"], &td).unwrap(); + git(&["branch", "-u", "main"], &td).unwrap(); + git_commits(&["target", "d"], &td).unwrap(); - let log = git_log(&td); + let log = git_log(&td).unwrap(); assert_eq!( log, "\ @@ -135,11 +137,15 @@ fn simple_straightline_commits() { ); td.child("new").touch().unwrap(); - git(&["add", "new"], &td); + git(&["add", "new"], &td).unwrap(); - fixup(&td).args(["-P", "target"]).assert().success(); + fixup(&td) + .unwrap() + .args(["-P", "target"]) + .assert() + .success(); - let (files, err) = git_changed_files("target", &td); + let (files, err) = git_changed_files("target", &td).unwrap(); assert_eq!( files, @@ -156,16 +162,16 @@ new #[test] fn simple_straightline_tag_and_reference() { let td = assert_fs::TempDir::new().unwrap(); - git_init(&td); + git_init(&td).unwrap(); let base = "v0.1.0"; - git_commits(&["a", "b"], &td); - git(&["tag", base], &td); - git(&["checkout", "-b", "changes"], &td); - git_commits(&["target", "d"], &td); + git_commits(&["a", "b"], &td).unwrap(); + git(&["tag", base], &td).unwrap(); + git(&["checkout", "-b", "changes"], &td).unwrap(); + git_commits(&["target", "d"], &td).unwrap(); - let log = git_log(&td); + let log = git_log(&td).unwrap(); assert_eq!( log, "\ @@ -179,14 +185,15 @@ fn simple_straightline_tag_and_reference() { ); td.child("new").touch().unwrap(); - git(&["add", "new"], &td); + git(&["add", "new"], &td).unwrap(); fixup(&td) + .unwrap() .args(["-P", "target", "-u", base]) .assert() .success(); - let (files, err) = git_changed_files("target", &td); + let (files, err) = git_changed_files("target", &td).unwrap(); assert_eq!( files, @@ -202,12 +209,13 @@ new // also check that we can use the full refspec definition td.child("new-full-ref").touch().unwrap(); - git(&["add", "new-full-ref"], &td); + git(&["add", "new-full-ref"], &td).unwrap(); fixup(&td) + .unwrap() .args(["-P", "target", "-u", &format!("refs/tags/{base}")]) .unwrap(); - let (files, err) = git_changed_files("target", &td); + let (files, err) = git_changed_files("target", &td).unwrap(); assert_eq!( files, "\ @@ -224,22 +232,22 @@ new-full-ref #[test] fn simple_straightline_remote_branch() { let remote_td = assert_fs::TempDir::new().unwrap(); - git_init(&remote_td); - git_commits(&["a", "b"], &remote_td); + git_init(&remote_td).unwrap(); + git_commits(&["a", "b"], &remote_td).unwrap(); let td = assert_fs::TempDir::new().unwrap(); - git_init(&td); + git_init(&td).unwrap(); let remote_path = &remote_td .path() .as_os_str() .to_owned() .into_string() .unwrap(); - git(&["remote", "add", "origin", remote_path], &td); - git(&["pull", "origin", "main:main"], &td); - git_commits(&["target", "d"], &td); + git(&["remote", "add", "origin", remote_path], &td).unwrap(); + git(&["pull", "origin", "main:main"], &td).unwrap(); + git_commits(&["target", "d"], &td).unwrap(); - let log = git_log(&td); + let log = git_log(&td).unwrap(); assert_eq!( log, "\ @@ -253,14 +261,15 @@ fn simple_straightline_remote_branch() { ); td.child("new").touch().unwrap(); - git(&["add", "new"], &td); + git(&["add", "new"], &td).unwrap(); fixup(&td) + .unwrap() .args(["-P", "target", "-u", "origin/main"]) .assert() .success(); - let (files, err) = git_changed_files("target", &td); + let (files, err) = git_changed_files("target", &td).unwrap(); assert_eq!( files, @@ -277,14 +286,14 @@ new #[test] fn stashes_before_rebase() { let td = assert_fs::TempDir::new().unwrap(); - git_init(&td); + git_init(&td).unwrap(); - git_commits(&["a", "b"], &td); - git(&["checkout", "-b", "changes"], &td); - git(&["branch", "-u", "main"], &td); - git_commits(&["target", "d"], &td); + git_commits(&["a", "b"], &td).unwrap(); + git(&["checkout", "-b", "changes"], &td).unwrap(); + git(&["branch", "-u", "main"], &td).unwrap(); + git_commits(&["target", "d"], &td).unwrap(); - let log = git_log(&td); + let log = git_log(&td).unwrap(); assert_eq!( log, "\ @@ -302,13 +311,17 @@ fn stashes_before_rebase() { let edited_file = "file_d"; td.child(edited_file).write_str("somthing").unwrap(); - git(&["add", "new"], &td); - let tracked_changed_files = git_worktree_changed_files(&td); + git(&["add", "new"], &td).unwrap(); + let tracked_changed_files = git_worktree_changed_files(&td).unwrap(); assert_eq!(tracked_changed_files.trim(), edited_file); - fixup(&td).args(["-P", "target"]).assert().success(); + fixup(&td) + .unwrap() + .args(["-P", "target"]) + .assert() + .success(); - let (files, err) = git_changed_files("target", &td); + let (files, err) = git_changed_files("target", &td).unwrap(); assert_eq!( files, @@ -321,7 +334,7 @@ new err ); - let popped_stashed_files = git_worktree_changed_files(&td); + let popped_stashed_files = git_worktree_changed_files(&td).unwrap(); assert_eq!(popped_stashed_files.trim(), edited_file); } @@ -329,14 +342,14 @@ new fn test_no_commit_in_range() { let td = assert_fs::TempDir::new().unwrap(); eprintln!("tempdir: {:?}", td.path()); - git_init(&td); + git_init(&td).unwrap(); - git_commits(&["a", "b", "c", "d"], &td); - git(&["checkout", "-b", "changes", ":/c"], &td); - git(&["branch", "-u", "main"], &td); - git_commits(&["target", "f", "g"], &td); + git_commits(&["a", "b", "c", "d"], &td).unwrap(); + git(&["checkout", "-b", "changes", ":/c"], &td).unwrap(); + git(&["branch", "-u", "main"], &td).unwrap(); + git_commits(&["target", "f", "g"], &td).unwrap(); - let out = git_log(&td); + let out = git_log(&td).unwrap(); assert_eq!( out, "\ @@ -354,10 +367,10 @@ fn test_no_commit_in_range() { ); td.child("new").touch().unwrap(); - git(&["add", "new"], &td); + git(&["add", "new"], &td).unwrap(); - let assertion = fixup(&td).args(["-P", "b"]).assert().failure(); - let out = string(assertion.get_output().stdout.clone()); + let assertion = fixup(&td).unwrap().args(["-P", "b"]).assert().failure(); + let out = String::from_utf8(assertion.get_output().stdout.clone()).unwrap(); let expected = "No commit contains the pattern"; assert!( out.contains(expected), @@ -366,9 +379,13 @@ fn test_no_commit_in_range() { out ); - fixup(&td).args(["-P", "target"]).assert().success(); + fixup(&td) + .unwrap() + .args(["-P", "target"]) + .assert() + .success(); - let (files, err) = git_changed_files("target", &td); + let (files, err) = git_changed_files("target", &td).unwrap(); assert_eq!( files, @@ -385,15 +402,15 @@ new #[test] fn retarget_branches_in_range() { let td = assert_fs::TempDir::new().unwrap(); - git_init(&td); + git_init(&td).unwrap(); - git_commits(&["a", "b"], &td); - git(&["checkout", "-b", "intermediate"], &td); - git_commits(&["target", "c", "d"], &td); - git(&["checkout", "-b", "points-at-intermediate"], &td); + git_commits(&["a", "b"], &td).unwrap(); + git(&["checkout", "-b", "intermediate"], &td).unwrap(); + git_commits(&["target", "c", "d"], &td).unwrap(); + git(&["checkout", "-b", "points-at-intermediate"], &td).unwrap(); - git(&["checkout", "-b", "changes"], &td); - git_commits(&["e", "f"], &td); + git(&["checkout", "-b", "changes"], &td).unwrap(); + git_commits(&["e", "f"], &td).unwrap(); let expected = "\ * f HEAD -> changes @@ -404,15 +421,19 @@ fn retarget_branches_in_range() { * b main * a "; - let out = git_log(&td); + let out = git_log(&td).unwrap(); assert_eq!(out, expected, "log:\n{}\nexpected:\n{}", out, expected); td.child("new").touch().unwrap(); - git(&["add", "new"], &td); + git(&["add", "new"], &td).unwrap(); - fixup(&td).args(["-P", "target"]).assert().success(); + fixup(&td) + .unwrap() + .args(["-P", "target"]) + .assert() + .success(); - let (files, err) = git_changed_files("target", &td); + let (files, err) = git_changed_files("target", &td).unwrap(); assert_eq!( files, @@ -426,21 +447,21 @@ new ); // should be identical to before - let out = git_log(&td); + let out = git_log(&td).unwrap(); assert_eq!(out, expected, "\nactual:\n{}\nexpected:\n{}", out, expected); } #[test] fn retarget_branch_target_of_edit() { let td = assert_fs::TempDir::new().unwrap(); - git_init(&td); + git_init(&td).unwrap(); - git_commits(&["a", "b"], &td); - git(&["checkout", "-b", "intermediate"], &td); - git_commits(&["c", "d", "target"], &td); + git_commits(&["a", "b"], &td).unwrap(); + git(&["checkout", "-b", "intermediate"], &td).unwrap(); + git_commits(&["c", "d", "target"], &td).unwrap(); - git(&["checkout", "-b", "changes"], &td); - git_commits(&["e", "f"], &td); + git(&["checkout", "-b", "changes"], &td).unwrap(); + git_commits(&["e", "f"], &td).unwrap(); let expected = "\ * f HEAD -> changes @@ -451,7 +472,7 @@ fn retarget_branch_target_of_edit() { * b main * a "; - let out = git_log(&td); + let out = git_log(&td).unwrap(); assert_eq!( out, expected, "before rebase:\nactual:\n{}\nexpected:\n{}", @@ -459,18 +480,22 @@ fn retarget_branch_target_of_edit() { ); td.child("new").touch().unwrap(); - git(&["add", "new"], &td); + git(&["add", "new"], &td).unwrap(); - fixup(&td).args(["-P", "target"]).assert().success(); + fixup(&td) + .unwrap() + .args(["-P", "target"]) + .assert() + .success(); - let out = git_log(&td); + let out = git_log(&td).unwrap(); assert_eq!( out, expected, "after rebase\nactual:\n{}\nexpected:\n{}", out, expected ); - let (files, err) = git_changed_files("target", &td); + let (files, err) = git_changed_files("target", &td).unwrap(); assert_eq!( files, "\ @@ -483,38 +508,42 @@ new ); // should be identical to before - let out = git_log(&td); + let out = git_log(&td).unwrap(); assert_eq!(out, expected, "\nactual:\n{}\nexpected:\n{}", out, expected); } /////////////////////////////////////////////////////////////////////////////// // Helpers -fn git_commits(ids: &[&str], tempdir: &assert_fs::TempDir) { +fn git_commits(ids: &[&str], tempdir: &assert_fs::TempDir) -> anyhow::Result<()> { for n in ids { - git_file_commit(n, tempdir); + git_file_commit(n, tempdir)?; } + Ok(()) } -fn git_init(tempdir: &assert_fs::TempDir) { - git_init_default_branch_name("main", tempdir) +fn git_init(tempdir: &assert_fs::TempDir) -> anyhow::Result<()> { + git_init_default_branch_name("main", tempdir)?; + Ok(()) } -fn git_init_default_branch_name(name: &str, tempdir: &assert_fs::TempDir) { - git(&["init", "--initial-branch", name], tempdir); - git(&["config", "user.email", "nobody@nowhere.com"], tempdir); - git(&["config", "user.name", "nobody"], tempdir); +fn git_init_default_branch_name(name: &str, tempdir: &assert_fs::TempDir) -> anyhow::Result<()> { + git(&["init", "--initial-branch", name], tempdir)?; + git(&["config", "user.email", "nobody@nowhere.com"], tempdir)?; + git(&["config", "user.name", "nobody"], tempdir)?; + Ok(()) } /// Create a file and commit it with a mesage that is just the name of the file -fn git_file_commit(name: &str, tempdir: &assert_fs::TempDir) { - tempdir.child(format!("file_{}", name)).touch().unwrap(); - git(&["add", "-A"], tempdir); - git(&["commit", "-m", name], tempdir); +fn git_file_commit(name: &str, tempdir: &assert_fs::TempDir) -> anyhow::Result<()> { + tempdir.child(format!("file_{}", name)).touch()?; + git(&["add", "-A"], tempdir)?; + git(&["commit", "-m", name], tempdir)?; + Ok(()) } /// Get the git shown output for the target commit -fn git_changed_files(name: &str, tempdir: &assert_fs::TempDir) -> (String, String) { +fn git_changed_files(name: &str, tempdir: &assert_fs::TempDir) -> anyhow::Result<(String, String)> { let out = git_out( &[ "diff-tree", @@ -524,40 +553,40 @@ fn git_changed_files(name: &str, tempdir: &assert_fs::TempDir) -> (String, Strin &format!(":/{}", name), ], tempdir, - ); - (string(out.stdout), string(out.stderr)) + )?; + Ok(( + String::from_utf8(out.stdout)?, + String::from_utf8(out.stderr)?, + )) } -fn git_worktree_changed_files(td: &assert_fs::TempDir) -> String { - string(git_out(&["diff", "--name-only"], td).stdout) +fn git_worktree_changed_files(td: &assert_fs::TempDir) -> anyhow::Result { + Ok(String::from_utf8( + git_out(&["diff", "--name-only"], td)?.stdout, + )?) } /// Run git in tempdir with args and panic if theres an error -fn git(args: &[&str], tempdir: &assert_fs::TempDir) { - git_inner(args, tempdir).ok().unwrap(); +fn git(args: &[&str], tempdir: &assert_fs::TempDir) -> anyhow::Result<()> { + git_inner(args, tempdir).ok()?; + Ok(()) } -fn git_out(args: &[&str], tempdir: &assert_fs::TempDir) -> Output { - git_inner(args, tempdir).output().unwrap() +fn git_out(args: &[&str], tempdir: &assert_fs::TempDir) -> anyhow::Result { + Ok(git_inner(args, tempdir).output()?) } -fn git_log(tempdir: &assert_fs::TempDir) -> String { +fn git_log(tempdir: &assert_fs::TempDir) -> anyhow::Result { let mut s = String::from_utf8( git_inner(&["log", "--all", "--format=%s %D", "--graph"], tempdir) - .output() - .unwrap() + .output()? .stdout, - ) - .unwrap() + )? .lines() .map(|l| l.trim_end()) .join("\n"); s.push('\n'); - s -} - -fn string(from: Vec) -> String { - String::from_utf8(from).unwrap() + Ok(s) } fn git_inner(args: &[&str], tempdir: &assert_fs::TempDir) -> Command { @@ -567,9 +596,9 @@ fn git_inner(args: &[&str], tempdir: &assert_fs::TempDir) -> Command { } /// Get something that can get args added to it -fn fixup(dir: &assert_fs::TempDir) -> Command { - let mut c = Command::cargo_bin("git-instafix").unwrap(); +fn fixup(dir: &assert_fs::TempDir) -> anyhow::Result { + let mut c = Command::cargo_bin("git-instafix")?; c.current_dir(dir.path()) .env_remove("GIT_INSTAFIX_UPSTREAM"); - c + Ok(c) }