diff --git a/endtoend_test.go b/endtoend_test.go index 176803c..c05509e 100644 --- a/endtoend_test.go +++ b/endtoend_test.go @@ -5,7 +5,6 @@ // go command is not available on android //go:build !android -// +build !android package main @@ -13,7 +12,6 @@ import ( "fmt" "go/build" "io" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -22,11 +20,9 @@ import ( "testing" ) -var ( - // GOEXE defines the executable file name suffix (".exe" on Windows, "" on other systems). - // Must be defined here, cannot be read from ENVIRONMENT variables - GOEXE = "" -) +// GOEXE defines the executable file name suffix (".exe" on Windows, "" on other systems). +// Must be defined here, cannot be read from ENVIRONMENT variables +var GOEXE = "" func init() { // Set GOEXE for Windows platform @@ -41,15 +37,11 @@ func init() { // binary panics if the String method for X is not correct, including for error cases. func TestEndToEnd(t *testing.T) { - dir, err := ioutil.TempDir("", "stringer") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() // Create stringer in temporary directory. stringer := filepath.Join(dir, fmt.Sprintf("stringer%s", GOEXE)) - err = run("go", "build", "-o", stringer) + err := run("go", "build", "-o", stringer) if err != nil { t.Fatalf("building stringer: %s", err) } diff --git a/golden_test.go b/golden_test.go index 62c1736..516d166 100644 --- a/golden_test.go +++ b/golden_test.go @@ -21,7 +21,7 @@ import ( type Golden struct { name string input string // input; the package clause is provided when running the test. - //output string // expected output. + // output string // expected output. } var golden = []Golden{ @@ -36,6 +36,7 @@ var golden = []Golden{ var goldenJSON = []Golden{ {"primeJson", primeJsonIn}, } + var goldenText = []Golden{ {"primeText", primeTextIn}, } @@ -457,19 +458,10 @@ func runGoldenTest(t *testing.T, test Golden, opts generateOptions) { file := test.name + ".go" input := "package test\n" + test.input - dir, err := os.MkdirTemp("", "stringer") - if err != nil { - t.Error(err) - } - defer func() { - err = os.RemoveAll(dir) - if err != nil { - t.Error(err) - } - }() + dir := t.TempDir() absFile := filepath.Join(dir, file) - err = os.WriteFile(absFile, []byte(input), 0644) + err := os.WriteFile(absFile, []byte(input), 0o644) if err != nil { t.Error(err) } @@ -482,15 +474,10 @@ func runGoldenTest(t *testing.T, test Golden, opts generateOptions) { g.generate(tokens[1], opts) got := string(g.format()) - expected, err := loadGolden(test.name) - if err != nil { - t.Fatalf("unexpected error while loading golden %q: %v", test.name, err) - } - - if got != expected { + if expected := loadGolden(t, test.name); got != expected { // Use this to help build a golden text when changes are needed //goldenFile := fmt.Sprintf("./testdata/%v.golden", test.name) - //err = ioutil.WriteFile(goldenFile, []byte(got), 0644) + //err = os.WriteFile(goldenFile, []byte(got), 0644) //if err != nil { // t.Error(err) //} @@ -498,16 +485,20 @@ func runGoldenTest(t *testing.T, test Golden, opts generateOptions) { } } -func loadGolden(name string) (string, error) { +func loadGolden(t *testing.T, name string) string { + t.Helper() + fh, err := os.Open("testdata/" + name + ".golden") if err != nil { - return "", err + t.Fatalf("unable to open golden file for %s: %v", name, err) } + defer fh.Close() + b, err := io.ReadAll(fh) if err != nil { - return "", err + t.Fatalf("unable to read golden file for %s: %v", name, err) } - return string(b), nil + return string(b) } diff --git a/stringer.go b/stringer.go index 87c21a6..26fa5c2 100644 --- a/stringer.go +++ b/stringer.go @@ -18,7 +18,6 @@ import ( "go/importer" "go/token" "go/types" - "io/ioutil" "log" "os" "path/filepath" @@ -178,7 +177,7 @@ func main() { } // Write to tmpfile first - tmpFile, err := ioutil.TempFile(dir, fmt.Sprintf("%s_enumer_", typs[0])) + tmpFile, err := os.CreateTemp(dir, fmt.Sprintf("%s_enumer_", typs[0])) if err != nil { log.Fatalf("creating temporary file for output: %s", err) } @@ -274,7 +273,7 @@ type Package struct { // parsePackage analyzes the single package constructed from the patterns and tags. // parsePackage exits if there is an error. -func (g *Generator) parsePackage(patterns []string, tags []string) { +func (g *Generator) parsePackage(patterns, tags []string) { cfg := &packages.Config{ Mode: packages.LoadSyntax, // TODO: Need to think about constants in test files. Maybe write type_string_test.go @@ -734,11 +733,11 @@ func (g *Generator) declareIndexAndNameVar(run []Value, typeName string) { g.Printf("var %s\n", index) index, n = g.createLowerIndexAndNameDecl(run, typeName, "") g.Printf("const %s\n", n) - //g.Printf("var %s\n", index) + // g.Printf("var %s\n", index) } // createIndexAndNameDecl returns the pair of declarations for the run. The caller will add "const" and "var". -func (g *Generator) createLowerIndexAndNameDecl(run []Value, typeName string, suffix string) (string, string) { +func (g *Generator) createLowerIndexAndNameDecl(run []Value, typeName, suffix string) (string, string) { b := new(bytes.Buffer) indexes := make([]int, len(run)) for i := range run { @@ -760,7 +759,7 @@ func (g *Generator) createLowerIndexAndNameDecl(run []Value, typeName string, su } // createIndexAndNameDecl returns the pair of declarations for the run. The caller will add "const" and "var". -func (g *Generator) createIndexAndNameDecl(run []Value, typeName string, suffix string) (string, string) { +func (g *Generator) createIndexAndNameDecl(run []Value, typeName, suffix string) (string, string) { b := new(bytes.Buffer) indexes := make([]int, len(run)) for i := range run { @@ -782,7 +781,7 @@ func (g *Generator) createIndexAndNameDecl(run []Value, typeName string, suffix } // declareNameVars declares the concatenated names string representing all the values in the runs. -func (g *Generator) declareNameVars(runs [][]Value, typeName string, suffix string) { +func (g *Generator) declareNameVars(runs [][]Value, typeName, suffix string) { g.Printf("const _%sName%s = \"", typeName, suffix) for _, run := range runs { for i := range run { diff --git a/util_test.go b/util_test.go index a572666..dda4cec 100644 --- a/util_test.go +++ b/util_test.go @@ -12,8 +12,10 @@ import ( ) // Helpers to save typing in the test cases. -type u []uint64 -type uu [][]uint64 +type ( + u []uint64 + uu [][]uint64 +) type SplitTest struct { input u