Skip to content

Instantly share code, notes, and snippets.

@efueyo
Created October 25, 2019 23:30
Show Gist options
  • Save efueyo/72611e0ce811f92f7adc96c7b3fe38f6 to your computer and use it in GitHub Desktop.
Save efueyo/72611e0ce811f92f7adc96c7b3fe38f6 to your computer and use it in GitHub Desktop.
GoldeFiles
// MustWriteGolden updates the golden file for a given test t.
func MustWriteGolden(t *testing.T, content []byte) {
gp := filepath.Join("testdata", t.Name() +".golden")
if err := ioutil.WriteFile(gp, content, 0644); err != nil {
t.FailF("failed to update golden file: %s", err)
}
}
// MustReadGolden reads the golden file for a given test t.
func MustReadGolden(t *testing.T) (content []byte) {
gp := filepath.Join("testdata", t.Name() +".golden")
g, err := ioutil.ReadFile(gp)
if err != nil {
t.FailF("failed reading .golden: %s", err)
}
return g
}
// MustCheckGolden compares v with its golden file
func MustCheckGolden(t *testing.T, v interface{}) {
res, ok := v.([]byte)
if !ok {
res = MustJSONMarshal(t, v)
}
if *update {
MustWriteGolden(t, res)
}
g := MustReadGolden(t)
if bytes.Equal(res, g) {
t.FatalF("err: %v not equal to %v", res, g)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment