mirror of
https://github.com/golang/go
synced 2024-09-29 20:24:34 -06:00
cmd/nm: use the test binary as 'nm' instead of rebuilding it
This not only reduces the latency of the test, but also respects build flags like '-race' and '-cover' passed to the 'go test' command. Change-Id: Iffdc60d444a9ff1d4ff5e688bca1c2ef0dfa03c8 Reviewed-on: https://go-review.googlesource.com/c/go/+/450703 Run-TryBot: Bryan Mills <bcmills@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com>
This commit is contained in:
parent
0b82b670ed
commit
5e73f6a029
@ -5,52 +5,48 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"internal/obscuretestdata"
|
"internal/obscuretestdata"
|
||||||
"internal/testenv"
|
"internal/testenv"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
var testnmpath string // path to nm command created for testing purposes
|
// TestMain executes the test binary as the nm command if
|
||||||
|
// GO_NMTEST_IS_NM is set, and runs the tests otherwise.
|
||||||
// The TestMain function creates a nm command for testing purposes and
|
|
||||||
// deletes it after the tests have been run.
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
os.Exit(testMain(m))
|
if os.Getenv("GO_NMTEST_IS_NM") != "" {
|
||||||
|
main()
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testMain(m *testing.M) int {
|
os.Setenv("GO_NMTEST_IS_NM", "1") // Set for subprocesses to inherit.
|
||||||
if !testenv.HasGoBuild() {
|
os.Exit(m.Run())
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpDir, err := os.MkdirTemp("", "TestNM")
|
// nmPath returns the path to the "nm" binary to run.
|
||||||
if err != nil {
|
func nmPath(t testing.TB) string {
|
||||||
fmt.Println("TempDir failed:", err)
|
t.Helper()
|
||||||
return 2
|
testenv.MustHaveExec(t)
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmpDir)
|
|
||||||
|
|
||||||
testnmpath = filepath.Join(tmpDir, "testnm.exe")
|
nmPathOnce.Do(func() {
|
||||||
gotool, err := testenv.GoTool()
|
nmExePath, nmPathErr = os.Executable()
|
||||||
if err != nil {
|
})
|
||||||
fmt.Println("GoTool failed:", err)
|
if nmPathErr != nil {
|
||||||
return 2
|
t.Fatal(nmPathErr)
|
||||||
}
|
}
|
||||||
out, err := exec.Command(gotool, "build", "-o", testnmpath, "cmd/nm").CombinedOutput()
|
return nmExePath
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("go build -o %v cmd/nm: %v\n%s", testnmpath, err, string(out))
|
|
||||||
return 2
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.Run()
|
var (
|
||||||
}
|
nmPathOnce sync.Once
|
||||||
|
nmExePath string
|
||||||
|
nmPathErr error
|
||||||
|
)
|
||||||
|
|
||||||
func TestNonGoExecs(t *testing.T) {
|
func TestNonGoExecs(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
@ -77,7 +73,7 @@ func TestNonGoExecs(t *testing.T) {
|
|||||||
exepath = tf
|
exepath = tf
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command(testnmpath, exepath)
|
cmd := testenv.Command(t, nmPath(t), exepath)
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("go tool nm %v: %v\n%s", exepath, err, string(out))
|
t.Errorf("go tool nm %v: %v\n%s", exepath, err, string(out))
|
||||||
@ -116,12 +112,12 @@ func testGoExec(t *testing.T, iscgo, isexternallinker bool) {
|
|||||||
args = append(args, "-ldflags", "-linkmode="+linkmode)
|
args = append(args, "-ldflags", "-linkmode="+linkmode)
|
||||||
}
|
}
|
||||||
args = append(args, src)
|
args = append(args, src)
|
||||||
out, err := exec.Command(testenv.GoToolPath(t), args...).CombinedOutput()
|
out, err := testenv.Command(t, testenv.GoToolPath(t), args...).CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("building test executable failed: %s %s", err, out)
|
t.Fatalf("building test executable failed: %s %s", err, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err = exec.Command(exe).CombinedOutput()
|
out, err = testenv.Command(t, exe).CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("running test executable failed: %s %s", err, out)
|
t.Fatalf("running test executable failed: %s %s", err, out)
|
||||||
}
|
}
|
||||||
@ -151,7 +147,7 @@ func testGoExec(t *testing.T, iscgo, isexternallinker bool) {
|
|||||||
runtimeSyms["runtime.epclntab"] = "D"
|
runtimeSyms["runtime.epclntab"] = "D"
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err = exec.Command(testnmpath, exe).CombinedOutput()
|
out, err = testenv.Command(t, nmPath(t), exe).CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("go tool nm: %v\n%s", err, string(out))
|
t.Fatalf("go tool nm: %v\n%s", err, string(out))
|
||||||
}
|
}
|
||||||
@ -250,7 +246,7 @@ func testGoLib(t *testing.T, iscgo bool) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command(testenv.GoToolPath(t), "build", "-buildmode=archive", "-o", "mylib.a", ".")
|
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-buildmode=archive", "-o", "mylib.a", ".")
|
||||||
cmd.Dir = libpath
|
cmd.Dir = libpath
|
||||||
cmd.Env = append(os.Environ(), "GOPATH="+gopath)
|
cmd.Env = append(os.Environ(), "GOPATH="+gopath)
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
@ -259,7 +255,7 @@ func testGoLib(t *testing.T, iscgo bool) {
|
|||||||
}
|
}
|
||||||
mylib := filepath.Join(libpath, "mylib.a")
|
mylib := filepath.Join(libpath, "mylib.a")
|
||||||
|
|
||||||
out, err = exec.Command(testnmpath, mylib).CombinedOutput()
|
out, err = testenv.Command(t, nmPath(t), mylib).CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("go tool nm: %v\n%s", err, string(out))
|
t.Fatalf("go tool nm: %v\n%s", err, string(out))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user