| 1 |
# Tests that the generated test main file has a generated code comment. |
| 2 |
# This is needed by analyzers that access source files through 'go list'. |
| 3 |
# Verifies golang.org/issue/31971. |
| 4 |
# TODO(jayconrod): This test is brittle. We should write _testmain.go as |
| 5 |
# a build action instead of with an ad-hoc WriteFile call |
| 6 |
# in internal/test/test.go. Then we could just grep 'go get -n'. |
| 7 |
go test x_test.go |
| 8 |
|
| 9 |
-- x_test.go -- |
| 10 |
package x |
| 11 |
|
| 12 |
import ( |
| 13 |
"os" |
| 14 |
"path/filepath" |
| 15 |
"regexp" |
| 16 |
"testing" |
| 17 |
) |
| 18 |
|
| 19 |
func Test(t *testing.T) { |
| 20 |
exePath, err := os.Executable() |
| 21 |
if err != nil { |
| 22 |
t.Fatal(err) |
| 23 |
} |
| 24 |
testmainPath := filepath.Join(filepath.Dir(exePath), "_testmain.go") |
| 25 |
source, err := os.ReadFile(testmainPath) |
| 26 |
if err != nil { |
| 27 |
t.Fatal(err) |
| 28 |
} |
| 29 |
if matched, err := regexp.Match(`(?m)^// Code generated .* DO NOT EDIT\.$`, source); err != nil { |
| 30 |
t.Fatal(err) |
| 31 |
} else if !matched { |
| 32 |
t.Error("_testmain.go does not have generated code comment") |
| 33 |
} |
| 34 |
} |