test: isolate package-manager dependent hooks and formatter tests

This commit is contained in:
Affaan Mustafa
2026-03-30 02:00:47 -04:00
parent 0cf7738720
commit 0124f6f6dc
3 changed files with 77 additions and 12 deletions

View File

@@ -1221,9 +1221,14 @@ async function runTests() {
fs.writeFileSync(path.join(rootDir, '.prettierrc'), '{}'); fs.writeFileSync(path.join(rootDir, '.prettierrc'), '{}');
fs.writeFileSync(filePath, 'export const value = 1;\n'); fs.writeFileSync(filePath, 'export const value = 1;\n');
createCommandShim(binDir, 'npx', logFile); createCommandShim(binDir, 'npx', logFile);
const isolatedHome = path.join(testDir, 'isolated-home');
fs.mkdirSync(path.join(isolatedHome, '.claude'), { recursive: true });
const stdinJson = JSON.stringify({ tool_input: { file_path: filePath } }); const stdinJson = JSON.stringify({ tool_input: { file_path: filePath } });
const result = await runScript(path.join(scriptsDir, 'post-edit-format.js'), stdinJson, withPrependedPath(binDir)); const result = await runScript(path.join(scriptsDir, 'post-edit-format.js'), stdinJson, withPrependedPath(binDir, {
HOME: isolatedHome,
USERPROFILE: isolatedHome
}));
assert.strictEqual(result.code, 0, 'Should exit 0 for config-only repo'); assert.strictEqual(result.code, 0, 'Should exit 0 for config-only repo');
const logEntries = readCommandLog(logFile); const logEntries = readCommandLog(logFile);

View File

@@ -37,6 +37,33 @@ function cleanupTestDir(testDir) {
fs.rmSync(testDir, { recursive: true, force: true }); fs.rmSync(testDir, { recursive: true, force: true });
} }
function withIsolatedHome(fn) {
const isolatedHome = fs.mkdtempSync(path.join(os.tmpdir(), 'pm-home-'));
const originalHome = process.env.HOME;
const originalUserProfile = process.env.USERPROFILE;
process.env.HOME = isolatedHome;
process.env.USERPROFILE = isolatedHome;
try {
return fn(isolatedHome);
} finally {
if (originalHome !== undefined) {
process.env.HOME = originalHome;
} else {
delete process.env.HOME;
}
if (originalUserProfile !== undefined) {
process.env.USERPROFILE = originalUserProfile;
} else {
delete process.env.USERPROFILE;
}
fs.rmSync(isolatedHome, { recursive: true, force: true });
}
}
// Test suite // Test suite
function runTests() { function runTests() {
console.log('\n=== Testing package-manager.js ===\n'); console.log('\n=== Testing package-manager.js ===\n');
@@ -711,9 +738,11 @@ function runTests() {
const originalEnv = process.env.CLAUDE_PACKAGE_MANAGER; const originalEnv = process.env.CLAUDE_PACKAGE_MANAGER;
try { try {
delete process.env.CLAUDE_PACKAGE_MANAGER; delete process.env.CLAUDE_PACKAGE_MANAGER;
const result = pm.getPackageManager({ projectDir: testDir }); withIsolatedHome(() => {
assert.strictEqual(result.name, 'npm', 'Should default to npm'); const result = pm.getPackageManager({ projectDir: testDir });
assert.strictEqual(result.source, 'default'); assert.strictEqual(result.name, 'npm', 'Should default to npm');
assert.strictEqual(result.source, 'default');
});
} finally { } finally {
if (originalEnv !== undefined) { if (originalEnv !== undefined) {
process.env.CLAUDE_PACKAGE_MANAGER = originalEnv; process.env.CLAUDE_PACKAGE_MANAGER = originalEnv;

View File

@@ -58,6 +58,33 @@ function cleanupTmpDirs() {
tmpDirs.length = 0; tmpDirs.length = 0;
} }
function withIsolatedHome(fn) {
const isolatedHome = fs.mkdtempSync(path.join(os.tmpdir(), 'resolve-fmt-home-'));
const originalHome = process.env.HOME;
const originalUserProfile = process.env.USERPROFILE;
process.env.HOME = isolatedHome;
process.env.USERPROFILE = isolatedHome;
try {
return fn(isolatedHome);
} finally {
if (originalHome !== undefined) {
process.env.HOME = originalHome;
} else {
delete process.env.HOME;
}
if (originalUserProfile !== undefined) {
process.env.USERPROFILE = originalUserProfile;
} else {
delete process.env.USERPROFILE;
}
fs.rmSync(isolatedHome, { recursive: true, force: true });
}
}
function runTests() { function runTests() {
console.log('\n=== Testing resolve-formatter.js ===\n'); console.log('\n=== Testing resolve-formatter.js ===\n');
@@ -168,10 +195,12 @@ function runTests() {
run('resolveFormatterBin: falls back to npx for biome', () => { run('resolveFormatterBin: falls back to npx for biome', () => {
const root = makeTmpDir(); const root = makeTmpDir();
const result = resolveFormatterBin(root, 'biome'); withIsolatedHome(() => {
const expectedBin = process.platform === 'win32' ? 'npx.cmd' : 'npx'; const result = resolveFormatterBin(root, 'biome');
assert.strictEqual(result.bin, expectedBin); const expectedBin = process.platform === 'win32' ? 'npx.cmd' : 'npx';
assert.deepStrictEqual(result.prefix, ['@biomejs/biome']); assert.strictEqual(result.bin, expectedBin);
assert.deepStrictEqual(result.prefix, ['@biomejs/biome']);
});
}); });
run('resolveFormatterBin: uses local prettier binary when available', () => { run('resolveFormatterBin: uses local prettier binary when available', () => {
@@ -188,10 +217,12 @@ function runTests() {
run('resolveFormatterBin: falls back to npx for prettier', () => { run('resolveFormatterBin: falls back to npx for prettier', () => {
const root = makeTmpDir(); const root = makeTmpDir();
const result = resolveFormatterBin(root, 'prettier'); withIsolatedHome(() => {
const expectedBin = process.platform === 'win32' ? 'npx.cmd' : 'npx'; const result = resolveFormatterBin(root, 'prettier');
assert.strictEqual(result.bin, expectedBin); const expectedBin = process.platform === 'win32' ? 'npx.cmd' : 'npx';
assert.deepStrictEqual(result.prefix, ['prettier']); assert.strictEqual(result.bin, expectedBin);
assert.deepStrictEqual(result.prefix, ['prettier']);
});
}); });
run('resolveFormatterBin: returns null for unknown formatter', () => { run('resolveFormatterBin: returns null for unknown formatter', () => {