1
0

helpers.go 764 B

1234567891011121314151617181920212223242526272829
  1. package test
  2. import (
  3. "flag"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. // Update is the -update flag: Golden rewrites its file instead of comparing against it.
  11. var Update = flag.Bool("update", false, "rewrite golden files instead of comparing against them")
  12. // Golden compares got with the file at path, or rewrites the file when -update is set.
  13. func Golden(t *testing.T, path, got string) {
  14. t.Helper()
  15. if *Update {
  16. require.NoError(t, os.MkdirAll(filepath.Dir(path), 0755))
  17. require.NoError(t, os.WriteFile(path, []byte(got), 0644))
  18. return
  19. }
  20. want, err := os.ReadFile(path)
  21. require.NoError(t, err, "missing golden file, run with -update")
  22. assert.Equal(t, string(want), got)
  23. }