From 36df8eef6ee6ea690c4d0e7dd3103f81de5e5e07 Mon Sep 17 00:00:00 2001 From: Nathan Baulch Date: Sun, 24 Aug 2025 20:18:46 +1000 Subject: [PATCH] General cleanup (#999) --- .github/workflows/codeql-analysis.yml | 2 +- CONTRIBUTING.md | 18 ++++++++-------- benchmark/bench_datasets_test.go | 4 ++-- benchmark/benchmark_test.go | 8 +++---- fuzz_test.go | 4 ++-- internal/cli/cli.go | 5 ++--- internal/cli/cli_test.go | 21 +++++++++---------- .../imported_tests/unmarshal_imported_test.go | 4 ++-- internal/testsuite/testsuite.go | 2 +- marshaler.go | 2 +- marshaler_test.go | 6 ++++-- toml_testgen_support_test.go | 2 +- unmarshaler_test.go | 9 +++++--- unstable/ast.go | 2 +- unstable/parser.go | 2 +- 15 files changed, 47 insertions(+), 44 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 937a2c2..301a151 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -47,7 +47,7 @@ jobs: # Prefix the list here with "+" to use these queries and those in the config file. # queries: ./path/to/local/query, your-org/your-repo/queries@main - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # If this step fails, then you should remove it and run the build manually (see below) - name: Autobuild uses: github/codeql-action/autobuild@v3 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 96ecf9e..3949d2a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -33,7 +33,7 @@ The documentation is present in the [README][readme] and thorough the source code. On release, it gets updated on [pkg.go.dev][pkg.go.dev]. To make a change to the documentation, create a pull request with your proposed changes. For simple changes like that, the easiest way to go is probably the "Fork this -project and edit the file" button on Github, displayed at the top right of the +project and edit the file" button on GitHub, displayed at the top right of the file. Unless it's a trivial change (for example a typo), provide a little bit of context in your pull request description or commit message. @@ -111,7 +111,7 @@ code lowers the coverage. Go-toml aims to stay efficient. We rely on a set of scenarios executed with Go's builtin benchmark systems. Because of their noisy nature, containers provided by -Github Actions cannot be reliably used for benchmarking. As a result, you are +GitHub Actions cannot be reliably used for benchmarking. As a result, you are responsible for checking that your changes do not incur a performance penalty. You can run their following to execute benchmarks: @@ -168,13 +168,13 @@ Checklist: 1. Decide on the next version number. Use semver. Review commits since last version to assess. 2. Tag release. For example: -``` -git checkout v2 -git pull -git tag v2.2.0 -git push --tags -``` -3. CI automatically builds a draft Github release. Review it and edit as + ``` + git checkout v2 + git pull + git tag v2.2.0 + git push --tags + ``` +3. CI automatically builds a draft GitHub release. Review it and edit as necessary. Look for "Other changes". That would indicate a pull request not labeled properly. Tweak labels and pull request titles until changelog looks good for users. diff --git a/benchmark/bench_datasets_test.go b/benchmark/bench_datasets_test.go index e45cbde..ee88f03 100644 --- a/benchmark/bench_datasets_test.go +++ b/benchmark/bench_datasets_test.go @@ -3,7 +3,7 @@ package benchmark_test import ( "compress/gzip" "encoding/json" - "io/ioutil" + "io" "os" "path/filepath" "testing" @@ -74,7 +74,7 @@ func fixture(tb testing.TB, path string) []byte { gz, err := gzip.NewReader(f) assert.NoError(tb, err) - buf, err := ioutil.ReadAll(gz) + buf, err := io.ReadAll(gz) assert.NoError(tb, err) return buf } diff --git a/benchmark/benchmark_test.go b/benchmark/benchmark_test.go index a9ea834..9cc2c20 100644 --- a/benchmark/benchmark_test.go +++ b/benchmark/benchmark_test.go @@ -2,7 +2,7 @@ package benchmark_test import ( "bytes" - "io/ioutil" + "os" "testing" "time" @@ -59,7 +59,7 @@ func BenchmarkUnmarshal(b *testing.B) { }) b.Run("ReferenceFile", func(b *testing.B) { - bytes, err := ioutil.ReadFile("benchmark.toml") + bytes, err := os.ReadFile("benchmark.toml") if err != nil { b.Fatal(err) } @@ -165,7 +165,7 @@ func BenchmarkMarshal(b *testing.B) { }) b.Run("ReferenceFile", func(b *testing.B) { - bytes, err := ioutil.ReadFile("benchmark.toml") + bytes, err := os.ReadFile("benchmark.toml") if err != nil { b.Fatal(err) } @@ -344,7 +344,7 @@ type benchmarkDoc struct { } func TestUnmarshalReferenceFile(t *testing.T) { - bytes, err := ioutil.ReadFile("benchmark.toml") + bytes, err := os.ReadFile("benchmark.toml") assert.NoError(t, err) d := benchmarkDoc{} err = toml.Unmarshal(bytes, &d) diff --git a/fuzz_test.go b/fuzz_test.go index 4da03f0..465f76b 100644 --- a/fuzz_test.go +++ b/fuzz_test.go @@ -1,7 +1,7 @@ package toml_test import ( - "io/ioutil" + "os" "strings" "testing" @@ -10,7 +10,7 @@ import ( ) func FuzzUnmarshal(f *testing.F) { - file, err := ioutil.ReadFile("benchmark/benchmark.toml") + file, err := os.ReadFile("benchmark/benchmark.toml") if err != nil { panic(err) } diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 0fdde19..079b19e 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -6,7 +6,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "os" "github.com/pelletier/go-toml/v2" @@ -72,7 +71,7 @@ func (p *Program) runAllFilesInPlace(files []string) error { } func (p *Program) runFileInPlace(path string) error { - in, err := ioutil.ReadFile(path) + in, err := os.ReadFile(path) if err != nil { return err } @@ -84,5 +83,5 @@ func (p *Program) runFileInPlace(path string) error { return err } - return ioutil.WriteFile(path, out.Bytes(), 0600) + return os.WriteFile(path, out.Bytes(), 0600) } diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index 33a4dec..ff8e1ea 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "path" "strings" @@ -63,7 +62,7 @@ func TestProcessMainStdinDecodeErr(t *testing.T) { } func TestProcessMainFileExists(t *testing.T) { - tmpfile, err := ioutil.TempFile("", "example") + tmpfile, err := os.CreateTemp("", "example") assert.NoError(t, err) defer os.Remove(tmpfile.Name()) _, err = tmpfile.Write([]byte(`some data`)) @@ -95,16 +94,16 @@ func TestProcessMainFileDoesNotExist(t *testing.T) { } func TestProcessMainFilesInPlace(t *testing.T) { - dir, err := ioutil.TempDir("", "") + dir, err := os.MkdirTemp("", "") assert.NoError(t, err) defer os.RemoveAll(dir) path1 := path.Join(dir, "file1") path2 := path.Join(dir, "file2") - err = ioutil.WriteFile(path1, []byte("content 1"), 0600) + err = os.WriteFile(path1, []byte("content 1"), 0600) assert.NoError(t, err) - err = ioutil.WriteFile(path2, []byte("content 2"), 0600) + err = os.WriteFile(path2, []byte("content 2"), 0600) assert.NoError(t, err) p := Program{ @@ -116,11 +115,11 @@ func TestProcessMainFilesInPlace(t *testing.T) { assert.Equal(t, 0, exit) - v1, err := ioutil.ReadFile(path1) + v1, err := os.ReadFile(path1) assert.NoError(t, err) assert.Equal(t, "1", string(v1)) - v2, err := ioutil.ReadFile(path2) + v2, err := os.ReadFile(path2) assert.NoError(t, err) assert.Equal(t, "2", string(v2)) } @@ -137,13 +136,13 @@ func TestProcessMainFilesInPlaceErrRead(t *testing.T) { } func TestProcessMainFilesInPlaceFailFn(t *testing.T) { - dir, err := ioutil.TempDir("", "") + dir, err := os.MkdirTemp("", "") assert.NoError(t, err) defer os.RemoveAll(dir) path1 := path.Join(dir, "file1") - err = ioutil.WriteFile(path1, []byte("content 1"), 0600) + err = os.WriteFile(path1, []byte("content 1"), 0600) assert.NoError(t, err) p := Program{ @@ -155,13 +154,13 @@ func TestProcessMainFilesInPlaceFailFn(t *testing.T) { assert.Equal(t, -1, exit) - v1, err := ioutil.ReadFile(path1) + v1, err := os.ReadFile(path1) assert.NoError(t, err) assert.Equal(t, "content 1", string(v1)) } func dummyFileFn(r io.Reader, w io.Writer) error { - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { return err } diff --git a/internal/imported_tests/unmarshal_imported_test.go b/internal/imported_tests/unmarshal_imported_test.go index 4d99f3b..1eb7ecc 100644 --- a/internal/imported_tests/unmarshal_imported_test.go +++ b/internal/imported_tests/unmarshal_imported_test.go @@ -2283,7 +2283,7 @@ func (c *Custom) UnmarshalTOML(v interface{}) error { return nil } -func TestGithubIssue431(t *testing.T) { +func TestGitHubIssue431(t *testing.T) { doc := `key = "value"` var c Config if err := toml.Unmarshal([]byte(doc), &c); err != nil { @@ -2321,7 +2321,7 @@ type config437 struct { } `toml:"HTTP"` } -func TestGithubIssue437(t *testing.T) { +func TestGitHubIssue437(t *testing.T) { t.Skipf("unmarshalTOML not implemented") src := ` [HTTP] diff --git a/internal/testsuite/testsuite.go b/internal/testsuite/testsuite.go index e2d9ff6..720cb8b 100644 --- a/internal/testsuite/testsuite.go +++ b/internal/testsuite/testsuite.go @@ -10,7 +10,7 @@ import ( "github.com/pelletier/go-toml/v2" ) -// Marshal is a helpfer function for calling toml.Marshal +// Marshal is a helper function for calling toml.Marshal // // Only needed to avoid package import loops. func Marshal(v interface{}) ([]byte, error) { diff --git a/marshaler.go b/marshaler.go index 7cf2602..334c828 100644 --- a/marshaler.go +++ b/marshaler.go @@ -846,7 +846,7 @@ func parseTag(tag string) (string, tagOptions) { } raw := tag[idx+1:] - tag = string(tag[:idx]) + tag = tag[:idx] for raw != "" { var o string i := strings.Index(raw, ",") diff --git a/marshaler_test.go b/marshaler_test.go index b8d8134..675e6d9 100644 --- a/marshaler_test.go +++ b/marshaler_test.go @@ -1326,7 +1326,8 @@ func TestIssue786(t *testing.T) { config.Custom = []Custom{{Name: "omit", General: General{Randomize: false}}} config.Custom = append(config.Custom, Custom{Name: "present", General: General{From: "-2d", Randomize: true}}) encoder := toml.NewEncoder(buf) - encoder.Encode(config) + err = encoder.Encode(config) + assert.NoError(t, err) expected := `# from in graphite-web format, the local TZ is used from = '-2d' @@ -1372,7 +1373,8 @@ func TestMarshalIssue888(t *testing.T) { } encoder := toml.NewEncoder(buf).SetIndentTables(true) - encoder.Encode(config) + err := encoder.Encode(config) + assert.NoError(t, err) expected := `# custom config [[Custom]] diff --git a/toml_testgen_support_test.go b/toml_testgen_support_test.go index 3ca1b79..8f7fd88 100644 --- a/toml_testgen_support_test.go +++ b/toml_testgen_support_test.go @@ -52,7 +52,7 @@ func testgenValid(t *testing.T, input string, jsonRef string) { assert.NoError(t, err) var actual interface{} - err = json.Unmarshal([]byte(j), &actual) + err = json.Unmarshal(j, &actual) assert.NoError(t, err) testsuite.CmpJSON(t, "", ref, actual) diff --git a/unmarshaler_test.go b/unmarshaler_test.go index d566313..14f3db9 100644 --- a/unmarshaler_test.go +++ b/unmarshaler_test.go @@ -2977,7 +2977,8 @@ func TestIssue931(t *testing.T) { Name = 'd' `) - toml.Unmarshal(b, &its) + err := toml.Unmarshal(b, &its) + assert.NoError(t, err) assert.Equal(t, items{[]item{{"c"}, {"d"}}}, its) } @@ -2998,7 +2999,8 @@ func TestIssue931Interface(t *testing.T) { Name = 'd' `) - toml.Unmarshal(b, &its) + err := toml.Unmarshal(b, &its) + assert.NoError(t, err) assert.Equal(t, items{[]interface{}{item{"Name": "c"}, item{"Name": "d"}}}, its) } @@ -3024,7 +3026,8 @@ func TestIssue931SliceInterface(t *testing.T) { Name = 'd' `) - toml.Unmarshal(b, &its) + err := toml.Unmarshal(b, &its) + assert.NoError(t, err) assert.Equal(t, items{[]interface{}{item{"Name": "c"}, item{"Name": "d"}}}, its) } diff --git a/unstable/ast.go b/unstable/ast.go index f526bf2..5f3acaf 100644 --- a/unstable/ast.go +++ b/unstable/ast.go @@ -89,7 +89,7 @@ func (n *Node) Next() *Node { } // Child returns a pointer to the first child node of this node. Other children -// can be accessed calling Next on the first child. Returns an nil if this Node +// can be accessed calling Next on the first child. Returns nil if this Node // has no child. func (n *Node) Child() *Node { if n.child == 0 { diff --git a/unstable/parser.go b/unstable/parser.go index 50358a4..e29f5e1 100644 --- a/unstable/parser.go +++ b/unstable/parser.go @@ -1076,7 +1076,7 @@ byteLoop: } case c == 'T' || c == 't' || c == ':' || c == '.': hasTime = true - case c == '+' || c == '-' || c == 'Z' || c == 'z': + case c == '+' || c == 'Z' || c == 'z': hasTz = true case c == ' ': if !seenSpace && i+1 < len(b) && isDigit(b[i+1]) {