-
Notifications
You must be signed in to change notification settings - Fork 327
/
fixture.go
50 lines (38 loc) · 1.13 KB
/
fixture.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package testutility
import (
"encoding/json"
"os"
"testing"
)
// load returns the contents of the fixture file after applying any replacements if on Windows
func load(t *testing.T, path string, windowsReplacements map[string]string) []byte {
t.Helper()
var file []byte
var err error
file, err = os.ReadFile(path)
if err != nil {
t.Fatalf("Failed to open fixture: %s", err)
}
return []byte(applyWindowsReplacements(string(file), windowsReplacements))
}
// LoadJSONFixture returns the contents of the fixture file parsed as JSON
func LoadJSONFixture[V any](t *testing.T, path string) V {
t.Helper()
return LoadJSONFixtureWithWindowsReplacements[V](t, path, map[string]string{})
}
// LoadJSONFixtureWithWindowsReplacements returns the contents of the fixture
// file parsed as JSON after applying any replacements if running on Windows
func LoadJSONFixtureWithWindowsReplacements[V any](
t *testing.T,
path string,
replacements map[string]string,
) V {
t.Helper()
file := load(t, path, replacements)
var elem V
err := json.Unmarshal(file, &elem)
if err != nil {
t.Fatalf("Failed to unmarshal val: %s", err)
}
return elem
}