Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions internal/prove/prove.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,17 @@ func ChangedTestNames(git func(...string) (string, error), mergeBase, root strin
default:
body := line[1:]
if name := decl(body); name != "" {
current = name
if line[0] == '+' {
switch line[0] {
case '+':
current = name
add(name)
case '-':
// A removed declaration: the lines below it belong to a test
// the change deleted (or renamed, and the "+" line names the
// new one). Nothing here can be run on the new code.
current = ""
default:
current = name
}
continue
}
Expand All @@ -339,6 +347,27 @@ func ChangedTestNames(git func(...string) (string, error), mergeBase, root strin
}
}
}
// Git labels a hunk with the nearest function of the OLD file, so a hunk
// can name a test the change deleted. Keep only tests still declared.
declared := map[string]bool{}
for _, p := range testFiles {
content, err := os.ReadFile(filepath.Join(root, p))
if err != nil {
continue
}
for _, line := range strings.Split(string(content), "\n") {
if name := decl(line); name != "" {
declared[name] = true
}
}
}
kept := names[:0]
for _, name := range names {
if declared[name] {
kept = append(kept, name)
}
}
names = kept
untracked, err := git(append([]string{"ls-files", "--others", "--exclude-standard", "-z", "--"}, testFiles...)...)
if err != nil {
return nil, err
Expand Down
42 changes: 42 additions & 0 deletions internal/prove/review_fixes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,45 @@ func TestOldRunSeesBaseSymlinkAsSymlink(t *testing.T) {
t.Fatalf("outcome = %s, want proven (old run must see a symlink)", report.Outcome)
}
}

// Found on gitmoot#2261: a test the change deletes was still listed, and then
// reported test_not_run because it no longer exists.
func TestChangedTestNamesSkipsDeletedAndRenamedAwayTests(t *testing.T) {
dir := t.TempDir()
git(t, dir, "init", "-q", "-b", "main")
put(t, dir, "x_test.go", "package x\n\nfunc TestKeep(t *testing.T) {\n\t_ = 1\n}\n\nfunc TestGone(t *testing.T) {\n\t_ = 2\n\t_ = 3\n}\n\nfunc TestOldName(t *testing.T) {\n\t_ = 4\n}\n")
git(t, dir, "add", ".")
git(t, dir, "commit", "-q", "-m", "base")
base := strings.TrimSpace(git(t, dir, "rev-parse", "HEAD"))
put(t, dir, "x_test.go", "package x\n\nfunc TestKeep(t *testing.T) {\n\t_ = 10\n}\n\nfunc TestNewName(t *testing.T) {\n\t_ = 4\n}\n")
names, err := ChangedTestNames(source.Git(context.Background(), dir), base, dir, []string{"x_test.go"})
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(names, []string{"TestKeep", "TestNewName"}) {
t.Fatalf("names = %v, want [TestKeep TestNewName]", names)
}
}

// Found on gitmoot#2261: a deleted test split across two hunks came back
// through the second hunk's header, which git takes from the old file.
func TestChangedTestNamesIgnoresDeletedTestNamedByHunkHeader(t *testing.T) {
dir := t.TempDir()
git(t, dir, "init", "-q", "-b", "main")
shared := "\tshared1()\n\tshared2()\n\tshared3()\n\tshared4()\n\tshared5()\n\tshared6()\n\tshared7()\n"
put(t, dir, "x_test.go", "package x\n\nfunc TestGone(t *testing.T) {\n\tgone1()\n\tgone2()\n"+shared+"\tgone3()\n}\n")
git(t, dir, "add", ".")
git(t, dir, "commit", "-q", "-m", "base")
base := strings.TrimSpace(git(t, dir, "rev-parse", "HEAD"))
put(t, dir, "x_test.go", "package x\n\nfunc TestKeep(t *testing.T) {\n"+shared+"\tkeep()\n}\n")
if diff := git(t, dir, "diff", base, "--", "x_test.go"); !strings.Contains(diff, "@@ func TestGone(") {
t.Fatalf("fixture no longer produces a hunk header naming the deleted test:\n%s", diff)
}
names, err := ChangedTestNames(source.Git(context.Background(), dir), base, dir, []string{"x_test.go"})
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(names, []string{"TestKeep"}) {
t.Fatalf("names = %v, want [TestKeep]", names)
}
}
2 changes: 1 addition & 1 deletion skills/test-check/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ A permanent test earns its place only if it protects behavior someone relies on
| outcome | exit | meaning |
|---|---|---|
| `proven` | 0 | Red on the old code, green on the new. Done. |
| `not_red_on_old` | 4 | The test passes without the fix: it protects nothing. Make it fail without the fix. |
| `not_red_on_old` | 4 | The test passes without the fix. Make it fail without the fix — unless it is a guard test ("X is *not* matched", "Y stays unchanged") or only its fixture changed: those pass on old code by design; say so in the PR. |
| `fails_on_new` | 4 | The test fails on your code. Fix that first. |
| `no_test_in_change` | 4 | No test file changed. Add one, or treat it as a one-off check. |
| `test_not_run` | 4 | The command never mentioned the test, so it probably selected nothing. Fix the command or the name. |
Expand Down
Loading