testutil

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewError added in v0.10.0

func NewError(msg string) error

NewError creates a simple error for testing.

func RunGit

func RunGit(t *testing.T, dir string, args ...string) string

RunGit executes a git command in the specified directory. Fails the test if the command fails.

func SetupTestRepo

func SetupTestRepo(t *testing.T, opts ...SetupOption) (repoDir, mainDir string)

SetupTestRepo creates a temporary git repository for testing. Returns repoDir (parent directory) and mainDir (git repository root).

By default, creates .twig/settings.toml without symlinks. Use Symlinks(...) to add symlink patterns. Use WithoutSettings() to skip creating settings entirely.

Types

type MockExitError added in v0.7.0

type MockExitError struct {
	Code int
}

MockExitError simulates exec.ExitError for testing.

func (*MockExitError) Error added in v0.7.0

func (e *MockExitError) Error() string

func (*MockExitError) ExitCode added in v0.7.0

func (e *MockExitError) ExitCode() int

type MockFS

type MockFS struct {
	// Override functions (takes precedence if set)
	StatFunc       func(name string) (fs.FileInfo, error)
	LstatFunc      func(name string) (fs.FileInfo, error)
	SymlinkFunc    func(oldname, newname string) error
	IsNotExistFunc func(err error) bool
	GlobFunc       func(dir, pattern string) ([]string, error)
	MkdirAllFunc   func(path string, perm fs.FileMode) error
	ReadDirFunc    func(name string) ([]os.DirEntry, error)
	RemoveFunc     func(name string) error
	WriteFileFunc  func(name string, data []byte, perm fs.FileMode) error

	// ExistingPaths is a list of paths that exist (Stat returns nil, nil).
	ExistingPaths []string

	// SymlinkErr is returned by Symlink if set.
	SymlinkErr error

	// GlobResults maps pattern to matching paths.
	GlobResults map[string][]string

	// GlobErr is returned by Glob if set.
	GlobErr error

	// MkdirAllErr is returned by MkdirAll if set.
	MkdirAllErr error

	// DirContents maps directory path to its entries.
	DirContents map[string][]os.DirEntry

	// ReadDirErr is returned by ReadDir if set.
	ReadDirErr error

	// RemoveErr is returned by Remove if set.
	RemoveErr error

	// WriteFileErr is returned by WriteFile if set.
	WriteFileErr error

	// WrittenFiles records files written by WriteFile.
	WrittenFiles map[string][]byte
}

MockFS is a mock implementation of twig.FileSystem for testing.

func (*MockFS) Glob

func (m *MockFS) Glob(dir, pattern string) ([]string, error)

func (*MockFS) IsNotExist

func (m *MockFS) IsNotExist(err error) bool

func (*MockFS) Lstat added in v0.10.0

func (m *MockFS) Lstat(name string) (fs.FileInfo, error)

func (*MockFS) MkdirAll

func (m *MockFS) MkdirAll(path string, perm fs.FileMode) error

func (*MockFS) ReadDir

func (m *MockFS) ReadDir(name string) ([]os.DirEntry, error)

func (*MockFS) Remove

func (m *MockFS) Remove(name string) error

func (*MockFS) Stat

func (m *MockFS) Stat(name string) (fs.FileInfo, error)
func (m *MockFS) Symlink(oldname, newname string) error

func (*MockFS) WriteFile

func (m *MockFS) WriteFile(name string, data []byte, perm fs.FileMode) error

type MockFileInfo added in v0.10.0

type MockFileInfo struct {
	NameVal    string
	SizeVal    int64
	ModeVal    fs.FileMode
	ModTimeVal time.Time
	IsDirVal   bool
}

MockFileInfo is a mock implementation of fs.FileInfo for testing.

func (*MockFileInfo) IsDir added in v0.10.0

func (m *MockFileInfo) IsDir() bool

func (*MockFileInfo) ModTime added in v0.10.0

func (m *MockFileInfo) ModTime() time.Time

func (*MockFileInfo) Mode added in v0.10.0

func (m *MockFileInfo) Mode() fs.FileMode

func (*MockFileInfo) Name added in v0.10.0

func (m *MockFileInfo) Name() string

func (*MockFileInfo) Size added in v0.10.0

func (m *MockFileInfo) Size() int64

func (*MockFileInfo) Sys added in v0.10.0

func (m *MockFileInfo) Sys() any

type MockGitExecutor

type MockGitExecutor struct {
	// RunFunc overrides the default behavior if set.
	RunFunc func(ctx context.Context, args ...string) ([]byte, error)

	// ExistingBranches is a list of branches that exist locally.
	ExistingBranches []string

	// Worktrees is a list of worktrees with their paths and branches.
	Worktrees []MockWorktree

	// WorktreeAddErr is returned when worktree add is called.
	WorktreeAddErr error

	// WorktreeRemoveErr is returned when worktree remove is called.
	WorktreeRemoveErr error

	// BranchDeleteErr is returned when branch -d/-D is called.
	BranchDeleteErr error

	// CapturedArgs captures the args passed to git commands.
	CapturedArgs *[]string

	// HasChanges indicates if git status --porcelain returns output.
	HasChanges bool

	// StatusOutput is the custom output for git status --porcelain.
	// If set, this overrides HasChanges.
	StatusOutput string

	// StashPushErr is returned when stash push is called.
	StashPushErr error

	// StashHash is returned when stash push succeeds and used for subsequent stash operations.
	StashHash string

	// StashApplyErr is returned when stash apply is called.
	StashApplyErr error

	// StashPopErr is returned when stash pop is called.
	StashPopErr error

	// StashDropErr is returned when stash drop is called.
	StashDropErr error

	// MergedBranches maps target branch to list of branches merged into it.
	MergedBranches map[string][]string

	// UpstreamGoneBranches is a list of branches whose upstream is gone.
	// Used by git for-each-ref to detect squash/rebase merged branches.
	UpstreamGoneBranches []string

	// WorktreePruneErr is returned when worktree prune is called.
	WorktreePruneErr error

	// BranchHEADs maps branch name to its HEAD commit hash.
	// Used by rev-parse and for-each-ref to return commit hashes for branches.
	BranchHEADs map[string]string

	// Remotes is a list of configured remote names.
	Remotes []string

	// RemoteBranches maps remote name to list of branches on that remote.
	// Used by for-each-ref to check local remote-tracking branches.
	RemoteBranches map[string][]string

	// FetchErr is returned when fetch is called.
	FetchErr error

	// SubmoduleStatusOutput is the output of `git submodule status --recursive`.
	// Empty string means no submodules.
	SubmoduleStatusOutput string

	// SubmoduleUpdateErr is returned when submodule update is called.
	SubmoduleUpdateErr error

	// SubmoduleUpdateCalled is set to true when submodule update is called.
	SubmoduleUpdateCalled bool

	// SubmoduleUpdateArgs captures the args passed to submodule update.
	SubmoduleUpdateArgs []string

	// WorktreeRootMap maps directory to its worktree root.
	// Used by rev-parse --show-toplevel to return the worktree root for a directory.
	WorktreeRootMap map[string]string
}

MockGitExecutor is a mock implementation of twig.GitExecutor for testing.

func (*MockGitExecutor) Run

func (m *MockGitExecutor) Run(ctx context.Context, args ...string) ([]byte, error)

type MockWorktree

type MockWorktree struct {
	Path           string
	Branch         string
	HEAD           string
	Detached       bool
	Locked         bool
	LockReason     string
	Prunable       bool
	PrunableReason string
	Bare           bool
}

MockWorktree represents a worktree entry for testing.

type SetupOption

type SetupOption func(*setupConfig)

SetupOption configures SetupTestRepo behavior.

func DefaultSource

func DefaultSource(branch string) SetupOption

DefaultSource sets the default_source field in settings.toml.

func ExtraSymlinks(patterns ...string) SetupOption

ExtraSymlinks sets the extra_symlinks patterns for settings.toml.

func Symlinks(patterns ...string) SetupOption

Symlinks sets the symlinks patterns for settings.toml. If not called, no symlinks are configured.

func WithoutSettings

func WithoutSettings() SetupOption

WithoutSettings skips creating .twig/settings.toml.

Jump to

Keyboard shortcuts

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