[go-fan] Go Module Review: charmbracelet/x/exp/golden #21756
Closed
Replies: 1 comment
-
|
This discussion has been marked as outdated by Go Fan. A newer discussion is available at Discussion #21925. |
Beta Was this translation helpful? Give feedback.
0 replies
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.
-
🐹 Go Fan Report: charmbracelet/x/exp/golden
Selected today as the most recently updated unreviewed direct dependency (charmbracelet/x repo pushed 2026-03-16, ranked #1 of 12 unreviewed GitHub-hosted direct deps).
Module Overview
github.com/charmbracelet/x/exp/goldenis a tiny but focused golden file testing utility from Charmbracelet's experimental monorepo (charmbracelet/x). It provides a single public function —RequireEqual— that compares test output against stored reference ("golden") files, with special handling for terminal escape sequences and control codes that appear frequently in TUI application output.The package was purpose-built so Charmbracelet's own TUI libraries (bubbletea, lipgloss, huh, etc.) could have stable, readable golden file tests even when their output contains ANSI escape sequences.
Current Usage in gh-aw
v0.0.0-20251215102626-e0db08df7383pkg/console/golden_test.go)pkg/console/testdata/)golden.RequireEqual(t, []byte(output))Test groups covered
pkg/console/golden_test.gouses the module across 6 test functions with 28 individual cases total:TestGolden_TableRendering— 5 cases (simple, titled, totals, wide, empty)TestGolden_BoxRendering— 5 cases (various widths, emoji)TestGolden_ErrorFormatting— 5 cases (basic, warning, context, multiline, info)TestGolden_ErrorWithSuggestions— 4 casesTestGolden_MessageFormatting— 6 cases (success, info, warning, error, command, progress)TestGolden_InfoSection— 3 cases (single line, multiline, special chars)The usage is clean and idiomatic: call the rendering function, pass output to
golden.RequireEqual, done. The 44 golden files live undertestdata/(TestFuncName)/(case).golden, exactly as the library prescribes.Research Findings
What the module does (in detail)
The implementation:
testdata/(tb.Name()).golden-updateflag is set, writes the output to the golden fileescapeSeqs()on both expected and actual — this callsstrconv.Quoteline-by-line, converting\x1b[32m→\\x1b[32metc., while preserving newlinesgithub.com/aymanbagabas/go-udiffunified diff formatThe generic constraint
[T []byte | string]means it works with either type without conversion.Recent activity (charmbracelet/x monorepo)
The last 10 commits to the monorepo are all dependabot dependency bumps (
golang.org/x/image,mattn/go-runewidth,charmbracelet/colorprofile) plus one feature commit migrating teatest v2 to usecharm.landmodule paths. Theexp/goldensubpackage itself has had no recent changes — it is stable and mature for its purpose.Deprecated API
RequireEqualEscape(tb, out, escapes bool)is markedDeprecated— useRequireEqualinstead. The project already uses the correct API. ✓Improvement Opportunities
🏃 Quick Wins
None required. The
pkg/consoleusage is exactly idiomatic — correct API, correct file layout, clean patterns.✨ Feature Opportunities
The module is intentionally minimal. The only noteworthy potential: the generic
RequireEqualacceptsstringdirectly, so any test currently doinggolden.RequireEqual(t, []byte(str))could simplify togolden.RequireEqual(t, str). The project uses[]byte(output)conversions throughout — switching to pass thestringdirectly would be a minor style improvement (no functional change).📐 Best Practice Alignment — Key Finding
pkg/workflow/wasm_golden_test.goimplements a parallel custom golden pattern instead of using this library:vs. the library-based approach in
pkg/console/golden_test.go:What the custom code lacks vs. the library:
require.Equalfailure)updateGoldenvs globalupdate)-updateis the same, but implementation differs)Migration complexity: Medium. The wasm tests use a custom path (
testdata/wasm_golden/TestWasmGolden_CompileFixtures/(name).golden) while the library hardcodestestdata/(tb.Name()).golden. Thetb.Name()for a subtestt.Run("foo", ...)becomesTestWasmGolden_CompileFixtures/foo, so the library would store attestdata/TestWasmGolden_CompileFixtures/foo.golden— which matches the existing structure! Migration is viable with a simple directory rename fromtestdata/wasm_golden/TestWasmGolden_CompileFixtures/totestdata/TestWasmGolden_CompileFixtures/.🔧 General Improvements
Consider consolidating golden test patterns. Having two different golden testing mechanisms in the same codebase creates inconsistency for contributors. The
charmbracelet/x/exp/goldenmodule is already a direct dependency — using it in both places costs nothing extra.Recommendations
stringdirectly toRequireEqualinstead of[]byte(str)in console testspkg/workflow/wasm_golden_test.goto usecharmbracelet/x/exp/goldenThe module is well-utilized in
pkg/console. The only meaningful improvement is unifying the golden testing pattern withpkg/workflowto reduce duplication and improve diff output quality for WASM compiler tests.Next Steps
pkg/workflow/wasm_golden_test.gocustom golden logic to usegolden.RequireEqualtestdata/wasm_golden/TestWasmGolden_CompileFixtures/→testdata/TestWasmGolden_CompileFixtures/updateGoldenflag andisUpdateMode()helper (~10 lines)Module summary saved to:
scratchpad/mods/charmbracelet-x-exp-golden.mdRun: §23284131658
References:
Beta Was this translation helpful? Give feedback.
All reactions