Files
msd-core/tests/prune-orphaned-worktrees.test.cjs
Tom Boucher 1d208e5af6 test(#3144): bound the git/worktree cluster onto the process seam (#3152)
* test(#3144): bound the git/worktree cluster onto the process seam

Migrates 180 unbounded sync spawn sites across 19 files. Every previously
unbounded call now carries an explicit timeout with a comment giving the
number and why.

The migration is not a callee swap. execSync and execFileSync throw on a
non-zero exit and the seam never does, so each site was classified first:
sites that rely on the throw route to gitOrThrow, and sites that already read
.status to detect an EXPECTED non-zero -- an intended cherry-pick conflict, a
rev-parse outside a repo driving a skip -- route to the never-throwing runGit
instead, which would otherwise throw on exactly the exit being probed for.

Two same-named git() helpers in worktree-cleanup.test.cjs have different
return contracts, one trimmed and one raw; both are preserved rather than
unified.

Collapses five hand-rolled throw wrappers onto one throwIfFailed in
git-fixture.cjs, which gitOrThrow now also uses so the shape cannot drift.

Allowlist drops 139 to 120; BASELINE lowered to match.

* test(#3144): fix pre-PR review findings

Documents throwIfFailed in the CONTEXT.md glossary and CONTRIBUTING.md --
it became the shared throw mechanism without either doc naming it.

Routes the sixth and seventh hand-rolled copies of the throw shape through
throwIfFailed (worktree-baseref-install, worktree-safety-reap); the first
consolidation missed both.

Converts ci-rebase-check's 8 fixture-setup calls from unchecked runGit to
gitOrThrow so a failed setup step aborts where it fails rather than
surfacing later as a confusing failure against the wrong subject.

Adds 12 direct unit tests for throwIfFailed, which until now was only
exercised transitively.

Splits verify.test.cjs's non-git grep/sed bound off GIT_TIMEOUT_MS.

---------

Co-authored-by: sim <sim@local>
2026-08-07 10:58:34 -04:00

203 lines
8.0 KiB
JavaScript

/**
* Tests for pruneOrphanedWorktrees()
*
* Uses real temporary git repos (no mocks).
* All 4 tests must fail (RED) before implementation is added.
*/
'use strict';
const { describe, test, beforeEach, afterEach } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const path = require('path');
const { createTempDir, cleanup } = require('./helpers.cjs');
const { gitOrThrow } = require('./helpers/git-fixture.cjs');
// 15000ms: git plumbing (init/config/add/commit/branch/worktree/merge/checkout)
// on a small mkdtemp fixture repo — far over any observed duration for that
// class of call.
const GIT_TIMEOUT_MS = 15000;
// Lazy-loaded so tests can fail clearly when the export doesn't exist yet.
function getPruneOrphanedWorktrees() {
const { pruneOrphanedWorktrees } = require('../gsd-core/bin/lib/worktree-safety.cjs');
return pruneOrphanedWorktrees;
}
// Create a minimal git repo with an initial commit on main.
function canonicalPath(p) {
try {
return fs.realpathSync.native(path.resolve(p));
} catch {
return path.resolve(p);
}
}
function listedWorktreePaths(repoDir) {
const out = gitOrThrow(['worktree', 'list', '--porcelain'], { cwd: repoDir, timeoutMs: GIT_TIMEOUT_MS });
return new Set(
out
.split('\n')
.filter((line) => line.startsWith('worktree '))
.map((line) => canonicalPath(line.slice('worktree '.length).trim()))
);
}
function createGitRepo(dir) {
fs.mkdirSync(dir, { recursive: true });
gitOrThrow(['init'], { cwd: dir, timeoutMs: GIT_TIMEOUT_MS });
gitOrThrow(['config', 'user.email', 'test@test.com'], { cwd: dir, timeoutMs: GIT_TIMEOUT_MS });
gitOrThrow(['config', 'user.name', 'Test'], { cwd: dir, timeoutMs: GIT_TIMEOUT_MS });
gitOrThrow(['config', 'commit.gpgsign', 'false'], { cwd: dir, timeoutMs: GIT_TIMEOUT_MS });
fs.writeFileSync(path.join(dir, 'README.md'), '# Test\n');
gitOrThrow(['add', '-A'], { cwd: dir, timeoutMs: GIT_TIMEOUT_MS });
gitOrThrow(['commit', '-m', 'initial commit'], { cwd: dir, timeoutMs: GIT_TIMEOUT_MS });
// Rename to main if it isn't already (handles older git defaults)
try {
gitOrThrow(['branch', '-m', 'master', 'main'], { cwd: dir, timeoutMs: GIT_TIMEOUT_MS });
} catch { /* already named main */ }
}
// --- Test suite ---------------------------------------------------------------
describe('pruneOrphanedWorktrees', () => {
let tmpBase;
beforeEach(() => {
tmpBase = createTempDir('prune-wt-test-');
});
afterEach(() => {
cleanup(tmpBase);
});
// Test 1: keeps a merged worktree (destructive removal disabled by default)
test('keeps a worktree whose branch is merged into main', () => {
const repoDir = path.join(tmpBase, 'repo');
const worktreeDir = path.join(tmpBase, 'wt-merged');
createGitRepo(repoDir);
// Create worktree on a new branch (main is checked out in repoDir)
gitOrThrow(['worktree', 'add', worktreeDir, '-b', 'fix/old-work'], { cwd: repoDir, timeoutMs: GIT_TIMEOUT_MS });
assert.ok(fs.existsSync(worktreeDir), 'worktree dir should exist before prune');
// Add a commit in the worktree
fs.writeFileSync(path.join(worktreeDir, 'feature.txt'), 'work\n');
gitOrThrow(['add', '-A'], { cwd: worktreeDir, timeoutMs: GIT_TIMEOUT_MS });
gitOrThrow(['commit', '-m', 'old work'], { cwd: worktreeDir, timeoutMs: GIT_TIMEOUT_MS });
// Merge the branch into main from repoDir
gitOrThrow(['merge', 'fix/old-work', '--no-ff', '-m', 'merge old-work'], { cwd: repoDir, timeoutMs: GIT_TIMEOUT_MS });
// Act
const pruneOrphanedWorktrees = getPruneOrphanedWorktrees();
pruneOrphanedWorktrees(repoDir);
// Assert: worktree directory still exists
assert.ok(
fs.existsSync(worktreeDir),
'merged worktree should not be removed by default: ' + worktreeDir
);
// Assert: git worktree list still shows it
const listed = listedWorktreePaths(repoDir);
assert.ok(
listed.has(canonicalPath(worktreeDir)),
'git worktree list should still reference merged worktree'
);
});
// Test 2: keeps a worktree whose branch has unmerged commits
test('keeps a worktree whose branch has unmerged commits', () => {
const repoDir = path.join(tmpBase, 'repo2');
const worktreeDir = path.join(tmpBase, 'wt-active');
createGitRepo(repoDir);
// Create the worktree on a new branch (main is checked out in repoDir)
gitOrThrow(['worktree', 'add', worktreeDir, '-b', 'fix/active-work'], { cwd: repoDir, timeoutMs: GIT_TIMEOUT_MS });
// Add a commit in the worktree (NOT merged into main)
fs.writeFileSync(path.join(worktreeDir, 'active.txt'), 'active\n');
gitOrThrow(['add', '-A'], { cwd: worktreeDir, timeoutMs: GIT_TIMEOUT_MS });
gitOrThrow(['commit', '-m', 'active work'], { cwd: worktreeDir, timeoutMs: GIT_TIMEOUT_MS });
// main stays at its original commit — no merge
// Act
const pruneOrphanedWorktrees = getPruneOrphanedWorktrees();
pruneOrphanedWorktrees(repoDir);
// Assert: worktree directory still exists
assert.ok(
fs.existsSync(worktreeDir),
'worktree directory should NOT have been removed: ' + worktreeDir
);
});
// Test 3: never removes the worktree at process.cwd()
test('never removes the worktree at process.cwd()', () => {
const repoDir = path.join(tmpBase, 'repo3');
const wtDir = path.join(tmpBase, 'wt-cwd-test');
createGitRepo(repoDir);
// Create a worktree, add a commit, merge it into main
gitOrThrow(['worktree', 'add', wtDir, '-b', 'fix/another-merged'], { cwd: repoDir, timeoutMs: GIT_TIMEOUT_MS });
fs.writeFileSync(path.join(wtDir, 'more.txt'), 'more\n');
gitOrThrow(['add', '-A'], { cwd: wtDir, timeoutMs: GIT_TIMEOUT_MS });
gitOrThrow(['commit', '-m', 'another merged'], { cwd: wtDir, timeoutMs: GIT_TIMEOUT_MS });
gitOrThrow(['checkout', 'main'], { cwd: repoDir, timeoutMs: GIT_TIMEOUT_MS });
gitOrThrow(['merge', 'fix/another-merged', '--no-ff', '-m', 'merge another'], { cwd: repoDir, timeoutMs: GIT_TIMEOUT_MS });
// Run pruning
const pruneOrphanedWorktrees = getPruneOrphanedWorktrees();
const pruned = pruneOrphanedWorktrees(repoDir);
// No destructive removals are performed by default
assert.deepStrictEqual(pruned, []);
// The main worktree (repoDir) itself must still exist
assert.ok(
fs.existsSync(repoDir),
'main repo dir should still exist: ' + repoDir
);
});
// Test 4: runs git worktree prune to clear stale references
test('runs git worktree prune to clear stale references', () => {
const repoDir = path.join(tmpBase, 'repo4');
const worktreeDir = path.join(tmpBase, 'wt-stale');
createGitRepo(repoDir);
// Create a worktree
gitOrThrow(['worktree', 'add', worktreeDir, '-b', 'fix/stale-ref'], { cwd: repoDir, timeoutMs: GIT_TIMEOUT_MS });
assert.ok(fs.existsSync(worktreeDir), 'worktree dir should exist before manual deletion');
// Use the canonicalPath helper so Windows 8.3 short-name (RUNNER~1) vs
// long-form (runneradmin) and slash-direction differences both collapse
// to the same key before comparison. git stores the long-form path in
// its administrative files; substring matching on the raw path fails.
// Capture the canonical key BEFORE deletion since canonicalPath calls
// realpathSync.native which fails on missing paths.
const wantedKey = canonicalPath(worktreeDir);
assert.ok(listedWorktreePaths(repoDir).has(wantedKey), 'worktree should appear in list before deletion');
// Manually delete the worktree directory (simulate orphan)
// eslint-disable-next-line local/no-raw-rmsync-in-tests -- mid-test fault injection: simulates an orphaned worktree dir that git still references
fs.rmSync(worktreeDir, { recursive: true, force: true });
// Act
const pruneOrphanedWorktrees = getPruneOrphanedWorktrees();
pruneOrphanedWorktrees(repoDir);
// Assert: git worktree list no longer shows the stale entry.
assert.ok(
!listedWorktreePaths(repoDir).has(wantedKey),
'git worktree list still shows stale entry after prune'
);
});
});