From 0896172331c6ea8c524df6f59f3030334bc4ab49 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 23 Jan 2025 13:59:55 +0100 Subject: [PATCH 1/6] substitute deprecated ioutil.TempDir by t.TempDir --- endtoend_test.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/endtoend_test.go b/endtoend_test.go index 203ecd6..bd325da 100644 --- a/endtoend_test.go +++ b/endtoend_test.go @@ -4,6 +4,7 @@ // go command is not available on android +//go:build !android // +build !android package main @@ -12,7 +13,6 @@ import ( "fmt" "go/build" "io" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -40,15 +40,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) } From 3701190ed042199a9e53528b5fad7dd5a2fd1916 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 23 Jan 2025 14:01:07 +0100 Subject: [PATCH 2/6] substitute deprecated ioutil and refactor loadGolden to load data just once --- golden_test.go | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/golden_test.go b/golden_test.go index 03479d2..1afa5ca 100644 --- a/golden_test.go +++ b/golden_test.go @@ -10,7 +10,7 @@ package main import ( - "io/ioutil" + "io" "os" "path/filepath" "strings" @@ -360,19 +360,10 @@ func runGoldenTest(t *testing.T, test Golden, file := test.name + ".go" input := "package test\n" + test.input - dir, err := ioutil.TempDir("", "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 = ioutil.WriteFile(absFile, []byte(input), 0644) + err := os.WriteFile(absFile, []byte(input), 0644) if err != nil { t.Error(err) } @@ -384,27 +375,31 @@ func runGoldenTest(t *testing.T, test Golden, } g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, generateGQLGen, "noop", trimPrefix, prefix, linecomment, generateValuesMethod) got := string(g.format()) - if got != loadGolden(test.name) { + 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, loadGolden(test.name)) + t.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, expected) } } -func loadGolden(name string) string { +func loadGolden(t *testing.T, name string) string { + t.Helper() + fh, err := os.Open("testdata/" + name + ".golden") if err != nil { - return "" + t.Fatalf("unable to open golden file for %s: %v", name, err) } + defer fh.Close() - b, err := ioutil.ReadAll(fh) + + b, err := io.ReadAll(fh) if err != nil { - return "" + t.Fatalf("unable to read golden file for %s: %v", name, err) } - return string(b) + return string(b) } From f84f9ceebdf421e1b701d2d91c7b3c0fa3b95e58 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 23 Jan 2025 14:01:34 +0100 Subject: [PATCH 3/6] substitute deprecated ioutil.TempFile by os.CreateTemp --- stringer.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stringer.go b/stringer.go index 33d3d09..2fbfa8c 100644 --- a/stringer.go +++ b/stringer.go @@ -18,7 +18,6 @@ import ( "go/importer" "go/token" "go/types" - "io/ioutil" "log" "os" "path/filepath" @@ -149,7 +148,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) } From ae2ec325b87af28d604d7906bf51467155dbe20b Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 23 Jan 2025 14:02:06 +0100 Subject: [PATCH 4/6] run go fix ./... and fix buildtag --- endtoend_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/endtoend_test.go b/endtoend_test.go index bd325da..3a08b5a 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 From 2d6d05f9e2952df5ad89cb311ada5247a06f001c Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 23 Jan 2025 14:02:47 +0100 Subject: [PATCH 5/6] run gofumpt -w . to format source code --- endtoend_test.go | 8 +++----- enumer.go | 10 ++++++++++ golden_test.go | 9 +++++---- gqlgen.go | 1 + sql.go | 1 + stringer.go | 5 +++-- util_test.go | 6 ++++-- 7 files changed, 27 insertions(+), 13 deletions(-) diff --git a/endtoend_test.go b/endtoend_test.go index 3a08b5a..f5f663d 100644 --- a/endtoend_test.go +++ b/endtoend_test.go @@ -20,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 diff --git a/enumer.go b/enumer.go index c234209..54ec5e8 100644 --- a/enumer.go +++ b/enumer.go @@ -3,6 +3,7 @@ package main import "fmt" // Arguments to format are: +// // [1]: type name const stringNameToValueMethod = `// %[1]sString retrieves an enum value from the enum constants string name. // Throws an error if the param is not part of the enum. @@ -19,6 +20,7 @@ func %[1]sString(s string) (%[1]s, error) { ` // Arguments to format are: +// // [1]: type name const stringValuesMethod = `// %[1]sValues returns all values of the enum func %[1]sValues() []%[1]s { @@ -27,6 +29,7 @@ func %[1]sValues() []%[1]s { ` // Arguments to format are: +// // [1]: type name const stringsMethod = `// %[1]sStrings returns a slice of all String values of the enum func %[1]sStrings() []string { @@ -37,6 +40,7 @@ func %[1]sStrings() []string { ` // Arguments to format are: +// // [1]: type name const stringBelongsMethodLoop = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise func (i %[1]s) IsA%[1]s() bool { @@ -50,6 +54,7 @@ func (i %[1]s) IsA%[1]s() bool { ` // Arguments to format are: +// // [1]: type name const stringBelongsMethodSet = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise func (i %[1]s) IsA%[1]s() bool { @@ -59,6 +64,7 @@ func (i %[1]s) IsA%[1]s() bool { ` // Arguments to format are: +// // [1]: type name const altStringValuesMethod = `func (%[1]s) Values() []string { return %[1]sStrings() @@ -121,6 +127,7 @@ func (g *Generator) printValueMap(runs [][]Value, typeName string, runsThreshold } g.Printf("}\n\n") } + func (g *Generator) printNamesSlice(runs [][]Value, typeName string, runsThreshold int) { thereAreRuns := len(runs) > 1 && len(runs) <= runsThreshold g.Printf("\nvar _%sNames = []string{\n", typeName) @@ -144,6 +151,7 @@ func (g *Generator) printNamesSlice(runs [][]Value, typeName string, runsThresho } // Arguments to format are: +// // [1]: type name const jsonMethods = ` // MarshalJSON implements the json.Marshaler interface for %[1]s @@ -169,6 +177,7 @@ func (g *Generator) buildJSONMethods(runs [][]Value, typeName string, runsThresh } // Arguments to format are: +// // [1]: type name const textMethods = ` // MarshalText implements the encoding.TextMarshaler interface for %[1]s @@ -189,6 +198,7 @@ func (g *Generator) buildTextMethods(runs [][]Value, typeName string, runsThresh } // Arguments to format are: +// // [1]: type name const yamlMethods = ` // MarshalYAML implements a YAML Marshaler for %[1]s diff --git a/golden_test.go b/golden_test.go index 1afa5ca..5bc1db6 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}, } @@ -354,8 +355,8 @@ func TestGolden(t *testing.T) { func runGoldenTest(t *testing.T, test Golden, generateJSON, generateYAML, generateSQL, generateText, linecomment, generateGQLGen, generateValuesMethod bool, - trimPrefix string, prefix string) { - + trimPrefix string, prefix string, +) { var g Generator file := test.name + ".go" input := "package test\n" + test.input @@ -363,7 +364,7 @@ func runGoldenTest(t *testing.T, test Golden, 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) } diff --git a/gqlgen.go b/gqlgen.go index 45dddbb..bee60e7 100644 --- a/gqlgen.go +++ b/gqlgen.go @@ -1,6 +1,7 @@ package main // Arguments to format are: +// // [1]: type name const gqlgenMethods = ` // MarshalGQL implements the graphql.Marshaler interface for %[1]s diff --git a/sql.go b/sql.go index 09f050c..cd775d3 100644 --- a/sql.go +++ b/sql.go @@ -1,6 +1,7 @@ package main // Arguments to format are: +// // [1]: type name const valueMethod = `func (i %[1]s) Value() (driver.Value, error) { return i.String(), nil diff --git a/stringer.go b/stringer.go index 2fbfa8c..d45b9e1 100644 --- a/stringer.go +++ b/stringer.go @@ -414,7 +414,8 @@ func (g *Generator) prefixValueNames(values []Value, prefix string) { // generate produces the String method for the named type. func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeSQL, includeText, includeGQLGen bool, - transformMethod string, trimPrefix string, addPrefix string, lineComment bool, includeValuesMethod bool) { + transformMethod string, trimPrefix string, addPrefix string, lineComment bool, includeValuesMethod bool, +) { values := make([]Value, 0, 100) for _, file := range g.pkg.files { file.lineComment = lineComment @@ -690,7 +691,7 @@ 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". 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 From f4482e55aa556a0b58741e154a2960a5f23898c3 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Thu, 23 Jan 2025 14:03:05 +0100 Subject: [PATCH 6/6] run gofumpt -w -extra . to format source code --- golden_test.go | 2 +- stringer.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/golden_test.go b/golden_test.go index 5bc1db6..dc9bfbd 100644 --- a/golden_test.go +++ b/golden_test.go @@ -355,7 +355,7 @@ func TestGolden(t *testing.T) { func runGoldenTest(t *testing.T, test Golden, generateJSON, generateYAML, generateSQL, generateText, linecomment, generateGQLGen, generateValuesMethod bool, - trimPrefix string, prefix string, + trimPrefix, prefix string, ) { var g Generator file := test.name + ".go" diff --git a/stringer.go b/stringer.go index d45b9e1..41a01b1 100644 --- a/stringer.go +++ b/stringer.go @@ -244,7 +244,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 @@ -414,7 +414,7 @@ func (g *Generator) prefixValueNames(values []Value, prefix string) { // generate produces the String method for the named type. func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeSQL, includeText, includeGQLGen bool, - transformMethod string, trimPrefix string, addPrefix string, lineComment bool, includeValuesMethod bool, + transformMethod, trimPrefix, addPrefix string, lineComment, includeValuesMethod bool, ) { values := make([]Value, 0, 100) for _, file := range g.pkg.files { @@ -695,7 +695,7 @@ func (g *Generator) declareIndexAndNameVar(run []Value, typeName string) { } // 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 { @@ -717,7 +717,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 { @@ -739,7 +739,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 {