scriptruntest

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package scriptruntest is the runner the suites of scriptrun's kinds share (hooks, scripts, and release): a real scriptrun Engine and its Service over a fresh database, the app's next start over the same history, a fake terminal app that runs each run meant for the user's terminal under /bin/sh (Terminal), and waits for a run's end that look again only when the engine says the runs changed, never on a timer (AwaitShell alone polls, for a shell the engine notes without telling).

It is test support only, built on internal/testutil: only _test.go files may import it, which a repoguard test enforces, so none of it reaches the app.

Index

Constants

View Source
const Wait = 30 * time.Second

Wait bounds how long a test waits for a run to end, or for what it awaits.

Variables

This section is empty.

Functions

This section is empty.

Types

type Harness

type Harness struct {
	// DB holds script_runs, LogDir the runs' logs, and Locks the clone locks, which the
	// service under test shares with the runner. The next start (Next) runs over the same
	// three.
	DB     *sql.DB
	LogDir string
	Locks  *clonelock.Set

	// Engine starts the runs, and Runs reads them back.
	Engine *scriptrun.Engine
	Runs   *scriptrun.Service
	// contains filtered or unexported fields
}

Harness is the runner as the app builds it, over a fresh database, a log folder and a set of clone locks of its own, and a home: the Engine the service under test starts its runs with, and the Service that reads them back as the window does. The engine stops every run when the test ends.

func New

func New(t testing.TB, home string, logger *slog.Logger, opts ...Option) *Harness

New returns a Harness whose runs' folders show under home with ~, and whose engine logs to logger (nil discards).

func (*Harness) Await

func (h *Harness) Await(cond func() bool, msgAndArgs ...any)

Await waits until cond holds, and fails the test with msgAndArgs, and why the wait most likely ran out, when it hasn't within Wait. It looks again each time the engine says the runs changed, so cond must hold once that is told: a run started, its end recorded or refused, its clone let go of, or what its kind's Released did, all of which come before the engine tells it.

func (*Harness) AwaitShell

func (h *Harness) AwaitShell(id int64)

AwaitShell waits until the engine has seen the shell the terminal runs the run id in: its PID recorded, so the shell's end is read as the run's, with its exit status or without one. A test that ends such a shell, as a user closes the terminal, waits for this first, since a shell that comes and goes unseen reads as a terminal that never ran the command, once the engine's wait for it is up. The engine tells no change as it notes a shell (the window shows no PID, so telling it would only list the runs again), so this wait alone looks again on a timer, every shellPoll, up to Wait, reading the run's record as scriptrun's own tests do.

func (*Harness) Ended

func (h *Harness) Ended(id int64) scriptrun.Run

Ended waits for the run id to end and let go of its clone, and returns it as the window reads it. It looks again each time the engine says the runs changed, which it does once a run's end is recorded and the run has let go of its clone, and fails the test when the run hasn't ended within Wait, saying where the run stands and why the wait most likely ran out.

func (*Harness) Next

func (h *Harness) Next(verifiers map[scriptrun.Kind]scriptrun.Verifier) *scriptrun.Engine

Next returns the runner as the app's next start builds it, over the same history, logs, locks, and home, with verifiers registered for their kinds. It isn't running yet.

func (*Harness) Restart

func (h *Harness) Restart(id int64, verifiers map[scriptrun.Kind]scriptrun.Verifier) scriptrun.Run

Restart runs the next start's runner (Next) until the run id is settled and has let go of its clone, stops it, and returns the run as that runner reads it.

type Option

type Option func(*Harness)

Option sets up a Harness as New builds it.

func WithTerminal

func WithTerminal(term scriptrun.TerminalOpener) Option

WithTerminal has the harness's engines, the next start's included, open the runs in the terminal in term, such as a Terminal. Without it, a run in the terminal fails to start.

type Terminal

type Terminal struct {
	// Env is the environment the terminal's shell starts with, beside the test's own and
	// what each run exports: the user's environment as a login shell would read it, such
	// as a fake tool's entry. Set it before the first run opens.
	Env []string
	// contains filtered or unexported fields
}

Terminal stands in for the user's terminal app, for the runs a Harness's engine opens there (WithTerminal): it starts each run's command with /bin/sh running testutil.TerminalScript, with no window, and records each run it was asked to open. Close closes it as a user closes the terminal's tab mid-run. A shell still going as the test ends is stopped before the folders its run writes into are removed.

func NewTerminal

func NewTerminal(t testing.TB) *Terminal

NewTerminal returns a Terminal for the test t.

func (*Terminal) Close

func (term *Terminal) Close() error

Close closes the terminal as a user closes its tab while a run goes on: every shell's process group is hung up (SIGHUP), so the command is cut off where it is and the shell ends without writing the exit status. It returns once every shell has ended.

func (*Terminal) OpenFolder

func (*Terminal) OpenFolder(context.Context, string, string) error

OpenFolder opens nothing: no test of a run asks for the terminal in its folder.

func (*Terminal) OpenRun

func (term *Terminal) OpenRun(ctx context.Context, run scriptrun.TerminalRun) error

OpenRun starts the run's command in a shell that outlives the call, as a terminal's does.

func (*Terminal) Opened

func (term *Terminal) Opened() []scriptrun.TerminalRun

Opened returns the runs it was asked to open, in order.

Jump to

Keyboard shortcuts

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