Skip to content

Commit dea0c3a

Browse files
fix: isolate CLI tests from real registry and add CODEGRAPH_REGISTRY_PATH env var
The cli.test.js afterAll called pruneRegistry() with no arguments, operating on the real ~/.codegraph/registry.json. The run() helper also lacked HOME isolation, risking writes to the real registry. Add CODEGRAPH_REGISTRY_PATH env var support to registry.js, isolate all CLI spawns with a fake HOME, and remove the bare pruneRegistry() call.
1 parent 39f4452 commit dea0c3a

5 files changed

Lines changed: 34 additions & 7 deletions

File tree

src/parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'node:path';
33
import { fileURLToPath } from 'node:url';
44
import { Language, Parser } from 'web-tree-sitter';
55
import { warn } from './logger.js';
6-
import { loadNative } from './native.js';
6+
import { getNative, loadNative } from './native.js';
77

88
// Re-export all extractors for backward compatibility
99
export {
@@ -78,7 +78,7 @@ function resolveEngine(opts = {}) {
7878
const native = loadNative();
7979
if (native) return { name: 'native', native };
8080
if (pref === 'native') {
81-
warn('Native engine requested but unavailable — falling back to WASM');
81+
getNative(); // throws with detailed error + install instructions
8282
}
8383
}
8484
return { name: 'wasm', native: null };

src/registry.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import os from 'node:os';
33
import path from 'node:path';
44
import { debug, warn } from './logger.js';
55

6-
export const REGISTRY_PATH = path.join(os.homedir(), '.codegraph', 'registry.json');
6+
export const REGISTRY_PATH =
7+
process.env.CODEGRAPH_REGISTRY_PATH || path.join(os.homedir(), '.codegraph', 'registry.json');
78

89
/** Default TTL: entries not accessed within 30 days are pruned. */
910
export const DEFAULT_TTL_DAYS = 30;

tests/integration/cli.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import fs from 'node:fs';
88
import os from 'node:os';
99
import path from 'node:path';
1010
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
11-
import { pruneRegistry } from '../../src/registry.js';
1211

1312
const CLI = path.resolve('src/cli.js');
1413

@@ -37,19 +36,21 @@ export function main() {
3736
`.trimStart(),
3837
};
3938

40-
let tmpDir, dbPath;
39+
let tmpDir, tmpHome, dbPath;
4140

4241
/** Run the CLI and return stdout as a string. Throws on non-zero exit. */
4342
function run(...args) {
4443
return execFileSync('node', [CLI, ...args], {
4544
cwd: tmpDir,
4645
encoding: 'utf-8',
4746
timeout: 30_000,
47+
env: { ...process.env, HOME: tmpHome, USERPROFILE: tmpHome },
4848
});
4949
}
5050

5151
beforeAll(async () => {
5252
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cli-'));
53+
tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-clihome-'));
5354
for (const [name, content] of Object.entries(FIXTURE_FILES)) {
5455
fs.writeFileSync(path.join(tmpDir, name), content);
5556
}
@@ -61,7 +62,7 @@ beforeAll(async () => {
6162

6263
afterAll(() => {
6364
if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
64-
pruneRegistry();
65+
if (tmpHome) fs.rmSync(tmpHome, { recursive: true, force: true });
6566
});
6667

6768
describe('CLI smoke tests', () => {

tests/parsers/unified.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ describe('Unified parser API', () => {
2222
expect(engine.name).toBe('wasm');
2323
expect(engine.version).toBeNull();
2424
});
25+
26+
it('throws when engine=native is explicitly requested but unavailable', () => {
27+
const engine = getActiveEngine();
28+
if (engine.name === 'native') return; // skip — native is available
29+
expect(() => getActiveEngine({ engine: 'native' })).toThrow(/[Nn]ative/);
30+
});
2531
});
2632

2733
describe('parseFileAuto', () => {

tests/unit/registry.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { execFileSync } from 'node:child_process';
12
import fs from 'node:fs';
23
import os from 'node:os';
34
import path from 'node:path';
@@ -29,9 +30,27 @@ afterEach(() => {
2930
// ─── REGISTRY_PATH ──────────────────────────────────────────────────
3031

3132
describe('REGISTRY_PATH', () => {
32-
it('points to ~/.codegraph/registry.json', () => {
33+
it('points to ~/.codegraph/registry.json by default', () => {
3334
expect(REGISTRY_PATH).toBe(path.join(os.homedir(), '.codegraph', 'registry.json'));
3435
});
36+
37+
it('respects CODEGRAPH_REGISTRY_PATH env var', () => {
38+
const customPath = path.join(tmpDir, 'custom', 'registry.json');
39+
const result = execFileSync(
40+
'node',
41+
[
42+
'--input-type=module',
43+
'-e',
44+
`import { REGISTRY_PATH } from './src/registry.js'; process.stdout.write(REGISTRY_PATH);`,
45+
],
46+
{
47+
cwd: path.resolve(import.meta.dirname, '..', '..'),
48+
encoding: 'utf-8',
49+
env: { ...process.env, CODEGRAPH_REGISTRY_PATH: customPath },
50+
},
51+
);
52+
expect(result).toBe(customPath);
53+
});
3554
});
3655

3756
// ─── loadRegistry ───────────────────────────────────────────────────

0 commit comments

Comments
 (0)