test(update): 为 cleanStaleBinaries 增加临时目录沙箱隔离

在 `src/cli/commands/update.ts` 中为 `cleanStaleBinaries` 增加可选的 `candidateOverrides` 参数,允许在单元测试中注入沙箱路径。
在 `src/cli/commands/update.test.ts` 中重构单测,使用 `mkdtempSync` 创建临时隔离文件测试活跃路径保留与废弃文件清理逻辑,杜绝在宿主机测试期间误伤或删除真实全局二进制。
This commit is contained in:
gitea committed 2026-10-29 15:48:34 +08:00
1 parent e71a81402c
commit a885d1478e
2 files changed
+26 -9

No files matched your search

+18 -3
View File
@@ -349,8 +349,23 @@ describe("update installation modes", () => {
expect(SYSTEM_BINARY_PATH).toBe("/usr/local/bin/seal-use");
});
test("cleanStaleBinaries ignores activePath and returns list of cleaned files", () => {
const cleaned = cleanStaleBinaries(SYSTEM_BINARY_PATH);
expect(Array.isArray(cleaned)).toBe(true);
test("cleanStaleBinaries ignores activePath and returns list of cleaned files in isolated sandbox", () => {
const tempDir = mkdtempSync(join(tmpdir(), "seal-use-clean-test-"));
try {
const fakeActive = join(tempDir, "active-bin");
const fakeStale = join(tempDir, "stale-bin");
const fakeUntouched = join(tempDir, "untouched-bin");
writeFileSync(fakeActive, "active");
writeFileSync(fakeStale, "stale");
writeFileSync(fakeUntouched, "untouched");
const cleaned = cleanStaleBinaries(fakeActive, [fakeActive, fakeStale]);
expect(cleaned).toEqual([fakeStale]);
expect(existsSync(fakeActive)).toBe(true);
expect(existsSync(fakeStale)).toBe(false);
expect(existsSync(fakeUntouched)).toBe(true);
} finally {
rmSync(tempDir, { recursive: true, force: true });
}
});
});
+8 -6
View File
@@ -39,7 +39,7 @@ export function isPathWritable(targetPath: string): boolean {
}
}
export function cleanStaleBinaries(activePath: string): string[] {
export function cleanStaleBinaries(activePath: string, candidateOverrides?: string[]): string[] {
const cleaned: string[] = [];
const home = homedir();
const userBinary = getUserBinaryPath();
@@ -49,11 +49,13 @@ export function cleanStaleBinaries(activePath: string): string[] {
const homebrewBinary = "/opt/homebrew/bin/seal-use";
const homebrewAlias = "/opt/homebrew/bin/seal-home";
const candidates = [homebrewBinary, homebrewAlias];
if (activePath === systemBinary) {
candidates.push(userBinary, userAlias);
} else if (activePath === userBinary) {
candidates.push(systemBinary, systemAlias);
const candidates = candidateOverrides ? [...candidateOverrides] : [homebrewBinary, homebrewAlias];
if (!candidateOverrides) {
if (activePath === systemBinary) {
candidates.push(userBinary, userAlias);
} else if (activePath === userBinary) {
candidates.push(systemBinary, systemAlias);
}
}
for (const candidate of candidates) {