Tooling

Goldens and testing

make golden and reviewing its diffs, sample sets, the lint package, make check, fuzzing, the coverage floor, and testing your own kit's templates.

lttr's own test suite is the model for testing templates you write against a kit: one sample set per kit, golden files reviewed in the diff, a structural lint run on every render, fuzzing on the parser, the compiler and kit loading, and a coverage floor enforced per package. The lint package that runs the goldens and the lint is public, so your tests use the same functions.

Regenerate and review goldens

Terminal
make golden
# = go test ./examples/quiet -run TestGolden -update

TestGolden is one call, lint.Golden(t, Set(), goldenDir, *update). For each of the nine Quiet examples in the sample set it compares the rendered .HTML and .Text with testdata/golden/<template name>.html and .txt, such as testdata/golden/order.out_for_delivery.html, byte for byte. With -update it rewrites those files from the current render instead of comparing. Without it, a mismatch fails the example's subtest with a line diff of its first 20 differing lines:

go test output
--- FAIL: TestGolden (0.00s)
    --- FAIL: TestGolden/invoice (0.00s)
        tutorial_test.go:52: invoice.md: the render differs from testdata/invoice.txt; run with -update and review the diff:
            --- testdata/invoice.txt
            +++ render
            @@ -10,1 +10,1 @@
            -Extra seats x2 .................. $27.00
            +Extra seats x3 .................. $27.00

(That one is from the tutorial's golden test, with its .txt golden edited by hand.) A missing golden is golden file testdata/invoice.html is missing; run with -update to write it. A sample that fails to compile or render fails its subtest with that error, and with -update nothing is written for it. Two samples with the same template name, or a name that isn't safe as a file name, fail the test before anything is rendered or written.

Goldens are reviewed in the PR diff, not rubber-stamped. After make golden, run git diff testdata/golden/ and read every changed line: a golden diff is the actual, final proof that a template, kit or engine change did what you meant and nothing else — it is the one place a stray attribute, a rounding change in Money, or a copy edit that slipped into the wrong component shows up as a literal, reviewable line of HTML or text. lint.Golden doesn't look at Message.Warnings; Quiet's TestLint fails on any, since a clean render has none.

Two more checks run over the files on disk (TestGoldenFiles): every text golden's lines are at most 70 runes unless they contain a URL, and every HTML golden passes the structural lint below. TestGoldenText01 additionally pins template 01's text body byte for byte against a literal in golden_test.go — the one example the test suite spells out in full.

kits/plain and examples/tutorial (the kit built in Build your own kit) run lint.Golden over their own sample sets — go test ./examples/tutorial -run Golden -update — but make golden itself only covers examples/quiet, the engine's conformance suite.

Describe samples once

lint.Golden, lint.Check, lttr kit check and the previews all take the same thing: an lttr.SampleSet, "these templates, with this data".

samples.go
type SampleSet struct {
    Kit     *kit.Kit
    Config  Config
    Samples []Sample
}
  • lttr.NewSample(file, src, data, to) makes one Sample: a template source, its data and the recipient to render it for. The data's Go type is kept, so a struct is compiled as typed data (every merge tag and binding checked against it) and a map[string]any as untyped data. Samples of different types sit in one set.
  • A Sample is an opaque value with methods: Name() (the template's name from its frontmatter), File(), Source(), Data(), WithData(json) and WithSource(src) (copies with new data or source), Compile(k) and Render(r).
  • lttr.KitSamples(k, cfg) builds a set from a kit's samples/*.md and samples/samples.yaml, as untyped data: what lttr kit check renders.
  • The shipped sets are quiet.Set() in examples/quiet, plain.Set(cfg) in kits/plain and tutorial.Set() in examples/tutorial.

A sample source other than files, such as a CMS or a database, builds its Samples with NewSample the same way.

Run the structural lint

github.com/sulv-io/lttr/lint is the structural lint, a public package your own tests import. lint.Lint(html string, opts lint.Options) []string returns one message per violation, and an empty result means clean. Options{Dark: bool} turns on the one rule that depends on the kit: with Dark: true (a kit with dark: true in kit.yaml), the document must carry <meta name="color-scheme" content="light dark">. Lint runs on every HTML golden (TestGoldenFiles), and cmd/lttr lint and cmd/lttr kit check call it with Dark: kit.Dark(). It checks:

  • no flex or grid in CSS (matched case-insensitively, but only inside style="…" attributes and <style> bodies — the word "grid" in visible copy is not an error);
  • no var(, @import, <script (case-insensitively) or unexpanded {{ anywhere in the document;
  • no ZgotmplZ — what html/template writes in place of a value it silently rejected (unsafe CSS, a bad URL scheme): the markup stays valid, but the style or link is lost, so this is the mechanical way to catch it;
  • every <table> has role="presentation", cellpadding="0", cellspacing="0" and border="0";
  • every <img> has alt, width, and border:0 (or 0px/none) in its style;
  • no <div style="..."> containing display:flex;
  • <meta name="color-scheme" content="light dark"> when opts.Dark is set;
  • the document is under 100 KB;
  • Outlook conditional comments (<!--[if …]> / <![endif]-->) are balanced, and none closes before it opens.

It also looks inside <!--[if mso]>…<![endif]--> blocks: the tables, images, <div>s and <style> bodies an Outlook-only branch contains obey the same rules, at their real byte offsets in the document; a VML element like v:roundrect is neither a table nor an image, so only the CSS checks apply to it. It is a small tokenizer, not a full HTML parser — enough to find tags, attributes and comments in the markup lttr itself generates, with comments skipped and <style>/<script> bodies treated as raw text. It is not a sanitiser either: it doesn't look for event handlers or javascript: URLs.

lint.Check(set *lttr.SampleSet) []lint.Problem runs the whole pipeline over a sample set: it compiles and renders every sample and lints each HTML part with Options{Dark: set.Kit.Dark()}. It returns every compile, render and lint problem, each a lint.Problem{Sample, File, Line, Col, Msg, Warning, Suggestion, Err} whose String() is file:line:col: msg (the position left out when unknown). Lint findings are prefixed html: , as in lttr kit check's output, which is lint.Check's. A sample's compile warnings come first, with Warning set and printed file:line:col: warning: msg; they never make a sample fail, and lint.Failures(problems) leaves them out, so a test fails only on real problems. Suggestion holds the "did you mean" name of an unknown one, for editors, and Err the underlying compile or render error, for errors.As (nil for lint findings and warnings). lint.Golden fails a set with no samples (lint: no samples).

Run make check

Terminal
make check
# = fmt vet staticcheck test cover fuzz

is everything CI runs, in order:

StepWhat it does
fmtgofmt -l . must report nothing
vetgo vet ./...
staticcheckAn installed staticcheck, falling back to go run honnef.co/go/tools/cmd/staticcheck@latest if the installed binary can't load current stdlib files — a real finding fails both
testgo test -race ./...
coverThe coverage floor (below)
fuzzEvery Fuzz* target for $FUZZTIME each (default 20s)

Run the fuzz targets

scripts/fuzz.sh lists every Fuzz* function across every package (go list ./..., then go test -list '^Fuzz' per package) and runs each for $FUZZTIME (default 20s, overridable: FUZZTIME=2m make check), with -fuzzminimizetime=1s and bounded parallelism (FUZZPARALLEL, default 2) — Go's 60s default minimisation window would make every target sit idle for most of a 20s smoke run. The five targets in this checkout:

TargetPackageGuards
FuzzParsecomarkThe Comark block parser never panics
FuzzParseTextcomarkThe inline/merge-tag text parser never panics
FuzzCompileirCompiling arbitrary Comark to IR never panics
FuzzKitLoadkitLoading arbitrary kit.yaml/component.yaml never panics
FuzzCompilekits/quietCompiling arbitrary Comark against the Quiet kit never panics

None of them is allowed to panic on any input; a crash found by fuzzing gets minimised and saved to that package's testdata/fuzz/ corpus.

Enforce the coverage floor

Terminal
scripts/cover.sh 85 . ./comark ./data ./ir ./kit ./internal/suggest ./internal/tmplcheck ./lint ./render ./render/html ./render/text

runs go test -cover on each listed directory and fails if any package's statement coverage is below the floor (COVER_MIN, default 85 in the Makefile). A package that exists but has no Go files is skipped; one whose only code is, say, a lone const declaration reports [no statements] and is treated as a pass rather than a missing figure — a package with zero coverable statements vacuously clears any floor.

Test your own kit's templates

examples/tutorial — the kit from Build your own kit — is the pattern for testing templates you compile against your own kit: describe them once as a sample set, then check it and compare it with goldens.

tutorial/tutorial_test.go#L17-L23
// TestCheck runs lint.Check over the tutorial's sample set (Set): the
// invoice, with its typed data, compiles, renders and lints clean.
func TestCheck(t *testing.T) {
    for _, p := range lint.Failures(lint.Check(Set())) {
        t.Error(p) // file:line:col: message
    }
}
tutorial/tutorial_test.go#L44-L53
// TestGolden compares the render of each sample in Set with its golden
// files, testdata/invoice.html and .txt. After a change to the kit or the
// template, run
//
//  go test ./examples/tutorial -run Golden -update
//
// and review the diff: every changed line is a change recipients see.
func TestGolden(t *testing.T) {
    lint.Golden(t, Set(), "testdata", *update)
}

Set() is the tutorial's lttr.SampleSet: its kit, a Config with the profile the template names, and the invoice template with typed sample data (see Test it from Go). Wire an -update flag the same way, flag.Bool("update", false, "rewrite the golden files in testdata/"), so go test . -run Golden -update regenerates your goldens the way make golden does lttr's own, and review the diff the same way before committing it.

Test a kit's untyped samples

A kit directory with no Go types — most of your own kit, before you add a component that binds one — is checked with lttr kit check ./mykit (see The lttr CLI): it loads the kit, renders every samples/*.md against samples/samples.yaml and lints the HTML, no Go test needed.

A kit that binds Go types or adds functions with kit.Funcs can only load from Go, with those options — the CLI loads a kit directory without them, so lttr kit check on it fails with types: "Invoice" is not registered (or function "money" not defined) and says so. Check such a kit's samples from a test in its own package instead: lttr.KitSamples builds the same untyped set the CLI renders, and lint.Check does the rest:

tutorial/tutorial_test.go#L25-L42
// TestSamples checks the kit's samples/*.md with their data from
// samples.yaml: lint.Check compiles and renders each one and lints the
// HTML. `lttr kit check` does this for a kit that loads without Go types;
// this kit binds Invoice, so its samples are checked here instead.
func TestSamples(t *testing.T) {
    set, err := lttr.KitSamples(MustKit(), lttr.Config{
        Profiles: []lttr.Profile{{Name: "default", From: mail.Address{Address: "hello@example.com"}}},
    })
    if err != nil {
        t.Fatal(err)
    }
    if len(set.Samples) == 0 {
        t.Fatal("the kit has no samples")
    }
    for _, p := range lint.Failures(lint.Check(set)) {
        t.Error(p)
    }
}

This is examples/tutorial's own TestSamples, which exists precisely because its kit binds Invoice: lttr kit check on that kit directory would fail with the "not registered" error above, so the CLI can't do this check for it — a Go test, with the kit loaded the way your production code loads it, can.

Next steps

  • Screenshots for the visual check goldens don't cover (layout overflow).
  • Message and MIME for what a render's Message guarantees before it ever reaches a golden.
Copyright © 2026