/
golden.go
58 lines (50 loc) · 1.65 KB
/
golden.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
package dstest
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/qri-io/dataset"
)
// UpdateGoldenFileEnvVarname is the envionment variable that dstest checks
// before writing
const UpdateGoldenFileEnvVarname = "QRI_UPDATE_GOLDEN_FILES"
// CompareGoldenDatasetAndUpdateIfEnvVarSet is a convenience wrapper for the
// common case of loading a golden file, comparing it to a dataset, and updating
// the dataset if it fails and the "update" enviornment variable is set
func CompareGoldenDatasetAndUpdateIfEnvVarSet(t *testing.T, goldenFilepath string, got *dataset.Dataset) {
t.Helper()
expect := LoadGoldenFile(t, goldenFilepath)
if diff := CompareDatasets(expect, got); diff != "" {
t.Errorf("dataset golden file mismatch (-want +got):\n%s", diff)
UpdateGoldenFileIfEnvVarSet(goldenFilepath, got)
}
}
// LoadGoldenFile loads a dataset from a JSON file
func LoadGoldenFile(t *testing.T, filename string) *dataset.Dataset {
t.Helper()
data, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatalf("opening JSON golden file: %s", err)
}
ds := &dataset.Dataset{}
if err := json.Unmarshal(data, ds); err != nil {
t.Fatalf("unmarshaling JSON golden file: %s", err)
}
return ds
}
// UpdateGoldenFileIfEnvVarSet overwrites the given filename if
// QRI_UPDATED_GOLDEN_FILES env var is set
func UpdateGoldenFileIfEnvVarSet(filename string, got *dataset.Dataset) {
if os.Getenv(UpdateGoldenFileEnvVarname) != "" {
fmt.Printf("updating golden file: %q\n", filename)
data, err := json.MarshalIndent(got, "", " ")
if err != nil {
panic(err)
}
if err := ioutil.WriteFile(filename, data, 0644); err != nil {
panic(err)
}
}
}