Skip to content
18 changes: 5 additions & 13 deletions endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
// go command is not available on android

//go:build !android
// +build !android

package main

import (
"fmt"
"go/build"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
37 changes: 14 additions & 23 deletions golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -36,6 +36,7 @@ var golden = []Golden{
var goldenJSON = []Golden{
{"primeJson", primeJsonIn},
}

var goldenText = []Golden{
{"primeText", primeTextIn},
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -482,32 +474,31 @@ 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)
//}
t.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, expected)
}
}

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)
}
13 changes: 6 additions & 7 deletions stringer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"go/importer"
"go/token"
"go/types"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading