tmuxfixture

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package tmuxfixture gives tests a private tmux server that cannot reach a developer's real tmux server.

Isolation rules, all enforced before any tmux process is started:

  • Every command is run as `tmux -S <socket> ...`. The socket is an absolute path inside a directory this fixture created; it never depends on TMUX, TMUX_TMPDIR or -L name resolution.
  • The child environment has TMUX, TMUX_PANE and TMUX_TMPDIR removed, so nothing inherited from a surrounding tmux can select another server.
  • Ownership is recorded on disk (owner.json with a random nonce) and is re-verified before each command. The socket must live inside the owned directory and must not be a default or inherited socket path.
  • Callers cannot pass global options (-L, -S, -f, ...) or chain commands with ";". kill-server is never allowed.
  • Sessions are created only through NewSession, which records the name before tmux runs. Cleanup kills exactly those recorded sessions with `kill-session -t =<name>`; it never kills the server.

Index

Constants

This section is empty.

Variables

View Source
var ErrRejected = errors.New("tmuxfixture: rejected")

ErrRejected marks a command refused by the guard. A rejected command was never executed.

Functions

func Env

func Env() []string

Env returns the scrubbed environment used for every tmux command.

func ExecTmux

func ExecTmux(argv []string, env []string) (string, error)

ExecTmux is the real executor.

func MaybeRunWrapper

func MaybeRunWrapper()

MaybeRunWrapper must be the first call in TestMain. If this process was started as the tmux wrapper it never returns.

func RunGuarded

func RunGuarded(m *testing.M) int

RunGuarded is the TestMain for packages whose code runs tmux by name (cmd, session, web):

func TestMain(m *testing.M) { os.Exit(tmuxfixture.RunGuarded(m)) }

It (1) serves wrapper-mode invocations, (2) installs the test-only tmux wrapper so every real tmux exec carries an explicit owned -S socket, and sets a fake HOME so metadata side effects stay in a test store, (3) runs the tests, (4) removes any sessions left on the OWNED socket by exact name (never kill-server; nothing else lives on that socket), and removes the directory.

func ShortTempBase

func ShortTempBase() string

ShortTempBase returns the directory fixture and wrapper dirs are created in: "/tmp" where it exists (short, because macOS limits socket paths to 104 bytes), otherwise the platform temp dir (e.g. on Windows, which has no /tmp; there are no tmux sockets there, but packages still install the guard in TestMain and must not fail before any test runs).

func ValidateArgv

func ValidateArgv(argv []string, socket, dir string) error

ValidateArgv checks a complete tmux argv just before execution: it must start with -S <socket> pointing inside the owned dir, carry no other global options, and never address a default/inherited server.

Types

type Executor

type Executor func(argv []string, env []string) (string, error)

Executor runs tmux with argv (excluding the "tmux" binary) and env.

type Fixture

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

Fixture is one private tmux server.

func New

func New(opts Options) (*Fixture, error)

New creates a fresh fixture directory and ownership record. It does not start tmux; the server starts with the first NewSession.

func (*Fixture) Cleanup

func (f *Fixture) Cleanup() ([]string, error)

Cleanup kills exactly the recorded sessions (never the server) and returns the names it attempted. A session that is already gone is not an error.

func (*Fixture) CommandLogPath

func (f *Fixture) CommandLogPath() string

CommandLogPath is the durable audit log of every argv this fixture executed (one JSON array per line).

func (*Fixture) Dir

func (f *Fixture) Dir() string

Dir returns the fixture-owned directory.

func (*Fixture) KillSession

func (f *Fixture) KillSession(name string) error

KillSession kills one fixture-owned session by exact name.

func (*Fixture) NewSession

func (f *Fixture) NewSession(name, dir string, command ...string) error

NewSession records name as owned, then creates it detached.

func (*Fixture) Owner

func (f *Fixture) Owner() (*Owner, error)

Owner returns the current on-disk ownership record.

func (*Fixture) Owns

func (f *Fixture) Owns(name string) bool

Owns reports whether name is a recorded session of this fixture.

func (*Fixture) Run

func (f *Fixture) Run(args ...string) (string, error)

Run executes an allowlisted tmux command on the fixture socket.

func (*Fixture) Socket

func (f *Fixture) Socket() string

Socket returns the fixture's absolute socket path.

type Options

type Options struct {
	// BaseDir is where the fixture directory is created. Default "/tmp"
	// (short, because macOS limits socket paths to 104 bytes).
	BaseDir string
	// Exec replaces the real tmux executor (tests of the guard itself).
	Exec Executor
}

Options configures New.

type Owner

type Owner struct {
	Nonce     string    `json:"nonce"`
	Socket    string    `json:"socket"`
	Dir       string    `json:"dir"`
	PID       int       `json:"pid"`
	CreatedAt time.Time `json:"created_at"`
	Sessions  []string  `json:"sessions"`
}

Owner is the durable ownership record written to <dir>/owner.json.

type Wrapped

type Wrapped struct {
	Dir      string // fixture-owned directory
	Socket   string // owned socket every exec is pinned to
	BinDir   string // prepended to PATH
	Home     string // fake HOME for the package's tests
	RealTmux string // absolute real tmux ("" if not installed)
}

Wrapped describes an installed wrapper.

func ActiveWrapper

func ActiveWrapper() *Wrapped

ActiveWrapper returns the wrapper installed by RunGuarded, if any.

func InstallWrapper

func InstallWrapper() (*Wrapped, func(), error)

InstallWrapper creates the owned dir and socket record, a fake HOME, and the wrapper, and rewrites this process's environment:

PATH        = <dir>/bin:<old PATH>    (bin/tmux -> this test binary)
HOME        = <dir>/home              (no real sessions.json/config)
XDG_CONFIG_HOME, XDG_STATE_HOME, XDG_CACHE_HOME under the fake HOME
TMUX, TMUX_PANE, TMUX_TMPDIR removed

If tmux is not installed the wrapper still installs, so a test that tries to exec tmux is rejected instead of finding one elsewhere.

func (*Wrapped) WrapperLogPath

func (w *Wrapped) WrapperLogPath() string

WrapperLogPath returns the audit log of a Wrapped install.

Jump to

Keyboard shortcuts

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