]> Cypherpunks repositories - gostls13.git/commitdiff
cmd: use testenv.Executable helper
authorKir Kolyshkin <kolyshkin@gmail.com>
Fri, 30 Aug 2024 02:00:00 +0000 (19:00 -0700)
committerDamien Neil <dneil@google.com>
Tue, 3 Sep 2024 18:03:19 +0000 (18:03 +0000)
Change-Id: I25ac0e8d25d760bfde3bb7700f0feaa23f3e8ab1
Reviewed-on: https://go-review.googlesource.com/c/go/+/609302
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/addr2line/addr2line_test.go
src/cmd/covdata/tool_test.go
src/cmd/cover/cover_test.go
src/cmd/nm/nm_test.go
src/cmd/objdump/objdump_test.go
src/cmd/pack/pack_test.go
src/cmd/pprof/pprof_test.go
src/cmd/vet/vet_test.go

index e5b0a0fdae01e040ea8ce89d18208244d3be1126..5393eb7fe0103ce82ee57536b9de3cc6e9ff4ecf 100644 (file)
@@ -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)
 
index 4e56f3579872eee4d840549b158e48d6790973e5..757a245047eddf788760184201425bc69618d4df 100644 (file)
@@ -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++ {
index 7bfe2d072830483ba66d9c9da8a1d5868fb1e9a8..431c0560f6eb23c5f3eee74a68981a971ee161ae 100644 (file)
@@ -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)
index 530a720f2b9eb70fee4ca1b980d3bb6bf4366adc..4ee4217c59b19412b7cf5257d411f7ffa51200b2 100644 (file)
@@ -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))
        }
index ba8c4fbe192343d33f59e6b81f462fac6da27a2f..d256e59afe3d6dd9385ab1da9ac29b2ec11da2a4 100644 (file)
@@ -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")
index c3a63424dd9d26f2eaaf07c8f09c90684b6d8fc9..083c12d9ef106605529ca381def158754470c823 100644 (file)
@@ -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")
index 494cd8f24c00c2659dd6e1c6e15ac68d1ec61e00..9afd817a4ee0256a6029d08c1111a0b67bf946e1 100644 (file)
@@ -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 {
index ad42cf1d7c46d8256620cd0c9cebcc20ff432bad..f1450dcbd28d2c1df40afbba99bec883a88ed13c 100644 (file)
@@ -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()