This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixture.go
112 lines (91 loc) · 2.6 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package test
import (
"bytes"
"encoding/json"
"flag"
"io/ioutil"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
const (
permissions = 0644
)
var (
regen = flag.Bool("regen", false, "Regenerate fixtures")
FixtureInputPath = "testdata/input/"
FixtureOutputPath = "testdata/output/"
)
func Regen() bool {
return *regen
}
// makeFixturePath makes a path from the test name, and optionally appends "extra".
func makeFixturePath(t *testing.T, extra string) string {
t.Helper()
name := strings.Replace(t.Name(), "/", "-", -1)
path := FixtureOutputPath + name
if extra != "" {
path += "-" + extra
}
path += ".fixture"
return path
}
// Fixture ensures that 'data' is equal to what's stored on disk.
//
// If 'data' is a string it gets written verbatim, otherwise it's json-encoded.
//
// The filename of the fixture is generated from the test name. To use multiple fixtures in one test see FixtureExtra()
func Fixture(t *testing.T, data interface{}) {
t.Helper()
FixtureExtra(t, "", data)
}
// FixtureExtra ensures that data is equal to what's stored on disk.
//
// If 'data' is a string it gets written verbatim, otherwise it's json-encoded.
//
// The filename of the fixture is generated from the test name with 'extra' appended.
func FixtureExtra(t *testing.T, extra string, data interface{}) {
t.Helper()
// Write strings verbatim, otherwise json-encode.
var got []byte
if b, ok := data.(string); ok {
got = []byte(b)
} else {
var err error
got, err = json.MarshalIndent(data, "", "\t")
require.NoError(t, err)
}
path := makeFixturePath(t, extra)
// If -regen then write and return
if *regen {
if err := ioutil.WriteFile(path, []byte(got), permissions); err != nil {
t.Fatalf("Error writing file %q: %v", path, err)
}
return
}
want, err := ioutil.ReadFile(path)
if err != nil {
t.Fatalf("Error reading file %q: %v", path, err)
}
if !bytes.Equal(got, want) {
if err := ioutil.WriteFile("/tmp/got", got, permissions); err != nil {
t.Fatalf("Error writing file /tmp/got: %v", err)
}
t.Fatalf("Error comparing with fixture. See: diff /tmp/got %s", path)
}
}
// InputFixture returns the contents of a fixture file
func InputFixture(t *testing.T, filename string) []byte {
t.Helper()
input, err := ioutil.ReadFile(FixtureInputPath + filename)
if err != nil {
t.Fatalf("Error reading fixture: %v", err)
}
return input
}
// InputFixtureJson returns the contents of a json fixture file, and unmarshals it
func InputFixtureJson(t *testing.T, filename string, v interface{}) {
t.Helper()
data := InputFixture(t, filename)
require.NoError(t, json.Unmarshal(data, v))
}