mo3ta-coverage 1.2.1 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENT.md +26 -4
- package/README.md +179 -2
- package/index.js +47 -6
- package/mcp/createServer.js +179 -0
- package/mcp/server.js +18 -0
- package/mcp/server.test.js +290 -0
- package/package.json +11 -5
- package/src/api.js +807 -0
- package/src/api.test.js +430 -0
- package/src/cli.js +425 -150
- package/src/cli.spawn.test.js +184 -0
- package/src/cli.test.js +225 -0
- package/src/fileUtils.js +18 -2
- package/src/gitUtils.js +42 -11
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import os from 'os';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { execSync, spawnSync } from 'child_process';
|
|
5
|
+
|
|
6
|
+
const CLI = path.resolve('index.js');
|
|
7
|
+
|
|
8
|
+
function git(repo, command) {
|
|
9
|
+
execSync(command, { cwd: repo, stdio: 'pipe' });
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function runCli(repo, args) {
|
|
13
|
+
return spawnSync(process.execPath, [CLI, ...args], {
|
|
14
|
+
cwd: repo,
|
|
15
|
+
encoding: 'utf8'
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe('CLI process behavior', () => {
|
|
20
|
+
let root;
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
root = fs.mkdtempSync(path.join(os.tmpdir(), 'mo3ta-cli-'));
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterEach(() => {
|
|
27
|
+
fs.rmSync(root, { recursive: true, force: true });
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('prints help with the --dryRun example', () => {
|
|
31
|
+
const result = runCli(root, ['--help']);
|
|
32
|
+
|
|
33
|
+
expect(result.status).toBe(0);
|
|
34
|
+
expect(result.stdout).toContain('--dryRun true --files src/a.js,src/b.ts --base main');
|
|
35
|
+
expect(result.stdout).not.toContain('--dry-run');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('lists the --jest option in help', () => {
|
|
39
|
+
const result = runCli(root, ['--help']);
|
|
40
|
+
|
|
41
|
+
expect(result.status).toBe(0);
|
|
42
|
+
expect(result.stdout).toContain('--jest <value>');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('lists the --filter option in help', () => {
|
|
46
|
+
const result = runCli(root, ['--help']);
|
|
47
|
+
|
|
48
|
+
expect(result.status).toBe(0);
|
|
49
|
+
expect(result.stdout).toContain('--filter <regex>');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('keeps only matching spec files in a dry run and logs both lists', () => {
|
|
53
|
+
git(root, 'git init -b main -q');
|
|
54
|
+
git(root, 'git config user.email test@example.com');
|
|
55
|
+
git(root, 'git config user.name Test');
|
|
56
|
+
fs.mkdirSync(path.join(root, 'src'));
|
|
57
|
+
fs.writeFileSync(path.join(root, 'src', 'a.js'), 'module.exports = 1;\n');
|
|
58
|
+
fs.writeFileSync(path.join(root, 'src', 'a.test.js'), 'test("a", () => expect(1).toBe(1));\n');
|
|
59
|
+
git(root, 'git add -A && git commit -q -m init');
|
|
60
|
+
|
|
61
|
+
const result = runCli(root, ['--dryRun', 'true', '--files', 'src/a.js', '--base', 'main', '--mode', 'fast', '--filter', 'does-not-match']);
|
|
62
|
+
|
|
63
|
+
expect(result.status).toBe(0);
|
|
64
|
+
expect(result.stdout).toBe('');
|
|
65
|
+
expect(result.stderr).toContain('Filter regex');
|
|
66
|
+
expect(result.stderr).toContain('src/a.test.js');
|
|
67
|
+
expect(result.stderr).toContain('after filter (0)');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('runs only the matching spec in a real dry run and exits 0', () => {
|
|
71
|
+
git(root, 'git init -b main -q');
|
|
72
|
+
git(root, 'git config user.email test@example.com');
|
|
73
|
+
git(root, 'git config user.name Test');
|
|
74
|
+
fs.mkdirSync(path.join(root, 'src'));
|
|
75
|
+
fs.writeFileSync(path.join(root, 'src', 'a.js'), 'module.exports = 1;\n');
|
|
76
|
+
fs.writeFileSync(path.join(root, 'src', 'a.test.js'), 'test("a", () => expect(1).toBe(1));\n');
|
|
77
|
+
git(root, 'git add -A && git commit -q -m init');
|
|
78
|
+
|
|
79
|
+
const result = runCli(root, ['--dryRun', 'true', '--files', 'src/a.js', '--base', 'main', '--mode', 'fast', '--filter', 'a\\.test\\.js']);
|
|
80
|
+
|
|
81
|
+
expect(result.status).toBe(0);
|
|
82
|
+
expect(result.stdout.trim()).toBe('src/a.test.js');
|
|
83
|
+
expect(result.stderr).toContain('before filter (1)');
|
|
84
|
+
expect(result.stderr).toContain('after filter (1)');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('exits 1 with a clear message when the filter regex is invalid', () => {
|
|
88
|
+
git(root, 'git init -b main -q');
|
|
89
|
+
git(root, 'git config user.email test@example.com');
|
|
90
|
+
git(root, 'git config user.name Test');
|
|
91
|
+
fs.mkdirSync(path.join(root, 'src'));
|
|
92
|
+
fs.writeFileSync(path.join(root, 'src', 'a.js'), 'module.exports = 1;\n');
|
|
93
|
+
git(root, 'git add -A && git commit -q -m init');
|
|
94
|
+
|
|
95
|
+
const result = runCli(root, ['--files', 'src/a.js', '--base', 'main', '--mode', 'fast', '--filter', '(unclosed']);
|
|
96
|
+
|
|
97
|
+
expect(result.status).toBe(1);
|
|
98
|
+
expect(result.stderr).toContain('Invalid filter regex "(unclosed"');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('shows the provided jest flags in the options summary', () => {
|
|
102
|
+
git(root, 'git init -b main -q');
|
|
103
|
+
git(root, 'git config user.email test@example.com');
|
|
104
|
+
git(root, 'git config user.name Test');
|
|
105
|
+
fs.mkdirSync(path.join(root, 'src'));
|
|
106
|
+
fs.writeFileSync(path.join(root, 'src', 'a.js'), 'module.exports = 1;\n');
|
|
107
|
+
fs.writeFileSync(path.join(root, 'src', 'a.test.js'), 'test("a", () => expect(1).toBe(1));\n');
|
|
108
|
+
git(root, 'git add -A && git commit -q -m init');
|
|
109
|
+
|
|
110
|
+
const result = runCli(root, ['--dryRun', 'true', '--files', 'src/a.js', '--base', 'main', '--mode', 'fast', '--jest', '--maxWorkers=2', '--jest', '--silent']);
|
|
111
|
+
|
|
112
|
+
expect(result.status).toBe(0);
|
|
113
|
+
expect(result.stderr).toContain('--maxWorkers=2');
|
|
114
|
+
expect(result.stderr).toContain('--silent');
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('reads a comma-separated jest flag list into separate flags', () => {
|
|
118
|
+
git(root, 'git init -b main -q');
|
|
119
|
+
git(root, 'git config user.email test@example.com');
|
|
120
|
+
git(root, 'git config user.name Test');
|
|
121
|
+
fs.mkdirSync(path.join(root, 'src'));
|
|
122
|
+
fs.writeFileSync(path.join(root, 'src', 'a.js'), 'module.exports = 1;\n');
|
|
123
|
+
fs.writeFileSync(path.join(root, 'src', 'a.test.js'), 'test("a", () => expect(1).toBe(1));\n');
|
|
124
|
+
git(root, 'git add -A && git commit -q -m init');
|
|
125
|
+
|
|
126
|
+
const result = runCli(root, ['--dryRun', 'true', '--files', 'src/a.js', '--base', 'main', '--mode', 'fast', '--jest', '--maxWorkers=2,--silent']);
|
|
127
|
+
|
|
128
|
+
expect(result.status).toBe(0);
|
|
129
|
+
expect(result.stderr).toContain('--maxWorkers=2');
|
|
130
|
+
expect(result.stderr).toContain('--silent');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('dryRun prints only the selected test path on stdout', () => {
|
|
134
|
+
git(root, 'git init -b main -q');
|
|
135
|
+
git(root, 'git config user.email test@example.com');
|
|
136
|
+
git(root, 'git config user.name Test');
|
|
137
|
+
fs.writeFileSync(path.join(root, 'README.md'), 'base\n');
|
|
138
|
+
git(root, 'git add README.md && git commit -q -m base');
|
|
139
|
+
fs.mkdirSync(path.join(root, 'src'));
|
|
140
|
+
fs.writeFileSync(path.join(root, 'src', 'a.js'), 'module.exports = 1;\n');
|
|
141
|
+
fs.writeFileSync(path.join(root, 'src', 'a.test.js'), 'test("a", () => expect(1).toBe(1));\n');
|
|
142
|
+
|
|
143
|
+
const result = runCli(root, ['--dryRun', 'true', '--files', 'src/a.js', '--base', 'main', '--mode', 'fast']);
|
|
144
|
+
|
|
145
|
+
expect(result.status).toBe(0);
|
|
146
|
+
expect(result.stdout.trim()).toBe('src/a.test.js');
|
|
147
|
+
expect(result.stderr).not.toMatch(/^src\/a\.test\.js$/m);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('exits 1 with the existing message when the directory is not a git repo', () => {
|
|
151
|
+
const result = runCli(root, ['--base', 'main']);
|
|
152
|
+
|
|
153
|
+
expect(result.status).toBe(1);
|
|
154
|
+
expect(result.stderr).toContain('❌ Not a git repository. Please run this tool inside a git repo.');
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('exits 1 with the existing message when the base ref is missing', () => {
|
|
158
|
+
git(root, 'git init -b main -q');
|
|
159
|
+
git(root, 'git config user.email test@example.com');
|
|
160
|
+
git(root, 'git config user.name Test');
|
|
161
|
+
fs.writeFileSync(path.join(root, 'README.md'), 'base\n');
|
|
162
|
+
git(root, 'git add README.md && git commit -q -m base');
|
|
163
|
+
|
|
164
|
+
const result = runCli(root, ['--base', 'origin/missing']);
|
|
165
|
+
|
|
166
|
+
expect(result.status).toBe(1);
|
|
167
|
+
expect(result.stderr).toContain('❌ Base branch "origin/missing" does not exist or is invalid. try git fetch to update your local branches.');
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('exits 1 with a parse error when .mo3ta-coverage.json is not valid JSON', () => {
|
|
171
|
+
git(root, 'git init -b main -q');
|
|
172
|
+
git(root, 'git config user.email test@example.com');
|
|
173
|
+
git(root, 'git config user.name Test');
|
|
174
|
+
fs.writeFileSync(path.join(root, 'README.md'), 'base\n');
|
|
175
|
+
git(root, 'git add README.md && git commit -q -m base');
|
|
176
|
+
fs.writeFileSync(path.join(root, '.mo3ta-coverage.json'), '{');
|
|
177
|
+
|
|
178
|
+
const result = runCli(root, ['config']);
|
|
179
|
+
|
|
180
|
+
expect(result.status).toBe(1);
|
|
181
|
+
expect(result.stderr).toContain('Could not parse');
|
|
182
|
+
expect(result.stderr).toContain('.mo3ta-coverage.json');
|
|
183
|
+
});
|
|
184
|
+
});
|
package/src/cli.test.js
CHANGED
|
@@ -6,12 +6,16 @@ import {
|
|
|
6
6
|
buildMarkdownCoverageReport,
|
|
7
7
|
classifyChangedLineCoverage,
|
|
8
8
|
dryRun,
|
|
9
|
+
filterTestFiles,
|
|
9
10
|
formatMissedLinesSummary,
|
|
10
11
|
getChangedFiles,
|
|
11
12
|
getExplicitFiles,
|
|
12
13
|
normalizeExplicitFiles,
|
|
14
|
+
parseFilter,
|
|
15
|
+
parseJestArgs,
|
|
13
16
|
parseLcov,
|
|
14
17
|
readExplicitFilesFromFile,
|
|
18
|
+
resolveTestPlan,
|
|
15
19
|
summarizeCoverageResult,
|
|
16
20
|
writeMarkdownReport
|
|
17
21
|
} from './cli.js';
|
|
@@ -238,6 +242,207 @@ describe('getExplicitFiles', () => {
|
|
|
238
242
|
});
|
|
239
243
|
});
|
|
240
244
|
|
|
245
|
+
describe('parseJestArgs', () => {
|
|
246
|
+
it('returns an empty array for no input', () => {
|
|
247
|
+
expect(parseJestArgs()).toEqual([]);
|
|
248
|
+
expect(parseJestArgs(null)).toEqual([]);
|
|
249
|
+
expect(parseJestArgs(undefined)).toEqual([]);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('accepts a single flag string', () => {
|
|
253
|
+
expect(parseJestArgs('--silent')).toEqual(['--silent']);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('splits a comma-separated string into flags', () => {
|
|
257
|
+
expect(parseJestArgs('--maxWorkers=2,--silent')).toEqual(['--maxWorkers=2', '--silent']);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('accepts an array of flags (repeatable --jest)', () => {
|
|
261
|
+
expect(parseJestArgs(['--maxWorkers=2', '--silent'])).toEqual(['--maxWorkers=2', '--silent']);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('trims, drops empty entries, and de-duplicates preserving order', () => {
|
|
265
|
+
expect(parseJestArgs([' --silent', ' --silent', '', ' ', '--ci'])).toEqual(['--silent', '--ci']);
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
describe('parseFilter', () => {
|
|
270
|
+
it('returns null for no input', () => {
|
|
271
|
+
expect(parseFilter()).toBeNull();
|
|
272
|
+
expect(parseFilter(null)).toBeNull();
|
|
273
|
+
expect(parseFilter('')).toBeNull();
|
|
274
|
+
expect(parseFilter(' ')).toBeNull();
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('compiles a string pattern into a RegExp', () => {
|
|
278
|
+
const filter = parseFilter('src/__tests__/login');
|
|
279
|
+
expect(filter).toBeInstanceOf(RegExp);
|
|
280
|
+
expect(filter.test('src/__tests__/login.spec.js')).toBe(true);
|
|
281
|
+
expect(filter.test('src/auth/logout.spec.js')).toBe(false);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it('trims surrounding whitespace from the pattern', () => {
|
|
285
|
+
expect(parseFilter(' login ').source).toBe('login');
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it('accepts a RegExp input and drops global/sticky flags', () => {
|
|
289
|
+
const filter = parseFilter(/login/g);
|
|
290
|
+
expect(filter.source).toBe('login');
|
|
291
|
+
expect(filter.flags).toBe('');
|
|
292
|
+
// Repeating .test() must not depend on lastIndex.
|
|
293
|
+
expect(filter.test('a/login.spec.js')).toBe(true);
|
|
294
|
+
expect(filter.test('a/login.spec.js')).toBe(true);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it('throws an INVALID_FILTER error for a malformed pattern', () => {
|
|
298
|
+
expect(() => parseFilter('(unclosed')).toThrow(/Invalid filter regex/);
|
|
299
|
+
try {
|
|
300
|
+
parseFilter('(unclosed');
|
|
301
|
+
} catch (error) {
|
|
302
|
+
expect(error.code).toBe('INVALID_FILTER');
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
describe('filterTestFiles', () => {
|
|
308
|
+
it('returns a copy of the input when no filter is given', () => {
|
|
309
|
+
const files = ['src/a.test.js', 'src/b.test.js'];
|
|
310
|
+
const result = filterTestFiles(files);
|
|
311
|
+
expect(result).toEqual(files);
|
|
312
|
+
expect(result).not.toBe(files);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('keeps only the paths that match the regex', () => {
|
|
316
|
+
const files = ['src/__tests__/login.spec.js', 'src/auth/logout.spec.js', 'src/login.tsx.test.ts'];
|
|
317
|
+
const result = filterTestFiles(files, 'login');
|
|
318
|
+
expect(result).toEqual(['src/__tests__/login.spec.js', 'src/login.tsx.test.ts']);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it('supports a RegExp instance as the filter', () => {
|
|
322
|
+
const files = ['src/foo.test.js', 'src/bar.test.js'];
|
|
323
|
+
const result = filterTestFiles(files, /foo/);
|
|
324
|
+
expect(result).toEqual(['src/foo.test.js']);
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
describe('resolveTestPlan spec filter', () => {
|
|
329
|
+
let tempDir;
|
|
330
|
+
let originalCwd;
|
|
331
|
+
|
|
332
|
+
beforeEach(() => {
|
|
333
|
+
originalCwd = process.cwd();
|
|
334
|
+
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mo3ta-filter-plan-'));
|
|
335
|
+
fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
|
|
336
|
+
fs.writeFileSync(path.join(tempDir, 'src', 'example.js'), 'export const example = 1;\n');
|
|
337
|
+
fs.writeFileSync(
|
|
338
|
+
path.join(tempDir, 'src', 'example.test.js'),
|
|
339
|
+
"import { example } from './example';\n\ntest('naming match', () => expect(example).toBe(1));\n"
|
|
340
|
+
);
|
|
341
|
+
fs.mkdirSync(path.join(tempDir, 'specs'), { recursive: true });
|
|
342
|
+
fs.writeFileSync(
|
|
343
|
+
path.join(tempDir, 'specs', 'custom-name.spec.js'),
|
|
344
|
+
"import { example } from '../src/example';\n\ntest('import match', () => expect(example).toBe(1));\n"
|
|
345
|
+
);
|
|
346
|
+
process.chdir(tempDir);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
afterEach(() => {
|
|
350
|
+
process.chdir(originalCwd);
|
|
351
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it('filters selected tests and keeps the pre-filter list for validation', () => {
|
|
355
|
+
const plan = resolveTestPlan(['src/example.js'], 'smart', 2, tempDir, [], 'specs\\/');
|
|
356
|
+
|
|
357
|
+
expect(plan.filter).toBe('specs\\/');
|
|
358
|
+
expect(plan.allTestsToRun).toEqual(['specs/custom-name.spec.js', 'src/example.test.js']);
|
|
359
|
+
expect(plan.testsToRun).toEqual(['specs/custom-name.spec.js']);
|
|
360
|
+
expect(plan.jestTargets).toEqual(['specs/custom-name.spec.js']);
|
|
361
|
+
expect(plan.jestArgs).toEqual(['--passWithNoTests', '--coverage', 'specs/custom-name.spec.js']);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it('removes test files when the filter matches none', () => {
|
|
365
|
+
const plan = resolveTestPlan(['src/example.js'], 'smart', 2, tempDir, [], 'not-a-match');
|
|
366
|
+
|
|
367
|
+
expect(plan.allTestsToRun).toEqual(['specs/custom-name.spec.js', 'src/example.test.js']);
|
|
368
|
+
expect(plan.testsToRun).toEqual([]);
|
|
369
|
+
expect(plan.jestArgs).toEqual(['--passWithNoTests', '--coverage']);
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
it('keeps the per-source "allTests" for the validation log when the filter drops everything', () => {
|
|
373
|
+
const plan = resolveTestPlan(['src/example.js'], 'smart', 2, tempDir, [], 'not-a-match');
|
|
374
|
+
|
|
375
|
+
plan.mappings.forEach(mapping => {
|
|
376
|
+
expect(mapping.tests).toEqual([]);
|
|
377
|
+
expect(mapping.allTests).toEqual(['specs/custom-name.spec.js', 'src/example.test.js']);
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it('leaves the plan untouched when no filter is given', () => {
|
|
382
|
+
const plan = resolveTestPlan(['src/example.js'], 'smart', 2, tempDir);
|
|
383
|
+
|
|
384
|
+
expect(plan.filter).toBeNull();
|
|
385
|
+
expect(plan.allTestsToRun).toEqual(['specs/custom-name.spec.js', 'src/example.test.js']);
|
|
386
|
+
expect(plan.testsToRun).toEqual(['specs/custom-name.spec.js', 'src/example.test.js']);
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
it('forwards the filter to Jest via --testPathPatterns in full mode', () => {
|
|
390
|
+
const plan = resolveTestPlan(
|
|
391
|
+
['src/example.js', 'src/example.test.js'],
|
|
392
|
+
'full',
|
|
393
|
+
2,
|
|
394
|
+
tempDir,
|
|
395
|
+
[],
|
|
396
|
+
'example'
|
|
397
|
+
);
|
|
398
|
+
|
|
399
|
+
expect(plan.delegatedToJest).toBe(true);
|
|
400
|
+
expect(plan.allTestsToRun).toEqual(['src/example.test.js']);
|
|
401
|
+
expect(plan.testsToRun).toEqual(['src/example.test.js']);
|
|
402
|
+
expect(plan.jestTargets).toEqual(['src/example.js', 'src/example.test.js']);
|
|
403
|
+
expect(plan.jestArgs).toContain('--findRelatedTests');
|
|
404
|
+
expect(plan.jestArgs).toContain('--testPathPatterns');
|
|
405
|
+
expect(plan.jestArgs).toContain('example');
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
it('does not duplicate a Jest path pattern the caller already set in full mode', () => {
|
|
409
|
+
const plan = resolveTestPlan(
|
|
410
|
+
['src/example.js'],
|
|
411
|
+
'full',
|
|
412
|
+
2,
|
|
413
|
+
tempDir,
|
|
414
|
+
['--testPathPatterns=login'],
|
|
415
|
+
'example'
|
|
416
|
+
);
|
|
417
|
+
|
|
418
|
+
expect(plan.jestArgs).toContain('--testPathPatterns=login');
|
|
419
|
+
expect(plan.jestArgs).not.toContain('--testPathPatterns');
|
|
420
|
+
expect(plan.jestArgs.filter(arg => arg.startsWith('--testPathPatterns'))).toEqual(
|
|
421
|
+
['--testPathPatterns=login']
|
|
422
|
+
);
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
describe('resolveTestPlan jest flags', () => {
|
|
427
|
+
it('appends extra flags to fast mode jestArgs after the test files', () => {
|
|
428
|
+
const plan = resolveTestPlan(['src/example.test.js'], 'fast', 2, process.cwd(), ['--maxWorkers=2', '--silent']);
|
|
429
|
+
|
|
430
|
+
expect(plan.jestArgs).toEqual(['--passWithNoTests', '--coverage', 'src/example.test.js', '--maxWorkers=2', '--silent']);
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
it('appends extra flags to full mode jestArgs after the targets', () => {
|
|
434
|
+
const plan = resolveTestPlan(['src/example.js', 'src/example.test.js'], 'full', 2, process.cwd(), ['--silent']);
|
|
435
|
+
|
|
436
|
+
expect(plan.jestArgs).toEqual(['--findRelatedTests', '--passWithNoTests', '--coverage', 'src/example.js', 'src/example.test.js', '--silent']);
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it('omits extra flags when none are provided', () => {
|
|
440
|
+
const plan = resolveTestPlan(['src/example.test.js'], 'fast', 2, process.cwd());
|
|
441
|
+
|
|
442
|
+
expect(plan.jestArgs).toEqual(['--passWithNoTests', '--coverage', 'src/example.test.js']);
|
|
443
|
+
});
|
|
444
|
+
});
|
|
445
|
+
|
|
241
446
|
describe('getChangedFiles', () => {
|
|
242
447
|
let tempDir;
|
|
243
448
|
let originalCwd;
|
|
@@ -381,4 +586,24 @@ describe('dryRun', () => {
|
|
|
381
586
|
expect(stdoutLines).toEqual(['src/example.test.js']);
|
|
382
587
|
expect(stderrLines.some(line => line.includes('--findRelatedTests'))).toBe(true);
|
|
383
588
|
});
|
|
589
|
+
|
|
590
|
+
it('prints the before/after filter log on stderr and only filtered paths on stdout', () => {
|
|
591
|
+
expect(() => dryRun(['src/example.js'], 'smart', 2, 'example\\.test\\.js$')).toThrow(/^exit:/);
|
|
592
|
+
|
|
593
|
+
// The changed file's naming-match test survives the filter; the import-scan
|
|
594
|
+
// spec does not.
|
|
595
|
+
expect(stdoutLines).toEqual(['src/example.test.js']);
|
|
596
|
+
expect(stderrLines.some(line => line.includes('Filter regex'))).toBe(true);
|
|
597
|
+
expect(stderrLines.some(line => line.includes('specs/custom-name.spec.js'))).toBe(true);
|
|
598
|
+
expect(stderrLines.some(line => line.includes('src/example.test.js'))).toBe(true);
|
|
599
|
+
expect(exitSpy).toHaveBeenCalledWith(0);
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
it('prints nothing on stdout when the filter matches no test file', () => {
|
|
603
|
+
expect(() => dryRun(['src/example.js'], 'smart', 2, 'zzz-no-match')).toThrow(/^exit:/);
|
|
604
|
+
|
|
605
|
+
expect(stdoutLines).toEqual([]);
|
|
606
|
+
expect(stderrLines.some(line => line.includes('after filter (0)'))).toBe(true);
|
|
607
|
+
expect(exitSpy).toHaveBeenCalledWith(0);
|
|
608
|
+
});
|
|
384
609
|
});
|
package/src/fileUtils.js
CHANGED
|
@@ -105,8 +105,24 @@ function getTestFileCandidates(sourceFile) {
|
|
|
105
105
|
* @param {string} sourceFile - Path to source file
|
|
106
106
|
* @returns {string[]} - Array of potential test file paths
|
|
107
107
|
*/
|
|
108
|
-
export function findTestFilesForSource(sourceFile) {
|
|
109
|
-
|
|
108
|
+
export function findTestFilesForSource(sourceFile, root = process.cwd()) {
|
|
109
|
+
const candidates = getTestFileCandidates(sourceFile);
|
|
110
|
+
// The default (cwd) path stays relative. Callers and tests observe the
|
|
111
|
+
// exact path strings passed to existsSync; resolving them would change that.
|
|
112
|
+
if (path.resolve(root) === process.cwd()) {
|
|
113
|
+
return candidates.filter(testFile => fs.existsSync(testFile));
|
|
114
|
+
}
|
|
115
|
+
return candidates.filter(testFile => fs.existsSync(path.resolve(root, testFile)));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Drops cached directory walks. The CLI keeps the cache for one process;
|
|
120
|
+
* long-running callers (the MCP server) clear it between tool calls.
|
|
121
|
+
*/
|
|
122
|
+
export function clearScanCaches() {
|
|
123
|
+
cachedAllTestFiles = null;
|
|
124
|
+
cachedAllJsFiles = null;
|
|
125
|
+
cachedReverseImportIndex = null;
|
|
110
126
|
}
|
|
111
127
|
|
|
112
128
|
/**
|
package/src/gitUtils.js
CHANGED
|
@@ -1,24 +1,55 @@
|
|
|
1
1
|
import { execFileSync } from 'child_process';
|
|
2
2
|
import { exit } from 'process';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Checks that `cwd` is inside a git work tree.
|
|
6
|
+
* Returns a result instead of exiting so callers (the CLI and the MCP server)
|
|
7
|
+
* can decide how to surface the failure.
|
|
8
|
+
* @param {string} [cwd]
|
|
9
|
+
* @returns {{ok: true} | {ok: false, message: string}}
|
|
10
|
+
*/
|
|
11
|
+
export function checkGitRepository(cwd = process.cwd()) {
|
|
5
12
|
try {
|
|
6
|
-
execFileSync('git', ['rev-parse', '--is-inside-work-tree'], { stdio: 'ignore' });
|
|
13
|
+
execFileSync('git', ['rev-parse', '--is-inside-work-tree'], { stdio: 'ignore', cwd });
|
|
14
|
+
return { ok: true };
|
|
7
15
|
} catch (err) {
|
|
8
|
-
|
|
16
|
+
return {
|
|
17
|
+
ok: false,
|
|
18
|
+
message: `Not a git repository. Please run this tool inside a git repo. ${err.message}`
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function verifyGitRepoExists(cwd = process.cwd()) {
|
|
24
|
+
const result = checkGitRepository(cwd);
|
|
25
|
+
if (!result.ok) {
|
|
26
|
+
console.error(`❌ ${result.message}`);
|
|
9
27
|
exit(1);
|
|
10
28
|
}
|
|
11
29
|
}
|
|
12
30
|
|
|
13
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Checks that `baseRef` resolves to a commit. The ref is passed as one
|
|
33
|
+
* argument (never through a shell) and git `--verify` rejects option-like
|
|
34
|
+
* values such as `--upload-pack=...`.
|
|
35
|
+
* @param {string} baseRef
|
|
36
|
+
* @param {string} [cwd]
|
|
37
|
+
* @returns {{ok: true} | {ok: false, message: string}}
|
|
38
|
+
*/
|
|
39
|
+
export function checkBaseRef(baseRef, cwd = process.cwd()) {
|
|
40
|
+
const message = `Base branch "${baseRef}" does not exist or is invalid. try git fetch to update your local branches.`;
|
|
14
41
|
try {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
42
|
+
execFileSync('git', ['rev-parse', '--verify', '--quiet', `${baseRef}^{commit}`], { stdio: 'ignore', cwd });
|
|
43
|
+
return { ok: true };
|
|
44
|
+
} catch {
|
|
45
|
+
return { ok: false, message };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function verifyBaseBranchExists(BASE_BRANCH, cwd = process.cwd()) {
|
|
50
|
+
const result = checkBaseRef(BASE_BRANCH, cwd);
|
|
51
|
+
if (!result.ok) {
|
|
52
|
+
console.error(`❌ ${result.message}`);
|
|
22
53
|
exit(1);
|
|
23
54
|
}
|
|
24
55
|
}
|