From 9ab15aa2ff7b412be3d5f59e914ff0959efe5c0f Mon Sep 17 00:00:00 2001 From: Tom Fleet Date: Mon, 5 Aug 2024 08:54:23 +0100 Subject: [PATCH] test.File: No longer use relative path to testdata (#30) --- README.md | 6 +++--- test.go | 12 ++++++++---- test_test.go | 4 ++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7221737..ecc18a5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/test.go b/test.go index 0d17690..35fb32c 100644 --- a/test.go +++ b/test.go @@ -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]. // @@ -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")) diff --git a/test_test.go b/test_test.go index 5acd5ee..93a69ba 100644 --- a/test_test.go +++ b/test_test.go @@ -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 { @@ -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") }, }