Skip to content

Commit b1adeca

Browse files
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.
1 parent e322738 commit b1adeca

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

docs/CONTRIBUTING.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ Notes for contributors:
4040
- If `GITHUB_TOKEN` is available in the environment, it also checks that the example returns the expected output when run against the real API. This is configured automatically in GitHub Actions, but you can also use a [personal access token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) with no scopes. There's no need for this to cover anything in particular; it's just to make sure the example in fact works.
4141
- Tests should use `testify/assert` and `testify/require` where convenient (when making many simple assertions).
4242

43-
If you update any code-generation logic or templates, even if no new tests are needed you'll likely need to:
44-
- Run `UPDATE_SNAPSHOTS=1 go test ./...` to update the [cupaloy](https://github.com/bradleyjkemp/cupaloy) snapshots.
45-
- Run `go generate ./...` to update the genqlient-generated files used in integration tests and documentation.
43+
If you update any code-generation logic or templates, even if no new tests are needed you'll likely need to run `UPDATE_SNAPSHOTS=1 go test ./...` to update the [cupaloy](https://github.com/bradleyjkemp/cupaloy) snapshots and the genqlient-generated files used in integration tests and documentation.
4644

4745
## Finding your way around
4846

internal/integration/util.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ func RunGenerateTest(t *testing.T, relConfigFilename string) {
5151
if testing.Verbose() {
5252
t.Errorf("got:\n%s\nwant:\n%s\n", content, expectedContent)
5353
}
54+
if os.Getenv("UPDATE_SNAPSHOTS") == "1" {
55+
err = os.WriteFile(filename, content, 0o644)
56+
if err != nil {
57+
t.Errorf("unable to update generated file %s: %v", filename, err)
58+
} else {
59+
t.Errorf("updated generated file for %s", filename)
60+
}
61+
}
5462
}
5563
}
5664
}

0 commit comments

Comments
 (0)