2021-11-25 14:07:56 -08:00
|
|
|
package test
|
2021-09-07 14:58:10 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"flag"
|
|
|
|
|
"io/ioutil"
|
2021-11-25 14:07:56 -08:00
|
|
|
"log"
|
2021-09-07 14:58:10 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var updateFixtures = flag.Bool("update-fixtures", false, "Regenerate fixtures in testdata based on current test output")
|
|
|
|
|
|
|
|
|
|
// RequireJSONMatchesFixture asserts that the JSON text in actual matches the
|
|
|
|
|
// JSON read from filename, without taking into account whitespace and
|
|
|
|
|
// ordering. Files can be specified relative to the calling test (e.g.
|
|
|
|
|
// testdata/example.json). To regenerate the expected test data automatically
|
|
|
|
|
// after making a code change, pass the `-update-fixtures` flag to `go test`.
|
|
|
|
|
func RequireJSONMatchesFixture(t *testing.T, filename string, actual string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
if *updateFixtures {
|
|
|
|
|
var indented bytes.Buffer
|
|
|
|
|
err := json.Indent(&indented, []byte(actual), "", " ")
|
|
|
|
|
require.NoError(t, err, "Failed to indent JSON")
|
|
|
|
|
err = ioutil.WriteFile(filename, indented.Bytes(), 0644)
|
|
|
|
|
require.NoError(t, err, "Failed to update fixture", filename)
|
2021-11-25 14:07:56 -08:00
|
|
|
|
|
|
|
|
log.Printf("Updating fixture file %#v", filename)
|
2021-09-07 14:58:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expectedData, err := ioutil.ReadFile(filename)
|
|
|
|
|
require.NoError(t, err, "Failed to read fixture", filename)
|
|
|
|
|
|
|
|
|
|
require.JSONEq(t, string(expectedData), actual)
|
|
|
|
|
}
|