Skip to content

Commit

Permalink
test.File: No longer use relative path to testdata (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Aug 5, 2024
1 parent eb1af9c commit 9ab15aa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func TestSomething(t *testing.T) {
// Get $CWD/testdata easily
test.Data(t) // /Users/you/project/package/testdata

// Check against contents of a file (relative to $CWD/testdata)
// including line ending normalisation
test.File(t, "hello\n", "expected.txt")
// Check against contents of a file including line ending normalisation
file := filepath.Join(test.Data(t), "expected.txt")
test.File(t, "hello\n", file)

// Just like the good old reflect.DeepEqual, but with a nicer format
test.DeepEqual(t, []string{"hello"}, []string{"world"}) // Fails
Expand Down
12 changes: 8 additions & 4 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ func Data(t testing.TB) string {

// File fails if got does not match the contents of the given file.
//
// It takes a string and the name of a file (relative to $CWD/testdata) to compare.
// It takes a string and the path of a file to compare, use [Data] to obtain
// the path to the current packages testdata directory.
//
// If the contents differ, the test will fail with output equivalent to [Diff].
//
Expand All @@ -192,10 +193,13 @@ func Data(t testing.TB) string {
// test.File(t, "hello\n", "expected.txt")
func File(t testing.TB, got, file string) {
t.Helper()
file = filepath.Join(Data(t), file)
contents, err := os.ReadFile(file)
f, err := filepath.Abs(file)
if err != nil {
t.Fatalf("could not read %s: %v", file, err)
t.Fatalf("could not make %s absolute: %v", file, err)
}
contents, err := os.ReadFile(f)
if err != nil {
t.Fatalf("could not read %s: %v", f, err)
}

contents = bytes.ReplaceAll(contents, []byte("\r\n"), []byte("\n"))
Expand Down
4 changes: 2 additions & 2 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestPass(t *testing.T) {
"DeepEqual string slice": func(tb testing.TB) { test.DeepEqual(tb, []string{"hello"}, []string{"hello"}) },
"WantErr true": func(tb testing.TB) { test.WantErr(tb, errors.New("uh oh"), true) },
"WantErr false": func(tb testing.TB) { test.WantErr(tb, nilErr(), false) },
"File": func(tb testing.TB) { test.File(tb, "hello\n", "file.txt") },
"File": func(tb testing.TB) { test.File(tb, "hello\n", filepath.Join(test.Data(t), "file.txt")) },
}

for name, fn := range passFns {
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestFail(t *testing.T) {
"DeepEqual string slice": func(tb testing.TB) { test.DeepEqual(tb, []string{"hello"}, []string{"world"}) },
"WantErr true": func(tb testing.TB) { test.WantErr(tb, errors.New("uh oh"), false) },
"WantErr false": func(tb testing.TB) { test.WantErr(tb, nilErr(), true) },
"File wrong": func(tb testing.TB) { test.File(tb, "wrong\n", "file.txt") },
"File wrong": func(tb testing.TB) { test.File(tb, "wrong\n", filepath.Join(test.Data(t), "file.txt")) },
"File missing": func(tb testing.TB) { test.File(tb, "wrong\n", "missing.txt") },
}

Expand Down

0 comments on commit 9ab15aa

Please sign in to comment.