Automatically update generated files if UPDATE_SNAPSHOTS=1 (#243)

If you do `UPDATE_SNAPSHOTS=1 go test ./...` that will:
1. run the snapshot tests, updating any changed snapshots
2. run the integration tests and (if you have tokens) example tests
3. check that the code for the integration tests and example tests is
up-to-date

Step 3 is not strictly a snapshot test; the generated code is actually
checked in and used. But, I mean, it's basically the same! So now, we
also update it if you asked to update snapshots, which is hopefully a
little more convenient.

Fixes #212.

Test plan:
Make a trivial change to `example/generated.go`; `go test ./...` should
now fail. `UPDATE_SNAPSHOTS=1 go test ./...` should also fail, but say
it updated the snapshot, and the change should be reverted. Run `go test
./...` again; it should pass again.
This commit is contained in:
Ben Kraft
2022-12-21 23:19:20 -08:00
committed by GitHub
parent e3227388fe
commit b1adeca6da
2 changed files with 9 additions and 3 deletions
+8
View File
@@ -51,6 +51,14 @@ func RunGenerateTest(t *testing.T, relConfigFilename string) {
if testing.Verbose() {
t.Errorf("got:\n%s\nwant:\n%s\n", content, expectedContent)
}
if os.Getenv("UPDATE_SNAPSHOTS") == "1" {
err = os.WriteFile(filename, content, 0o644)
if err != nil {
t.Errorf("unable to update generated file %s: %v", filename, err)
} else {
t.Errorf("updated generated file for %s", filename)
}
}
}
}
}