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
9 changes: 7 additions & 2 deletions internal/mutate/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"os"
Expand Down Expand Up @@ -111,7 +112,9 @@ func Changed(base string) (map[string]map[int]bool, error) {
// asked is whether a changed file is one this asks the tests about: Go that is not a test, in a
// directory the go command builds. Under testdata, or a directory named with a leading . or _,
// it is a fixture go never compiles into the package - one written to fail on purpose, as
// cmd/mutate's example is.
// cmd/mutate's example is. And a file this machine's build leaves out - a js/wasm program's,
// behind its constraint or its _js.go name - is one no test run here compiles: asked, its
// package is one go list does not find at all.
func asked(path string) bool {
if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
return false
Expand All @@ -121,7 +124,9 @@ func asked(path string) bool {
return false
}
}
return true
// A file that cannot be read is one a later step says so of; built, it is asked.
built, err := build.Default.MatchFile(pathpkg.Dir(path), pathpkg.Base(path))
return err != nil || built
}

// hunk is the range of the new file a hunk header names: @@ -old,n +new,m @@.
Expand Down
8 changes: 8 additions & 0 deletions internal/mutate/mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ func TestTheLinesAChangeTouchesAreTheOnesItChanged(t *testing.T) {
require.NoError(t, os.WriteFile(filepath.Join(dir, "fixture.go"), []byte("package fixture\n\nfunc F() int { return 1 }\n"), 0o600))
}
run("add", "testdata")
// And a browser's Go, new: behind its constraint, and by its name - what this machine never
// builds, so no test here could refuse an edit of it.
require.NoError(t, os.MkdirAll("term", 0o750))
require.NoError(t, os.WriteFile(filepath.Join("term", "main.go"), []byte("//go:build js && wasm\n\npackage main\n\nfunc main() {}\n"), 0o600))
require.NoError(t, os.WriteFile(filepath.Join("term", "page_js.go"), []byte("package main\n\nfunc page() int { return 1 }\n"), 0o600))
// A file that is not Go, changed in the same diff: its hunks are nobody's lines.
require.NoError(t, os.WriteFile("notes.txt", []byte("a\nb\n"), 0o600))
run("add", "notes.txt")
Expand All @@ -356,6 +361,9 @@ func TestTheLinesAChangeTouchesAreTheOnesItChanged(t *testing.T) {
for _, fixture := range []string{"testdata/example/fixture.go", "_scratch/fixture.go", ".hidden/fixture.go"} {
assert.NotContains(t, changed, fixture, "a fixture the go command never builds was broken")
}
for _, browser := range []string{"term/main.go", "term/page_js.go"} {
assert.NotContains(t, changed, browser, "a file this machine's build leaves out was broken")
}
require.Contains(t, changed, "sample.go")
assert.Equal(t, map[int]bool{12: true, 33: true, 34: true}, changed["sample.go"], "the lines the change touched are not the lines reported")
require.Contains(t, changed, "new.go", "a file that is not committed yet is not in any diff, and is all new code")
Expand Down
4 changes: 4 additions & 0 deletions site/src/content/docs/mutate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ conditions of branches that only log, generated code, and lines with a `mutate-e
the last, these used to be answered with exemptions by hand, often enough that `mutate` now skips
them itself.

It also leaves out files the machine running it does not build: a `//go:build js && wasm`
program, or a file named `_windows.go` on Linux. No test that runs there compiles them, so none
could refuse an edit.

## How a run stays fast

Mutation testing has a reputation for being slow, because the naive approach builds and tests the
Expand Down
Loading