cli-parse-warning.test.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { afterEach, beforeEach, describe, expect, it } from 'vitest';
  2. import { spawnSync } from 'child_process';
  3. import * as fs from 'fs';
  4. import * as os from 'os';
  5. import * as path from 'path';
  6. const BIN = path.resolve(__dirname, '../dist/bin/codegraph.js');
  7. const COLLAPSE_WARNING = 'parse produced no symbols (tree has errors)';
  8. const SOURCE = `const char* kTemplate = R"FILE_TEMPLATE_V1(
  9. struct Ignored { int v; };
  10. )FILE_TEMPLATE_V1";
  11. int after_the_raw_string(int x) {
  12. return x + 1;
  13. }
  14. `;
  15. describe('CLI parse warnings (#1522)', () => {
  16. let root: string;
  17. beforeEach(() => {
  18. root = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-parse-warning-'));
  19. });
  20. afterEach(() => {
  21. fs.rmSync(root, { recursive: true, force: true });
  22. });
  23. function run(args: string[]) {
  24. const result = spawnSync(process.execPath, [BIN, ...args], {
  25. cwd: root,
  26. encoding: 'utf-8',
  27. timeout: 20_000,
  28. env: {
  29. ...process.env,
  30. CODEGRAPH_NO_DAEMON: '1',
  31. CODEGRAPH_WASM_RELAUNCHED: '1',
  32. CODEGRAPH_TELEMETRY: '0',
  33. NO_COLOR: '1',
  34. },
  35. });
  36. return { status: result.status, out: (result.stdout ?? '') + (result.stderr ?? '') };
  37. }
  38. it('shows a collapsed parse without failing, then stays quiet after a healthy re-index', () => {
  39. const sourcePath = path.join(root, 'min.cpp');
  40. fs.writeFileSync(sourcePath, SOURCE);
  41. const collapsed = run(['init', '--yes']);
  42. expect(collapsed.status, collapsed.out).toBe(0);
  43. expect(collapsed.out).toContain('Indexed 1 files');
  44. expect(collapsed.out).toContain(`min.cpp: ${COLLAPSE_WARNING}`);
  45. fs.writeFileSync(sourcePath, SOURCE.replaceAll('FILE_TEMPLATE_V1', 'FILE_TEMPLATE_V'));
  46. const healthy = run(['index']);
  47. expect(healthy.status, healthy.out).toBe(0);
  48. expect(healthy.out).not.toContain(COLLAPSE_WARNING);
  49. const query = run(['query', 'after_the_raw_string']);
  50. expect(query.status, query.out).toBe(0);
  51. expect(query.out).toMatch(/function\s+after_the_raw_string/);
  52. }, 30_000);
  53. });