dmitri.shuralyov.com/go/generated/...

modernize test code slightly

Use t.Fatal instead of t.Error + return, it's shorter and simpler.

Use ReadFile from package os instead of the deprecated io/ioutil.
dmitshur committed 2 years ago commit 6904a2a3d88d6c3ad39737a7c5bf226f5ebbd482
Collapse all
generated_test.go
@@ -1,10 +1,10 @@
package generated_test

import (
	"bytes"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"dmitri.shuralyov.com/go/generated"
)
@@ -38,37 +38,33 @@ func TestParseFile(t *testing.T) {
		{"../generated_test.go", false},
		{"../fuzz_test.go", false},
		{"../LICENSE", false},
	}
	for _, tc := range tests {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			hasGeneratedComment, err := generated.ParseFile(filepath.Join("testdata", tc.name))
			if err != nil {
				t.Error(err)
				return
				t.Fatal(err)
			}
			if got, want := hasGeneratedComment, tc.want; got != want {
				t.Errorf("got hasGeneratedComment %v, want %v", got, want)
			}
		})

		// On Windows, a file that hasn't been gofmt'ed can have \r\n line endings.
		// Though rare and unusual, it's still a valid .go file and needs to be supported.
		t.Run(tc.name+` \r\n line ending version`, func(t *testing.T) {
			// Replace all "\n" line endings with "\r\n".
			b, err := ioutil.ReadFile(filepath.Join("testdata", tc.name))
			b, err := os.ReadFile(filepath.Join("testdata", tc.name))
			if err != nil {
				t.Error(err)
				return
				t.Fatal(err)
			}
			b = bytes.Replace(b, []byte("\n"), []byte("\r\n"), -1)

			hasGeneratedComment, err := generated.Parse(bytes.NewReader(b))
			if err != nil {
				t.Error(err)
				return
				t.Fatal(err)
			}
			if got, want := hasGeneratedComment, tc.want; got != want {
				t.Errorf("got hasGeneratedComment %v, want %v", got, want)
			}
		})
@@ -76,11 +72,11 @@ func TestParseFile(t *testing.T) {
}

func TestParseFileError(t *testing.T) {
	_, err := generated.ParseFile(filepath.Join("testdata", "doesnotexist"))
	if err == nil {
		t.Fatal("got nil error, want non-nil")
		t.Error("got nil error, want non-nil")
	}
}

func BenchmarkParseFile(b *testing.B) {
	for i := 0; i < b.N; i++ {