test: cover setGlobal/setProject catch blocks and session-start main().catch

- setup-package-manager: setGlobal catch when HOME is non-directory (ENOTDIR)
- setup-package-manager: setProject catch when CWD is read-only (EACCES)
- session-start: main().catch handler when ensureDir throws (exit 0, don't block)

Total tests: 825 → 828
This commit is contained in:
Affaan Mustafa
2026-02-13 09:55:00 -08:00
parent 0c67e0571e
commit 241c35a589
2 changed files with 65 additions and 0 deletions

View File

@@ -3153,6 +3153,26 @@ async function runTests() {
}
})) passed++; else failed++;
// ── Round 74: session-start.js main().catch handler ──
console.log('\nRound 74: session-start.js (main catch — unrecoverable error):');
if (await asyncTest('session-start exits 0 with error message when HOME is non-directory', async () => {
if (process.platform === 'win32') {
console.log(' (skipped — /dev/null not available on Windows)');
return;
}
// HOME=/dev/null makes ensureDir(sessionsDir) throw ENOTDIR,
// which propagates to main().catch — the top-level error boundary
const result = await runScript(path.join(scriptsDir, 'session-start.js'), '', {
HOME: '/dev/null',
USERPROFILE: '/dev/null'
});
assert.strictEqual(result.code, 0,
`Should exit 0 (don't block on errors), got ${result.code}`);
assert.ok(result.stderr.includes('[SessionStart] Error:'),
`stderr should contain [SessionStart] Error:, got: ${result.stderr}`);
})) passed++; else failed++;
// Summary
console.log('\n=== Test Results ===');
console.log(`Passed: ${passed}`);

View File

@@ -344,6 +344,51 @@ function runTests() {
assert.strictEqual(currentCount, 1, `Expected exactly 1 "(current)" in --list, found ${currentCount}`);
})) passed++; else failed++;
// ── Round 74: setGlobal catch — setPreferredPackageManager throws ──
console.log('\nRound 74: setGlobal catch (save failure):');
if (test('--global npm fails when HOME is not a directory', () => {
if (process.platform === 'win32') {
console.log(' (skipped — /dev/null not available on Windows)');
return;
}
// HOME=/dev/null causes ensureDir to throw ENOTDIR when creating ~/.claude/
const result = run(['--global', 'npm'], { HOME: '/dev/null', USERPROFILE: '/dev/null' });
assert.strictEqual(result.code, 1, `Expected exit 1, got ${result.code}`);
assert.ok(result.stderr.includes('Error:'),
`stderr should contain Error:, got: ${result.stderr}`);
})) passed++; else failed++;
// ── Round 74: setProject catch — setProjectPackageManager throws ──
console.log('\nRound 74: setProject catch (save failure):');
if (test('--project npm fails when CWD is read-only', () => {
if (process.platform === 'win32' || process.getuid?.() === 0) {
console.log(' (skipped — chmod ineffective on Windows/root)');
return;
}
const tmpDir = path.join(os.tmpdir(), `spm-test-ro-${Date.now()}`);
fs.mkdirSync(tmpDir, { recursive: true });
try {
// Make CWD read-only so .claude/ dir creation fails with EACCES
fs.chmodSync(tmpDir, 0o555);
const result = require('child_process').spawnSync('node', [SCRIPT, '--project', 'npm'], {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'pipe'],
env: { ...process.env },
timeout: 10000,
cwd: tmpDir
});
assert.strictEqual(result.status, 1,
`Expected exit 1, got ${result.status}. stderr: ${result.stderr}`);
assert.ok(result.stderr.includes('Error:'),
`stderr should contain Error:, got: ${result.stderr}`);
} finally {
try { fs.chmodSync(tmpDir, 0o755); } catch { /* best-effort */ }
fs.rmSync(tmpDir, { recursive: true, force: true });
}
})) passed++; else failed++;
// Summary
console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`);
process.exit(failed > 0 ? 1 : 0);