-
Notifications
You must be signed in to change notification settings - Fork 9
/
files.go
65 lines (51 loc) · 1.65 KB
/
files.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package testhelper
import (
"encoding/json"
"os"
"path/filepath"
"testing"
clabernetesconstants "github.com/srl-labs/clabernetes/constants"
)
// ReadTestFixtureFile is a helper to read a test fixture file.
func ReadTestFixtureFile(t *testing.T, f string) []byte { //nolint:thelper
return ReadTestFile(t, filepath.Join("test-fixtures", f))
}
// ReadTestFile is a helper to read a (usually) golden file in the context of a test
// (hence testing.T).
func ReadTestFile(t *testing.T, f string) []byte {
t.Helper()
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
content, err := os.ReadFile(filepath.Join(wd, f)) //nolint:gosec
if err != nil {
t.Fatal(err)
}
return content
}
// WriteTestFixtureFile is a helper to write out a test fixture file.
func WriteTestFixtureFile(t *testing.T, f string, b []byte) { //nolint:thelper
WriteTestFile(t, filepath.Join("test-fixtures", f), b)
}
// WriteTestFile is a helper to write json to the specified file in the context of a test.
func WriteTestFile(t *testing.T, f string, b []byte) {
t.Helper()
err := os.WriteFile(f, b, clabernetesconstants.PermissionsEveryoneReadWriteOwnerExecute)
if err != nil {
t.Fatal(err)
}
}
// WriteTestFixtureJSON is a helper to write JSON into a test fixture file.
func WriteTestFixtureJSON(t *testing.T, f string, o interface{}) { //nolint:thelper
WriteTestJSON(t, filepath.Join("test-fixtures", f), o)
}
// WriteTestJSON is a helper to write JSON to the specified file in the context of a test.
func WriteTestJSON(t *testing.T, f string, o interface{}) {
t.Helper()
j, err := json.MarshalIndent(o, "", " ")
if err != nil {
t.Fatal(err)
}
WriteTestFile(t, f, j)
}