Git tests merge into fewer fixtures and drop duplicate checks
Browse files at this commitbffc1945f8e4393e4751fa0bfefbab21f70ff266Parent: 36ee1fa- test
- git tests merge into fewer fixtures and drop duplicate checks
2 changed files +31 -59
- crates/forge-git/src/pulls.rs +2 -12
- crates/forge-git/src/search.rs +29 -47
crates/forge-git/src/pulls.rsmodified+2 -12
| @@ -1278,13 +1278,14 @@ mod tests { | |||
| 1278 | } | 1278 | } |
| 1279 | 1279 | ||
| 1280 | #[tokio::test] | 1280 | #[tokio::test] |
| 1281 | - async fn blob_at_rejects_invalid_paths_and_commits() { | 1281 | + async fn invalid_inputs_are_rejected_before_git() { |
| 1282 | let sb = Sandbox::new(); | 1282 | let sb = Sandbox::new(); |
| 1283 | let env = sb.env(); | 1283 | let env = sb.env(); |
| 1284 | let w = sb.repo("repo"); | 1284 | let w = sb.repo("repo"); |
| 1285 | w.write("a.txt", "1\n"); | 1285 | w.write("a.txt", "1\n"); |
| 1286 | let c0 = w.commit("c0"); | 1286 | let c0 = w.commit("c0"); |
| 1287 | let gd = w.git_dir(); | 1287 | let gd = w.git_dir(); |
| 1288 | + let short = "a".repeat(39); | ||
| 1288 | 1289 | ||
| 1289 | for bad in [ | 1290 | for bad in [ |
| 1290 | "", "../x", "a/../x", "/a", "a//b", "a/", "./a", "a/./b", "a\nb", "a\0b", | 1291 | "", "../x", "a/../x", "/a", "a//b", "a/", "./a", "a/./b", "a\nb", "a\0b", |
| @@ -1294,17 +1295,6 @@ mod tests { | |||
| 1294 | } | 1295 | } |
| 1295 | let r = blob_at(&env, &gd, "not-hex", "a.txt").await; | 1296 | let r = blob_at(&env, &gd, "not-hex", "a.txt").await; |
| 1296 | assert!(matches!(r, Err(Error::InvalidInput(_))), "{r:?}"); | 1297 | assert!(matches!(r, Err(Error::InvalidInput(_))), "{r:?}"); |
| 1297 | - } | ||
| 1298 | - | ||
| 1299 | - #[tokio::test] | ||
| 1300 | - async fn invalid_inputs_are_rejected_before_git() { | ||
| 1301 | - let sb = Sandbox::new(); | ||
| 1302 | - let env = sb.env(); | ||
| 1303 | - let w = sb.repo("repo"); | ||
| 1304 | - w.write("a.txt", "1\n"); | ||
| 1305 | - let c0 = w.commit("c0"); | ||
| 1306 | - let gd = w.git_dir(); | ||
| 1307 | - let short = "a".repeat(39); | ||
| 1308 | 1298 | ||
| 1309 | let bad_oid = |r: Result<()>| assert!(matches!(r, Err(Error::InvalidInput(_))), "{r:?}"); | 1299 | let bad_oid = |r: Result<()>| assert!(matches!(r, Err(Error::InvalidInput(_))), "{r:?}"); |
| 1310 | bad_oid(check_merge(&env, &gd, "not-hex", &c0).await.map(|_| ())); | 1300 | bad_oid(check_merge(&env, &gd, "not-hex", &c0).await.map(|_| ())); |
crates/forge-git/src/search.rsmodified+29 -47
| @@ -258,27 +258,25 @@ mod tests { | |||
| 258 | (work.join(".git"), oid) | 258 | (work.join(".git"), oid) |
| 259 | } | 259 | } |
| 260 | 260 | ||
| 261 | + /// Fixed and regex modes, case folding, path globs, the match cap and the | ||
| 262 | + /// empty result share one fixture, so the repo is built once. | ||
| 261 | #[tokio::test] | 263 | #[tokio::test] |
| 262 | - async fn fixed_search_finds_lines_across_files() { | 264 | + async fn grep_modes_and_limits_on_one_fixture() { |
| 263 | let dir = tempfile::tempdir().unwrap(); | 265 | let dir = tempfile::tempdir().unwrap(); |
| 264 | let (repo, oid) = fixture(dir.path()); | 266 | let (repo, oid) = fixture(dir.path()); |
| 265 | - let res = grep(&env(dir.path()), &repo, &oid, "a.b", GrepOptions::default()) | 267 | + let env = env(dir.path()); |
| 268 | + | ||
| 269 | + // Fixed mode: the dot is literal, so only `a.b` matches. | ||
| 270 | + let res = grep(&env, &repo, &oid, "a.b", GrepOptions::default()) | ||
| 266 | .await | 271 | .await |
| 267 | .unwrap(); | 272 | .unwrap(); |
| 268 | - // Fixed mode: the dot is literal, so only `a.b` matches. | 273 | + assert_eq!(res.hits.len(), 1, "fixed"); |
| 269 | - assert_eq!(res.hits.len(), 1); | 274 | + assert_eq!(res.hits[0].path, "src/main.rs", "fixed"); |
| 270 | - assert_eq!(res.hits[0].path, "src/main.rs"); | 275 | + assert_eq!(res.hits[0].line_no, 4, "fixed"); |
| 271 | - assert_eq!(res.hits[0].line_no, 4); | 276 | + assert_eq!(res.hits[0].line, "let a.b = 1;", "fixed"); |
| 272 | - assert_eq!(res.hits[0].line, "let a.b = 1;"); | 277 | + assert!(!res.truncated, "fixed"); |
| 273 | - assert!(!res.truncated); | ||
| 274 | - } | ||
| 275 | - | ||
| 276 | - #[tokio::test] | ||
| 277 | - async fn ignore_case_and_regex_mode() { | ||
| 278 | - let dir = tempfile::tempdir().unwrap(); | ||
| 279 | - let (repo, oid) = fixture(dir.path()); | ||
| 280 | - let env = env(dir.path()); | ||
| 281 | 278 | ||
| 279 | + // Case-sensitive versus ignore-case. | ||
| 282 | let sensitive = grep(&env, &repo, &oid, "hello", GrepOptions::default()) | 280 | let sensitive = grep(&env, &repo, &oid, "hello", GrepOptions::default()) |
| 283 | .await | 281 | .await |
| 284 | .unwrap(); | 282 | .unwrap(); |
| @@ -287,14 +285,14 @@ mod tests { | |||
| 287 | 2, | 285 | 2, |
| 288 | "lib.rs and Guide.md, case-sensitive" | 286 | "lib.rs and Guide.md, case-sensitive" |
| 289 | ); | 287 | ); |
| 290 | - | ||
| 291 | let opts = GrepOptions { | 288 | let opts = GrepOptions { |
| 292 | ignore_case: true, | 289 | ignore_case: true, |
| 293 | ..GrepOptions::default() | 290 | ..GrepOptions::default() |
| 294 | }; | 291 | }; |
| 295 | let insensitive = grep(&env, &repo, &oid, "HELLO", opts).await.unwrap(); | 292 | let insensitive = grep(&env, &repo, &oid, "HELLO", opts).await.unwrap(); |
| 296 | - assert_eq!(insensitive.hits.len(), 3); | 293 | + assert_eq!(insensitive.hits.len(), 3, "ignore case"); |
| 297 | 294 | ||
| 295 | + // Regex mode. | ||
| 298 | let regex = GrepOptions { | 296 | let regex = GrepOptions { |
| 299 | fixed: false, | 297 | fixed: false, |
| 300 | ..GrepOptions::default() | 298 | ..GrepOptions::default() |
| @@ -302,47 +300,31 @@ mod tests { | |||
| 302 | let res = grep(&env, &repo, &oid, "^pub fn \\w+", regex) | 300 | let res = grep(&env, &repo, &oid, "^pub fn \\w+", regex) |
| 303 | .await | 301 | .await |
| 304 | .unwrap(); | 302 | .unwrap(); |
| 305 | - assert_eq!(res.hits.len(), 1); | 303 | + assert_eq!(res.hits.len(), 1, "regex"); |
| 306 | - assert_eq!(res.hits[0].path, "src/lib.rs"); | 304 | + assert_eq!(res.hits[0].path, "src/lib.rs", "regex"); |
| 307 | - } | ||
| 308 | 305 | ||
| 309 | - #[tokio::test] | 306 | + // Path glob limits the search. |
| 310 | - async fn path_glob_limits_search() { | ||
| 311 | - let dir = tempfile::tempdir().unwrap(); | ||
| 312 | - let (repo, oid) = fixture(dir.path()); | ||
| 313 | let opts = GrepOptions { | 307 | let opts = GrepOptions { |
| 314 | path_glob: Some("*.md".into()), | 308 | path_glob: Some("*.md".into()), |
| 315 | ..GrepOptions::default() | 309 | ..GrepOptions::default() |
| 316 | }; | 310 | }; |
| 317 | - let res = grep(&env(dir.path()), &repo, &oid, "hello", opts) | 311 | + let res = grep(&env, &repo, &oid, "hello", opts).await.unwrap(); |
| 318 | - .await | 312 | + assert_eq!(res.hits.len(), 1, "path glob"); |
| 319 | - .unwrap(); | 313 | + assert_eq!(res.hits[0].path, "docs/Guide.md", "path glob"); |
| 320 | - assert_eq!(res.hits.len(), 1); | ||
| 321 | - assert_eq!(res.hits[0].path, "docs/Guide.md"); | ||
| 322 | - } | ||
| 323 | 314 | ||
| 324 | - #[tokio::test] | 315 | + // The match cap truncates. |
| 325 | - async fn truncates_at_max_matches() { | ||
| 326 | - let dir = tempfile::tempdir().unwrap(); | ||
| 327 | - let (repo, oid) = fixture(dir.path()); | ||
| 328 | let opts = GrepOptions { | 316 | let opts = GrepOptions { |
| 329 | ignore_case: true, | 317 | ignore_case: true, |
| 330 | max_matches: 2, | 318 | max_matches: 2, |
| 331 | ..GrepOptions::default() | 319 | ..GrepOptions::default() |
| 332 | }; | 320 | }; |
| 333 | - let res = grep(&env(dir.path()), &repo, &oid, "hello", opts) | 321 | + let res = grep(&env, &repo, &oid, "hello", opts).await.unwrap(); |
| 334 | - .await | 322 | + assert_eq!(res.hits.len(), 2, "max matches"); |
| 335 | - .unwrap(); | 323 | + assert!(res.truncated, "max matches"); |
| 336 | - assert_eq!(res.hits.len(), 2); | ||
| 337 | - assert!(res.truncated); | ||
| 338 | - } | ||
| 339 | 324 | ||
| 340 | - #[tokio::test] | 325 | + // No match is an empty result, not an error. |
| 341 | - async fn no_match_is_empty_not_error() { | ||
| 342 | - let dir = tempfile::tempdir().unwrap(); | ||
| 343 | - let (repo, oid) = fixture(dir.path()); | ||
| 344 | let res = grep( | 326 | let res = grep( |
| 345 | - &env(dir.path()), | 327 | + &env, |
| 346 | &repo, | 328 | &repo, |
| 347 | &oid, | 329 | &oid, |
| 348 | "zzz-not-here", | 330 | "zzz-not-here", |
| @@ -350,8 +332,8 @@ mod tests { | |||
| 350 | ) | 332 | ) |
| 351 | .await | 333 | .await |
| 352 | .unwrap(); | 334 | .unwrap(); |
| 353 | - assert!(res.hits.is_empty()); | 335 | + assert!(res.hits.is_empty(), "no match"); |
| 354 | - assert!(!res.truncated); | 336 | + assert!(!res.truncated, "no match"); |
| 355 | } | 337 | } |
| 356 | 338 | ||
| 357 | #[tokio::test] | 339 | #[tokio::test] |