cli-affected-paths.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * `codegraph affected` input-path normalization (#825).
  3. *
  4. * The index stores project-relative, forward-slash paths. A user (or a wrapping
  5. * script) may pass a `./`-prefixed path or an absolute path; before #825 those
  6. * silently matched nothing and reported 0 affected tests. All three spellings
  7. * must now resolve the same affected test file.
  8. *
  9. * Exercised end-to-end against the built binary.
  10. */
  11. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  12. import { execFileSync } from 'child_process';
  13. import * as fs from 'fs';
  14. import * as os from 'os';
  15. import * as path from 'path';
  16. import { CodeGraph } from '../src';
  17. const BIN = path.resolve(__dirname, '../dist/bin/codegraph.js');
  18. function affected(cwd: string, arg: string): string[] {
  19. const out = execFileSync(process.execPath, [BIN, 'affected', arg, '--quiet', '-p', cwd], {
  20. encoding: 'utf-8',
  21. env: { ...process.env, CODEGRAPH_NO_DAEMON: '1', CODEGRAPH_WASM_RELAUNCHED: '1' },
  22. stdio: ['ignore', 'pipe', 'pipe'],
  23. });
  24. return out.split('\n').map((s) => s.trim()).filter(Boolean);
  25. }
  26. describe('codegraph affected — input path normalization (#825)', () => {
  27. let tempDir: string;
  28. beforeEach(async () => {
  29. tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-affected-paths-'));
  30. fs.mkdirSync(path.join(tempDir, 'src'));
  31. // util.ts <- helper.ts <- helper.test.ts (transitive test dependency)
  32. fs.writeFileSync(path.join(tempDir, 'src/util.ts'), 'export function util(x: number){ return x + 1; }\n');
  33. fs.writeFileSync(
  34. path.join(tempDir, 'src/helper.ts'),
  35. "import { util } from './util';\nexport function helper(){ return util(1); }\n",
  36. );
  37. fs.writeFileSync(
  38. path.join(tempDir, 'src/helper.test.ts'),
  39. "import { helper } from './helper';\ntest('t', () => helper());\n",
  40. );
  41. const cg = CodeGraph.initSync(tempDir);
  42. await cg.indexAll();
  43. cg.close();
  44. });
  45. afterEach(() => {
  46. fs.rmSync(tempDir, { recursive: true, force: true });
  47. });
  48. it('bare-relative, ./-prefixed, and absolute paths all resolve the same affected test', () => {
  49. const expected = ['src/helper.test.ts'];
  50. // Baseline that always worked.
  51. expect(affected(tempDir, 'src/util.ts')).toEqual(expected);
  52. // Both of these returned [] before the normalization fix.
  53. expect(affected(tempDir, './src/util.ts')).toEqual(expected);
  54. expect(affected(tempDir, path.join(tempDir, 'src/util.ts'))).toEqual(expected);
  55. });
  56. });