mo3ta-coverage 1.2.0 → 1.3.0
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/README.md +82 -1
- package/index.js +8 -2
- package/mcp/createServer.js +169 -0
- package/mcp/server.js +18 -0
- package/mcp/server.test.js +260 -0
- package/package.json +11 -5
- package/src/api.js +745 -0
- package/src/api.test.js +322 -0
- package/src/cli.js +289 -143
- package/src/cli.spawn.test.js +89 -0
- package/src/fileUtils.js +18 -2
- package/src/gitUtils.js +42 -11
package/src/cli.js
CHANGED
|
@@ -3,6 +3,10 @@ import fs from 'fs';
|
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { exit } from 'process';
|
|
5
5
|
import { verifyBaseBranchExists, verifyGitRepoExists } from './gitUtils.js';
|
|
6
|
+
|
|
7
|
+
// Set when Jest exits non-zero. `main` resets it so a later in-process call
|
|
8
|
+
// does not inherit the previous failure. The CLI process still exits.
|
|
9
|
+
let testsFailed = false;
|
|
6
10
|
import {
|
|
7
11
|
getJsOnlyFiles,
|
|
8
12
|
separateSourceAndTestFiles,
|
|
@@ -12,6 +16,9 @@ import {
|
|
|
12
16
|
} from './fileUtils.js';
|
|
13
17
|
|
|
14
18
|
export function main(options) {
|
|
19
|
+
// A single CLI process exits after one run. Reset anyway so an in-process
|
|
20
|
+
// caller does not inherit a failure from an earlier invocation.
|
|
21
|
+
testsFailed = false;
|
|
15
22
|
try {
|
|
16
23
|
// Check if current directory is a git repository
|
|
17
24
|
verifyGitRepoExists();
|
|
@@ -113,52 +120,69 @@ export function readExplicitFilesFromFile(listPath, root = process.cwd()) {
|
|
|
113
120
|
* @param {{files?: string|string[], filesFrom?: string}} options
|
|
114
121
|
* @returns {string[]}
|
|
115
122
|
*/
|
|
116
|
-
export function getExplicitFiles(options = {}) {
|
|
123
|
+
export function getExplicitFiles(options = {}, root = process.cwd()) {
|
|
117
124
|
const files = [];
|
|
118
125
|
|
|
119
126
|
if (options.files !== undefined && options.files !== null && options.files !== false) {
|
|
120
|
-
files.push(...normalizeExplicitFiles(options.files));
|
|
127
|
+
files.push(...normalizeExplicitFiles(options.files, root));
|
|
121
128
|
}
|
|
122
129
|
|
|
123
130
|
if (options.filesFrom) {
|
|
124
|
-
files.push(...readExplicitFilesFromFile(options.filesFrom));
|
|
131
|
+
files.push(...readExplicitFilesFromFile(options.filesFrom, root));
|
|
125
132
|
}
|
|
126
133
|
|
|
127
134
|
return Array.from(new Set(files));
|
|
128
135
|
}
|
|
129
136
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
137
|
+
/**
|
|
138
|
+
* Lists changed JS/TS files vs `baseBranch` without logging or exiting.
|
|
139
|
+
* Includes commits from every author since the merge base, plus staged and
|
|
140
|
+
* unstaged edits. Throws when git itself fails.
|
|
141
|
+
* @param {string} baseBranch
|
|
142
|
+
* @param {string} [root]
|
|
143
|
+
* @returns {string[]}
|
|
144
|
+
*/
|
|
145
|
+
export function listChangedSourceFiles(baseBranch, root = process.cwd()) {
|
|
134
146
|
// Compare the merge base with HEAD so every file changed on this branch is
|
|
135
147
|
// included, regardless of which author committed it.
|
|
136
148
|
//
|
|
137
149
|
// Git is always invoked with an argument array (never through a shell) so
|
|
138
150
|
// refs and file paths can't inject commands, and output is de-duplicated
|
|
139
151
|
// in JS instead of piping through `sort -u`.
|
|
152
|
+
const gitFiles = (args) => execFileSync('git', args, {
|
|
153
|
+
cwd: root,
|
|
154
|
+
stdio: ['ignore', 'pipe', 'ignore']
|
|
155
|
+
}).toString();
|
|
156
|
+
|
|
157
|
+
const mergeBase = gitFiles(['merge-base', 'HEAD', baseBranch]).trim();
|
|
158
|
+
|
|
159
|
+
const seen = new Set();
|
|
160
|
+
const changedFiles = [
|
|
161
|
+
// Added or modified files between the merge base and HEAD
|
|
162
|
+
gitFiles(['diff', '--name-only', '--diff-filter=AM', `${mergeBase}..HEAD`]),
|
|
163
|
+
// Unstaged changes in the working tree
|
|
164
|
+
gitFiles(['diff', '--name-only', '--diff-filter=AM']),
|
|
165
|
+
// Staged changes
|
|
166
|
+
gitFiles(['diff', '--cached', '--name-only', '--diff-filter=AM'])
|
|
167
|
+
]
|
|
168
|
+
.flatMap(output => output.split(/\r?\n/))
|
|
169
|
+
.map(file => file.trim())
|
|
170
|
+
.filter(file => file.length > 0 && file.match(/\.(js|ts|jsx|tsx)$/))
|
|
171
|
+
// Defensive: ignore dependency/build/coverage artifacts even if git tracked them
|
|
172
|
+
.filter(file => !file.match(/(^|\/)(node_modules|coverage|dist|build)\/|(^|\/)\.git\//))
|
|
173
|
+
// De-duplicate (replaces `sort -u`); insertion order keeps first-seen wins
|
|
174
|
+
.filter(file => (seen.has(file) ? false : (seen.add(file), true)));
|
|
175
|
+
|
|
176
|
+
changedFiles.sort();
|
|
177
|
+
return changedFiles;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function getChangedFiles(BASE_BRANCH, log = console.log, root = process.cwd()) {
|
|
181
|
+
// Check if BASE_BRANCH exists (also rejects ref-like options before use)
|
|
182
|
+
verifyBaseBranchExists(BASE_BRANCH, root);
|
|
183
|
+
|
|
140
184
|
try {
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
const mergeBase = gitFiles(['merge-base', 'HEAD', BASE_BRANCH]).trim();
|
|
144
|
-
|
|
145
|
-
const seen = new Set();
|
|
146
|
-
const changedFiles = [
|
|
147
|
-
// Added or renamed files between the merge base and HEAD
|
|
148
|
-
gitFiles(['diff', '--name-only', '--diff-filter=AM', `${mergeBase}..HEAD`]),
|
|
149
|
-
// Unstaged changes in the working tree
|
|
150
|
-
gitFiles(['diff', '--name-only', '--diff-filter=AM']),
|
|
151
|
-
// Staged changes
|
|
152
|
-
gitFiles(['diff', '--cached', '--name-only', '--diff-filter=AM'])
|
|
153
|
-
]
|
|
154
|
-
.flatMap(output => output.split('\n'))
|
|
155
|
-
.filter(f => f.trim().length > 0 && f.match(/\.(js|ts|jsx|tsx)$/))
|
|
156
|
-
// Defensive: ignore dependency/build/coverage artifacts even if git tracked them
|
|
157
|
-
.filter(f => !f.match(/(^|\/)(node_modules|coverage|dist|build)\/|(^|\/)\.git\//))
|
|
158
|
-
// De-duplicate (replaces `sort -u`); insertion order keeps first-seen wins
|
|
159
|
-
.filter(f => (seen.has(f) ? false : (seen.add(f), true)));
|
|
160
|
-
|
|
161
|
-
changedFiles.sort();
|
|
185
|
+
const changedFiles = listChangedSourceFiles(BASE_BRANCH, root);
|
|
162
186
|
log(`Changed files:\n${changedFiles.join('\n')}`);
|
|
163
187
|
return changedFiles;
|
|
164
188
|
} catch (error) {
|
|
@@ -167,7 +191,14 @@ export function getChangedFiles(BASE_BRANCH, log = console.log) {
|
|
|
167
191
|
}
|
|
168
192
|
}
|
|
169
193
|
|
|
170
|
-
|
|
194
|
+
/**
|
|
195
|
+
* Absolute path of the Jest binary the CLI invokes directly (no shell, no npx).
|
|
196
|
+
* @param {string} [root]
|
|
197
|
+
* @returns {string}
|
|
198
|
+
*/
|
|
199
|
+
export function resolveJestBin(root = process.cwd()) {
|
|
200
|
+
return path.resolve(root, 'node_modules', 'jest', 'bin', 'jest.js');
|
|
201
|
+
}
|
|
171
202
|
|
|
172
203
|
function runJest(jestArgs) {
|
|
173
204
|
console.log(` Jest command: npx jest ${jestArgs.join(' ')}`);
|
|
@@ -176,7 +207,7 @@ function runJest(jestArgs) {
|
|
|
176
207
|
// no npx), with an argument array, so file paths with spaces or special
|
|
177
208
|
// characters can't break the invocation.
|
|
178
209
|
try {
|
|
179
|
-
const jestBin =
|
|
210
|
+
const jestBin = resolveJestBin();
|
|
180
211
|
execFileSync(process.execPath, [jestBin, ...jestArgs], { stdio: 'inherit' });
|
|
181
212
|
} catch (err) {
|
|
182
213
|
// Log and continue so the coverage report still runs and CI can
|
|
@@ -186,6 +217,80 @@ function runJest(jestArgs) {
|
|
|
186
217
|
}
|
|
187
218
|
}
|
|
188
219
|
|
|
220
|
+
/**
|
|
221
|
+
* Resolves the test files a mode would run, without logging, running Jest, or exiting.
|
|
222
|
+
* `testsToRun` keeps CLI insertion order (changed tests first, then matches).
|
|
223
|
+
* Full mode cannot discover related tests without Jest; `testsToRun` is then
|
|
224
|
+
* only the changed test files, and `jestTargets` is what `--findRelatedTests` receives.
|
|
225
|
+
* @param {string[]} files
|
|
226
|
+
* @param {string} mode
|
|
227
|
+
* @param {number} [depth]
|
|
228
|
+
* @param {string} [root]
|
|
229
|
+
*/
|
|
230
|
+
export function resolveTestPlan(files, mode, depth = 2, root = process.cwd()) {
|
|
231
|
+
const { sourceFiles, testFiles } = separateSourceAndTestFiles(files);
|
|
232
|
+
|
|
233
|
+
if (mode === 'full') {
|
|
234
|
+
const jestTargets = [...sourceFiles, ...testFiles];
|
|
235
|
+
return {
|
|
236
|
+
mode,
|
|
237
|
+
depth,
|
|
238
|
+
sourceFiles,
|
|
239
|
+
testFiles,
|
|
240
|
+
mappings: [],
|
|
241
|
+
testsToRun: [...testFiles],
|
|
242
|
+
jestTargets,
|
|
243
|
+
jestArgs: ['--findRelatedTests', '--passWithNoTests', '--coverage', ...jestTargets],
|
|
244
|
+
delegatedToJest: true
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Fast: naming-convention mapping with a direct-import fallback.
|
|
249
|
+
// Smart: naming-convention mapping plus a depth-limited reverse-dependency walk.
|
|
250
|
+
const testFilesToRun = new Set(testFiles);
|
|
251
|
+
const mappings = [];
|
|
252
|
+
|
|
253
|
+
sourceFiles.forEach(sourceFile => {
|
|
254
|
+
let relatedTests;
|
|
255
|
+
let matchType;
|
|
256
|
+
|
|
257
|
+
if (mode === 'smart') {
|
|
258
|
+
relatedTests = findRelatedTestFilesBySource(sourceFile, { maxDepth: depth, root });
|
|
259
|
+
matchType = `naming + import graph, depth ≤ ${depth}`;
|
|
260
|
+
} else {
|
|
261
|
+
relatedTests = findTestFilesForSource(sourceFile, root);
|
|
262
|
+
matchType = 'naming convention';
|
|
263
|
+
|
|
264
|
+
if (relatedTests.length === 0) {
|
|
265
|
+
relatedTests = findTestFilesByImport(sourceFile, root);
|
|
266
|
+
if (relatedTests.length > 0) {
|
|
267
|
+
matchType = 'import scan';
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
mappings.push({
|
|
273
|
+
source: sourceFile,
|
|
274
|
+
tests: [...relatedTests],
|
|
275
|
+
matchType
|
|
276
|
+
});
|
|
277
|
+
relatedTests.forEach(test => testFilesToRun.add(test));
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
const testsToRun = Array.from(testFilesToRun);
|
|
281
|
+
return {
|
|
282
|
+
mode,
|
|
283
|
+
depth,
|
|
284
|
+
sourceFiles,
|
|
285
|
+
testFiles,
|
|
286
|
+
mappings,
|
|
287
|
+
testsToRun,
|
|
288
|
+
jestTargets: testsToRun,
|
|
289
|
+
jestArgs: ['--passWithNoTests', '--coverage', ...testsToRun],
|
|
290
|
+
delegatedToJest: false
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
189
294
|
function runRelatedTests(files, mode, depth = 2) {
|
|
190
295
|
if (files.length === 0) {
|
|
191
296
|
console.log('✅ No changed source files found.');
|
|
@@ -194,8 +299,8 @@ function runRelatedTests(files, mode, depth = 2) {
|
|
|
194
299
|
|
|
195
300
|
console.log('🔍 Analyzing changed files for optimal test execution...');
|
|
196
301
|
|
|
197
|
-
|
|
198
|
-
const { sourceFiles, testFiles } =
|
|
302
|
+
const plan = resolveTestPlan(files, mode, depth);
|
|
303
|
+
const { sourceFiles, testFiles } = plan;
|
|
199
304
|
|
|
200
305
|
console.log(`\n📊 Changed files breakdown:`);
|
|
201
306
|
console.log(` Source files: ${sourceFiles.length}`);
|
|
@@ -211,7 +316,7 @@ function runRelatedTests(files, mode, depth = 2) {
|
|
|
211
316
|
|
|
212
317
|
// Full mode delegates discovery to Jest, so pass the changed files themselves.
|
|
213
318
|
if (mode === 'full') {
|
|
214
|
-
const jestTargets =
|
|
319
|
+
const jestTargets = plan.jestTargets;
|
|
215
320
|
|
|
216
321
|
if (jestTargets.length === 0) {
|
|
217
322
|
console.log('\n⚠️ No changed source or test files found to run.');
|
|
@@ -224,42 +329,19 @@ function runRelatedTests(files, mode, depth = 2) {
|
|
|
224
329
|
console.log(' Files passed to Jest:');
|
|
225
330
|
jestTargets.forEach(file => console.log(` - ${file}`));
|
|
226
331
|
|
|
227
|
-
runJest(
|
|
332
|
+
runJest(plan.jestArgs);
|
|
228
333
|
return;
|
|
229
334
|
}
|
|
230
335
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
sourceFiles.forEach(sourceFile => {
|
|
236
|
-
let relatedTests;
|
|
237
|
-
let matchType;
|
|
238
|
-
|
|
239
|
-
if (mode === 'smart') {
|
|
240
|
-
relatedTests = findRelatedTestFilesBySource(sourceFile, { maxDepth: depth });
|
|
241
|
-
matchType = `naming + import graph, depth ≤ ${depth}`;
|
|
336
|
+
plan.mappings.forEach(({ source, tests, matchType }) => {
|
|
337
|
+
if (tests.length > 0) {
|
|
338
|
+
console.log(` ✓ ${source} → ${tests.length} test file(s) (${matchType})`);
|
|
242
339
|
} else {
|
|
243
|
-
|
|
244
|
-
matchType = 'naming convention';
|
|
245
|
-
|
|
246
|
-
if (relatedTests.length === 0) {
|
|
247
|
-
relatedTests = findTestFilesByImport(sourceFile);
|
|
248
|
-
if (relatedTests.length > 0) {
|
|
249
|
-
matchType = 'import scan';
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
if (relatedTests.length > 0) {
|
|
255
|
-
console.log(` ✓ ${sourceFile} → ${relatedTests.length} test file(s) (${matchType})`);
|
|
256
|
-
relatedTests.forEach(test => testFilesToRun.add(test));
|
|
257
|
-
} else {
|
|
258
|
-
console.log(` ⚠ ${sourceFile} → No test file found`);
|
|
340
|
+
console.log(` ⚠ ${source} → No test file found`);
|
|
259
341
|
}
|
|
260
342
|
});
|
|
261
343
|
|
|
262
|
-
const finalTestFiles =
|
|
344
|
+
const finalTestFiles = plan.testsToRun;
|
|
263
345
|
|
|
264
346
|
if (finalTestFiles.length === 0) {
|
|
265
347
|
console.log('\n⚠️ No test files found to run.');
|
|
@@ -281,14 +363,14 @@ function runRelatedTests(files, mode, depth = 2) {
|
|
|
281
363
|
console.log(` Source files detected: ${sourceFiles.length}`);
|
|
282
364
|
}
|
|
283
365
|
|
|
284
|
-
runJest(
|
|
366
|
+
runJest(plan.jestArgs);
|
|
285
367
|
}
|
|
286
368
|
|
|
287
369
|
/**
|
|
288
370
|
* Dry run: only resolve the test files that the selected mode would run,
|
|
289
371
|
* then print one path per line and exit without running Jest or checking
|
|
290
372
|
* coverage. The stdout is clean enough for another tool to consume:
|
|
291
|
-
* node index.js --
|
|
373
|
+
* node index.js --dryRun true --files src/a.js --base origin/main
|
|
292
374
|
* Errors still go to stderr via the catch in main().
|
|
293
375
|
*
|
|
294
376
|
* @param {string[]} files - Changed source and test files (repo-relative)
|
|
@@ -302,40 +384,28 @@ export function dryRun(files, mode, depth = 2) {
|
|
|
302
384
|
exit(0);
|
|
303
385
|
}
|
|
304
386
|
|
|
305
|
-
const
|
|
387
|
+
const plan = resolveTestPlan(files, mode, depth);
|
|
306
388
|
|
|
307
389
|
// Full mode delegates discovery to Jest; without running it, the only
|
|
308
|
-
// guaranteed output is the changed files themselves.
|
|
390
|
+
// guaranteed output is the changed test files themselves.
|
|
309
391
|
if (mode === 'full') {
|
|
310
392
|
// Keep stdout clean (one path per line) for other tools; warn on stderr.
|
|
311
393
|
console.error('⚠️ full mode relies on Jest --findRelatedTests; --dry-run prints only the changed test files.');
|
|
312
|
-
testFiles.forEach(file => console.log(file));
|
|
394
|
+
plan.testFiles.forEach(file => console.log(file));
|
|
313
395
|
exit(0);
|
|
314
396
|
}
|
|
315
397
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
sourceFiles.forEach(sourceFile => {
|
|
319
|
-
// Same matching rules as the real run (see runRelatedTests), just no Jest.
|
|
320
|
-
const relatedTests = mode === 'smart'
|
|
321
|
-
? findRelatedTestFilesBySource(sourceFile, { maxDepth: depth })
|
|
322
|
-
: findTestFilesForSource(sourceFile);
|
|
323
|
-
|
|
324
|
-
if (mode === 'fast' && relatedTests.length === 0) {
|
|
325
|
-
relatedTests.push(...findTestFilesByImport(sourceFile));
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
relatedTests.forEach(test => testFilesToRun.add(test));
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
Array.from(testFilesToRun).sort().forEach(file => console.log(file));
|
|
398
|
+
[...plan.testsToRun].sort().forEach(file => console.log(file));
|
|
332
399
|
exit(0);
|
|
333
400
|
}
|
|
334
401
|
|
|
335
|
-
function getChangedLines(filePath, BASE_BRANCH) {
|
|
402
|
+
export function getChangedLines(filePath, BASE_BRANCH, root = process.cwd()) {
|
|
336
403
|
// Argument array + `--` separator keeps file paths (and the ref, already
|
|
337
404
|
// validated in verifyBaseBranchExists) out of any shell interpretation.
|
|
338
|
-
const diff = execFileSync('git', ['diff', '-U0', BASE_BRANCH, '--', filePath], {
|
|
405
|
+
const diff = execFileSync('git', ['diff', '-U0', BASE_BRANCH, '--', filePath], {
|
|
406
|
+
cwd: root,
|
|
407
|
+
stdio: ['ignore', 'pipe', 'ignore']
|
|
408
|
+
}).toString();
|
|
339
409
|
const lines = [];
|
|
340
410
|
|
|
341
411
|
const hunkRegex = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/gm;
|
|
@@ -351,14 +421,16 @@ function getChangedLines(filePath, BASE_BRANCH) {
|
|
|
351
421
|
return lines;
|
|
352
422
|
}
|
|
353
423
|
|
|
354
|
-
export function parseLcov(LCOV_PATH) {
|
|
424
|
+
export function parseLcov(LCOV_PATH, root = process.cwd()) {
|
|
355
425
|
const lcovRaw = fs.readFileSync(LCOV_PATH, 'utf8');
|
|
356
426
|
const files = {};
|
|
357
427
|
let currentFile = null;
|
|
358
428
|
|
|
359
429
|
lcovRaw.split('\n').forEach(line => {
|
|
360
430
|
if (line.startsWith('SF:')) {
|
|
361
|
-
|
|
431
|
+
// Absolute SF paths stay absolute. Relative ones resolve against `root`,
|
|
432
|
+
// which is the process cwd for the CLI.
|
|
433
|
+
currentFile = path.resolve(root, line.substring(3).trim());
|
|
362
434
|
files[currentFile] = {
|
|
363
435
|
coverableLines: new Set(),
|
|
364
436
|
uncoveredLines: new Set()
|
|
@@ -475,6 +547,107 @@ export function formatMissedLinesSummary(uncoveredByFile) {
|
|
|
475
547
|
];
|
|
476
548
|
}
|
|
477
549
|
|
|
550
|
+
/**
|
|
551
|
+
* Reads lcov and keeps only source files that participate in the coverage check.
|
|
552
|
+
* Throws an error with code LCOV_NOT_FOUND when the report file is missing.
|
|
553
|
+
* @param {string[]} changedFiles
|
|
554
|
+
* @param {string} lcovPath
|
|
555
|
+
* @param {string} [root]
|
|
556
|
+
*/
|
|
557
|
+
export function loadCoverageIndex(changedFiles, lcovPath, root = process.cwd()) {
|
|
558
|
+
const resolvedLcov = path.resolve(root, lcovPath);
|
|
559
|
+
if (!fs.existsSync(resolvedLcov)) {
|
|
560
|
+
const error = new Error('lcov.info not found. Make sure coverage ran correctly.');
|
|
561
|
+
error.code = 'LCOV_NOT_FOUND';
|
|
562
|
+
error.lcovPath = resolvedLcov;
|
|
563
|
+
throw error;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
return {
|
|
567
|
+
lcovPath: resolvedLcov,
|
|
568
|
+
coverageByFile: parseLcov(resolvedLcov, root),
|
|
569
|
+
relevantFiles: getJsOnlyFiles(changedFiles)
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Classifies one file's changed lines against an lcov index.
|
|
575
|
+
* Line numbers come from `git diff -U0 <base> -- <file>` (working tree vs the ref).
|
|
576
|
+
* @param {string} relPath
|
|
577
|
+
* @param {Record<string, {coverableLines: Set<number>, uncoveredLines: Set<number>}>} coverageByFile
|
|
578
|
+
* @param {string} baseBranch
|
|
579
|
+
* @param {string} [root]
|
|
580
|
+
*/
|
|
581
|
+
export function analyzeChangedFile(relPath, coverageByFile, baseBranch, root = process.cwd()) {
|
|
582
|
+
const absolutePath = path.resolve(root, relPath);
|
|
583
|
+
const changedLines = getChangedLines(relPath, baseBranch, root);
|
|
584
|
+
const fileCoverage = coverageByFile[absolutePath];
|
|
585
|
+
const { coveredLines, uncoveredLines } = classifyChangedLineCoverage(changedLines, fileCoverage);
|
|
586
|
+
const trackedChangedLines = coveredLines.length + uncoveredLines.length;
|
|
587
|
+
|
|
588
|
+
return {
|
|
589
|
+
filePath: relPath,
|
|
590
|
+
absolutePath,
|
|
591
|
+
changedLines,
|
|
592
|
+
coveredLines,
|
|
593
|
+
uncoveredLines,
|
|
594
|
+
trackedChangedLines,
|
|
595
|
+
hasCoverageInfo: Boolean(fileCoverage),
|
|
596
|
+
fileCoverage: trackedChangedLines > 0
|
|
597
|
+
? `${((coveredLines.length / trackedChangedLines) * 100).toFixed(2)}%`
|
|
598
|
+
: '-',
|
|
599
|
+
status: uncoveredLines.length > 0 ? '🚨' : trackedChangedLines > 0 ? '✅' : 'ℹ️'
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Totals coverable changed lines the same way the CLI threshold does.
|
|
605
|
+
* Files with an empty diff are ignored. Lines absent from lcov are not counted.
|
|
606
|
+
* @param {ReturnType<typeof analyzeChangedFile>[]} fileResults
|
|
607
|
+
* @param {number} coverageLimit
|
|
608
|
+
*/
|
|
609
|
+
export function summarizeChangedLineResults(fileResults, coverageLimit) {
|
|
610
|
+
let totalChanged = 0;
|
|
611
|
+
let totalNotCovered = 0;
|
|
612
|
+
const uncoveredByFile = [];
|
|
613
|
+
const rows = [];
|
|
614
|
+
|
|
615
|
+
fileResults.forEach(result => {
|
|
616
|
+
if (result.changedLines.length === 0) return;
|
|
617
|
+
|
|
618
|
+
totalChanged += result.trackedChangedLines;
|
|
619
|
+
totalNotCovered += result.uncoveredLines.length;
|
|
620
|
+
|
|
621
|
+
if (result.uncoveredLines.length > 0) {
|
|
622
|
+
uncoveredByFile.push({
|
|
623
|
+
filePath: result.filePath,
|
|
624
|
+
lines: result.uncoveredLines
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
rows.push({
|
|
629
|
+
filePath: result.filePath,
|
|
630
|
+
covered: result.coveredLines.length,
|
|
631
|
+
uncovered: result.uncoveredLines.length,
|
|
632
|
+
trackedChangedLines: result.trackedChangedLines,
|
|
633
|
+
fileCoverage: result.fileCoverage,
|
|
634
|
+
status: result.status
|
|
635
|
+
});
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
const summary = totalChanged > 0
|
|
639
|
+
? summarizeCoverageResult(totalChanged, totalNotCovered, coverageLimit)
|
|
640
|
+
: { overallCoverage: null, passed: true, icon: '✅' };
|
|
641
|
+
|
|
642
|
+
return {
|
|
643
|
+
totalChanged,
|
|
644
|
+
totalNotCovered,
|
|
645
|
+
uncoveredByFile,
|
|
646
|
+
rows,
|
|
647
|
+
...summary
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
|
|
478
651
|
function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVERAGE_LIMIT, SHOW_COVERED, CI = false, CI_REPORT_PATH) {
|
|
479
652
|
if (!fs.existsSync(LCOV_PATH)) {
|
|
480
653
|
console.error('❌ lcov.info not found. Make sure coverage ran correctly.');
|
|
@@ -489,11 +662,9 @@ function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVER
|
|
|
489
662
|
console.log(`\n🔍 Checking coverage of changed lines vs branch: ${BASE_BRANCH}\n`);
|
|
490
663
|
}
|
|
491
664
|
|
|
492
|
-
const coverageByFile =
|
|
493
|
-
|
|
494
|
-
changedFiles = getJsOnlyFiles(changedFiles);
|
|
665
|
+
const { coverageByFile, relevantFiles } = loadCoverageIndex(changedFiles, LCOV_PATH);
|
|
495
666
|
|
|
496
|
-
if (
|
|
667
|
+
if (relevantFiles.length === 0) {
|
|
497
668
|
if (!CI) {
|
|
498
669
|
console.log('✅ No relevant files found for coverage check.');
|
|
499
670
|
}
|
|
@@ -501,70 +672,39 @@ function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVER
|
|
|
501
672
|
}
|
|
502
673
|
|
|
503
674
|
if (!CI) {
|
|
504
|
-
console.log('Files with changed lines:',
|
|
675
|
+
console.log('Files with changed lines:', relevantFiles.join('\n'));
|
|
505
676
|
}
|
|
506
677
|
|
|
507
|
-
|
|
508
|
-
let totalNotCovered = 0;
|
|
509
|
-
const uncoveredByFile = [];
|
|
510
|
-
const rows = [];
|
|
678
|
+
const fileResults = [];
|
|
511
679
|
|
|
512
|
-
|
|
513
|
-
const
|
|
514
|
-
|
|
680
|
+
relevantFiles.forEach(relPath => {
|
|
681
|
+
const result = analyzeChangedFile(relPath, coverageByFile, BASE_BRANCH);
|
|
682
|
+
fileResults.push(result);
|
|
515
683
|
|
|
516
|
-
if (
|
|
684
|
+
if (result.changedLines.length === 0) {
|
|
517
685
|
if (!CI) {
|
|
518
|
-
console.log(`ℹ️ ${
|
|
686
|
+
console.log(`ℹ️ ${result.absolutePath} - No changed lines detected.`);
|
|
519
687
|
}
|
|
520
688
|
return;
|
|
521
689
|
}
|
|
522
690
|
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
console.log(`⚠️ No coverage info for: ${absPath}`);
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
const {
|
|
529
|
-
coveredLines,
|
|
530
|
-
uncoveredLines
|
|
531
|
-
} = classifyChangedLineCoverage(addedLines, fileCoverage);
|
|
532
|
-
const trackedChangedLines = coveredLines.length + uncoveredLines.length;
|
|
533
|
-
|
|
534
|
-
totalChanged += trackedChangedLines;
|
|
535
|
-
totalNotCovered += uncoveredLines.length;
|
|
536
|
-
|
|
537
|
-
if (uncoveredLines.length > 0) {
|
|
538
|
-
uncoveredByFile.push({
|
|
539
|
-
filePath: relPath,
|
|
540
|
-
lines: uncoveredLines
|
|
541
|
-
});
|
|
691
|
+
if (!result.hasCoverageInfo && !CI) {
|
|
692
|
+
console.log(`⚠️ No coverage info for: ${result.absolutePath}`);
|
|
542
693
|
}
|
|
543
694
|
|
|
544
|
-
rows.push({
|
|
545
|
-
filePath: relPath,
|
|
546
|
-
covered: coveredLines.length,
|
|
547
|
-
uncovered: uncoveredLines.length,
|
|
548
|
-
trackedChangedLines,
|
|
549
|
-
fileCoverage: trackedChangedLines > 0
|
|
550
|
-
? `${((coveredLines.length / trackedChangedLines) * 100).toFixed(2)}%`
|
|
551
|
-
: '-',
|
|
552
|
-
status: uncoveredLines.length > 0 ? '🚨' : trackedChangedLines > 0 ? '✅' : 'ℹ️'
|
|
553
|
-
});
|
|
554
|
-
|
|
555
695
|
if (!CI) {
|
|
556
|
-
if (uncoveredLines.length > 0) {
|
|
557
|
-
console.log(`🚨 ${
|
|
558
|
-
} else if (trackedChangedLines > 0) {
|
|
559
|
-
console.log(`✅ ${
|
|
696
|
+
if (result.uncoveredLines.length > 0) {
|
|
697
|
+
console.log(`🚨 ${result.absolutePath} - Uncovered changed lines: [${result.uncoveredLines.join(', ')}]`);
|
|
698
|
+
} else if (result.trackedChangedLines > 0) {
|
|
699
|
+
console.log(`✅ ${result.absolutePath} - All changed lines are covered`);
|
|
560
700
|
}
|
|
561
701
|
|
|
562
|
-
console.log(` Total coverable changed lines: ${trackedChangedLines}, Covered: ${coveredLines.length}, Uncovered: ${uncoveredLines.length}`)
|
|
563
|
-
if (SHOW_COVERED && coveredLines.length > 0) {
|
|
564
|
-
console.log(` Covered lines: [${coveredLines.join(', ')}]`);
|
|
702
|
+
console.log(` Total coverable changed lines: ${result.trackedChangedLines}, Covered: ${result.coveredLines.length}, Uncovered: ${result.uncoveredLines.length}`)
|
|
703
|
+
if (SHOW_COVERED && result.coveredLines.length > 0) {
|
|
704
|
+
console.log(` Covered lines: [${result.coveredLines.join(', ')}]`);
|
|
565
705
|
}
|
|
566
|
-
if (trackedChangedLines > 0) {
|
|
567
|
-
console.log(` Coverage for changed lines: ${((coveredLines.length / trackedChangedLines) * 100).toFixed(2)}%`);
|
|
706
|
+
if (result.trackedChangedLines > 0) {
|
|
707
|
+
console.log(` Coverage for changed lines: ${((result.coveredLines.length / result.trackedChangedLines) * 100).toFixed(2)}%`);
|
|
568
708
|
} else {
|
|
569
709
|
console.log(` No coverable lines in this file's changed lines (e.g., imports, comments).`);
|
|
570
710
|
}
|
|
@@ -572,9 +712,15 @@ function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVER
|
|
|
572
712
|
});
|
|
573
713
|
|
|
574
714
|
// Cumulative coverage check
|
|
575
|
-
const {
|
|
576
|
-
|
|
577
|
-
|
|
715
|
+
const {
|
|
716
|
+
overallCoverage,
|
|
717
|
+
passed,
|
|
718
|
+
icon,
|
|
719
|
+
totalChanged,
|
|
720
|
+
totalNotCovered,
|
|
721
|
+
rows,
|
|
722
|
+
uncoveredByFile
|
|
723
|
+
} = summarizeChangedLineResults(fileResults, COVERAGE_LIMIT);
|
|
578
724
|
|
|
579
725
|
if (CI) {
|
|
580
726
|
const report = buildMarkdownCoverageReport(rows, {
|