From f26c29723f372284ff7dc0dbf30bae5561a3e618 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 29 Aug 2024 19:00:00 -0700 Subject: [PATCH] cmd: use testenv.Executable helper Change-Id: I25ac0e8d25d760bfde3bb7700f0feaa23f3e8ab1 Reviewed-on: https://go-review.googlesource.com/c/go/+/609302 Reviewed-by: Ian Lance Taylor Reviewed-by: Cherry Mui Auto-Submit: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI --- src/cmd/addr2line/addr2line_test.go | 28 +++------------------------ src/cmd/covdata/tool_test.go | 13 +------------ src/cmd/cover/cover_test.go | 11 +---------- src/cmd/nm/nm_test.go | 27 +++----------------------- src/cmd/objdump/objdump_test.go | 30 ++++------------------------- src/cmd/pack/pack_test.go | 18 +---------------- src/cmd/pprof/pprof_test.go | 18 +---------------- src/cmd/vet/vet_test.go | 18 +---------------- 8 files changed, 15 insertions(+), 148 deletions(-) diff --git a/src/cmd/addr2line/addr2line_test.go b/src/cmd/addr2line/addr2line_test.go index e5b0a0fdae..5393eb7fe0 100644 --- a/src/cmd/addr2line/addr2line_test.go +++ b/src/cmd/addr2line/addr2line_test.go @@ -12,7 +12,6 @@ import ( "path/filepath" "runtime" "strings" - "sync" "testing" ) @@ -28,26 +27,6 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } -// addr2linePath returns the path to the "addr2line" binary to run. -func addr2linePath(t testing.TB) string { - t.Helper() - testenv.MustHaveExec(t) - - addr2linePathOnce.Do(func() { - addr2lineExePath, addr2linePathErr = os.Executable() - }) - if addr2linePathErr != nil { - t.Fatal(addr2linePathErr) - } - return addr2lineExePath -} - -var ( - addr2linePathOnce sync.Once - addr2lineExePath string - addr2linePathErr error -) - func loadSyms(t *testing.T, dbgExePath string) map[string]string { cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "nm", dbgExePath) out, err := cmd.CombinedOutput() @@ -70,7 +49,7 @@ func loadSyms(t *testing.T, dbgExePath string) map[string]string { } func runAddr2Line(t *testing.T, dbgExePath, addr string) (funcname, path, lineno string) { - cmd := testenv.Command(t, addr2linePath(t), dbgExePath) + cmd := testenv.Command(t, testenv.Executable(t), dbgExePath) cmd.Stdin = strings.NewReader(addr) out, err := cmd.CombinedOutput() if err != nil { @@ -108,19 +87,18 @@ func testAddr2Line(t *testing.T, dbgExePath, addr string) { // Debug paths are stored slash-separated, so convert to system-native. srcPath = filepath.FromSlash(srcPath) fi2, err := os.Stat(srcPath) - if err != nil { t.Fatalf("Stat failed: %v", err) } if !os.SameFile(fi1, fi2) { t.Fatalf("addr2line_test.go and %s are not same file", srcPath) } - if want := "124"; srcLineNo != want { + if want := "102"; srcLineNo != want { t.Fatalf("line number = %v; want %s", srcLineNo, want) } } -// This is line 123. The test depends on that. +// This is line 101. The test depends on that. func TestAddr2Line(t *testing.T) { testenv.MustHaveGoBuild(t) diff --git a/src/cmd/covdata/tool_test.go b/src/cmd/covdata/tool_test.go index 4e56f35798..757a245047 100644 --- a/src/cmd/covdata/tool_test.go +++ b/src/cmd/covdata/tool_test.go @@ -21,17 +21,6 @@ import ( "testing" ) -// testcovdata returns the path to the unit test executable to be used as -// standin for 'go tool covdata'. -func testcovdata(t testing.TB) string { - exe, err := os.Executable() - if err != nil { - t.Helper() - t.Fatal(err) - } - return exe -} - // Top level tempdir for test. var testTempDir string @@ -184,7 +173,7 @@ func TestCovTool(t *testing.T) { s.exepath3, s.exedir3 = buildProg(t, "prog1", dir, "atomic", flags) // Reuse unit test executable as tool to be tested. - s.tool = testcovdata(t) + s.tool = testenv.Executable(t) // Create a few coverage output dirs. for i := 0; i < 4; i++ { diff --git a/src/cmd/cover/cover_test.go b/src/cmd/cover/cover_test.go index 7bfe2d0728..431c0560f6 100644 --- a/src/cmd/cover/cover_test.go +++ b/src/cmd/cover/cover_test.go @@ -33,12 +33,7 @@ const ( // test. At one point this was created via "go build"; we now reuse the unit // test executable itself. func testcover(t testing.TB) string { - exe, err := os.Executable() - if err != nil { - t.Helper() - t.Fatal(err) - } - return exe + return testenv.Executable(t) } // testTempDir is a temporary directory created in TestMain. @@ -113,8 +108,6 @@ func tempDir(t *testing.T) string { // "-toolexec" wrapper program to invoke the cover test executable // itself via "go test -cover". func TestCoverWithToolExec(t *testing.T) { - testenv.MustHaveExec(t) - toolexecArg := "-toolexec=" + testcover(t) t.Run("CoverHTML", func(t *testing.T) { @@ -338,8 +331,6 @@ func findDirectives(source []byte) []directiveInfo { // Makes sure that `cover -func=profile.cov` reports accurate coverage. // Issue #20515. func TestCoverFunc(t *testing.T) { - testenv.MustHaveExec(t) - // testcover -func ./testdata/profile.cov coverProfile := filepath.Join(testdata, "profile.cov") cmd := testenv.Command(t, testcover(t), "-func", coverProfile) diff --git a/src/cmd/nm/nm_test.go b/src/cmd/nm/nm_test.go index 530a720f2b..4ee4217c59 100644 --- a/src/cmd/nm/nm_test.go +++ b/src/cmd/nm/nm_test.go @@ -12,7 +12,6 @@ import ( "path/filepath" "runtime" "strings" - "sync" "testing" "text/template" ) @@ -29,26 +28,6 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } -// nmPath returns the path to the "nm" binary to run. -func nmPath(t testing.TB) string { - t.Helper() - testenv.MustHaveExec(t) - - nmPathOnce.Do(func() { - nmExePath, nmPathErr = os.Executable() - }) - if nmPathErr != nil { - t.Fatal(nmPathErr) - } - return nmExePath -} - -var ( - nmPathOnce sync.Once - nmExePath string - nmPathErr error -) - func TestNonGoExecs(t *testing.T) { t.Parallel() testfiles := []string{ @@ -74,7 +53,7 @@ func TestNonGoExecs(t *testing.T) { exepath = tf } - cmd := testenv.Command(t, nmPath(t), exepath) + cmd := testenv.Command(t, testenv.Executable(t), exepath) out, err := cmd.CombinedOutput() if err != nil { t.Errorf("go tool nm %v: %v\n%s", exepath, err, string(out)) @@ -148,7 +127,7 @@ func testGoExec(t *testing.T, iscgo, isexternallinker bool) { runtimeSyms["runtime.epclntab"] = "D" } - out, err = testenv.Command(t, nmPath(t), exe).CombinedOutput() + out, err = testenv.Command(t, testenv.Executable(t), exe).CombinedOutput() if err != nil { t.Fatalf("go tool nm: %v\n%s", err, string(out)) } @@ -259,7 +238,7 @@ func testGoLib(t *testing.T, iscgo bool) { } mylib := filepath.Join(libpath, "mylib.a") - out, err = testenv.Command(t, nmPath(t), mylib).CombinedOutput() + out, err = testenv.Command(t, testenv.Executable(t), mylib).CombinedOutput() if err != nil { t.Fatalf("go tool nm: %v\n%s", err, string(out)) } diff --git a/src/cmd/objdump/objdump_test.go b/src/cmd/objdump/objdump_test.go index ba8c4fbe19..d256e59afe 100644 --- a/src/cmd/objdump/objdump_test.go +++ b/src/cmd/objdump/objdump_test.go @@ -14,7 +14,6 @@ import ( "path/filepath" "runtime" "strings" - "sync" "testing" ) @@ -30,26 +29,6 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } -// objdumpPath returns the path to the "objdump" binary to run. -func objdumpPath(t testing.TB) string { - t.Helper() - testenv.MustHaveExec(t) - - objdumpPathOnce.Do(func() { - objdumpExePath, objdumpPathErr = os.Executable() - }) - if objdumpPathErr != nil { - t.Fatal(objdumpPathErr) - } - return objdumpExePath -} - -var ( - objdumpPathOnce sync.Once - objdumpExePath string - objdumpPathErr error -) - var x86Need = []string{ // for both 386 and AMD64 "JMP main.main(SB)", "CALL main.Println(SB)", @@ -222,7 +201,7 @@ func testDisasm(t *testing.T, srcfname string, printCode bool, printGnuAsm bool, if printGnuAsm { args = append([]string{"-gnu"}, args...) } - cmd = testenv.Command(t, objdumpPath(t), args...) + cmd = testenv.Command(t, testenv.Executable(t), args...) cmd.Dir = "testdata" // "Bad line" bug #36683 is sensitive to being run in the source directory out, err = cmd.CombinedOutput() t.Logf("Running %v", cmd.Args) @@ -320,7 +299,7 @@ func TestDisasmGoobj(t *testing.T) { hello, } - out, err = testenv.Command(t, objdumpPath(t), args...).CombinedOutput() + out, err = testenv.Command(t, testenv.Executable(t), args...).CombinedOutput() if err != nil { t.Fatalf("objdump fmthello.o: %v\n%s", err, out) } @@ -361,7 +340,7 @@ func TestGoobjFileNumber(t *testing.T) { t.Fatalf("build failed: %v\n%s", err, out) } - cmd = testenv.Command(t, objdumpPath(t), obj) + cmd = testenv.Command(t, testenv.Executable(t), obj) out, err = cmd.CombinedOutput() if err != nil { t.Fatalf("objdump failed: %v\n%s", err, out) @@ -380,11 +359,10 @@ func TestGoobjFileNumber(t *testing.T) { } func TestGoObjOtherVersion(t *testing.T) { - testenv.MustHaveExec(t) t.Parallel() obj := filepath.Join("testdata", "go116.o") - cmd := testenv.Command(t, objdumpPath(t), obj) + cmd := testenv.Command(t, testenv.Executable(t), obj) out, err := cmd.CombinedOutput() if err == nil { t.Fatalf("objdump go116.o succeeded unexpectedly") diff --git a/src/cmd/pack/pack_test.go b/src/cmd/pack/pack_test.go index c3a63424dd..083c12d9ef 100644 --- a/src/cmd/pack/pack_test.go +++ b/src/cmd/pack/pack_test.go @@ -15,7 +15,6 @@ import ( "path/filepath" "runtime" "strings" - "sync" "testing" "time" ) @@ -34,24 +33,9 @@ func TestMain(m *testing.M) { // packPath returns the path to the "pack" binary to run. func packPath(t testing.TB) string { - t.Helper() - testenv.MustHaveExec(t) - - packPathOnce.Do(func() { - packExePath, packPathErr = os.Executable() - }) - if packPathErr != nil { - t.Fatal(packPathErr) - } - return packExePath + return testenv.Executable(t) } -var ( - packPathOnce sync.Once - packExePath string - packPathErr error -) - // testCreate creates an archive in the specified directory. func testCreate(t *testing.T, dir string) { name := filepath.Join(dir, "pack.a") diff --git a/src/cmd/pprof/pprof_test.go b/src/cmd/pprof/pprof_test.go index 494cd8f24c..9afd817a4e 100644 --- a/src/cmd/pprof/pprof_test.go +++ b/src/cmd/pprof/pprof_test.go @@ -10,7 +10,6 @@ import ( "path/filepath" "runtime" "strings" - "sync" "testing" ) @@ -28,24 +27,9 @@ func TestMain(m *testing.M) { // pprofPath returns the path to the "pprof" binary to run. func pprofPath(t testing.TB) string { - t.Helper() - testenv.MustHaveExec(t) - - pprofPathOnce.Do(func() { - pprofExePath, pprofPathErr = os.Executable() - }) - if pprofPathErr != nil { - t.Fatal(pprofPathErr) - } - return pprofExePath + return testenv.Executable(t) } -var ( - pprofPathOnce sync.Once - pprofExePath string - pprofPathErr error -) - // See also runtime/pprof.cpuProfilingBroken. func mustHaveCPUProfiling(t *testing.T) { switch runtime.GOOS { diff --git a/src/cmd/vet/vet_test.go b/src/cmd/vet/vet_test.go index ad42cf1d7c..f1450dcbd2 100644 --- a/src/cmd/vet/vet_test.go +++ b/src/cmd/vet/vet_test.go @@ -17,7 +17,6 @@ import ( "regexp" "strconv" "strings" - "sync" "testing" ) @@ -35,24 +34,9 @@ func TestMain(m *testing.M) { // vetPath returns the path to the "vet" binary to run. func vetPath(t testing.TB) string { - t.Helper() - testenv.MustHaveExec(t) - - vetPathOnce.Do(func() { - vetExePath, vetPathErr = os.Executable() - }) - if vetPathErr != nil { - t.Fatal(vetPathErr) - } - return vetExePath + return testenv.Executable(t) } -var ( - vetPathOnce sync.Once - vetExePath string - vetPathErr error -) - func vetCmd(t *testing.T, arg, pkg string) *exec.Cmd { cmd := testenv.Command(t, testenv.GoToolPath(t), "vet", "-vettool="+vetPath(t), arg, path.Join("cmd/vet/testdata", pkg)) cmd.Env = os.Environ() -- 2.48.1