termitest

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2026 License: MIT Imports: 9 Imported by: 0

README

./termitest

Golden file testing for terminal output, inspired by testify

GitHub go.mod Go version Go Report Card

Installation

go get github.com/dottermi/termitest@latest

Usage

termitest provides two packages following the testify pattern:

  • assert - Non-fatal assertions (test continues on failure)
  • require - Fatal assertions (test stops on failure)
Basic Example
package myapp_test

import (
    "testing"

    "github.com/dottermi/termitest/assert"
)

func TestOutput(t *testing.T) {
    output := runMyProgram()
    assert.Golden(t, output)
}

Golden files are stored in testdata/{TestName}.golden and created automatically on first run.

Custom Messages
assert.Golden(t, output, "should match expected output")
assert.Golden(t, output, "user %s should see welcome message", username)
With Options

Use New() to create assertions with pre-configured options:

import (
    "github.com/dottermi/termitest"
    "github.com/dottermi/termitest/assert"
)

func TestTerminalOutput(t *testing.T) {
    a := assert.New(termitest.WithSize(80, 24))
    a.Golden(t, output)
}

func TestMultipleOutputs(t *testing.T) {
    simple := assert.New(termitest.WithName("simple"))
    simple.Golden(t, output1)

    fancy := assert.New(termitest.WithName("fancy"))
    fancy.Golden(t, output2)
}
Text Only (strip ANSI)

Use GoldenText to compare text without ANSI escape codes:

func TestColoredOutput(t *testing.T) {
    output := "\033[31mError:\033[0m something failed\n"
    assert.GoldenText(t, output)  // Compares: "Error: something failed\n"
}
Debugging Golden Files

Use PrintGolden to inspect the expected content during test development:

func TestOutput(t *testing.T) {
    termitest.PrintGolden(t)           // prints testdata/TestOutput.golden
    termitest.PrintGolden(t, "fancy")  // prints testdata/TestOutput_fancy.golden
}
Updating Golden Files

Use the -update flag to regenerate golden files:

go test -update              # Update all golden files
go test -run TestOutput -update  # Update specific test

API

Functions
Function Description
Golden(t, got, msgAndArgs...) Compare against golden file
GoldenText(t, got, msgAndArgs...) Compare after stripping ANSI codes
New(opts...).Golden(t, got, msgAndArgs...) Compare with options
New(opts...).GoldenText(t, got, msgAndArgs...) Strip ANSI with options
PrintGolden(t, name...) Print golden file content to stdout
Options
Option Description
WithName(name) Suffix for golden file (TestName_suffix.golden)
WithSize(width, height) Process as terminal with dimensions
WithWidth(width) Wrap lines at width

License

License

Documentation

Overview

Package termitest provides golden file testing utilities for terminal applications.

Golden files are stored in testdata/{TestName}.golden and are created automatically when they don't exist. To update golden files, run tests with the -update flag: go test -update

Index

Constants

This section is empty.

Variables

View Source
var UpdateGolden = flag.Bool("update", false, "update golden files")

UpdateGolden is a flag to update golden files instead of comparing. Use with: go test -update.

Functions

func Compare

func Compare(got, want []byte) bool

Compare compares actual content against expected golden content. Returns true if they match after normalization.

func Diff

func Diff(want, got []byte, wantLabel, gotLabel string) string

Diff generates a unified diff between want (expected) and got (actual) content.

func GoldenPath

func GoldenPath(testName string, name ...string) string

GoldenPath returns the path to a golden file for the given test name. If name is empty, returns testdata/{testName}.golden. If name is provided, returns testdata/{testName}_{name}.golden.

func Normalize

func Normalize(content []byte) []byte

Normalize normalizes content for comparison: - Converts CRLF to LF - Trims trailing whitespace from each line - Ensures single trailing newline.

func PrintGolden added in v0.0.2

func PrintGolden(tb testing.TB, name ...string)

PrintGolden prints the golden file content for the current test to stdout. This is useful for debugging during test development. If name is provided, prints testdata/{testName}_{name}.golden.

func ProcessWithOptions

func ProcessWithOptions(text string, opts *Options) string

ProcessWithOptions processes text through a virtual screen if options specify dimensions.

func ReadGolden

func ReadGolden(path string) ([]byte, error)

ReadGolden reads the golden file content. Returns nil if the file doesn't exist.

func StripANSI

func StripANSI(s string) string

StripANSI removes all ANSI escape codes from the input string, returning plain text without any styling.

func WriteGolden

func WriteGolden(path string, content []byte) error

WriteGolden writes content to the golden file, creating directories as needed.

Types

type Option

type Option func(*Options)

Option is a functional option for configuring golden file comparison.

func WithName

func WithName(name string) Option

WithName sets the suffix for the golden file name (e.g., WithName("dark") produces testdata/TestName_dark.golden).

func WithSize

func WithSize(width, height int) Option

WithSize sets the terminal width and height for processing output. Lines longer than width will be wrapped. Only the last height lines will be kept.

func WithWidth

func WithWidth(width int) Option

WithWidth sets only the terminal width for processing output. Lines longer than width will be wrapped.

type Options

type Options struct {
	Name   string
	Width  int
	Height int
}

Options configures how golden file comparison is performed.

func ApplyOptions

func ApplyOptions(opts ...Option) *Options

ApplyOptions applies all options and returns the configured Options.

type Screen

type Screen struct {
	// contains filtered or unexported fields
}

Screen represents a virtual terminal screen with fixed dimensions.

func NewScreen

func NewScreen(width, height int) *Screen

NewScreen creates a new virtual screen with the given dimensions. If width is 0, no line wrapping is performed. If height is 0, all lines are kept.

func (*Screen) String

func (s *Screen) String() string

String returns the final screen content.

func (*Screen) Write

func (s *Screen) Write(text string)

Write processes the output string, handling line wraps and carriage returns.

Directories

Path Synopsis
Package assert provides non-fatal golden file assertions for testing.
Package assert provides non-fatal golden file assertions for testing.
internal
golden
Package golden provides shared golden file testing logic for assert and require packages.
Package golden provides shared golden file testing logic for assert and require packages.
Package require provides fatal golden file assertions for testing.
Package require provides fatal golden file assertions for testing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL