Commit dca29095 authored by Russ Cox's avatar Russ Cox

cmd/go: add t.Helper calls to test helpers

Now that we have t.Helper, might as well use it to make the
reported failure lines more helpful.

Change-Id: I2a0c64e9ca7bdc0eaf2b62f9f855c41467767084
Reviewed-on: https://go-review.googlesource.com/56274
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Reviewed-by: default avatarDave Cheney <dave@cheney.net>
parent 2bc2b103
...@@ -177,6 +177,7 @@ type testgoData struct { ...@@ -177,6 +177,7 @@ type testgoData struct {
// testgo sets up for a test that runs testgo. // testgo sets up for a test that runs testgo.
func testgo(t *testing.T) *testgoData { func testgo(t *testing.T) *testgoData {
t.Helper()
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
if skipExternal { if skipExternal {
...@@ -188,6 +189,7 @@ func testgo(t *testing.T) *testgoData { ...@@ -188,6 +189,7 @@ func testgo(t *testing.T) *testgoData {
// must gives a fatal error if err is not nil. // must gives a fatal error if err is not nil.
func (tg *testgoData) must(err error) { func (tg *testgoData) must(err error) {
tg.t.Helper()
if err != nil { if err != nil {
tg.t.Fatal(err) tg.t.Fatal(err)
} }
...@@ -195,6 +197,7 @@ func (tg *testgoData) must(err error) { ...@@ -195,6 +197,7 @@ func (tg *testgoData) must(err error) {
// check gives a test non-fatal error if err is not nil. // check gives a test non-fatal error if err is not nil.
func (tg *testgoData) check(err error) { func (tg *testgoData) check(err error) {
tg.t.Helper()
if err != nil { if err != nil {
tg.t.Error(err) tg.t.Error(err)
} }
...@@ -202,6 +205,7 @@ func (tg *testgoData) check(err error) { ...@@ -202,6 +205,7 @@ func (tg *testgoData) check(err error) {
// parallel runs the test in parallel by calling t.Parallel. // parallel runs the test in parallel by calling t.Parallel.
func (tg *testgoData) parallel() { func (tg *testgoData) parallel() {
tg.t.Helper()
if tg.ran { if tg.ran {
tg.t.Fatal("internal testsuite error: call to parallel after run") tg.t.Fatal("internal testsuite error: call to parallel after run")
} }
...@@ -222,6 +226,7 @@ func (tg *testgoData) parallel() { ...@@ -222,6 +226,7 @@ func (tg *testgoData) parallel() {
// pwd returns the current directory. // pwd returns the current directory.
func (tg *testgoData) pwd() string { func (tg *testgoData) pwd() string {
tg.t.Helper()
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {
tg.t.Fatalf("could not get working directory: %v", err) tg.t.Fatalf("could not get working directory: %v", err)
...@@ -233,6 +238,7 @@ func (tg *testgoData) pwd() string { ...@@ -233,6 +238,7 @@ func (tg *testgoData) pwd() string {
// using this means that the test must not be run in parallel with any // using this means that the test must not be run in parallel with any
// other tests. // other tests.
func (tg *testgoData) cd(dir string) { func (tg *testgoData) cd(dir string) {
tg.t.Helper()
if tg.inParallel { if tg.inParallel {
tg.t.Fatal("internal testsuite error: changing directory when running in parallel") tg.t.Fatal("internal testsuite error: changing directory when running in parallel")
} }
...@@ -256,6 +262,7 @@ func (tg *testgoData) sleep() { ...@@ -256,6 +262,7 @@ func (tg *testgoData) sleep() {
// setenv sets an environment variable to use when running the test go // setenv sets an environment variable to use when running the test go
// command. // command.
func (tg *testgoData) setenv(name, val string) { func (tg *testgoData) setenv(name, val string) {
tg.t.Helper()
if tg.inParallel && (name == "GOROOT" || name == "GOPATH" || name == "GOBIN") && (strings.HasPrefix(val, "testdata") || strings.HasPrefix(val, "./testdata")) { if tg.inParallel && (name == "GOROOT" || name == "GOPATH" || name == "GOBIN") && (strings.HasPrefix(val, "testdata") || strings.HasPrefix(val, "./testdata")) {
tg.t.Fatalf("internal testsuite error: call to setenv with testdata (%s=%s) after parallel", name, val) tg.t.Fatalf("internal testsuite error: call to setenv with testdata (%s=%s) after parallel", name, val)
} }
...@@ -286,6 +293,7 @@ func (tg *testgoData) goTool() string { ...@@ -286,6 +293,7 @@ func (tg *testgoData) goTool() string {
// doRun runs the test go command, recording stdout and stderr and // doRun runs the test go command, recording stdout and stderr and
// returning exit status. // returning exit status.
func (tg *testgoData) doRun(args []string) error { func (tg *testgoData) doRun(args []string) error {
tg.t.Helper()
if !canRun { if !canRun {
panic("testgoData.doRun called but canRun false") panic("testgoData.doRun called but canRun false")
} }
...@@ -331,6 +339,7 @@ func (tg *testgoData) doRun(args []string) error { ...@@ -331,6 +339,7 @@ func (tg *testgoData) doRun(args []string) error {
// run runs the test go command, and expects it to succeed. // run runs the test go command, and expects it to succeed.
func (tg *testgoData) run(args ...string) { func (tg *testgoData) run(args ...string) {
tg.t.Helper()
if status := tg.doRun(args); status != nil { if status := tg.doRun(args); status != nil {
tg.t.Logf("go %v failed unexpectedly: %v", args, status) tg.t.Logf("go %v failed unexpectedly: %v", args, status)
tg.t.FailNow() tg.t.FailNow()
...@@ -339,6 +348,7 @@ func (tg *testgoData) run(args ...string) { ...@@ -339,6 +348,7 @@ func (tg *testgoData) run(args ...string) {
// runFail runs the test go command, and expects it to fail. // runFail runs the test go command, and expects it to fail.
func (tg *testgoData) runFail(args ...string) { func (tg *testgoData) runFail(args ...string) {
tg.t.Helper()
if status := tg.doRun(args); status == nil { if status := tg.doRun(args); status == nil {
tg.t.Fatal("testgo succeeded unexpectedly") tg.t.Fatal("testgo succeeded unexpectedly")
} else { } else {
...@@ -348,6 +358,7 @@ func (tg *testgoData) runFail(args ...string) { ...@@ -348,6 +358,7 @@ func (tg *testgoData) runFail(args ...string) {
// runGit runs a git command, and expects it to succeed. // runGit runs a git command, and expects it to succeed.
func (tg *testgoData) runGit(dir string, args ...string) { func (tg *testgoData) runGit(dir string, args ...string) {
tg.t.Helper()
cmd := exec.Command("git", args...) cmd := exec.Command("git", args...)
tg.stdout.Reset() tg.stdout.Reset()
tg.stderr.Reset() tg.stderr.Reset()
...@@ -372,6 +383,7 @@ func (tg *testgoData) runGit(dir string, args ...string) { ...@@ -372,6 +383,7 @@ func (tg *testgoData) runGit(dir string, args ...string) {
// getStdout returns standard output of the testgo run as a string. // getStdout returns standard output of the testgo run as a string.
func (tg *testgoData) getStdout() string { func (tg *testgoData) getStdout() string {
tg.t.Helper()
if !tg.ran { if !tg.ran {
tg.t.Fatal("internal testsuite error: stdout called before run") tg.t.Fatal("internal testsuite error: stdout called before run")
} }
...@@ -380,6 +392,7 @@ func (tg *testgoData) getStdout() string { ...@@ -380,6 +392,7 @@ func (tg *testgoData) getStdout() string {
// getStderr returns standard error of the testgo run as a string. // getStderr returns standard error of the testgo run as a string.
func (tg *testgoData) getStderr() string { func (tg *testgoData) getStderr() string {
tg.t.Helper()
if !tg.ran { if !tg.ran {
tg.t.Fatal("internal testsuite error: stdout called before run") tg.t.Fatal("internal testsuite error: stdout called before run")
} }
...@@ -390,6 +403,7 @@ func (tg *testgoData) getStderr() string { ...@@ -390,6 +403,7 @@ func (tg *testgoData) getStderr() string {
// whether it is found. The regular expression is matched against // whether it is found. The regular expression is matched against
// each line separately, as with the grep command. // each line separately, as with the grep command.
func (tg *testgoData) doGrepMatch(match string, b *bytes.Buffer) bool { func (tg *testgoData) doGrepMatch(match string, b *bytes.Buffer) bool {
tg.t.Helper()
if !tg.ran { if !tg.ran {
tg.t.Fatal("internal testsuite error: grep called before run") tg.t.Fatal("internal testsuite error: grep called before run")
} }
...@@ -407,6 +421,7 @@ func (tg *testgoData) doGrepMatch(match string, b *bytes.Buffer) bool { ...@@ -407,6 +421,7 @@ func (tg *testgoData) doGrepMatch(match string, b *bytes.Buffer) bool {
// searching, "output" or "error". The msg argument is logged on // searching, "output" or "error". The msg argument is logged on
// failure. // failure.
func (tg *testgoData) doGrep(match string, b *bytes.Buffer, name, msg string) { func (tg *testgoData) doGrep(match string, b *bytes.Buffer, name, msg string) {
tg.t.Helper()
if !tg.doGrepMatch(match, b) { if !tg.doGrepMatch(match, b) {
tg.t.Log(msg) tg.t.Log(msg)
tg.t.Logf("pattern %v not found in standard %s", match, name) tg.t.Logf("pattern %v not found in standard %s", match, name)
...@@ -417,18 +432,21 @@ func (tg *testgoData) doGrep(match string, b *bytes.Buffer, name, msg string) { ...@@ -417,18 +432,21 @@ func (tg *testgoData) doGrep(match string, b *bytes.Buffer, name, msg string) {
// grepStdout looks for a regular expression in the test run's // grepStdout looks for a regular expression in the test run's
// standard output and fails, logging msg, if it is not found. // standard output and fails, logging msg, if it is not found.
func (tg *testgoData) grepStdout(match, msg string) { func (tg *testgoData) grepStdout(match, msg string) {
tg.t.Helper()
tg.doGrep(match, &tg.stdout, "output", msg) tg.doGrep(match, &tg.stdout, "output", msg)
} }
// grepStderr looks for a regular expression in the test run's // grepStderr looks for a regular expression in the test run's
// standard error and fails, logging msg, if it is not found. // standard error and fails, logging msg, if it is not found.
func (tg *testgoData) grepStderr(match, msg string) { func (tg *testgoData) grepStderr(match, msg string) {
tg.t.Helper()
tg.doGrep(match, &tg.stderr, "error", msg) tg.doGrep(match, &tg.stderr, "error", msg)
} }
// grepBoth looks for a regular expression in the test run's standard // grepBoth looks for a regular expression in the test run's standard
// output or stand error and fails, logging msg, if it is not found. // output or stand error and fails, logging msg, if it is not found.
func (tg *testgoData) grepBoth(match, msg string) { func (tg *testgoData) grepBoth(match, msg string) {
tg.t.Helper()
if !tg.doGrepMatch(match, &tg.stdout) && !tg.doGrepMatch(match, &tg.stderr) { if !tg.doGrepMatch(match, &tg.stdout) && !tg.doGrepMatch(match, &tg.stderr) {
tg.t.Log(msg) tg.t.Log(msg)
tg.t.Logf("pattern %v not found in standard output or standard error", match) tg.t.Logf("pattern %v not found in standard output or standard error", match)
...@@ -439,6 +457,7 @@ func (tg *testgoData) grepBoth(match, msg string) { ...@@ -439,6 +457,7 @@ func (tg *testgoData) grepBoth(match, msg string) {
// doGrepNot looks for a regular expression in a buffer and fails if // doGrepNot looks for a regular expression in a buffer and fails if
// it is found. The name and msg arguments are as for doGrep. // it is found. The name and msg arguments are as for doGrep.
func (tg *testgoData) doGrepNot(match string, b *bytes.Buffer, name, msg string) { func (tg *testgoData) doGrepNot(match string, b *bytes.Buffer, name, msg string) {
tg.t.Helper()
if tg.doGrepMatch(match, b) { if tg.doGrepMatch(match, b) {
tg.t.Log(msg) tg.t.Log(msg)
tg.t.Logf("pattern %v found unexpectedly in standard %s", match, name) tg.t.Logf("pattern %v found unexpectedly in standard %s", match, name)
...@@ -449,12 +468,14 @@ func (tg *testgoData) doGrepNot(match string, b *bytes.Buffer, name, msg string) ...@@ -449,12 +468,14 @@ func (tg *testgoData) doGrepNot(match string, b *bytes.Buffer, name, msg string)
// grepStdoutNot looks for a regular expression in the test run's // grepStdoutNot looks for a regular expression in the test run's
// standard output and fails, logging msg, if it is found. // standard output and fails, logging msg, if it is found.
func (tg *testgoData) grepStdoutNot(match, msg string) { func (tg *testgoData) grepStdoutNot(match, msg string) {
tg.t.Helper()
tg.doGrepNot(match, &tg.stdout, "output", msg) tg.doGrepNot(match, &tg.stdout, "output", msg)
} }
// grepStderrNot looks for a regular expression in the test run's // grepStderrNot looks for a regular expression in the test run's
// standard error and fails, logging msg, if it is found. // standard error and fails, logging msg, if it is found.
func (tg *testgoData) grepStderrNot(match, msg string) { func (tg *testgoData) grepStderrNot(match, msg string) {
tg.t.Helper()
tg.doGrepNot(match, &tg.stderr, "error", msg) tg.doGrepNot(match, &tg.stderr, "error", msg)
} }
...@@ -462,6 +483,7 @@ func (tg *testgoData) grepStderrNot(match, msg string) { ...@@ -462,6 +483,7 @@ func (tg *testgoData) grepStderrNot(match, msg string) {
// standard output or stand error and fails, logging msg, if it is // standard output or stand error and fails, logging msg, if it is
// found. // found.
func (tg *testgoData) grepBothNot(match, msg string) { func (tg *testgoData) grepBothNot(match, msg string) {
tg.t.Helper()
if tg.doGrepMatch(match, &tg.stdout) || tg.doGrepMatch(match, &tg.stderr) { if tg.doGrepMatch(match, &tg.stdout) || tg.doGrepMatch(match, &tg.stderr) {
tg.t.Log(msg) tg.t.Log(msg)
tg.t.Fatalf("pattern %v found unexpectedly in standard output or standard error", match) tg.t.Fatalf("pattern %v found unexpectedly in standard output or standard error", match)
...@@ -470,6 +492,7 @@ func (tg *testgoData) grepBothNot(match, msg string) { ...@@ -470,6 +492,7 @@ func (tg *testgoData) grepBothNot(match, msg string) {
// doGrepCount counts the number of times a regexp is seen in a buffer. // doGrepCount counts the number of times a regexp is seen in a buffer.
func (tg *testgoData) doGrepCount(match string, b *bytes.Buffer) int { func (tg *testgoData) doGrepCount(match string, b *bytes.Buffer) int {
tg.t.Helper()
if !tg.ran { if !tg.ran {
tg.t.Fatal("internal testsuite error: doGrepCount called before run") tg.t.Fatal("internal testsuite error: doGrepCount called before run")
} }
...@@ -486,6 +509,7 @@ func (tg *testgoData) doGrepCount(match string, b *bytes.Buffer) int { ...@@ -486,6 +509,7 @@ func (tg *testgoData) doGrepCount(match string, b *bytes.Buffer) int {
// grepCountBoth returns the number of times a regexp is seen in both // grepCountBoth returns the number of times a regexp is seen in both
// standard output and standard error. // standard output and standard error.
func (tg *testgoData) grepCountBoth(match string) int { func (tg *testgoData) grepCountBoth(match string) int {
tg.t.Helper()
return tg.doGrepCount(match, &tg.stdout) + tg.doGrepCount(match, &tg.stderr) return tg.doGrepCount(match, &tg.stdout) + tg.doGrepCount(match, &tg.stderr)
} }
...@@ -494,6 +518,7 @@ func (tg *testgoData) grepCountBoth(match string) int { ...@@ -494,6 +518,7 @@ func (tg *testgoData) grepCountBoth(match string) int {
// removed. When the test completes, the file or directory will be // removed. When the test completes, the file or directory will be
// removed if it exists. // removed if it exists.
func (tg *testgoData) creatingTemp(path string) { func (tg *testgoData) creatingTemp(path string) {
tg.t.Helper()
if filepath.IsAbs(path) && !strings.HasPrefix(path, tg.tempdir) { if filepath.IsAbs(path) && !strings.HasPrefix(path, tg.tempdir) {
tg.t.Fatalf("internal testsuite error: creatingTemp(%q) with absolute path not in temporary directory", path) tg.t.Fatalf("internal testsuite error: creatingTemp(%q) with absolute path not in temporary directory", path)
} }
...@@ -510,6 +535,7 @@ func (tg *testgoData) creatingTemp(path string) { ...@@ -510,6 +535,7 @@ func (tg *testgoData) creatingTemp(path string) {
// makeTempdir makes a temporary directory for a run of testgo. If // makeTempdir makes a temporary directory for a run of testgo. If
// the temporary directory was already created, this does nothing. // the temporary directory was already created, this does nothing.
func (tg *testgoData) makeTempdir() { func (tg *testgoData) makeTempdir() {
tg.t.Helper()
if tg.tempdir == "" { if tg.tempdir == "" {
var err error var err error
tg.tempdir, err = ioutil.TempDir("", "gotest") tg.tempdir, err = ioutil.TempDir("", "gotest")
...@@ -519,6 +545,7 @@ func (tg *testgoData) makeTempdir() { ...@@ -519,6 +545,7 @@ func (tg *testgoData) makeTempdir() {
// tempFile adds a temporary file for a run of testgo. // tempFile adds a temporary file for a run of testgo.
func (tg *testgoData) tempFile(path, contents string) { func (tg *testgoData) tempFile(path, contents string) {
tg.t.Helper()
tg.makeTempdir() tg.makeTempdir()
tg.must(os.MkdirAll(filepath.Join(tg.tempdir, filepath.Dir(path)), 0755)) tg.must(os.MkdirAll(filepath.Join(tg.tempdir, filepath.Dir(path)), 0755))
bytes := []byte(contents) bytes := []byte(contents)
...@@ -533,6 +560,7 @@ func (tg *testgoData) tempFile(path, contents string) { ...@@ -533,6 +560,7 @@ func (tg *testgoData) tempFile(path, contents string) {
// tempDir adds a temporary directory for a run of testgo. // tempDir adds a temporary directory for a run of testgo.
func (tg *testgoData) tempDir(path string) { func (tg *testgoData) tempDir(path string) {
tg.t.Helper()
tg.makeTempdir() tg.makeTempdir()
if err := os.MkdirAll(filepath.Join(tg.tempdir, path), 0755); err != nil && !os.IsExist(err) { if err := os.MkdirAll(filepath.Join(tg.tempdir, path), 0755); err != nil && !os.IsExist(err) {
tg.t.Fatal(err) tg.t.Fatal(err)
...@@ -542,6 +570,7 @@ func (tg *testgoData) tempDir(path string) { ...@@ -542,6 +570,7 @@ func (tg *testgoData) tempDir(path string) {
// path returns the absolute pathname to file with the temporary // path returns the absolute pathname to file with the temporary
// directory. // directory.
func (tg *testgoData) path(name string) string { func (tg *testgoData) path(name string) string {
tg.t.Helper()
if tg.tempdir == "" { if tg.tempdir == "" {
tg.t.Fatalf("internal testsuite error: path(%q) with no tempdir", name) tg.t.Fatalf("internal testsuite error: path(%q) with no tempdir", name)
} }
...@@ -553,6 +582,7 @@ func (tg *testgoData) path(name string) string { ...@@ -553,6 +582,7 @@ func (tg *testgoData) path(name string) string {
// mustExist fails if path does not exist. // mustExist fails if path does not exist.
func (tg *testgoData) mustExist(path string) { func (tg *testgoData) mustExist(path string) {
tg.t.Helper()
if _, err := os.Stat(path); err != nil { if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
tg.t.Fatalf("%s does not exist but should", path) tg.t.Fatalf("%s does not exist but should", path)
...@@ -563,6 +593,7 @@ func (tg *testgoData) mustExist(path string) { ...@@ -563,6 +593,7 @@ func (tg *testgoData) mustExist(path string) {
// mustNotExist fails if path exists. // mustNotExist fails if path exists.
func (tg *testgoData) mustNotExist(path string) { func (tg *testgoData) mustNotExist(path string) {
tg.t.Helper()
if _, err := os.Stat(path); err == nil || !os.IsNotExist(err) { if _, err := os.Stat(path); err == nil || !os.IsNotExist(err) {
tg.t.Fatalf("%s exists but should not (%v)", path, err) tg.t.Fatalf("%s exists but should not (%v)", path, err)
} }
...@@ -570,6 +601,7 @@ func (tg *testgoData) mustNotExist(path string) { ...@@ -570,6 +601,7 @@ func (tg *testgoData) mustNotExist(path string) {
// wantExecutable fails with msg if path is not executable. // wantExecutable fails with msg if path is not executable.
func (tg *testgoData) wantExecutable(path, msg string) { func (tg *testgoData) wantExecutable(path, msg string) {
tg.t.Helper()
if st, err := os.Stat(path); err != nil { if st, err := os.Stat(path); err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
tg.t.Log(err) tg.t.Log(err)
...@@ -584,6 +616,7 @@ func (tg *testgoData) wantExecutable(path, msg string) { ...@@ -584,6 +616,7 @@ func (tg *testgoData) wantExecutable(path, msg string) {
// wantArchive fails if path is not an archive. // wantArchive fails if path is not an archive.
func (tg *testgoData) wantArchive(path string) { func (tg *testgoData) wantArchive(path string) {
tg.t.Helper()
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
tg.t.Fatal(err) tg.t.Fatal(err)
...@@ -598,6 +631,7 @@ func (tg *testgoData) wantArchive(path string) { ...@@ -598,6 +631,7 @@ func (tg *testgoData) wantArchive(path string) {
// isStale reports whether pkg is stale, and why // isStale reports whether pkg is stale, and why
func (tg *testgoData) isStale(pkg string) (bool, string) { func (tg *testgoData) isStale(pkg string) (bool, string) {
tg.t.Helper()
tg.run("list", "-f", "{{.Stale}}:{{.StaleReason}}", pkg) tg.run("list", "-f", "{{.Stale}}:{{.StaleReason}}", pkg)
v := strings.TrimSpace(tg.getStdout()) v := strings.TrimSpace(tg.getStdout())
f := strings.SplitN(v, ":", 2) f := strings.SplitN(v, ":", 2)
...@@ -615,6 +649,7 @@ func (tg *testgoData) isStale(pkg string) (bool, string) { ...@@ -615,6 +649,7 @@ func (tg *testgoData) isStale(pkg string) (bool, string) {
// wantStale fails with msg if pkg is not stale. // wantStale fails with msg if pkg is not stale.
func (tg *testgoData) wantStale(pkg, reason, msg string) { func (tg *testgoData) wantStale(pkg, reason, msg string) {
tg.t.Helper()
stale, why := tg.isStale(pkg) stale, why := tg.isStale(pkg)
if !stale { if !stale {
tg.t.Fatal(msg) tg.t.Fatal(msg)
...@@ -626,6 +661,7 @@ func (tg *testgoData) wantStale(pkg, reason, msg string) { ...@@ -626,6 +661,7 @@ func (tg *testgoData) wantStale(pkg, reason, msg string) {
// wantNotStale fails with msg if pkg is stale. // wantNotStale fails with msg if pkg is stale.
func (tg *testgoData) wantNotStale(pkg, reason, msg string) { func (tg *testgoData) wantNotStale(pkg, reason, msg string) {
tg.t.Helper()
stale, why := tg.isStale(pkg) stale, why := tg.isStale(pkg)
if stale { if stale {
tg.t.Fatal(msg) tg.t.Fatal(msg)
...@@ -637,6 +673,7 @@ func (tg *testgoData) wantNotStale(pkg, reason, msg string) { ...@@ -637,6 +673,7 @@ func (tg *testgoData) wantNotStale(pkg, reason, msg string) {
// cleanup cleans up a test that runs testgo. // cleanup cleans up a test that runs testgo.
func (tg *testgoData) cleanup() { func (tg *testgoData) cleanup() {
tg.t.Helper()
if tg.wd != "" { if tg.wd != "" {
if err := os.Chdir(tg.wd); err != nil { if err := os.Chdir(tg.wd); err != nil {
// We are unlikely to be able to continue. // We are unlikely to be able to continue.
...@@ -655,6 +692,7 @@ func (tg *testgoData) cleanup() { ...@@ -655,6 +692,7 @@ func (tg *testgoData) cleanup() {
// failSSH puts an ssh executable in the PATH that always fails. // failSSH puts an ssh executable in the PATH that always fails.
// This is to stub out uses of ssh by go get. // This is to stub out uses of ssh by go get.
func (tg *testgoData) failSSH() { func (tg *testgoData) failSSH() {
tg.t.Helper()
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {
tg.t.Fatal(err) tg.t.Fatal(err)
...@@ -990,6 +1028,7 @@ func TestGoInstallDetectsRemovedFilesInPackageMain(t *testing.T) { ...@@ -990,6 +1028,7 @@ func TestGoInstallDetectsRemovedFilesInPackageMain(t *testing.T) {
} }
func testLocalRun(tg *testgoData, exepath, local, match string) { func testLocalRun(tg *testgoData, exepath, local, match string) {
tg.t.Helper()
out, err := exec.Command(exepath).Output() out, err := exec.Command(exepath).Output()
if err != nil { if err != nil {
tg.t.Fatalf("error running %v: %v", exepath, err) tg.t.Fatalf("error running %v: %v", exepath, err)
...@@ -1001,6 +1040,7 @@ func testLocalRun(tg *testgoData, exepath, local, match string) { ...@@ -1001,6 +1040,7 @@ func testLocalRun(tg *testgoData, exepath, local, match string) {
} }
func testLocalEasy(tg *testgoData, local string) { func testLocalEasy(tg *testgoData, local string) {
tg.t.Helper()
exepath := "./easy" + exeSuffix exepath := "./easy" + exeSuffix
tg.creatingTemp(exepath) tg.creatingTemp(exepath)
tg.run("build", "-o", exepath, filepath.Join("testdata", local, "easy.go")) tg.run("build", "-o", exepath, filepath.Join("testdata", local, "easy.go"))
...@@ -1008,6 +1048,7 @@ func testLocalEasy(tg *testgoData, local string) { ...@@ -1008,6 +1048,7 @@ func testLocalEasy(tg *testgoData, local string) {
} }
func testLocalEasySub(tg *testgoData, local string) { func testLocalEasySub(tg *testgoData, local string) {
tg.t.Helper()
exepath := "./easysub" + exeSuffix exepath := "./easysub" + exeSuffix
tg.creatingTemp(exepath) tg.creatingTemp(exepath)
tg.run("build", "-o", exepath, filepath.Join("testdata", local, "easysub", "main.go")) tg.run("build", "-o", exepath, filepath.Join("testdata", local, "easysub", "main.go"))
...@@ -1015,6 +1056,7 @@ func testLocalEasySub(tg *testgoData, local string) { ...@@ -1015,6 +1056,7 @@ func testLocalEasySub(tg *testgoData, local string) {
} }
func testLocalHard(tg *testgoData, local string) { func testLocalHard(tg *testgoData, local string) {
tg.t.Helper()
exepath := "./hard" + exeSuffix exepath := "./hard" + exeSuffix
tg.creatingTemp(exepath) tg.creatingTemp(exepath)
tg.run("build", "-o", exepath, filepath.Join("testdata", local, "hard.go")) tg.run("build", "-o", exepath, filepath.Join("testdata", local, "hard.go"))
...@@ -1022,6 +1064,7 @@ func testLocalHard(tg *testgoData, local string) { ...@@ -1022,6 +1064,7 @@ func testLocalHard(tg *testgoData, local string) {
} }
func testLocalInstall(tg *testgoData, local string) { func testLocalInstall(tg *testgoData, local string) {
tg.t.Helper()
tg.runFail("install", filepath.Join("testdata", local, "easy.go")) tg.runFail("install", filepath.Join("testdata", local, "easy.go"))
} }
...@@ -1052,6 +1095,7 @@ func TestLocalImportsGoInstallShouldFail(t *testing.T) { ...@@ -1052,6 +1095,7 @@ func TestLocalImportsGoInstallShouldFail(t *testing.T) {
const badDirName = `#$%:, &()*;<=>?\^{}` const badDirName = `#$%:, &()*;<=>?\^{}`
func copyBad(tg *testgoData) { func copyBad(tg *testgoData) {
tg.t.Helper()
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
tg.t.Skipf("skipping test because %q is an invalid directory name", badDirName) tg.t.Skipf("skipping test because %q is an invalid directory name", badDirName)
} }
...@@ -2178,6 +2222,7 @@ func TestSourceFileNameOrderPreserved(t *testing.T) { ...@@ -2178,6 +2222,7 @@ func TestSourceFileNameOrderPreserved(t *testing.T) {
// Check that coverage analysis works at all. // Check that coverage analysis works at all.
// Don't worry about the exact numbers but require not 0.0%. // Don't worry about the exact numbers but require not 0.0%.
func checkCoverage(tg *testgoData, data string) { func checkCoverage(tg *testgoData, data string) {
tg.t.Helper()
if regexp.MustCompile(`[^0-9]0\.0%`).MatchString(data) { if regexp.MustCompile(`[^0-9]0\.0%`).MatchString(data) {
tg.t.Error("some coverage results are 0.0%") tg.t.Error("some coverage results are 0.0%")
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment