Skip to content

Commit

Permalink
cmd/stringer: streamline test subprocesses
Browse files Browse the repository at this point in the history
- Execute the test binary itself as cmd/stringer instead of invoking (and
cleaning up after) 'go build'.
- Replace os.MkdirTemp with T.TempDir

Changes are similar to https://go.dev/cl/377836.

Change-Id: I5f9fca20e0f1f045826c385d556257fc5982ed53
GitHub-Last-Rev: f6c6b77
GitHub-Pull-Request: #425
Reviewed-on: https://go-review.googlesource.com/c/tools/+/464350
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
  • Loading branch information
alexandear authored and gopherbot committed Feb 6, 2023
1 parent 6b6857a commit f124b50
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
54 changes: 33 additions & 21 deletions cmd/stringer/endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"path"
"path/filepath"
"strings"
"sync"
"testing"

"golang.org/x/tools/internal/testenv"
"golang.org/x/tools/internal/typeparams"
)

Expand All @@ -30,9 +30,22 @@ import (
// we run stringer -type X and then compile and run the program. The resulting
// binary panics if the String method for X is not correct, including for error cases.

func TestMain(m *testing.M) {
if os.Getenv("STRINGER_TEST_IS_STRINGER") != "" {
main()
os.Exit(0)
}

// Inform subprocesses that they should run the cmd/stringer main instead of
// running tests. It's a close approximation to building and running the real
// command, and much less complicated and expensive to build and clean up.
os.Setenv("STRINGER_TEST_IS_STRINGER", "1")

os.Exit(m.Run())
}

func TestEndToEnd(t *testing.T) {
dir, stringer := buildStringer(t)
defer os.RemoveAll(dir)
stringer := stringerPath(t)
// Read the testdata directory.
fd, err := os.Open("testdata")
if err != nil {
Expand Down Expand Up @@ -64,7 +77,7 @@ func TestEndToEnd(t *testing.T) {
t.Logf("cgo is not enabled for %s", name)
continue
}
stringerCompileAndRun(t, dir, stringer, typeName(name), name)
stringerCompileAndRun(t, t.TempDir(), stringer, typeName(name), name)
}
}

Expand All @@ -91,8 +104,8 @@ func moreTests(t *testing.T, dirname, prefix string) []string {

// TestTags verifies that the -tags flag works as advertised.
func TestTags(t *testing.T) {
dir, stringer := buildStringer(t)
defer os.RemoveAll(dir)
stringer := stringerPath(t)
dir := t.TempDir()
var (
protectedConst = []byte("TagProtected")
output = filepath.Join(dir, "const_string.go")
Expand Down Expand Up @@ -139,8 +152,8 @@ func TestTags(t *testing.T) {
// TestConstValueChange verifies that if a constant value changes and
// the stringer code is not regenerated, we'll get a compiler error.
func TestConstValueChange(t *testing.T) {
dir, stringer := buildStringer(t)
defer os.RemoveAll(dir)
stringer := stringerPath(t)
dir := t.TempDir()
source := filepath.Join(dir, "day.go")
err := copy(source, filepath.Join("testdata", "day.go"))
if err != nil {
Expand Down Expand Up @@ -178,21 +191,20 @@ func TestConstValueChange(t *testing.T) {
}
}

// buildStringer creates a temporary directory and installs stringer there.
func buildStringer(t *testing.T) (dir string, stringer string) {
t.Helper()
testenv.NeedsTool(t, "go")
var exe struct {
path string
err error
once sync.Once
}

dir, err := os.MkdirTemp("", "stringer")
if err != nil {
t.Fatal(err)
}
stringer = filepath.Join(dir, "stringer.exe")
err = run("go", "build", "-o", stringer)
if err != nil {
t.Fatalf("building stringer: %s", err)
func stringerPath(t *testing.T) string {
exe.once.Do(func() {
exe.path, exe.err = os.Executable()
})
if exe.err != nil {
t.Fatal(exe.err)
}
return dir, stringer
return exe.path
}

// stringerCompileAndRun runs stringer for the named file and compiles and
Expand Down
7 changes: 1 addition & 6 deletions cmd/stringer/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,7 @@ func (i Token) String() string {
func TestGolden(t *testing.T) {
testenv.NeedsTool(t, "go")

dir, err := os.MkdirTemp("", "stringer")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir)

dir := t.TempDir()
for _, test := range golden {
g := Generator{
trimPrefix: test.trimPrefix,
Expand Down

0 comments on commit f124b50

Please sign in to comment.