Documentation
¶
Overview ¶
Package termoscope tests terminal (TUI) programs: it runs a binary under a headless pseudo-terminal so a test can read the screen cell by cell, wait for screen states, and keep PNG, static SVG, animated SVG and animated GIF evidence of every checked screen. Fonts and color emoji are embedded, so the images are identical on a laptop and on CI with nothing installed but Go.
A typical test builds its binary, calls Start, registers Record so recording.svg and recording.gif land beside the screenshots, waits with Terminal.WaitFor or Terminal.WaitUntil, and calls SavePNG and SaveSVG at each assertion point. Artifacts go to test/results/<dateTimeISO>-<testName>/ under the module root of the test being run. See the Examples below and test/e2e in the repository.
Not yet supported: text attributes other than foreground and background color (bold, italic, underline are dropped by the renderers), the cursor, scrollback, and Windows. The scope and the list of planned work are in README.md; CONTRIBUTING.md explains where each piece lives. The renderers are small, and small pull requests usually land within a day.
Index ¶
- func Dir(t *testing.T) string
- func Record(t *testing.T, tm *Terminal)
- func RecordWith(t *testing.T, tm *Terminal, opts Options)
- func SavePNG(t *testing.T, s raster.Screen, name string) string
- func SaveSVG(t *testing.T, s svg.Screen, name string) string
- func SetResultsRoot(dir string)
- func StripANSI(s string) string
- type Options
- type Terminal
- func (t *Terminal) CellAt(x, y int) *uv.Cell
- func (t *Terminal) Close()
- func (t *Terminal) Done() <-chan struct{}
- func (t *Terminal) Height() int
- func (t *Terminal) Line(y int) string
- func (t *Terminal) Screen() string
- func (t *Terminal) Send(s string) error
- func (t *Terminal) SendLine(s string) error
- func (t *Terminal) Wait() error
- func (t *Terminal) WaitFor(ctx context.Context, substr string) (string, error)
- func (t *Terminal) WaitUntil(ctx context.Context, pred func(*Terminal) bool) error
- func (t *Terminal) Width() int
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dir ¶
Dir returns test/results/<dateTimeISO>-<testName>, creating it. The timestamp is fixed for the whole test binary run.
func Record ¶
Record samples the terminal until it exits and writes <Dir>/<dateTimeISO>-recording.svg and <Dir>/<dateTimeISO>-recording.gif when the test ends. Call it right after Start. The terminal is closed during cleanup, so a still-running program is killed and the recording ends there. The SVG stays crisp at any zoom and shows the viewer's color emoji; the GIF is pixel-exact with the bundled fonts and needs nothing from the viewer.
Example ¶
ExampleRecord is the test flow, a copy of test/e2e/example_countdown_test.go in the repository: the body of a TestXxx that records every frame and leaves <dateTimeISO>-start.png, <dateTimeISO>-liftoff.png, <dateTimeISO>-liftoff.svg, <dateTimeISO>-recording.svg and <dateTimeISO>-recording.gif under test/results/<dateTimeISO>-<testName>/.
package main
import (
"context"
"testing"
"time"
"github.com/dimmkirr/termoscope"
)
func main() {
testCountdown := func(t *testing.T) {
bin := "./countdown" // normally built into t.TempDir() with go build
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
tm, err := termoscope.Start(ctx, 40, 4, bin, "-pace=120ms")
if err != nil {
t.Fatal(err)
}
termoscope.Record(t, tm) // <dateTimeISO>-recording.svg and .gif on cleanup
if _, err := tm.WaitFor(ctx, "Countdown"); err != nil {
t.Fatal(err)
}
termoscope.SavePNG(t, tm, "start")
if _, err := tm.WaitFor(ctx, "Liftoff"); err != nil {
t.Fatal(err)
}
if err := tm.Wait(); err != nil {
t.Fatalf("countdown exited with %v", err)
}
termoscope.SavePNG(t, tm, "liftoff")
termoscope.SaveSVG(t, tm, "liftoff")
for _, x := range []int{11, 13, 15} {
c := tm.CellAt(x, 0)
if c == nil || c.Content != "▄" {
t.Errorf("cell (%d,0) should be a tile, got %+v", x, c)
}
}
}
_ = testCountdown
}
Output:
func RecordWith ¶
RecordWith is Record with Options: sampling interval, hold, minimum canvas size and emoji embedding. The SVG and the GIF share them.
func SavePNG ¶
SavePNG renders the screen to <Dir>/<dateTimeISO>-<name>.png and returns the path. Like the SVG, the image is cropped to the cells holding content and padded by one cell height (raster.Margin) on every side, at 2x.
func SaveSVG ¶
SaveSVG writes a static SVG of the screen to <Dir>/<dateTimeISO>-<name>.svg and returns the path.
func SetResultsRoot ¶
func SetResultsRoot(dir string)
SetResultsRoot overrides the directory that holds test/results. Empty restores auto-detection: walk up from the working directory to go.mod.
Types ¶
type Options ¶ added in v0.3.0
type Options struct {
Interval time.Duration // sampling interval, default 40ms
Hold time.Duration // how long the last frame stays before looping, default 2s
MinCols int // minimum canvas width in cells; the canvas still grows to fit content
MinRows int // minimum canvas height in cells; the canvas still grows to fit content
EmbedEmoji bool // embed the monochrome Noto Emoji face in the SVG (about 750 KB) for a render that is identical everywhere
}
Options tunes what RecordWith records and renders. The zero value is what Record uses. Fields that apply to both the SVG and the GIF are set once here so the two recordings always agree. A new knob that both recordings should share belongs here, mapped onto svg.Options and gif.Options below, rather than in one renderer's options alone.
type Terminal ¶
type Terminal struct {
// contains filtered or unexported fields
}
Terminal is a running child process with its screen.
func Start ¶
Start launches name with args in a width×height PTY. The environment advertises a truecolor xterm so colors are emitted.
Example ¶
ExampleStart drives a program outside go test: start it in a 40x4 PTY, wait for a screen state, read cells, and wait for exit. The renderer packages raster, svg and gif turn the same *Terminal into images.
package main
import (
"context"
"fmt"
"time"
"github.com/dimmkirr/termoscope"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
tm, err := termoscope.Start(ctx, 40, 4, "./countdown", "-pace=120ms")
if err != nil {
panic(err)
}
defer tm.Close()
if _, err := tm.WaitFor(ctx, "Liftoff"); err != nil {
panic(err) // the error carries the current screen
}
if err := tm.Wait(); err != nil {
panic(err)
}
if c := tm.CellAt(11, 0); c != nil {
fmt.Println(c.Content, c.Style.Fg)
}
fmt.Println(tm.Line(1))
}
Output:
func (*Terminal) CellAt ¶
CellAt returns a copy of one screen cell, or nil outside the screen. A copy is returned because the emulator keeps writing while tests read.
func (*Terminal) Close ¶
func (t *Terminal) Close()
Close releases the PTY and kills the process if still running.
func (*Terminal) Done ¶
func (t *Terminal) Done() <-chan struct{}
Done is closed when the process has exited. Safe for multiple waiters.
func (*Terminal) Wait ¶
Wait blocks until the process exits and returns its error. It may be called more than once.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
termoscope
command
Command termoscope records a program running under a headless pseudo-terminal and writes an animated SVG, optionally with a PNG of the final screen.
|
Command termoscope records a program running under a headless pseudo-terminal and writes an animated SVG, optionally with a PNG of the final screen. |
|
examples
|
|
|
countdown
command
Command countdown is a tiny in-place updating TUI used by termoscope's own tests and README.
|
Command countdown is a tiny in-place updating TUI used by termoscope's own tests and README. |
|
emoji
command
Command emoji is a tiny in-place updating TUI used by termoscope's own tests and README to prove wide emoji cells render and align.
|
Command emoji is a tiny in-place updating TUI used by termoscope's own tests and README to prove wide emoji cells render and align. |
|
Package gif encodes recorded terminal frames as an animated GIF.
|
Package gif encodes recorded terminal frames as an animated GIF. |
|
internal
|
|
|
fonts
Package fonts embeds the typefaces used for screenshots and README animations so renders look the same on every machine: JetBrains Mono Regular for text and Noto Emoji Regular (monochrome) for emoji glyphs JetBrains Mono lacks.
|
Package fonts embeds the typefaces used for screenshots and README animations so renders look the same on every machine: JetBrains Mono Regular for text and Noto Emoji Regular (monochrome) for emoji glyphs JetBrains Mono lacks. |
|
twemoji
Package twemoji embeds the Twemoji 72x72 PNG set (jdecked/twemoji, graphics CC-BY 4.0, see LICENSE-GRAPHICS) and resolves a grapheme cluster to its image.
|
Package twemoji embeds the Twemoji 72x72 PNG set (jdecked/twemoji, graphics CC-BY 4.0, see LICENSE-GRAPHICS) and resolves a grapheme cluster to its image. |
|
Package raster rasterizes a headless terminal screen into a hi-DPI RGBA image: JetBrains Mono at 2x for text, Twemoji PNGs for color emoji, and monochrome Noto Emoji as the fallback.
|
Package raster rasterizes a headless terminal screen into a hi-DPI RGBA image: JetBrains Mono at 2x for text, Twemoji PNGs for color emoji, and monochrome Noto Emoji as the fallback. |
|
Package svg records a headless terminal screen over time and renders the frames as a self-contained animated SVG suitable for README files.
|
Package svg records a headless terminal screen over time and renders the frames as a self-contained animated SVG suitable for README files. |