testutil

package
v0.11.4 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package testutil provides shared test utilities for both integration and e2e tests. This package has no build tags, making it usable by all test packages.

Index

Constants

View Source
const EnvGitHermetic = gitenv.EnvHermetic

EnvGitHermetic opts a test process into gitenv's hermetic transport config; see gitenv.EnvHermetic.

Variables

This section is empty.

Functions

func AddRemote added in v0.10.0

func AddRemote(t *testing.T, repoDir, name, url string)

AddRemote adds a git remote named name pointing at url in repoDir.

func BranchExists

func BranchExists(t *testing.T, repoDir, branchName string) bool

BranchExists checks if a branch exists in the repository.

func CreateBranch added in v0.5.6

func CreateBranch(t *testing.T, dir string, name string)

CreateBranch creates a local branch at the current HEAD.

func GetHeadHash

func GetHeadHash(t *testing.T, repoDir string) string

GetHeadHash returns the current HEAD commit hash.

func GitAdd

func GitAdd(t *testing.T, repoDir string, paths ...string)

GitAdd stages files for commit.

func GitAddForce added in v0.10.2

func GitAddForce(t *testing.T, repoDir string, paths ...string)

GitAddForce stages paths past .gitignore. GitAdd goes through go-git's worktree.Add, which has no force option.

func GitCheckoutNewBranch

func GitCheckoutNewBranch(t *testing.T, repoDir, branchName string)

GitCheckoutNewBranch creates and checks out a new branch. Uses git CLI to work around go-git v5 bug with checkout deleting untracked files.

func GitCommit

func GitCommit(t *testing.T, repoDir, message string)

GitCommit creates a commit with all staged files.

func GitGrepGuard added in v0.11.0

func GitGrepGuard(t *testing.T, repoRoot string, args ...string) string

GitGrepGuard runs `git grep` for a source-level guard test and returns its output, failing the test when the pattern matched nothing.

It exists because this repo has four guards that scan their own source with `git grep` — the root-base allowlist, the transcript-read ratchet, the `git status` flag check, and the agent hook-config coverage check — and getting the invocation right is not obvious. Each of the three flags below was missing from at least one of them, and the same bug was found in three: two copies is how they drift, and four is how they drifted.

--untracked, because git grep otherwise searches only the INDEX. Every one of these guards has "someone just added a file" as its subject, so the file it most needs to see is the one not yet staged. The hole is worse than a plain miss in two of them: the hook-config guard compares two sets built from the same blind grep, so a new agent missing its locator is invisible on both sides and the comparison passes; and the root-base guard's staleness half reported a just-added entry as STALE, telling the author to delete the entry that legitimises their new root. Ignored files stay excluded, which is right.

--no-color, because `color.ui` or `color.grep` set to `always` colorizes even into a pipe, and the escapes land in the FILENAME field. Confirmed for both output shapes: `-n` gives `\e[35mpath\e[m\e[36m:\e[m...`, and `-l` — which looks like it should be immune, being nothing but filenames — gives `\e[35mpath\e[m`. Every one of these guards then either misparses (a `.go` suffix check fails and the guard reports itself stale, which was read as a real staleness failure on main, #2248) or, worse, silently compares garbage to garbage while its "did we match anything" assertion still passes.

Repo-selector scrubbing, because git exports GIT_DIR / GIT_WORK_TREE to the hooks it runs and those take precedence over cmd.Dir. A `go test` launched from anywhere that exports them — a `git rebase --exec`, a hook, this CLI's own harnesses — pointed the grep at some other repository. Measured against a decoy repo, an unscrubbed grep returns the decoy's files while cmd.Dir names this one. GIT_CONFIG_* isolation is applied too (gitenv.Isolated), so a developer's global grep settings cannot change the pattern semantics either.

args are the caller's own flags, pattern and pathspecs, appended after the three above.

func GitGrepGuardRepoRoot added in v0.11.0

func GitGrepGuardRepoRoot(t *testing.T) (string, bool)

GitGrepGuardRepoRoot resolves the repository root for a guard test, skipping when there is no checkout to scan.

func GitIsolatedEnv added in v0.5.0

func GitIsolatedEnv() []string

GitIsolatedEnv returns os.Environ() with git isolation variables set; see gitenv.Isolated. The implementation lives in the dependency-free gitenv subpackage so internal tests of packages testutil imports (e.g. gitrepo) can use it without an import cycle; everything else keeps this spelling.

func GitReset added in v0.5.6

func GitReset(t *testing.T, dir string, ref string)

GitReset runs git reset --hard to the given ref.

func GitUpdateRef added in v0.10.0

func GitUpdateRef(t *testing.T, repoDir, ref, hash string)

GitUpdateRef points ref at hash in repoDir via git update-ref.

func GuardGitEnv added in v0.11.0

func GuardGitEnv() []string

GuardGitEnv drops the repo selectors and the caller's git config, keeping both isolations in one place so the two subprocesses above cannot disagree about which repository they are looking at.

func InitRepo

func InitRepo(t *testing.T, repoDir string)

InitRepo initializes a git repository in the given directory with test user config.

func IsolateGitConfigEnv added in v0.7.0

func IsolateGitConfigEnv(t *testing.T)

IsolateGitConfigEnv applies git config isolation to the current process; see gitenv.IsolateProcess.

func MigrateToReftable added in v0.11.4

func MigrateToReftable(t *testing.T, root string)

MigrateToReftable migrates a temporary fixture repository, skipping the test when installed Git lacks ref-format migration support.

func RunGit added in v0.10.2

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

RunGit runs one git command in dir with an isolated git config, failing the test on error and returning combined output. Use it for operations go-git cannot express (force-add past .gitignore, rm --cached, worktree add) or where shelling out is simply clearer. See gitenv.Run.

func SkipWithoutSymlinks(t *testing.T)

SkipWithoutSymlinks skips a test that needs to create a symlink. On Windows that takes elevation or developer mode, neither of which CI has.

Here rather than per-package because six copies in three different wordings had accumulated across cli, agent, worktreedir and vercelconfig tests, so there was nowhere to make the change when a runner does gain the privilege.

func WriteCheckpointPushRemoteSetting added in v0.10.0

func WriteCheckpointPushRemoteSetting(t *testing.T, repoDir, remoteName string)

WriteCheckpointPushRemoteSetting writes .entire/settings.json configuring strategy_options.checkpoint_push_remote to remoteName (with enabled: true).

func WriteFile

func WriteFile(t *testing.T, repoDir, path, content string)

WriteFile creates a file with the given content in the repo directory. It creates parent directories as needed.

Types

type PendingCheckpoint added in v0.10.4

type PendingCheckpoint struct {
	ID               string    `json:"id"`
	Message          string    `json:"message"`
	MetadataDir      string    `json:"metadata_dir"`
	Date             time.Time `json:"date"`
	IsTaskCheckpoint bool      `json:"is_task_checkpoint"`
	ToolUseID        string    `json:"tool_use_id"`
	IsLogsOnly       bool      `json:"is_logs_only"`
	CondensationID   string    `json:"condensation_id"`
}

PendingCheckpoint mirrors the `checkpoint list --pending --json` JSON output.

Directories

Path Synopsis
Package gitenv isolates test git invocations from the host's git config.
Package gitenv isolates test git invocations from the host's git config.

Jump to

Keyboard shortcuts

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