aboutsummaryrefslogtreecommitdiff
path: root/cmd/stringer
diff options
context:
space:
mode:
authorDan Willemsen <dwillemsen@google.com>2023-03-15 13:19:36 -0400
committerDan Willemsen <dwillemsen@google.com>2023-03-15 14:18:08 -0400
commit09c5a32afc5b66f28f166a68afe1fc71afbf9b73 (patch)
tree194d7b0e539d014393564a256bec571e18d6533a /cmd/stringer
parentf10932f763d058b0dcb3acfb795c869996fef47b (diff)
parent031fc75960d487b0b15db12fb328676236a3a39c (diff)
downloadgolang-x-tools-09c5a32afc5b66f28f166a68afe1fc71afbf9b73.tar.gz
Upgrade golang-x-tools to v0.7.0HEADmastermain
Not using external_updater this time to switch to the new upstream tags. Test: treehugger Change-Id: I31488b4958a366ed7f183bb387d3e1446acc13ae
Diffstat (limited to 'cmd/stringer')
-rw-r--r--cmd/stringer/endtoend_test.go59
-rw-r--r--cmd/stringer/golden_test.go10
-rw-r--r--cmd/stringer/stringer.go8
3 files changed, 42 insertions, 35 deletions
diff --git a/cmd/stringer/endtoend_test.go b/cmd/stringer/endtoend_test.go
index 5b969a52e..29eb91860 100644
--- a/cmd/stringer/endtoend_test.go
+++ b/cmd/stringer/endtoend_test.go
@@ -14,15 +14,14 @@ import (
"fmt"
"go/build"
"io"
- "io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
+ "sync"
"testing"
- "golang.org/x/tools/internal/testenv"
"golang.org/x/tools/internal/typeparams"
)
@@ -31,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 {
@@ -65,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)
}
}
@@ -92,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")
@@ -113,7 +125,7 @@ func TestTags(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- result, err := ioutil.ReadFile(output)
+ result, err := os.ReadFile(output)
if err != nil {
t.Fatal(err)
}
@@ -128,7 +140,7 @@ func TestTags(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- result, err = ioutil.ReadFile(output)
+ result, err = os.ReadFile(output)
if err != nil {
t.Fatal(err)
}
@@ -140,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 {
@@ -179,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 := ioutil.TempDir("", "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
diff --git a/cmd/stringer/golden_test.go b/cmd/stringer/golden_test.go
index b29763174..250af05f9 100644
--- a/cmd/stringer/golden_test.go
+++ b/cmd/stringer/golden_test.go
@@ -10,7 +10,6 @@
package main
import (
- "io/ioutil"
"os"
"path/filepath"
"strings"
@@ -452,12 +451,7 @@ func (i Token) String() string {
func TestGolden(t *testing.T) {
testenv.NeedsTool(t, "go")
- dir, err := ioutil.TempDir("", "stringer")
- if err != nil {
- t.Error(err)
- }
- defer os.RemoveAll(dir)
-
+ dir := t.TempDir()
for _, test := range golden {
g := Generator{
trimPrefix: test.trimPrefix,
@@ -466,7 +460,7 @@ func TestGolden(t *testing.T) {
input := "package test\n" + test.input
file := test.name + ".go"
absFile := filepath.Join(dir, file)
- err := ioutil.WriteFile(absFile, []byte(input), 0644)
+ err := os.WriteFile(absFile, []byte(input), 0644)
if err != nil {
t.Error(err)
}
diff --git a/cmd/stringer/stringer.go b/cmd/stringer/stringer.go
index 558a234d6..998d1a51b 100644
--- a/cmd/stringer/stringer.go
+++ b/cmd/stringer/stringer.go
@@ -5,7 +5,9 @@
// Stringer is a tool to automate the creation of methods that satisfy the fmt.Stringer
// interface. Given the name of a (signed or unsigned) integer type T that has constants
// defined, stringer will create a new self-contained Go source file implementing
+//
// func (t T) String() string
+//
// The file is created in the same package and directory as the package that defines T.
// It has helpful defaults designed for use with go generate.
//
@@ -74,7 +76,6 @@ import (
"go/format"
"go/token"
"go/types"
- "io/ioutil"
"log"
"os"
"path/filepath"
@@ -164,7 +165,7 @@ func main() {
baseName := fmt.Sprintf("%s_string.go", types[0])
outputName = filepath.Join(dir, strings.ToLower(baseName))
}
- err := ioutil.WriteFile(outputName, src, 0644)
+ err := os.WriteFile(outputName, src, 0644)
if err != nil {
log.Fatalf("writing output: %s", err)
}
@@ -215,7 +216,7 @@ type Package struct {
// parsePackage exits if there is an error.
func (g *Generator) parsePackage(patterns []string, tags []string) {
cfg := &packages.Config{
- Mode: packages.LoadSyntax,
+ Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax,
// TODO: Need to think about constants in test files. Maybe write type_string_test.go
// in a separate pass? For later.
Tests: false,
@@ -570,6 +571,7 @@ func (g *Generator) buildOneRun(runs [][]Value, typeName string) {
}
// Arguments to format are:
+//
// [1]: type name
// [2]: size of index element (8 for uint8 etc.)
// [3]: less than zero check (for signed types)