mo3ta-coverage 1.0.9 → 1.0.11
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 +52 -57
- package/RECOMMENDED_USAGE.md +67 -0
- package/coverage/clover.xml +236 -0
- package/coverage/coverage-final.json +4 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/cli.js.html +1090 -0
- package/coverage/lcov-report/coverage/index.html +116 -0
- package/coverage/lcov-report/coverage/index.js.html +364 -0
- package/coverage/lcov-report/coverage/src/cli.js.html +652 -0
- package/coverage/lcov-report/coverage/src/fileUtils.js.html +130 -0
- package/coverage/lcov-report/coverage/src/gitUtils.js.html +145 -0
- package/coverage/lcov-report/coverage/src/index.html +146 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/fileUtils.js.html +361 -0
- package/coverage/lcov-report/gitUtils.js.html +145 -0
- package/coverage/lcov-report/index.html +146 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov.info +416 -0
- package/index.js +19 -8
- package/package.json +1 -1
- package/src/cli.js +202 -52
- package/src/cli.test.js +113 -0
- package/src/fileUtils.js +77 -0
- package/src/fileUtils.test.js +151 -1
- package/.github/workflows/publish.yml +0 -29
- package/.mo3ta-coverage.json +0 -4
package/index.js
CHANGED
|
@@ -34,7 +34,7 @@ program
|
|
|
34
34
|
.option('--lcov <path>', 'Path to lcov.info file generated by test coverage tools', 'coverage/lcov.info')
|
|
35
35
|
.option('--limit <percent>', 'Minimum required coverage percentage for all changed lines (e.g., 80)', '80')
|
|
36
36
|
.option('--showCovered <bool>', 'Show covered changed lines in the output', false)
|
|
37
|
-
.option('--
|
|
37
|
+
.option('--mode <mode>', 'Execution mode: fast, smart, or full', 'smart')
|
|
38
38
|
.enablePositionalOptions(); // used to allow using same options in subcommands
|
|
39
39
|
|
|
40
40
|
|
|
@@ -42,7 +42,7 @@ program
|
|
|
42
42
|
.command('config')
|
|
43
43
|
.description('Set or get configuration options')
|
|
44
44
|
.option('--base <branch>', 'Set default base branch')
|
|
45
|
-
.option('--
|
|
45
|
+
.option('--mode <mode>', 'Set default mode (fast/smart/full)')
|
|
46
46
|
.action((opts) => {
|
|
47
47
|
let config = readConfig();
|
|
48
48
|
|
|
@@ -56,10 +56,15 @@ program
|
|
|
56
56
|
console.log(`Default base branch set to: ${opts.base}`);
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
if (
|
|
60
|
-
|
|
59
|
+
if (opts.mode !== undefined) {
|
|
60
|
+
const normalizedMode = String(opts.mode).toLowerCase();
|
|
61
|
+
if (!['fast', 'smart', 'full'].includes(normalizedMode)) {
|
|
62
|
+
console.error(`Invalid mode: ${opts.mode}. Expected one of: fast, smart, full.`);
|
|
63
|
+
process.exit(1);
|
|
64
|
+
}
|
|
65
|
+
config.mode = normalizedMode;
|
|
61
66
|
updated = true;
|
|
62
|
-
console.log(`Default
|
|
67
|
+
console.log(`Default mode set to: ${config.mode}`);
|
|
63
68
|
}
|
|
64
69
|
if (updated) {
|
|
65
70
|
writeConfig(config);
|
|
@@ -81,10 +86,16 @@ program.action((opts) => {
|
|
|
81
86
|
} else if (!options.base) {
|
|
82
87
|
options.base = 'origin/main';
|
|
83
88
|
}
|
|
84
|
-
// Use config shallow if not provided in CLI
|
|
85
|
-
console.log(`Using shallow: ${options.shallow} ${config.shallow}`);
|
|
86
89
|
|
|
87
|
-
options.
|
|
90
|
+
if (!options.mode && config.mode) {
|
|
91
|
+
options.mode = config.mode;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
options.mode = String(options.mode || 'smart').toLowerCase();
|
|
95
|
+
if (!['fast', 'smart', 'full'].includes(options.mode)) {
|
|
96
|
+
console.error(`Invalid mode: ${options.mode}. Expected one of: fast, smart, full.`);
|
|
97
|
+
process.exit(1);
|
|
98
|
+
}
|
|
88
99
|
|
|
89
100
|
console.log(`Final options: ${JSON.stringify(options, null, 2)}`);
|
|
90
101
|
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -3,7 +3,7 @@ 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
|
-
import { getJsOnlyFiles } from './fileUtils.js';
|
|
6
|
+
import { getJsOnlyFiles, separateSourceAndTestFiles, findTestFilesForSource } from './fileUtils.js';
|
|
7
7
|
|
|
8
8
|
export function main(options) {
|
|
9
9
|
try {
|
|
@@ -14,12 +14,12 @@ export function main(options) {
|
|
|
14
14
|
const LCOV_PATH = options.lcov;
|
|
15
15
|
const COVERAGE_LIMIT = parseFloat(options.limit);
|
|
16
16
|
const SHOW_COVERED = options.showCovered === true || options.showCovered === 'true';
|
|
17
|
-
const
|
|
17
|
+
const MODE = options.mode;
|
|
18
18
|
|
|
19
|
-
console.log(`Options:\nBase Branch: ${BASE_BRANCH} \nLCOV Path: ${LCOV_PATH} \nCoverage Limit: ${COVERAGE_LIMIT} \nShow Covered Lines: ${SHOW_COVERED} \
|
|
19
|
+
console.log(`Options:\nBase Branch: ${BASE_BRANCH} \nLCOV Path: ${LCOV_PATH} \nCoverage Limit: ${COVERAGE_LIMIT} \nShow Covered Lines: ${SHOW_COVERED} \nMode: ${MODE}`);
|
|
20
20
|
|
|
21
21
|
const changedFiles = getChangedFiles(BASE_BRANCH);
|
|
22
|
-
runRelatedTests(changedFiles,
|
|
22
|
+
runRelatedTests(changedFiles, MODE);
|
|
23
23
|
reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVERAGE_LIMIT, SHOW_COVERED);
|
|
24
24
|
} catch (error) {
|
|
25
25
|
console.error('❌ An unexpected error occurred:', error.message || error);
|
|
@@ -50,38 +50,107 @@ function getChangedFiles(BASE_BRANCH) {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
function runRelatedTests(files,
|
|
53
|
+
function runRelatedTests(files, mode) {
|
|
54
54
|
if (files.length === 0) {
|
|
55
55
|
console.log('✅ No changed source files found.');
|
|
56
|
-
|
|
57
56
|
exit(0);
|
|
58
57
|
}
|
|
59
58
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
59
|
+
console.log('🔍 Analyzing changed files for optimal test execution...');
|
|
60
|
+
|
|
61
|
+
// Separate source files and test files
|
|
62
|
+
const { sourceFiles, testFiles } = separateSourceAndTestFiles(files);
|
|
63
|
+
|
|
64
|
+
console.log(`\n📊 Changed files breakdown:`);
|
|
65
|
+
console.log(` Source files: ${sourceFiles.length}`);
|
|
66
|
+
console.log(` Test files: ${testFiles.length}`);
|
|
67
|
+
if (sourceFiles.length > 0) {
|
|
68
|
+
console.log(' Source file list:');
|
|
69
|
+
sourceFiles.forEach(file => console.log(` - ${file}`));
|
|
70
|
+
}
|
|
71
|
+
if (testFiles.length > 0) {
|
|
72
|
+
console.log(' Changed test file list:');
|
|
73
|
+
testFiles.forEach(file => console.log(` - ${file}`));
|
|
63
74
|
}
|
|
64
75
|
|
|
65
|
-
|
|
76
|
+
// Find test files for changed source files
|
|
77
|
+
const testFilesToRun = new Set(testFiles);
|
|
78
|
+
const mappedTests = new Map(); // Track which tests map to which source files
|
|
79
|
+
|
|
80
|
+
sourceFiles.forEach(sourceFile => {
|
|
81
|
+
const relatedTests = findTestFilesForSource(sourceFile);
|
|
82
|
+
if (relatedTests.length > 0) {
|
|
83
|
+
console.log(` ✓ ${sourceFile} → ${relatedTests.length} test file(s)`);
|
|
84
|
+
relatedTests.forEach(test => {
|
|
85
|
+
testFilesToRun.add(test);
|
|
86
|
+
if (!mappedTests.has(test)) {
|
|
87
|
+
mappedTests.set(test, []);
|
|
88
|
+
}
|
|
89
|
+
mappedTests.get(test).push(sourceFile);
|
|
90
|
+
});
|
|
91
|
+
} else {
|
|
92
|
+
console.log(` ⚠ ${sourceFile} → No test file found`);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
66
95
|
|
|
67
|
-
|
|
68
|
-
files = getJsOnlyFiles(files, runTestsOnly);
|
|
96
|
+
const finalTestFiles = Array.from(testFilesToRun);
|
|
69
97
|
|
|
70
|
-
if (
|
|
71
|
-
console.log('
|
|
72
|
-
|
|
98
|
+
if (finalTestFiles.length === 0) {
|
|
99
|
+
console.log('\n⚠️ No test files found to run.');
|
|
100
|
+
console.log('Consider creating test files for your changed source files.');
|
|
73
101
|
exit(0);
|
|
74
102
|
}
|
|
75
103
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
104
|
+
console.log(`\n🧪 Running ${finalTestFiles.length} test file(s):\n`, finalTestFiles.join('\n'));
|
|
105
|
+
|
|
106
|
+
if (mode === 'fast') {
|
|
107
|
+
console.log(`\n📝 Test strategy: fast (Direct execution, no Jest dependency traversal)`);
|
|
108
|
+
console.log(` Test files: ${finalTestFiles.length}`);
|
|
109
|
+
console.log(` Source files detected: ${sourceFiles.length}`);
|
|
110
|
+
console.log(' Files passed to Jest:');
|
|
111
|
+
finalTestFiles.forEach(file => console.log(` - ${file}`));
|
|
112
|
+
const command = `npx jest --passWithNoTests --coverage ${finalTestFiles.join(' ')}`;
|
|
113
|
+
console.log(` Jest command: ${command}`);
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
execSync(command, { stdio: 'inherit' });
|
|
117
|
+
} catch (err) {
|
|
118
|
+
console.error('❌ Tests failed:', err.message || err);
|
|
119
|
+
exit(1);
|
|
120
|
+
}
|
|
121
|
+
} else if (mode === 'smart') {
|
|
122
|
+
console.log(`\n📝 Test strategy: smart (Combine source and mapped test files, no Jest dependency traversal)`);
|
|
123
|
+
console.log(` Test files: ${finalTestFiles.length}`);
|
|
124
|
+
console.log(` Source files for coverage: ${sourceFiles.length}`);
|
|
125
|
+
const allFiles = [...sourceFiles, ...finalTestFiles];
|
|
126
|
+
console.log(` Total files passed to Jest: ${allFiles.length}`);
|
|
127
|
+
console.log(' Files passed to Jest:');
|
|
128
|
+
allFiles.forEach(file => console.log(` - ${file}`));
|
|
129
|
+
const command = `npx jest --passWithNoTests --coverage ${allFiles.join(' ')}`;
|
|
130
|
+
console.log(` Jest command: ${command}`);
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
execSync(command, { stdio: 'inherit' });
|
|
134
|
+
} catch (err) {
|
|
135
|
+
console.error('❌ Tests failed:', err.message || err);
|
|
136
|
+
exit(1);
|
|
137
|
+
}
|
|
138
|
+
} else {
|
|
139
|
+
console.log(`\n📝 Test strategy: full (Run mapped tests through Jest --findRelatedTests)`);
|
|
140
|
+
console.log(` Running tests for ${sourceFiles.length} source file(s) + ${testFiles.length} changed test(s)`);
|
|
141
|
+
console.log(` Test files in Jest command: ${finalTestFiles.length}`);
|
|
142
|
+
console.log(` ⚠️ If this expands to too many tests, switch to --mode smart`);
|
|
143
|
+
console.log(' Test files passed to Jest:');
|
|
144
|
+
finalTestFiles.forEach(file => console.log(` - ${file}`));
|
|
145
|
+
const command = `npx jest --findRelatedTests --passWithNoTests --coverage ${finalTestFiles.join(' ')}`;
|
|
146
|
+
console.log(` Jest command: ${command}`);
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
execSync(command, { stdio: 'inherit' });
|
|
150
|
+
} catch (err) {
|
|
151
|
+
console.error('❌ Tests failed:', err.message || err);
|
|
152
|
+
exit(1);
|
|
153
|
+
}
|
|
85
154
|
}
|
|
86
155
|
}
|
|
87
156
|
|
|
@@ -102,7 +171,7 @@ function getChangedLines(filePath, BASE_BRANCH) {
|
|
|
102
171
|
return lines;
|
|
103
172
|
}
|
|
104
173
|
|
|
105
|
-
function parseLcov(LCOV_PATH) {
|
|
174
|
+
export function parseLcov(LCOV_PATH) {
|
|
106
175
|
const lcovRaw = fs.readFileSync(LCOV_PATH, 'utf8');
|
|
107
176
|
const files = {};
|
|
108
177
|
let currentFile = null;
|
|
@@ -110,11 +179,15 @@ function parseLcov(LCOV_PATH) {
|
|
|
110
179
|
lcovRaw.split('\n').forEach(line => {
|
|
111
180
|
if (line.startsWith('SF:')) {
|
|
112
181
|
currentFile = path.resolve(line.substring(3).trim());
|
|
113
|
-
files[currentFile] =
|
|
182
|
+
files[currentFile] = {
|
|
183
|
+
coverableLines: new Set(),
|
|
184
|
+
uncoveredLines: new Set()
|
|
185
|
+
};
|
|
114
186
|
} else if (line.startsWith('DA:') && currentFile) {
|
|
115
187
|
const [lineNo, hits] = line.substring(3).split(',').map(Number);
|
|
188
|
+
files[currentFile].coverableLines.add(lineNo);
|
|
116
189
|
if (hits === 0) {
|
|
117
|
-
files[currentFile].add(lineNo);
|
|
190
|
+
files[currentFile].uncoveredLines.add(lineNo);
|
|
118
191
|
}
|
|
119
192
|
}
|
|
120
193
|
});
|
|
@@ -122,13 +195,77 @@ function parseLcov(LCOV_PATH) {
|
|
|
122
195
|
return files;
|
|
123
196
|
}
|
|
124
197
|
|
|
198
|
+
export function classifyChangedLineCoverage(changedLines, fileCoverage) {
|
|
199
|
+
const coverage = fileCoverage || {
|
|
200
|
+
coverableLines: new Set(),
|
|
201
|
+
uncoveredLines: new Set()
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const coveredLines = [];
|
|
205
|
+
const uncoveredLines = [];
|
|
206
|
+
|
|
207
|
+
changedLines.forEach(line => {
|
|
208
|
+
// Only classify lines that appear in LCOV (are coverable).
|
|
209
|
+
// Lines not in LCOV (imports, non-executable lines) are filtered out.
|
|
210
|
+
if (!coverage.coverableLines.has(line)) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (coverage.uncoveredLines.has(line)) {
|
|
215
|
+
uncoveredLines.push(line);
|
|
216
|
+
} else {
|
|
217
|
+
coveredLines.push(line);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
return {
|
|
222
|
+
coveredLines,
|
|
223
|
+
uncoveredLines
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function summarizeCoverageResult(totalChanged, totalNotCovered, coverageLimit) {
|
|
228
|
+
if (totalChanged === 0) {
|
|
229
|
+
return {
|
|
230
|
+
overallCoverage: null,
|
|
231
|
+
passed: true,
|
|
232
|
+
icon: '✅'
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const overallCoverage = ((totalChanged - totalNotCovered) / totalChanged) * 100;
|
|
237
|
+
const passed = overallCoverage >= coverageLimit;
|
|
238
|
+
|
|
239
|
+
return {
|
|
240
|
+
overallCoverage,
|
|
241
|
+
passed,
|
|
242
|
+
icon: passed ? '✅' : '🚨'
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function formatMissedLinesSummary(uncoveredByFile) {
|
|
247
|
+
if (uncoveredByFile.length === 0) {
|
|
248
|
+
return [];
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return [
|
|
252
|
+
'',
|
|
253
|
+
'Missed changed lines by file:',
|
|
254
|
+
...uncoveredByFile.flatMap(({ filePath, lines }) => {
|
|
255
|
+
const absolutePath = path.resolve(filePath);
|
|
256
|
+
|
|
257
|
+
return lines.map(line => ` - ${absolutePath}:${line}`);
|
|
258
|
+
})
|
|
259
|
+
];
|
|
260
|
+
}
|
|
261
|
+
|
|
125
262
|
function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVERAGE_LIMIT, SHOW_COVERED) {
|
|
126
263
|
if (!fs.existsSync(LCOV_PATH)) {
|
|
127
264
|
console.error('❌ lcov.info not found. Make sure coverage ran correctly.');
|
|
128
265
|
process.exit(1);
|
|
129
266
|
}
|
|
130
267
|
|
|
131
|
-
const
|
|
268
|
+
const coverageByFile = parseLcov(LCOV_PATH);
|
|
132
269
|
|
|
133
270
|
console.log(`\n🔍 Checking coverage of changed lines vs branch: ${BASE_BRANCH}\n`);
|
|
134
271
|
|
|
@@ -142,48 +279,61 @@ function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVER
|
|
|
142
279
|
console.log('Files with changed lines:', changedFiles.join('\n'));
|
|
143
280
|
|
|
144
281
|
let totalChanged = 0;
|
|
145
|
-
let
|
|
282
|
+
let totalNotCovered = 0;
|
|
283
|
+
const uncoveredByFile = [];
|
|
146
284
|
|
|
147
285
|
changedFiles.forEach(relPath => {
|
|
148
286
|
const absPath = path.resolve(relPath);
|
|
149
|
-
|
|
150
|
-
|
|
287
|
+
const addedLines = getChangedLines(relPath, BASE_BRANCH);
|
|
288
|
+
|
|
289
|
+
if (addedLines.length === 0) {
|
|
290
|
+
console.log(`ℹ️ ${absPath} - No changed lines detected.`);
|
|
151
291
|
return;
|
|
152
292
|
}
|
|
153
293
|
|
|
154
|
-
const
|
|
155
|
-
|
|
294
|
+
const fileCoverage = coverageByFile[absPath];
|
|
295
|
+
if (!fileCoverage) {
|
|
296
|
+
console.log(`⚠️ No coverage info for: ${absPath}`);
|
|
297
|
+
}
|
|
156
298
|
|
|
157
|
-
const
|
|
158
|
-
|
|
299
|
+
const {
|
|
300
|
+
coveredLines,
|
|
301
|
+
uncoveredLines
|
|
302
|
+
} = classifyChangedLineCoverage(addedLines, fileCoverage);
|
|
303
|
+
const trackedChangedLines = coveredLines.length + uncoveredLines.length;
|
|
159
304
|
|
|
160
|
-
totalChanged +=
|
|
161
|
-
|
|
305
|
+
totalChanged += trackedChangedLines;
|
|
306
|
+
totalNotCovered += uncoveredLines.length;
|
|
162
307
|
|
|
163
|
-
if (
|
|
164
|
-
|
|
165
|
-
|
|
308
|
+
if (uncoveredLines.length > 0) {
|
|
309
|
+
uncoveredByFile.push({
|
|
310
|
+
filePath: relPath,
|
|
311
|
+
lines: uncoveredLines
|
|
312
|
+
});
|
|
166
313
|
}
|
|
167
314
|
|
|
168
|
-
if (
|
|
169
|
-
console.log(`🚨 ${absPath} - Uncovered changed lines: [${
|
|
170
|
-
} else {
|
|
315
|
+
if (uncoveredLines.length > 0) {
|
|
316
|
+
console.log(`🚨 ${absPath} - Uncovered changed lines: [${uncoveredLines.join(', ')}]`);
|
|
317
|
+
} else if (trackedChangedLines > 0) {
|
|
171
318
|
console.log(`✅ ${absPath} - All changed lines are covered`);
|
|
172
319
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
if (SHOW_COVERED) {
|
|
320
|
+
|
|
321
|
+
console.log(` Total coverable changed lines: ${trackedChangedLines}, Covered: ${coveredLines.length}, Uncovered: ${uncoveredLines.length}`)
|
|
322
|
+
if (SHOW_COVERED && coveredLines.length > 0) {
|
|
177
323
|
console.log(` Covered lines: [${coveredLines.join(', ')}]`);
|
|
178
324
|
}
|
|
179
|
-
|
|
325
|
+
if (trackedChangedLines > 0) {
|
|
326
|
+
console.log(` Coverage for changed lines: ${((coveredLines.length / trackedChangedLines) * 100).toFixed(2)}%`);
|
|
327
|
+
} else {
|
|
328
|
+
console.log(` No coverable lines in this file's changed lines (e.g., imports, comments).`);
|
|
329
|
+
}
|
|
180
330
|
});
|
|
181
331
|
|
|
182
332
|
// Cumulative coverage check
|
|
183
333
|
if (totalChanged > 0) {
|
|
184
|
-
const overallCoverage = (
|
|
185
|
-
console.log(`\n
|
|
186
|
-
console.log(`
|
|
187
|
-
|
|
334
|
+
const { overallCoverage, icon } = summarizeCoverageResult(totalChanged, totalNotCovered, COVERAGE_LIMIT);
|
|
335
|
+
console.log(`\n${icon} Overall coverage for all changed lines: ${overallCoverage.toFixed(2)}% (limit: ${COVERAGE_LIMIT}%)`);
|
|
336
|
+
console.log(`Not covered lines: ${totalNotCovered} out of ${totalChanged} changed lines`);
|
|
337
|
+
formatMissedLinesSummary(uncoveredByFile).forEach(line => console.log(line));
|
|
188
338
|
}
|
|
189
339
|
}
|
package/src/cli.test.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import os from 'os';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import {
|
|
5
|
+
classifyChangedLineCoverage,
|
|
6
|
+
formatMissedLinesSummary,
|
|
7
|
+
parseLcov,
|
|
8
|
+
summarizeCoverageResult
|
|
9
|
+
} from './cli.js';
|
|
10
|
+
|
|
11
|
+
function writeLcovFixture(records) {
|
|
12
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mo3ta-coverage-'));
|
|
13
|
+
const sourceFile = path.join(tempDir, 'src/example.js');
|
|
14
|
+
const lcovFile = path.join(tempDir, 'lcov.info');
|
|
15
|
+
|
|
16
|
+
fs.mkdirSync(path.dirname(sourceFile), { recursive: true });
|
|
17
|
+
fs.writeFileSync(sourceFile, 'const x = 1;\nconst y = 2;\n');
|
|
18
|
+
fs.writeFileSync(
|
|
19
|
+
lcovFile,
|
|
20
|
+
[
|
|
21
|
+
'TN:',
|
|
22
|
+
`SF:${sourceFile}`,
|
|
23
|
+
...records,
|
|
24
|
+
'end_of_record'
|
|
25
|
+
].join('\n')
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
return { sourceFile, lcovFile };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe('parseLcov', () => {
|
|
32
|
+
it('records both coverableLines and uncoveredLines', () => {
|
|
33
|
+
const { sourceFile, lcovFile } = writeLcovFixture(['DA:10,1', 'DA:11,0']);
|
|
34
|
+
|
|
35
|
+
const result = parseLcov(lcovFile);
|
|
36
|
+
|
|
37
|
+
expect(result[sourceFile].coverableLines.has(10)).toBe(true);
|
|
38
|
+
expect(result[sourceFile].coverableLines.has(11)).toBe(true);
|
|
39
|
+
expect(result[sourceFile].uncoveredLines.has(10)).toBe(false);
|
|
40
|
+
expect(result[sourceFile].uncoveredLines.has(11)).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('classifyChangedLineCoverage', () => {
|
|
45
|
+
it('filters out changed lines not in LCOV (like imports)', () => {
|
|
46
|
+
const { sourceFile, lcovFile } = writeLcovFixture(['DA:10,1']);
|
|
47
|
+
const coverage = parseLcov(lcovFile)[sourceFile];
|
|
48
|
+
|
|
49
|
+
const result = classifyChangedLineCoverage([11, 12, 13, 14], coverage);
|
|
50
|
+
|
|
51
|
+
expect(result.coveredLines).toEqual([]);
|
|
52
|
+
expect(result.uncoveredLines).toEqual([]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('ignores changed lines with no LCOV entry and classifies rest as covered or uncovered', () => {
|
|
56
|
+
const { sourceFile, lcovFile } = writeLcovFixture(['DA:10,3', 'DA:11,0']);
|
|
57
|
+
const coverage = parseLcov(lcovFile)[sourceFile];
|
|
58
|
+
|
|
59
|
+
const result = classifyChangedLineCoverage([10, 11, 12, 13], coverage);
|
|
60
|
+
|
|
61
|
+
expect(result.coveredLines).toEqual([10]);
|
|
62
|
+
expect(result.uncoveredLines).toEqual([11]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('keeps existing behavior for normal covered and uncovered LCOV lines', () => {
|
|
66
|
+
const { sourceFile, lcovFile } = writeLcovFixture(['DA:20,5', 'DA:21,0']);
|
|
67
|
+
const coverage = parseLcov(lcovFile)[sourceFile];
|
|
68
|
+
|
|
69
|
+
const result = classifyChangedLineCoverage([20, 21], coverage);
|
|
70
|
+
|
|
71
|
+
expect(result.coveredLines).toEqual([20]);
|
|
72
|
+
expect(result.uncoveredLines).toEqual([21]);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('summarizeCoverageResult', () => {
|
|
77
|
+
it('passes when overall coverage equals the configured limit', () => {
|
|
78
|
+
const result = summarizeCoverageResult(5, 1, 80);
|
|
79
|
+
|
|
80
|
+
expect(result.overallCoverage).toBe(80);
|
|
81
|
+
expect(result.passed).toBe(true);
|
|
82
|
+
expect(result.icon).toBe('✅');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('fails when overall coverage is below the configured limit', () => {
|
|
86
|
+
const result = summarizeCoverageResult(5, 2, 80);
|
|
87
|
+
|
|
88
|
+
expect(result.overallCoverage).toBe(60);
|
|
89
|
+
expect(result.passed).toBe(false);
|
|
90
|
+
expect(result.icon).toBe('🚨');
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe('formatMissedLinesSummary', () => {
|
|
95
|
+
it('formats uncovered files and lines for the final summary', () => {
|
|
96
|
+
const result = formatMissedLinesSummary([
|
|
97
|
+
{ filePath: 'src/a.js', lines: [10, 11] },
|
|
98
|
+
{ filePath: 'src/b.js', lines: [7] }
|
|
99
|
+
]);
|
|
100
|
+
|
|
101
|
+
expect(result).toEqual([
|
|
102
|
+
'',
|
|
103
|
+
'Missed changed lines by file:',
|
|
104
|
+
` - ${path.resolve('src/a.js')}:10`,
|
|
105
|
+
` - ${path.resolve('src/a.js')}:11`,
|
|
106
|
+
` - ${path.resolve('src/b.js')}:7`
|
|
107
|
+
]);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('returns no summary lines when no files have missed lines', () => {
|
|
111
|
+
expect(formatMissedLinesSummary([])).toEqual([]);
|
|
112
|
+
});
|
|
113
|
+
});
|
package/src/fileUtils.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
1
4
|
export function getJsOnlyFiles(files, runTestsOnly) {
|
|
2
5
|
// Only include source code files, exclude test, config, and json files
|
|
3
6
|
return files.filter(f => {
|
|
@@ -13,3 +16,77 @@ export function getJsOnlyFiles(files, runTestsOnly) {
|
|
|
13
16
|
return f.endsWith('.js') || f.endsWith('.ts') || f.endsWith('.jsx') || f.endsWith('.tsx');
|
|
14
17
|
});
|
|
15
18
|
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Separates files into source files and test files
|
|
22
|
+
* @param {string[]} files - Array of file paths
|
|
23
|
+
* @returns {{sourceFiles: string[], testFiles: string[]}}
|
|
24
|
+
*/
|
|
25
|
+
export function separateSourceAndTestFiles(files) {
|
|
26
|
+
const sourceFiles = [];
|
|
27
|
+
const testFiles = [];
|
|
28
|
+
|
|
29
|
+
files.forEach(file => {
|
|
30
|
+
// Exclude config files
|
|
31
|
+
if (file.match(/(jest|babel|webpack|tsconfig|eslint|prettier|rollup|vite|package)\.(js|ts|json)$/) || file.includes('.config.')) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
// Exclude json files
|
|
35
|
+
if (file.endsWith('.json')) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Check if it's a test file
|
|
40
|
+
if (file.match(/(\.test|\.spec)\.(js|ts|jsx|tsx)$/)) {
|
|
41
|
+
testFiles.push(file);
|
|
42
|
+
} else if (file.match(/\.(js|ts|jsx|tsx)$/)) {
|
|
43
|
+
sourceFiles.push(file);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return { sourceFiles, testFiles };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Finds potential test files for a given source file
|
|
52
|
+
* Checks multiple naming conventions and locations
|
|
53
|
+
* @param {string} sourceFile - Path to source file
|
|
54
|
+
* @returns {string[]} - Array of potential test file paths
|
|
55
|
+
*/
|
|
56
|
+
export function findTestFilesForSource(sourceFile) {
|
|
57
|
+
const testFiles = [];
|
|
58
|
+
const parsed = path.parse(sourceFile);
|
|
59
|
+
const extensions = ['.js', '.ts', '.jsx', '.tsx'];
|
|
60
|
+
|
|
61
|
+
// Pattern 1: Same directory with .test or .spec suffix
|
|
62
|
+
// e.g., src/utils/helper.js → src/utils/helper.test.js
|
|
63
|
+
extensions.forEach(ext => {
|
|
64
|
+
testFiles.push(path.join(parsed.dir, `${parsed.name}.test${ext}`));
|
|
65
|
+
testFiles.push(path.join(parsed.dir, `${parsed.name}.spec${ext}`));
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Pattern 2: __tests__ subdirectory
|
|
69
|
+
// e.g., src/utils/helper.js → src/utils/__tests__/helper.test.js
|
|
70
|
+
extensions.forEach(ext => {
|
|
71
|
+
testFiles.push(path.join(parsed.dir, '__tests__', `${parsed.name}.test${ext}`));
|
|
72
|
+
testFiles.push(path.join(parsed.dir, '__tests__', `${parsed.name}.spec${ext}`));
|
|
73
|
+
testFiles.push(path.join(parsed.dir, '__tests__', `${parsed.name}${ext}`));
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Pattern 3: tests directory at same level
|
|
77
|
+
// e.g., src/utils/helper.js → src/tests/utils/helper.test.js
|
|
78
|
+
const srcMatch = sourceFile.match(/^(.*?\/src)\/(.*)/);
|
|
79
|
+
if (srcMatch) {
|
|
80
|
+
const baseDir = srcMatch[1];
|
|
81
|
+
const relativePath = srcMatch[2];
|
|
82
|
+
const relParsed = path.parse(relativePath);
|
|
83
|
+
|
|
84
|
+
extensions.forEach(ext => {
|
|
85
|
+
testFiles.push(path.join(baseDir, 'tests', relParsed.dir, `${relParsed.name}.test${ext}`));
|
|
86
|
+
testFiles.push(path.join(baseDir, 'tests', relParsed.dir, `${relParsed.name}.spec${ext}`));
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Check which test files actually exist
|
|
91
|
+
return testFiles.filter(testFile => fs.existsSync(testFile));
|
|
92
|
+
}
|