···33package test
4455import (
66+ "bytes"
67 "fmt"
78 "os"
89 "path/filepath"
···171172 }
172173173174 return filepath.Join(cwd, "testdata")
175175+}
176176+177177+// File fails if the contents of the given file do not match want.
178178+//
179179+// It takes the name of a file (relative to $CWD/testdata) and the contents to compare.
180180+//
181181+// If the contents differ, the test will fail with output equivalent to test.Diff.
182182+//
183183+// Files with differing line endings (e.g windows CR LF \r\n vs unix LF \n) will be normalised to
184184+// \n prior to comparison so this function will behave identically across multiple platforms.
185185+//
186186+// test.File(t, "expected.txt", "hello\n")
187187+func File(t testing.TB, file, want string) {
188188+ t.Helper()
189189+ file = filepath.Join(Data(t), file)
190190+ contents, err := os.ReadFile(file)
191191+ if err != nil {
192192+ t.Fatalf("could not read %s: %v", file, err)
193193+ }
194194+195195+ contents = bytes.ReplaceAll(contents, []byte("\r\n"), []byte("\n"))
196196+197197+ Diff(t, string(contents), want)
174198}