test(cmd/morphic): pin the rendered help text with goldens - #207
test(cmd/morphic): pin the rendered help text with goldens#207fuad-daoud wants to merge 1 commit into
Conversation
680137c to
b99e2be
Compare
b99e2be to
1ebb5a3
Compare
OmarAlJarrah
left a comment
There was a problem hiding this comment.
The golden mechanism holds up. I checked the three things that usually go wrong with goldens rather than reading it:
- golden removed: red, with the message that tells you to run
-update -update: recreates both files byte for byte- wording changed in
writeRootHelp: red
Gate clean on the merged state: golangci-lint 0 issues, coverage 4234/4234.
Also worth noting this lands the README update that #205's Breaking section already claims. That claim is still wrong on #205 read on its own, since the file isn't in that PR.
Last thing: the five follow-ups in the description are good catches, but a squashed PR body isn't a tracker. None of them are open issues right now, so they'll be gone once this merges. Worth filing the ones you mean to keep, particularly the flag.ErrHelp handling being compile-specific, since that one gets more expensive with every subcommand added.
| } | ||
| } | ||
|
|
||
| var updateHelp = flag.Bool("update", false, "rewrite help golden files instead of comparing") |
There was a problem hiding this comment.
Don't define a second -update. ir/irtest already owns one, and this isn't a latent hazard, it's a live landmine: import ir/irtest anywhere in this package's tests and the binary panics at init before a single test runs.
morphic.test flag redefined: update
panic: ... flag redefined: update
Reuse the existing one and the collision can't happen:
| var updateHelp = flag.Bool("update", false, "rewrite help golden files instead of comparing") |
Then if *updateHelp { becomes if irtest.Update() {, with "github.com/dexpace/morphic/ir/irtest" in the imports. I ran it: -update still rewrites both goldens byte for byte, the package tests pass, and archtest is happy with the import.
That also clears one of the five follow-ups in the description.
There was a problem hiding this comment.
Fixed — it uses irtest.Update() now.
Reproduced the panic first (a probe test importing irtest → flag redefined: update before any test ran), then confirmed it's gone: the same probe now runs clean. -update still rewrites both goldens byte for byte, checked with md5sum -c against the committed files, and archtest is green (it only audits non-test files, so the import is outside its allowlist by design).
| {"root", nil, "root-help.txt"}, | ||
| {"compile", []string{"help", "compile"}, "compile-help.txt"}, |
There was a problem hiding this comment.
The misuse text is the third thing that renders and the only one not pinned here. writeCommandUsage and the root help that misuse prints to stderr can both drift silently. Worth a third row, capturing stderr:
{"compile misuse", []string{"compile"}, "compile-usage.txt"},Needs the helper to take stderr for that case, so only do it if you think the text is worth pinning. The two you have are the ones that matter most.
There was a problem hiding this comment.
Holding this one until the rest of the stack settles — it needs the helper to take stderr, and I want to decide it alongside the flagSet→printFlags question on #204, which touches the same rendering. Leaning yes: the misuse text is the only one of the three renders that nothing pins.
| ``` | ||
| usage: | ||
| morphic compile <spec-file> [-o out.json] [--fail-on error|warning] [--skip-validate] | ||
| morphic <command> [flags] |
There was a problem hiding this comment.
Dropping the compile synopsis leaves the flag table sitting under morphic <command> [flags], so -o and --fail-on now read as root flags. They aren't. The old line was wrong in form but it did tell you whose flags these were.
Add compile's real synopsis back, in the form the CLI actually prints, so this can't drift again:
| morphic <command> [flags] | |
| morphic <command> [flags] | |
| morphic compile <spec-file> [flags] |
There was a problem hiding this comment.
Added compile's synopsis back in the form the CLI prints, alongside the root one. Also put a line above the table saying the flags are compile's, since the table itself gives no clue.
| `morphic`, `morphic help`, and `morphic -h`/`--help` print the command list. `morphic help compile` | ||
| and `morphic compile --help` print a command's flags. Help always prints to stdout and exits `0`. |
There was a problem hiding this comment.
-help works too, same as -h and --help. Either list it or say "a help flag" rather than naming two of the three.
There was a problem hiding this comment.
Fixed — it now says a help flag and lists all three.
Golden files make the rendered help reviewable as a diff, so wording changes are deliberate rather than incidental. The -update flag comes from ir/irtest rather than a second declaration here, which would panic the test binary at init the moment anything else imported irtest. Document the help forms and their exit code in the README, and replace the compile synopsis the CLI never prints with the one it does — the flag table below it is compile's, not the root command's. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1ebb5a3 to
cdcc016
Compare
|
Filed the surviving follow-ups so they outlive the squash — you were right that a PR body isn't a tracker:
The other two are closed by the stack rather than filed: the |
Summary
Adds golden files for the rendered root help and compile help, so wording changes show up as a reviewable diff rather than slipping by unnoticed. Regenerate with:
go test ./cmd/morphic -run TestHelp_MatchesGolden -updateThat
-updateisir/irtest's, not a second declaration in this package. Twoflag.Bool("update", ...)calls do not shadow each other — both register on the same command-lineFlagSet, so the test binary panics at init (flag redefined: update) the moment anything pullsirtestinto these tests. Reusing the existing flag makes the collision unrepresentable.The README gains the help forms and states that help exits
0. It also getscompile's real synopsis back alongside the root one: with onlymorphic <command> [flags]above it, the flag table read as though-oand--fail-onwere root flags.Test plan
TestHelp_MatchesGoldencompares both help texts against the committed goldens; deliberately not parallel because-updatewrites files.read golden testdata/root-help.txt (run with -update to create)-update→ recreates both files,md5sum -cclean against the committed byteswriteRootHelp→ red,golden mismatch for testdata/root-help.txt-updatecollision is gone: a test file importingirtestalongside these now runs, where before it panicked at init.gofmt -l,go vet ./...,golangci-lint run(0 issues),go build ./...,./scripts/check-coverage.sh— 4232/4232 statements.