golden.go 683 B

12345678910111213141516171819202122232425262728
  1. // Package golden provides function and helpers to use golden file for
  2. // testing purpose.
  3. package golden
  4. import (
  5. "flag"
  6. "io/ioutil"
  7. "path/filepath"
  8. "testing"
  9. )
  10. var update = flag.Bool("test.update", false, "update golden file")
  11. // Get returns the golden file content. If the `test.update` is specified, it updates the
  12. // file with the current output and returns it.
  13. func Get(t *testing.T, actual []byte, filename string) []byte {
  14. golden := filepath.Join("testdata", filename)
  15. if *update {
  16. if err := ioutil.WriteFile(golden, actual, 0644); err != nil {
  17. t.Fatal(err)
  18. }
  19. }
  20. expected, err := ioutil.ReadFile(golden)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. return expected
  25. }