|
| 1 | +package contract |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +// initRepoTwoCommits seeds a repo with one *_test.go (2 tests), commits, |
| 8 | +// then optionally mutates and recommits. Returns the dir. |
| 9 | +func initRepoTwoCommits(t *testing.T, mutate func(dir string)) string { |
| 10 | + t.Helper() |
| 11 | + dir := t.TempDir() |
| 12 | + runGit(t, dir, "init", "-q") |
| 13 | + writeFile(t, dir, "x_test.go", `package x |
| 14 | +
|
| 15 | +import "testing" |
| 16 | +
|
| 17 | +func TestAlpha(t *testing.T) { _ = t } |
| 18 | +func TestBeta(t *testing.T) { _ = t } |
| 19 | +`) |
| 20 | + runGit(t, dir, "add", "x_test.go") |
| 21 | + runGit(t, dir, "commit", "-q", "-m", "base") |
| 22 | + if mutate != nil { |
| 23 | + mutate(dir) |
| 24 | + runGit(t, dir, "add", "-A") |
| 25 | + runGit(t, dir, "commit", "-q", "-m", "head") |
| 26 | + } |
| 27 | + return dir |
| 28 | +} |
| 29 | + |
| 30 | +func TestTestCountBaseline_NoChange_Passes(t *testing.T) { |
| 31 | + dir := initRepoTwoCommits(t, func(d string) { |
| 32 | + writeFile(t, d, "y.go", `package x |
| 33 | +`) |
| 34 | + }) |
| 35 | + v := &testCountBaselineValidator{} |
| 36 | + if err := v.Validate(ContractConfig{Type: "test_count_baseline"}, dir); err != nil { |
| 37 | + t.Fatalf("expected pass, got: %v", err) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestTestCountBaseline_Addition_Passes(t *testing.T) { |
| 42 | + dir := initRepoTwoCommits(t, func(d string) { |
| 43 | + writeFile(t, d, "x_test.go", `package x |
| 44 | +
|
| 45 | +import "testing" |
| 46 | +
|
| 47 | +func TestAlpha(t *testing.T) { _ = t } |
| 48 | +func TestBeta(t *testing.T) { _ = t } |
| 49 | +func TestGamma(t *testing.T) { _ = t } |
| 50 | +`) |
| 51 | + }) |
| 52 | + v := &testCountBaselineValidator{} |
| 53 | + if err := v.Validate(ContractConfig{Type: "test_count_baseline"}, dir); err != nil { |
| 54 | + t.Fatalf("expected pass on addition, got: %v", err) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestTestCountBaseline_Deletion_Fails(t *testing.T) { |
| 59 | + dir := initRepoTwoCommits(t, func(d string) { |
| 60 | + writeFile(t, d, "x_test.go", `package x |
| 61 | +
|
| 62 | +import "testing" |
| 63 | +
|
| 64 | +func TestAlpha(t *testing.T) { _ = t } |
| 65 | +`) |
| 66 | + }) |
| 67 | + v := &testCountBaselineValidator{} |
| 68 | + if err := v.Validate(ContractConfig{Type: "test_count_baseline"}, dir); err == nil { |
| 69 | + t.Fatal("expected error on deletion, got nil") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestTestCountBaseline_FileMove_NetsZero(t *testing.T) { |
| 74 | + dir := initRepoTwoCommits(t, func(d string) { |
| 75 | + // Delete x_test.go, recreate same tests under different filename. |
| 76 | + runGit(t, d, "rm", "-q", "x_test.go") |
| 77 | + writeFile(t, d, "renamed_test.go", `package x |
| 78 | +
|
| 79 | +import "testing" |
| 80 | +
|
| 81 | +func TestAlpha(t *testing.T) { _ = t } |
| 82 | +func TestBeta(t *testing.T) { _ = t } |
| 83 | +`) |
| 84 | + }) |
| 85 | + v := &testCountBaselineValidator{} |
| 86 | + if err := v.Validate(ContractConfig{Type: "test_count_baseline"}, dir); err != nil { |
| 87 | + t.Fatalf("expected pass on file move, got: %v", err) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestTestCountBaseline_HigherTolerance_Passes(t *testing.T) { |
| 92 | + dir := initRepoTwoCommits(t, func(d string) { |
| 93 | + writeFile(t, d, "x_test.go", `package x |
| 94 | +`) |
| 95 | + }) |
| 96 | + v := &testCountBaselineValidator{} |
| 97 | + cfg := ContractConfig{Type: "test_count_baseline", MaxTestDeletions: 2} |
| 98 | + if err := v.Validate(cfg, dir); err != nil { |
| 99 | + t.Fatalf("expected pass with tolerance=2, got: %v", err) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestTestCountBaseline_NoBaseRef_PassesSilently(t *testing.T) { |
| 104 | + // Single-commit repo — HEAD~1 doesn't resolve. |
| 105 | + dir := initRepoTwoCommits(t, nil) |
| 106 | + v := &testCountBaselineValidator{} |
| 107 | + if err := v.Validate(ContractConfig{Type: "test_count_baseline"}, dir); err != nil { |
| 108 | + t.Fatalf("expected silent pass without base ref, got: %v", err) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestTestCountBaseline_PythonConfig_DetectsDeletion(t *testing.T) { |
| 113 | + dir := t.TempDir() |
| 114 | + runGit(t, dir, "init", "-q") |
| 115 | + writeFile(t, dir, "test_things.py", `def test_alpha(): |
| 116 | + pass |
| 117 | +
|
| 118 | +def test_beta(): |
| 119 | + pass |
| 120 | +`) |
| 121 | + runGit(t, dir, "add", "-A") |
| 122 | + runGit(t, dir, "commit", "-q", "-m", "base") |
| 123 | + writeFile(t, dir, "test_things.py", `def test_alpha(): |
| 124 | + pass |
| 125 | +`) |
| 126 | + runGit(t, dir, "add", "-A") |
| 127 | + runGit(t, dir, "commit", "-q", "-m", "head") |
| 128 | + v := &testCountBaselineValidator{} |
| 129 | + cfg := ContractConfig{ |
| 130 | + Type: "test_count_baseline", |
| 131 | + TestFilePattern: []string{"test_*.py", "*_test.py"}, |
| 132 | + TestFuncPattern: `(?m)^[ \t]*def[ \t]+test_\w+`, |
| 133 | + } |
| 134 | + if err := v.Validate(cfg, dir); err == nil { |
| 135 | + t.Fatal("expected error for python deletion, got nil") |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestTestCountBaseline_NoGit_PassesSilently(t *testing.T) { |
| 140 | + dir := t.TempDir() |
| 141 | + v := &testCountBaselineValidator{} |
| 142 | + if err := v.Validate(ContractConfig{Type: "test_count_baseline"}, dir); err != nil { |
| 143 | + t.Fatalf("expected silent pass without git, got: %v", err) |
| 144 | + } |
| 145 | +} |
0 commit comments