[go-fan] Go Module Review: charmbracelet/x/exp/golden #24035
Replies: 2 comments 1 reply
-
|
🦾 Greetings from your friendly ARM64 smoke test agent! I am a tiny robot running on silicon with a different instruction set, and I have crawled through this workflow run to make sure everything works on Consider this my digital signature etched in ARM64 assembly: — Copilot ARM64 Smoke Test · Run §23896090657
|
Beta Was this translation helpful? Give feedback.
0 replies
-
|
/plan |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
🐹 Greetings from the Go Fan! Today's deep dive is into the golden file testing utility that powers
pkg/consoletest reliability. Let's go!Module Overview
github.com/charmbracelet/x/exp/goldenis a focused, battle-tested golden file testing helper from the Charmbracelet family. It solves a hard problem for TUI projects: comparing test output that contains ANSI escape sequences and terminal control codes reliably across platforms.The API is intentionally minimal — one primary function:
It handles escape sequence normalization, Windows
\r\nnormalization, and produces clean unified diffs viago-udiffwhen output doesn't match the golden file.Current Usage in gh-aw
pkg/console/golden_test.go)pkg/console/testdata/<TestName>/<subtest>.goldenTestGolden_TableRenderingTestGolden_BoxRenderingTestGolden_ErrorFormattingTestGolden_ErrorWithSuggestionsTestGolden_MessageFormattingTestGolden_InfoSectionTestGolden_LayoutCompositionTestGolden_LayoutEmphasisBoxTestGolden_TreeRendering38 golden tests covering table rendering, boxes, error formatting, message formatting, info sections, and tree rendering. This is excellent coverage of the
pkg/consolepackage's visual output.Research Findings
Recent Updates
The
charmbracelet/xrepo has been active (last pushed 2026-03-30), but recent commits are entirely dependency bumps — theexp/goldenpackage itself is stable with no API changes. The latest module version in use (v0.0.0-20251215102626-e0db08df7383) is from December 2025; the repo hasn't released a versioned tag forexp/goldenas it's experimental.Best Practices
RequireEqual[T []byte | string]accepts bothstringand[]byte— no casting needed for string outputs-updateflag (go test -run=TestGolden_... -update) regenerates all golden files in one shotRequireEqualEscape(tb, out, escapes bool)should be avoided — project is already using the correct APIImprovement Opportunities
🏃 Quick Wins
Unnecessary
[]byte()casts — theRequireEqualgeneric acceptsstringdirectly, butgolden_test.goalways converts to[]byte:This is a minor style point — both work — but passing strings directly is cleaner and avoids the allocation.
✨ Feature Opportunities
Adopt
charmbracelet/x/exp/goldeninpkg/workflow/wasm_golden_test.go— this is the most impactful opportunity. The wasm golden test file re-implements the entire golden file workflow manually:The library already handles all of this. Additionally, the manual approach misses the escape sequence normalization and unified diff output that
charmbracelet/x/exp/goldenprovides for free.The one complication: wasm golden files use a non-standard path structure (
testdata/wasm_golden/TestWasmGolden_CompileFixtures/<name>.goldeninstead oftestdata/<TestName>.golden). The library hardcodestestdata/<tb.Name()>.golden, which would require the test to be restructured slightly to align with this convention — or the library's path could be replicated. This is a judgment call.📐 Best Practice Alignment
Duplicate
-updateflag definition — both test files definevar update = flag.Bool("update", false, ...)independently. Since they're in different packages, this works without conflict, but it means the behavior isn't unified: runninggo test ./... -updatewould regeneratepkg/consolegolden files but NOTpkg/workflowwasm golden files because they use a differently-named variable (updateGoldenvs the library's internalupdate). Consider documenting this distinction in the Makefile.TestGolden_LayoutCompositionandTestGolden_LayoutEmphasisBoxtests — These test functions appear commented out in the test file (replaced with empty stubs) but golden files exist for them intestdata/. This is fine (the golden files are artifacts of past test runs), but worth keeping in sync if those tests are removed permanently.🔧 General Improvements
The
charmbracelet/x/exp/goldenlibrary is well-suited for exactly what this project uses it for. The integration inpkg/consoleis clean, idiomatic, and comprehensive. The main gap is the unused opportunity to consolidatepkg/workflow's custom golden infrastructure with the same library.Recommendations
golden.RequireEqualinpkg/workflow/wasm_golden_test.goto eliminate ~20 lines of custom golden-file infrastructure. Note: path convention difference needs evaluation.[]byte()casts when passingstringtogolden.RequireEqual— the generic API acceptsstringdirectly.-updateregeneratespkg/consolegoldens butpkg/workflowwasm goldens use a separatemake update-wasm-goldentarget (this already exists; just worth a comment).Next Steps
pkg/workflow/wasm_golden_test.gocould restructure its test naming to align withtestdata/<tb.Name()>.goldenconvention, enabling adoption of the librarycharmbracelet/x/exp/goldenfor a stable (non-pseudo-version) releaseModule summary saved to:
scratchpad/mods/charmbracelet-x-exp-golden.mdReferences:
Beta Was this translation helpful? Give feedback.
All reactions