fixtures: add Open() function

Adds a method for streaming fixture files
This commit is contained in:
Richard Patel 2022-09-17 16:43:19 +02:00
parent 452d388b5c
commit 07c1b166a4
No known key found for this signature in database
GPG Key ID: BEC24858656CAF2B
1 changed files with 9 additions and 2 deletions

View File

@ -11,7 +11,7 @@ import (
)
// Path returns the absolute path for the fixture at the given relative path.
func Path(t *testing.T, strs ...string) string {
func Path(t testing.TB, strs ...string) string {
_, file, _, ok := runtime.Caller(0)
require.True(t, ok, "runtime.Caller failed")
parts := make([]string, 1, 1+len(strs))
@ -21,8 +21,15 @@ func Path(t *testing.T, strs ...string) string {
}
// Load returns the fixture at the given path.
func Load(t *testing.T, strs ...string) []byte {
func Load(t testing.TB, strs ...string) []byte {
data, err := os.ReadFile(Path(t, strs...))
require.NoError(t, err)
return data
}
// Open returns a file handle for a fixture.
func Open(t testing.TB, strs ...string) *os.File {
f, err := os.Open(Path(t, strs...))
require.NoError(t, err)
return f
}