From bffc1945f8e4393e4751fa0bfefbab21f70ff266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 23 Oct 2026 05:46:06 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20test:=20git=20tests=20merge=20into?= =?UTF-8?q?=20fewer=20fixtures=20and=20drop=20duplicate=20checks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test count in forge-git src went from 68 to 63. The five grep mode and limit tests in search.rs now share one fixture repo (8 to 4 tests). The blob_at rejection loop moves into the invalid input test in pulls.rs, which drops one test and one repo build. Every case and assertion is kept, with a case label in its message. --- crates/forge-git/src/pulls.rs | 14 +------ crates/forge-git/src/search.rs | 76 +++++++++++++--------------------- 2 files changed, 31 insertions(+), 59 deletions(-) diff --git a/crates/forge-git/src/pulls.rs b/crates/forge-git/src/pulls.rs index 054ce71..cbccb1e 100644 --- a/crates/forge-git/src/pulls.rs +++ b/crates/forge-git/src/pulls.rs @@ -1278,13 +1278,14 @@ mod tests { } #[tokio::test] - async fn blob_at_rejects_invalid_paths_and_commits() { + async fn invalid_inputs_are_rejected_before_git() { let sb = Sandbox::new(); let env = sb.env(); let w = sb.repo("repo"); w.write("a.txt", "1\n"); let c0 = w.commit("c0"); let gd = w.git_dir(); + let short = "a".repeat(39); for bad in [ "", "../x", "a/../x", "/a", "a//b", "a/", "./a", "a/./b", "a\nb", "a\0b", @@ -1294,17 +1295,6 @@ mod tests { } let r = blob_at(&env, &gd, "not-hex", "a.txt").await; assert!(matches!(r, Err(Error::InvalidInput(_))), "{r:?}"); - } - - #[tokio::test] - async fn invalid_inputs_are_rejected_before_git() { - let sb = Sandbox::new(); - let env = sb.env(); - let w = sb.repo("repo"); - w.write("a.txt", "1\n"); - let c0 = w.commit("c0"); - let gd = w.git_dir(); - let short = "a".repeat(39); let bad_oid = |r: Result<()>| assert!(matches!(r, Err(Error::InvalidInput(_))), "{r:?}"); bad_oid(check_merge(&env, &gd, "not-hex", &c0).await.map(|_| ())); diff --git a/crates/forge-git/src/search.rs b/crates/forge-git/src/search.rs index b7ae44c..b0f80e6 100644 --- a/crates/forge-git/src/search.rs +++ b/crates/forge-git/src/search.rs @@ -258,27 +258,25 @@ mod tests { (work.join(".git"), oid) } + /// Fixed and regex modes, case folding, path globs, the match cap and the + /// empty result share one fixture, so the repo is built once. #[tokio::test] - async fn fixed_search_finds_lines_across_files() { + async fn grep_modes_and_limits_on_one_fixture() { let dir = tempfile::tempdir().unwrap(); let (repo, oid) = fixture(dir.path()); - let res = grep(&env(dir.path()), &repo, &oid, "a.b", GrepOptions::default()) + let env = env(dir.path()); + + // Fixed mode: the dot is literal, so only `a.b` matches. + let res = grep(&env, &repo, &oid, "a.b", GrepOptions::default()) .await .unwrap(); - // Fixed mode: the dot is literal, so only `a.b` matches. - assert_eq!(res.hits.len(), 1); - assert_eq!(res.hits[0].path, "src/main.rs"); - assert_eq!(res.hits[0].line_no, 4); - assert_eq!(res.hits[0].line, "let a.b = 1;"); - assert!(!res.truncated); - } - - #[tokio::test] - async fn ignore_case_and_regex_mode() { - let dir = tempfile::tempdir().unwrap(); - let (repo, oid) = fixture(dir.path()); - let env = env(dir.path()); + assert_eq!(res.hits.len(), 1, "fixed"); + assert_eq!(res.hits[0].path, "src/main.rs", "fixed"); + assert_eq!(res.hits[0].line_no, 4, "fixed"); + assert_eq!(res.hits[0].line, "let a.b = 1;", "fixed"); + assert!(!res.truncated, "fixed"); + // Case-sensitive versus ignore-case. let sensitive = grep(&env, &repo, &oid, "hello", GrepOptions::default()) .await .unwrap(); @@ -287,14 +285,14 @@ mod tests { 2, "lib.rs and Guide.md, case-sensitive" ); - let opts = GrepOptions { ignore_case: true, ..GrepOptions::default() }; let insensitive = grep(&env, &repo, &oid, "HELLO", opts).await.unwrap(); - assert_eq!(insensitive.hits.len(), 3); + assert_eq!(insensitive.hits.len(), 3, "ignore case"); + // Regex mode. let regex = GrepOptions { fixed: false, ..GrepOptions::default() @@ -302,47 +300,31 @@ mod tests { let res = grep(&env, &repo, &oid, "^pub fn \\w+", regex) .await .unwrap(); - assert_eq!(res.hits.len(), 1); - assert_eq!(res.hits[0].path, "src/lib.rs"); - } + assert_eq!(res.hits.len(), 1, "regex"); + assert_eq!(res.hits[0].path, "src/lib.rs", "regex"); - #[tokio::test] - async fn path_glob_limits_search() { - let dir = tempfile::tempdir().unwrap(); - let (repo, oid) = fixture(dir.path()); + // Path glob limits the search. let opts = GrepOptions { path_glob: Some("*.md".into()), ..GrepOptions::default() }; - let res = grep(&env(dir.path()), &repo, &oid, "hello", opts) - .await - .unwrap(); - assert_eq!(res.hits.len(), 1); - assert_eq!(res.hits[0].path, "docs/Guide.md"); - } + let res = grep(&env, &repo, &oid, "hello", opts).await.unwrap(); + assert_eq!(res.hits.len(), 1, "path glob"); + assert_eq!(res.hits[0].path, "docs/Guide.md", "path glob"); - #[tokio::test] - async fn truncates_at_max_matches() { - let dir = tempfile::tempdir().unwrap(); - let (repo, oid) = fixture(dir.path()); + // The match cap truncates. let opts = GrepOptions { ignore_case: true, max_matches: 2, ..GrepOptions::default() }; - let res = grep(&env(dir.path()), &repo, &oid, "hello", opts) - .await - .unwrap(); - assert_eq!(res.hits.len(), 2); - assert!(res.truncated); - } + let res = grep(&env, &repo, &oid, "hello", opts).await.unwrap(); + assert_eq!(res.hits.len(), 2, "max matches"); + assert!(res.truncated, "max matches"); - #[tokio::test] - async fn no_match_is_empty_not_error() { - let dir = tempfile::tempdir().unwrap(); - let (repo, oid) = fixture(dir.path()); + // No match is an empty result, not an error. let res = grep( - &env(dir.path()), + &env, &repo, &oid, "zzz-not-here", @@ -350,8 +332,8 @@ mod tests { ) .await .unwrap(); - assert!(res.hits.is_empty()); - assert!(!res.truncated); + assert!(res.hits.is_empty(), "no match"); + assert!(!res.truncated, "no match"); } #[tokio::test]