mo3ta-coverage 1.0.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 +66 -0
- package/babel.config.json +3 -0
- package/index.js +17 -0
- package/jest.config.json +7 -0
- package/package.json +22 -0
- package/src/cli.js +164 -0
- package/src/cli.test.js +10 -0
package/README.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# mo3ta-coverage
|
|
2
|
+
|
|
3
|
+
**mo3ta-coverage** is a CLI tool for checking test coverage of changed lines in a pull request. It runs related tests, parses coverage reports, and ensures cumulative coverage meets a specified threshold—ideal for CI and code review workflows.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Detects changed files between branches using `git diff`
|
|
8
|
+
- Runs related Jest tests for changed files
|
|
9
|
+
- Parses `lcov.info` for coverage data
|
|
10
|
+
- Reports uncovered changed lines
|
|
11
|
+
- Checks overall coverage against a user-defined limit
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
mo3ta-coverage --base origin/main --lcov coverage/lcov.info --limit 80
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## CLI Options
|
|
20
|
+
|
|
21
|
+
| Option | Description | Default |
|
|
22
|
+
|----------------|------------------------------------------------------------------|--------------------|
|
|
23
|
+
| `--base` | Base branch to compare against | `origin/main` |
|
|
24
|
+
| `--lcov` | Path to lcov.info file generated by test coverage tools | `coverage/lcov.info`|
|
|
25
|
+
| `--limit` | Minimum required coverage percentage for all changed lines | `80` |
|
|
26
|
+
|
|
27
|
+
## How It Works
|
|
28
|
+
|
|
29
|
+
1. **Parse CLI options** using [commander](https://www.npmjs.com/package/commander) in [`index.js`](index.js).
|
|
30
|
+
2. **Get changed files**: Uses `git diff --name-only <base>` to find modified JS/TS files ([`src/cli.js`](src/cli.js)).
|
|
31
|
+
3. **Run related tests**: Executes Jest for changed files with `--findRelatedTests` ([`src/cli.js`](src/cli.js)).
|
|
32
|
+
4. **Parse coverage**: Reads `lcov.info` to find uncovered lines ([`src/cli.js`](src/cli.js)).
|
|
33
|
+
5. **Report**: Prints uncovered changed lines and overall coverage ([`src/cli.js`](src/cli.js)).
|
|
34
|
+
6. **Enforce limit**: Exits with error if coverage is below the specified threshold.
|
|
35
|
+
|
|
36
|
+
## Architecture Diagram
|
|
37
|
+
|
|
38
|
+
```mermaid
|
|
39
|
+
flowchart TD
|
|
40
|
+
A[User runs mo3ta-coverage] --> B[Parse CLI options in index.js]
|
|
41
|
+
B --> C[Get changed files via git diff in cli.js]
|
|
42
|
+
C --> D[Run related Jest tests in cli.js]
|
|
43
|
+
D --> E[Parse lcov.info for coverage in cli.js]
|
|
44
|
+
E --> F[Report uncovered changed lines in cli.js]
|
|
45
|
+
F --> G[Check overall coverage vs limit in cli.js]
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Example Output
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
Options: Base Branch: origin/main
|
|
52
|
+
LCOV Path: coverage/lcov.info
|
|
53
|
+
Coverage Limit: 80
|
|
54
|
+
🧪 Running tests related to changed files:
|
|
55
|
+
src/foo.js
|
|
56
|
+
src/bar.js
|
|
57
|
+
🚨 /abs/path/src/foo.js - Uncovered changed lines: [12, 15]
|
|
58
|
+
✅ /abs/path/src/bar.js - All changed lines are covered
|
|
59
|
+
Total changed lines: 5, Covered: 5, Uncovered: 0
|
|
60
|
+
Coverage for changed lines: 100.00%
|
|
61
|
+
🔢 Overall coverage for all changed lines: 85.71% limit is 80%
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
|
|
66
|
+
MIT
|
package/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { Command } from 'commander';
|
|
4
|
+
import { main } from './src/cli.js';
|
|
5
|
+
|
|
6
|
+
const program = new Command();
|
|
7
|
+
program
|
|
8
|
+
.name('mo3ta-coverage')
|
|
9
|
+
.description('Run tests and report uncovered changed lines in a PR. Checks cumulative coverage for all changed lines against a specified limit. Useful for CI and code review workflows.')
|
|
10
|
+
.option('--base <branch>', 'Base branch to compare against (e.g., origin/main)', 'origin/main')
|
|
11
|
+
.option('--lcov <path>', 'Path to lcov.info file generated by test coverage tools', 'coverage/lcov.info')
|
|
12
|
+
.option('--limit <percent>', 'Minimum required coverage percentage for all changed lines (e.g., 80)', '80');
|
|
13
|
+
|
|
14
|
+
program.parse(process.argv);
|
|
15
|
+
const options = program.opts();
|
|
16
|
+
|
|
17
|
+
main(options);
|
package/jest.config.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mo3ta-coverage",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Run tests and report uncovered changed lines in a PR",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"mo3ta-coverage": "./index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {},
|
|
10
|
+
"author": "Mahmoud Mabrok",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@babel/core": "^7.28.0",
|
|
15
|
+
"@babel/preset-env": "^7.28.0",
|
|
16
|
+
"babel-jest": "^30.0.5",
|
|
17
|
+
"jest": "^30.0.5"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"commander": "^14.0.0"
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
|
|
2
|
+
import { execSync } from 'child_process';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
|
|
6
|
+
export function main(options) {
|
|
7
|
+
const BASE_BRANCH = options.base;
|
|
8
|
+
const LCOV_PATH = options.lcov;
|
|
9
|
+
const COVERAGE_LIMIT = parseFloat(options.limit);
|
|
10
|
+
|
|
11
|
+
// log the options for debugging
|
|
12
|
+
|
|
13
|
+
console.log(`Options: Base Branch: ${BASE_BRANCH} \nLCOV Path: ${LCOV_PATH} \nCoverage Limit: ${COVERAGE_LIMIT}`);
|
|
14
|
+
|
|
15
|
+
const changedFiles = getChangedFiles(BASE_BRANCH);
|
|
16
|
+
runRelatedTests(changedFiles);
|
|
17
|
+
reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVERAGE_LIMIT);
|
|
18
|
+
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function getChangedFiles(BASE_BRANCH) {
|
|
22
|
+
const output = execSync(`git diff --name-only ${BASE_BRANCH}`).toString();
|
|
23
|
+
return output
|
|
24
|
+
.split('\n')
|
|
25
|
+
.filter(f => f.trim().length > 0 && f.match(/\.(js|ts|jsx|tsx)$/));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function runRelatedTests(files) {
|
|
29
|
+
if (files.length === 0) {
|
|
30
|
+
console.log('✅ No changed source files found.');
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
console.log('🧪 Running tests related to changed files:\n', files.join('\n'));
|
|
35
|
+
|
|
36
|
+
// filter files to only those that are JavaScript or TypeScript files
|
|
37
|
+
files = getJsOnlyFiles(files);
|
|
38
|
+
|
|
39
|
+
if (files.length === 0) {
|
|
40
|
+
console.log('✅ No relevant files found for testing.');
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// log files to be tested
|
|
45
|
+
console.log('Files to be tested:', files.join(', '));
|
|
46
|
+
|
|
47
|
+
const command = `npx jest --findRelatedTests --passWithNoTests --coverage ${files.join(' ')}`;
|
|
48
|
+
try {
|
|
49
|
+
execSync(command, { stdio: 'inherit' });
|
|
50
|
+
} catch (err) {
|
|
51
|
+
console.error('❌ Some tests failed.');
|
|
52
|
+
process.exit(1);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function getJsOnlyFiles(files) {
|
|
57
|
+
// Only include source code files, exclude test, config, and json files
|
|
58
|
+
return files.filter(f => {
|
|
59
|
+
// Exclude test files
|
|
60
|
+
if (f.match(/(\.test|\.spec)\.(js|ts|jsx|tsx)$/)) return false;
|
|
61
|
+
// Exclude config files
|
|
62
|
+
if (f.match(/(jest|babel|webpack|tsconfig|eslint|prettier|rollup|vite|package)\.(js|ts|json)$/)) return false;
|
|
63
|
+
// Exclude json files
|
|
64
|
+
if (f.endsWith('.json')) return false;
|
|
65
|
+
// Only include code files
|
|
66
|
+
return f.endsWith('.js') || f.endsWith('.ts') || f.endsWith('.jsx') || f.endsWith('.tsx');
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function getChangedLines(filePath, BASE_BRANCH) {
|
|
71
|
+
const diff = execSync(`git diff -U0 ${BASE_BRANCH} -- ${filePath}`).toString();
|
|
72
|
+
const lines = [];
|
|
73
|
+
|
|
74
|
+
const hunkRegex = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/gm;
|
|
75
|
+
let match;
|
|
76
|
+
while ((match = hunkRegex.exec(diff)) !== null) {
|
|
77
|
+
const start = parseInt(match[1]);
|
|
78
|
+
const count = match[2] ? parseInt(match[2]) : 1;
|
|
79
|
+
for (let i = 0; i < count; i++) {
|
|
80
|
+
lines.push(start + i);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return lines;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function parseLcov(LCOV_PATH) {
|
|
88
|
+
const lcovRaw = fs.readFileSync(LCOV_PATH, 'utf8');
|
|
89
|
+
const files = {};
|
|
90
|
+
let currentFile = null;
|
|
91
|
+
|
|
92
|
+
lcovRaw.split('\n').forEach(line => {
|
|
93
|
+
if (line.startsWith('SF:')) {
|
|
94
|
+
currentFile = path.resolve(line.substring(3).trim());
|
|
95
|
+
files[currentFile] = new Set();
|
|
96
|
+
} else if (line.startsWith('DA:') && currentFile) {
|
|
97
|
+
const [lineNo, hits] = line.substring(3).split(',').map(Number);
|
|
98
|
+
if (hits === 0) {
|
|
99
|
+
files[currentFile].add(lineNo);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
return files;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVERAGE_LIMIT) {
|
|
108
|
+
if (!fs.existsSync(LCOV_PATH)) {
|
|
109
|
+
console.error('❌ lcov.info not found. Make sure coverage ran correctly.');
|
|
110
|
+
process.exit(1);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const uncoveredLines = parseLcov(LCOV_PATH);
|
|
114
|
+
|
|
115
|
+
console.log(`\n🔍 Checking coverage of changed lines vs branch: ${BASE_BRANCH}\n`);
|
|
116
|
+
|
|
117
|
+
changedFiles = getJsOnlyFiles(changedFiles);
|
|
118
|
+
|
|
119
|
+
if (changedFiles.length === 0) {
|
|
120
|
+
console.log('✅ No relevant files found for coverage check.');
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
console.log('Files with changed lines:', changedFiles.join(', '));
|
|
125
|
+
|
|
126
|
+
let totalChanged = 0;
|
|
127
|
+
let totalUncovered = 0;
|
|
128
|
+
|
|
129
|
+
changedFiles.forEach(relPath => {
|
|
130
|
+
const absPath = path.resolve(relPath);
|
|
131
|
+
if (!uncoveredLines[absPath]) {
|
|
132
|
+
console.log(`⚠️ No coverage info for: ${absPath}`);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const addedLines = getChangedLines(relPath, BASE_BRANCH);
|
|
137
|
+
const fileUncovered = uncoveredLines[absPath];
|
|
138
|
+
|
|
139
|
+
const uncoveredInDiff = addedLines.filter(line => fileUncovered.has(line));
|
|
140
|
+
const coveredInDiff = addedLines.length - uncoveredInDiff.length;
|
|
141
|
+
|
|
142
|
+
totalChanged += addedLines.length;
|
|
143
|
+
totalUncovered += uncoveredInDiff.length;
|
|
144
|
+
|
|
145
|
+
if (addedLines.length === 0) {
|
|
146
|
+
console.log(`ℹ️ ${absPath} - No changed lines detected.`);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (uncoveredInDiff.length > 0) {
|
|
151
|
+
console.log(`🚨 ${absPath} - Uncovered changed lines: [${uncoveredInDiff.join(', ')}]`);
|
|
152
|
+
} else {
|
|
153
|
+
console.log(`✅ ${absPath} - All changed lines are covered`);
|
|
154
|
+
}
|
|
155
|
+
console.log(` Total changed lines: ${addedLines.length}, Covered: ${coveredInDiff}, Uncovered: ${uncoveredInDiff.length}`);
|
|
156
|
+
console.log(` Coverage for changed lines: ${((coveredInDiff / addedLines.length) * 100).toFixed(2)}%`);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// Cumulative coverage check
|
|
160
|
+
if (totalChanged > 0) {
|
|
161
|
+
const overallCoverage = (((totalChanged - totalUncovered) / totalChanged) * 100).toFixed(2);
|
|
162
|
+
console.log(`\n🔢 Overall coverage for all changed lines: ${overallCoverage}% limit is ${COVERAGE_LIMIT}%`);
|
|
163
|
+
}
|
|
164
|
+
}
|