1
0

cli-affected-test-conventions.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * `codegraph affected` recognises every ecosystem's test-file convention (#1507).
  3. *
  4. * The command used to carry its own six regexes — `.test.`, `.spec.`,
  5. * `/tests/`… — so a Go `foo_test.go`, a Python `test_foo.py` or a JVM
  6. * `FooTest.kt` beside the changed file was never reported, and "no tests
  7. * affected" read as "no coverage". It now shares `isTestPath` with search and
  8. * the MCP tools. Exercised end-to-end against the built binary.
  9. */
  10. import { describe, it, expect, beforeAll, afterAll } from 'vitest';
  11. import { execFileSync } from 'child_process';
  12. import * as fs from 'fs';
  13. import * as os from 'os';
  14. import * as path from 'path';
  15. import { CodeGraph } from '../src';
  16. const BIN = path.resolve(__dirname, '../dist/bin/codegraph.js');
  17. function affected(cwd: string, args: string[]): string[] {
  18. const out = execFileSync(process.execPath, [BIN, 'affected', ...args, '--quiet', '-p', cwd], {
  19. encoding: 'utf-8',
  20. env: { ...process.env, CODEGRAPH_NO_DAEMON: '1', CODEGRAPH_WASM_RELAUNCHED: '1' },
  21. stdio: ['ignore', 'pipe', 'pipe'],
  22. });
  23. return out.split('\n').map((s) => s.trim()).filter(Boolean);
  24. }
  25. describe('codegraph affected — test-file conventions (#1507)', () => {
  26. let dir: string;
  27. beforeAll(async () => {
  28. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-affected-conv-'));
  29. const w = (rel: string, body: string) => {
  30. fs.mkdirSync(path.dirname(path.join(dir, rel)), { recursive: true });
  31. fs.writeFileSync(path.join(dir, rel), body);
  32. };
  33. w('go.mod', 'module example.com/demo\n\ngo 1.22\n');
  34. w('math.go', 'package demo\n\nfunc Add(a, b int) int { return a + b }\n');
  35. w('math_test.go', 'package demo\n\nimport "testing"\n\nfunc TestAdd(t *testing.T) { if Add(1, 2) != 3 { t.Fatal("boom") } }\n');
  36. w('pkg/calc.py', 'def add(a, b):\n return a + b\n');
  37. w('pkg/test_calc.py', 'from pkg.calc import add\n\ndef test_add():\n assert add(1, 2) == 3\n');
  38. w('src/main/kotlin/app/Calc.kt', 'package app\n\nclass Calc {\n fun add(a: Int, b: Int): Int = a + b\n}\n');
  39. w('src/test/kotlin/app/CalcTest.kt', 'package app\n\nclass CalcTest {\n fun addsNumbers() { Calc().add(1, 2) }\n}\n');
  40. const cg = CodeGraph.initSync(dir);
  41. await cg.indexAll();
  42. cg.close();
  43. });
  44. afterAll(() => {
  45. fs.rmSync(dir, { recursive: true, force: true });
  46. });
  47. it('reports the sibling Go _test.go file', () => {
  48. expect(affected(dir, ['math.go'])).toEqual(['math_test.go']);
  49. });
  50. it('reports the Python test_ module and the JVM FooTest class', () => {
  51. expect(affected(dir, ['pkg/calc.py'])).toEqual(['pkg/test_calc.py']);
  52. expect(affected(dir, ['src/main/kotlin/app/Calc.kt'])).toEqual(['src/test/kotlin/app/CalcTest.kt']);
  53. });
  54. it('still honours an explicit --filter glob', () => {
  55. expect(affected(dir, ['math.go', '--filter', '*_test.go'])).toEqual(['math_test.go']);
  56. expect(affected(dir, ['math.go', '--filter', '*.spec.ts'])).toEqual([]);
  57. });
  58. });