|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "os" |
5 | 6 | "os/exec" |
6 | 7 | "path/filepath" |
@@ -338,6 +339,162 @@ func TestClassifyHonorsLegitBaseGitattributes(t *testing.T) { |
338 | 339 | } |
339 | 340 | } |
340 | 341 |
|
| 342 | +// TestClassifyRenameIntoAttrGeneratedTreeStillCounts is the attribute half of |
| 343 | +// the rename anti-gaming guard (the path-based buckets are covered by |
| 344 | +// TestRenameIntoExclusionBucketsStillCounts, which cannot reach this one — the |
| 345 | +// attribute lookup needs a real repo). Renaming a hand-written file INTO a |
| 346 | +// `linguist-generated` tree and editing it in the same commit must not hide the |
| 347 | +// edit: classifying on the destination alone excluded it from both the size |
| 348 | +// count and the --reviewed-diff-out patch fed to the review panel. A rename |
| 349 | +// whose BOTH ends carry the attribute is still excluded, so genuine moves |
| 350 | +// inside a generated tree are unaffected. |
| 351 | +func TestClassifyRenameIntoAttrGeneratedTreeStillCounts(t *testing.T) { |
| 352 | + dir := initTestRepo(t) |
| 353 | + // The two base files must NOT be byte-identical: gen/keep2.json below is an |
| 354 | + // exact-blob rename of gen/keep.json, and if handwritten/x.json carried the |
| 355 | + // same bytes git's exact-rename phase could pair keep2 with the hand-written |
| 356 | + // source instead (neither basename matches, so the tie-break is hashmap |
| 357 | + // order and varies by git version), inverting both OldPath assertions. |
| 358 | + body := strings.Repeat("{\"k\": \"v\"}\n", 40) |
| 359 | + keepBody := strings.Repeat("{\"keep\": \"generated\"}\n", 25) |
| 360 | + writeFile(t, dir, ".gitattributes", "gen/** linguist-generated=true\n") |
| 361 | + writeFile(t, dir, "handwritten/x.json", body) |
| 362 | + writeFile(t, dir, "gen/keep.json", keepBody) |
| 363 | + base := commitAll(t, dir, "base") |
| 364 | + |
| 365 | + // PR head, touching NOTHING under .gitattributes: a hand-written file moves |
| 366 | + // into the generated tree carrying an injected edit, and a genuinely |
| 367 | + // generated file moves within that tree. |
| 368 | + gitRun(t, dir, "mv", "handwritten/x.json", "gen/x.json") |
| 369 | + writeFile(t, dir, "gen/x.json", body+"{\"injected\": \"marker\"}\n") |
| 370 | + gitRun(t, dir, "mv", "gen/keep.json", "gen/keep2.json") |
| 371 | + head := commitAll(t, dir, "head") |
| 372 | + t.Chdir(dir) |
| 373 | + |
| 374 | + // Probe AFTER the chdir: base is a SHA in this throwaway repo, so running it |
| 375 | + // from the outer repo would fail for "not a valid object" — which |
| 376 | + // checkAttrSourceSupported deliberately reports as supported — and the skip |
| 377 | + // could never fire on a runner whose git predates check-attr --source. |
| 378 | + if !checkAttrSourceSupported(base) { |
| 379 | + t.Skip("git too old for check-attr --source") |
| 380 | + } |
| 381 | + |
| 382 | + files, err := diffFiles(base, head) |
| 383 | + if err != nil { |
| 384 | + t.Fatalf("diffFiles: %v", err) |
| 385 | + } |
| 386 | + attr := attrPolicy{source: base, useSource: true} |
| 387 | + attr.trusted = attrTrusted(attr.useSource, TouchesGitattributes(files), false) |
| 388 | + if !attr.trusted { |
| 389 | + t.Fatal("attribute path should be trusted (PR does not touch .gitattributes)") |
| 390 | + } |
| 391 | + classify(files, base, head, attr, Extras{}, false) |
| 392 | + |
| 393 | + byPath := make(map[string]FileChange, len(files)) |
| 394 | + for _, f := range files { |
| 395 | + byPath[f.Path] = f |
| 396 | + } |
| 397 | + moved, ok := byPath["gen/x.json"] |
| 398 | + if !ok { |
| 399 | + t.Fatalf("git did not report gen/x.json as changed; got %+v", files) |
| 400 | + } |
| 401 | + if moved.OldPath != "handwritten/x.json" { |
| 402 | + t.Fatalf("gen/x.json OldPath = %q, want handwritten/x.json (rename detection did not fire)", moved.OldPath) |
| 403 | + } |
| 404 | + if moved.Generated { |
| 405 | + t.Error("a hand-written file renamed INTO a linguist-generated tree must not be classified generated — its edit would vanish from both the count and the reviewed diff") |
| 406 | + } |
| 407 | + kept, ok := byPath["gen/keep2.json"] |
| 408 | + if !ok { |
| 409 | + t.Fatalf("git did not report gen/keep2.json as changed; got %+v", files) |
| 410 | + } |
| 411 | + if !kept.Generated { |
| 412 | + t.Error("a rename whose source AND destination both carry linguist-generated should stay excluded") |
| 413 | + } |
| 414 | + |
| 415 | + res := Evaluate(files, Policy{Max: 1000}) |
| 416 | + if res.Counted == 0 { |
| 417 | + t.Errorf("Counted = %d, want the injected edit counted", res.Counted) |
| 418 | + } |
| 419 | + |
| 420 | + // End-to-end on the second symptom: the injected edit must reach the patch |
| 421 | + // the review panel reads, while the genuinely generated rename stays out. |
| 422 | + out := filepath.Join(t.TempDir(), "pr-diff.patch") |
| 423 | + if err := writeReviewedDiff(base, head, nil, files, out); err != nil { |
| 424 | + t.Fatalf("writeReviewedDiff: %v", err) |
| 425 | + } |
| 426 | + patch, err := os.ReadFile(out) |
| 427 | + if err != nil { |
| 428 | + t.Fatal(err) |
| 429 | + } |
| 430 | + if !strings.Contains(string(patch), "injected") { |
| 431 | + t.Error("the injected edit is missing from the --reviewed-diff-out patch — the review panel would never see it") |
| 432 | + } |
| 433 | + if strings.Contains(string(patch), "keep2.json") { |
| 434 | + t.Error("a rename entirely inside a generated tree should still be dropped from the reviewed diff") |
| 435 | + } |
| 436 | +} |
| 437 | + |
| 438 | +// TestAnnotateDiscountsPairsRenamesSoTheEditStillCounts guards the downstream |
| 439 | +// half of the rename guard. classify() correctly leaves a hand-written file |
| 440 | +// renamed out of its tree non-generated, but annotateDiscounts then re-derives a |
| 441 | +// patch from git; naming only the DESTINATION in that pathspec hid the deletion |
| 442 | +// side, git could not pair the rename, and it re-emitted the file as a |
| 443 | +// whole-file ADDITION whose every comment/blank line became a discount. Clamped |
| 444 | +// at Changed(), that drove the rename's Counted() to 0 — silently cancelling the |
| 445 | +// guard for any caller running with --ignore-comments (cursor-review's |
| 446 | +// ignore_comments input feeds exactly that). |
| 447 | +func TestAnnotateDiscountsPairsRenamesSoTheEditStillCounts(t *testing.T) { |
| 448 | + dir := initTestRepo(t) |
| 449 | + // Comment-heavy on purpose: unpaired, the whole-file addition discounts far |
| 450 | + // more lines than the rename actually changed, so the clamp zeroes it out. |
| 451 | + var sb strings.Builder |
| 452 | + sb.WriteString("package hw\n") |
| 453 | + for i := 0; i < 200; i++ { |
| 454 | + fmt.Fprintf(&sb, "// comment line %d\n\nvar V%d = %d\n", i, i, i) |
| 455 | + } |
| 456 | + writeFile(t, dir, "handwritten/big.go", sb.String()) |
| 457 | + base := commitAll(t, dir, "base") |
| 458 | + |
| 459 | + if err := os.MkdirAll(filepath.Join(dir, "moved"), 0o755); err != nil { |
| 460 | + t.Fatal(err) |
| 461 | + } |
| 462 | + gitRun(t, dir, "mv", "handwritten/big.go", "moved/big.go") |
| 463 | + writeFile(t, dir, "moved/big.go", sb.String()+"var Injected = 42\n") |
| 464 | + head := commitAll(t, dir, "head") |
| 465 | + t.Chdir(dir) |
| 466 | + |
| 467 | + files, err := diffFiles(base, head) |
| 468 | + if err != nil { |
| 469 | + t.Fatalf("diffFiles: %v", err) |
| 470 | + } |
| 471 | + classify(files, base, head, attrPolicy{}, Extras{}, false) |
| 472 | + |
| 473 | + var moved *FileChange |
| 474 | + for i := range files { |
| 475 | + if files[i].Path == "moved/big.go" { |
| 476 | + moved = &files[i] |
| 477 | + } |
| 478 | + } |
| 479 | + if moved == nil { |
| 480 | + t.Fatalf("git did not report moved/big.go as changed; got %+v", files) |
| 481 | + } |
| 482 | + if moved.OldPath != "handwritten/big.go" { |
| 483 | + t.Fatalf("moved/big.go OldPath = %q, want handwritten/big.go (rename detection did not fire)", moved.OldPath) |
| 484 | + } |
| 485 | + if moved.Generated { |
| 486 | + t.Fatal("a plain hand-written rename must not be classified generated") |
| 487 | + } |
| 488 | + |
| 489 | + annotateDiscounts(files, base, head) |
| 490 | + if moved.Discounted != 0 { |
| 491 | + t.Errorf("Discounted = %d, want 0 — the rename was re-read as a whole-file addition", moved.Discounted) |
| 492 | + } |
| 493 | + if got := Evaluate(files, Policy{Max: 1000}).Counted; got != 1 { |
| 494 | + t.Errorf("Counted = %d, want 1 (the injected line); discounting cancelled the rename guard", got) |
| 495 | + } |
| 496 | +} |
| 497 | + |
341 | 498 | // TestWriteReviewedDiff proves the end-to-end --reviewed-diff-out path against |
342 | 499 | // real git: the emitted patch keeps the hand-written file's section, drops the |
343 | 500 | // generated file's section entirely, and honors a verbatim exclude pathspec — |
|
0 commit comments