mo3ta-coverage 1.0.0 โ 1.0.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/README.md +21 -13
- package/index.js +2 -1
- package/package.json +1 -1
- package/src/cli.js +36 -13
package/README.md
CHANGED
|
@@ -8,21 +8,23 @@
|
|
|
8
8
|
- Runs related Jest tests for changed files
|
|
9
9
|
- Parses `lcov.info` for coverage data
|
|
10
10
|
- Reports uncovered changed lines
|
|
11
|
+
- Optionally displays covered changed lines
|
|
11
12
|
- Checks overall coverage against a user-defined limit
|
|
12
13
|
|
|
13
14
|
## Usage
|
|
14
15
|
|
|
15
16
|
```sh
|
|
16
|
-
mo3ta-coverage --base origin/main --lcov coverage/lcov.info --limit 80
|
|
17
|
+
mo3ta-coverage --base origin/main --lcov coverage/lcov.info --limit 80 --showCovered
|
|
17
18
|
```
|
|
18
19
|
|
|
19
20
|
## CLI Options
|
|
20
21
|
|
|
21
|
-
| Option
|
|
22
|
-
|
|
23
|
-
| `--base`
|
|
24
|
-
| `--lcov`
|
|
25
|
-
| `--limit`
|
|
22
|
+
| Option | Description | Default |
|
|
23
|
+
|------------------|------------------------------------------------------------------|----------------------|
|
|
24
|
+
| `--base` | Base branch to compare against | `origin/main` |
|
|
25
|
+
| `--lcov` | Path to lcov.info file generated by test coverage tools | `coverage/lcov.info` |
|
|
26
|
+
| `--limit` | Minimum required coverage percentage for all changed lines | `80` |
|
|
27
|
+
| `--showCovered` | Show covered changed lines in the output | `false` |
|
|
26
28
|
|
|
27
29
|
## How It Works
|
|
28
30
|
|
|
@@ -30,7 +32,7 @@ mo3ta-coverage --base origin/main --lcov coverage/lcov.info --limit 80
|
|
|
30
32
|
2. **Get changed files**: Uses `git diff --name-only <base>` to find modified JS/TS files ([`src/cli.js`](src/cli.js)).
|
|
31
33
|
3. **Run related tests**: Executes Jest for changed files with `--findRelatedTests` ([`src/cli.js`](src/cli.js)).
|
|
32
34
|
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)).
|
|
35
|
+
5. **Report**: Prints uncovered changed lines, optionally covered lines, and overall coverage ([`src/cli.js`](src/cli.js)).
|
|
34
36
|
6. **Enforce limit**: Exits with error if coverage is below the specified threshold.
|
|
35
37
|
|
|
36
38
|
## Architecture Diagram
|
|
@@ -41,24 +43,30 @@ flowchart TD
|
|
|
41
43
|
B --> C[Get changed files via git diff in cli.js]
|
|
42
44
|
C --> D[Run related Jest tests in cli.js]
|
|
43
45
|
D --> E[Parse lcov.info for coverage in cli.js]
|
|
44
|
-
E --> F[Report uncovered changed lines in cli.js]
|
|
46
|
+
E --> F[Report uncovered & covered changed lines in cli.js]
|
|
45
47
|
F --> G[Check overall coverage vs limit in cli.js]
|
|
46
48
|
```
|
|
47
49
|
|
|
48
50
|
## Example Output
|
|
49
51
|
|
|
50
52
|
```
|
|
51
|
-
Options:
|
|
53
|
+
Options:
|
|
54
|
+
Base Branch: origin/main
|
|
52
55
|
LCOV Path: coverage/lcov.info
|
|
53
|
-
Coverage Limit: 80
|
|
54
|
-
|
|
56
|
+
Coverage Limit: 80
|
|
57
|
+
Show Covered Lines: true
|
|
58
|
+
๐งช Running tests related to the changed files:
|
|
55
59
|
src/foo.js
|
|
56
60
|
src/bar.js
|
|
57
61
|
๐จ /abs/path/src/foo.js - Uncovered changed lines: [12, 15]
|
|
62
|
+
Total changed lines: 5, Covered: 3, Uncovered: 2
|
|
63
|
+
Covered lines: [10, 11, 13]
|
|
64
|
+
Coverage for changed lines: 60.00%
|
|
58
65
|
โ
/abs/path/src/bar.js - All changed lines are covered
|
|
59
|
-
Total changed lines:
|
|
66
|
+
Total changed lines: 4, Covered: 4, Uncovered: 0
|
|
67
|
+
Covered lines: [20, 21, 22, 23]
|
|
60
68
|
Coverage for changed lines: 100.00%
|
|
61
|
-
๐ข Overall coverage for all changed lines:
|
|
69
|
+
๐ข Overall coverage for all changed lines: 80.00% limit is 80%
|
|
62
70
|
```
|
|
63
71
|
|
|
64
72
|
## License
|
package/index.js
CHANGED
|
@@ -9,7 +9,8 @@ program
|
|
|
9
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
10
|
.option('--base <branch>', 'Base branch to compare against (e.g., origin/main)', 'origin/main')
|
|
11
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')
|
|
12
|
+
.option('--limit <percent>', 'Minimum required coverage percentage for all changed lines (e.g., 80)', '80')
|
|
13
|
+
.option('--showCovered', 'Show covered changed lines in the output', false);
|
|
13
14
|
|
|
14
15
|
program.parse(process.argv);
|
|
15
16
|
const options = program.opts();
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -1,25 +1,41 @@
|
|
|
1
|
-
|
|
2
1
|
import { execSync } from 'child_process';
|
|
3
2
|
import fs from 'fs';
|
|
4
3
|
import path from 'path';
|
|
4
|
+
import { exit } from 'process';
|
|
5
5
|
|
|
6
6
|
export function main(options) {
|
|
7
|
+
// Check if current directory is a git repository
|
|
8
|
+
try {
|
|
9
|
+
execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
|
|
10
|
+
} catch (err) {
|
|
11
|
+
console.error(`โ Not a git repository. Please run this tool inside a git repo. ${err.message}`);
|
|
12
|
+
|
|
13
|
+
exit(1);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
7
17
|
const BASE_BRANCH = options.base;
|
|
8
18
|
const LCOV_PATH = options.lcov;
|
|
9
19
|
const COVERAGE_LIMIT = parseFloat(options.limit);
|
|
20
|
+
const SHOW_COVERED = options.showCovered === true || options.showCovered === 'true';
|
|
10
21
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
console.log(`Options: Base Branch: ${BASE_BRANCH} \nLCOV Path: ${LCOV_PATH} \nCoverage Limit: ${COVERAGE_LIMIT}`);
|
|
22
|
+
console.log(`Options:\nBase Branch: ${BASE_BRANCH} \nLCOV Path: ${LCOV_PATH} \nCoverage Limit: ${COVERAGE_LIMIT} \nShow Covered Lines: ${SHOW_COVERED}`);
|
|
14
23
|
|
|
15
24
|
const changedFiles = getChangedFiles(BASE_BRANCH);
|
|
16
25
|
runRelatedTests(changedFiles);
|
|
17
|
-
reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVERAGE_LIMIT);
|
|
26
|
+
reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVERAGE_LIMIT, SHOW_COVERED);
|
|
18
27
|
|
|
19
28
|
}
|
|
20
29
|
|
|
21
30
|
function getChangedFiles(BASE_BRANCH) {
|
|
22
|
-
const
|
|
31
|
+
const command = `(
|
|
32
|
+
git log --pretty=format: --name-only --diff-filter=AM --author="$(git config user.name)" $(git merge-base HEAD ${BASE_BRANCH})..HEAD
|
|
33
|
+
git diff --name-only --diff-filter=AM
|
|
34
|
+
git diff --cached --name-only --diff-filter=AM
|
|
35
|
+
) | sort -u`;
|
|
36
|
+
|
|
37
|
+
const output = execSync(command).toString();
|
|
38
|
+
|
|
23
39
|
return output
|
|
24
40
|
.split('\n')
|
|
25
41
|
.filter(f => f.trim().length > 0 && f.match(/\.(js|ts|jsx|tsx)$/));
|
|
@@ -28,28 +44,30 @@ function getChangedFiles(BASE_BRANCH) {
|
|
|
28
44
|
function runRelatedTests(files) {
|
|
29
45
|
if (files.length === 0) {
|
|
30
46
|
console.log('โ
No changed source files found.');
|
|
31
|
-
|
|
47
|
+
|
|
48
|
+
exit(0);
|
|
32
49
|
}
|
|
33
50
|
|
|
34
|
-
console.log('๐งช Running tests related to changed files:\n', files.join('\n'));
|
|
51
|
+
console.log('๐งช Running tests related to the changed files:\n', files.join('\n'));
|
|
35
52
|
|
|
36
53
|
// filter files to only those that are JavaScript or TypeScript files
|
|
37
54
|
files = getJsOnlyFiles(files);
|
|
38
55
|
|
|
39
56
|
if (files.length === 0) {
|
|
40
57
|
console.log('โ
No relevant files found for testing.');
|
|
41
|
-
|
|
58
|
+
|
|
59
|
+
exit(0);
|
|
42
60
|
}
|
|
43
61
|
|
|
44
62
|
// log files to be tested
|
|
45
|
-
console.log('Files to be tested:', files.join('
|
|
63
|
+
console.log('Files to be tested:', files.join('\n'));
|
|
46
64
|
|
|
47
65
|
const command = `npx jest --findRelatedTests --passWithNoTests --coverage ${files.join(' ')}`;
|
|
48
66
|
try {
|
|
49
67
|
execSync(command, { stdio: 'inherit' });
|
|
50
68
|
} catch (err) {
|
|
51
|
-
console.error('
|
|
52
|
-
|
|
69
|
+
console.error('Error:', err.message || err);
|
|
70
|
+
exit(1);
|
|
53
71
|
}
|
|
54
72
|
}
|
|
55
73
|
|
|
@@ -104,7 +122,7 @@ function parseLcov(LCOV_PATH) {
|
|
|
104
122
|
return files;
|
|
105
123
|
}
|
|
106
124
|
|
|
107
|
-
function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVERAGE_LIMIT) {
|
|
125
|
+
function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVERAGE_LIMIT, SHOW_COVERED) {
|
|
108
126
|
if (!fs.existsSync(LCOV_PATH)) {
|
|
109
127
|
console.error('โ lcov.info not found. Make sure coverage ran correctly.');
|
|
110
128
|
process.exit(1);
|
|
@@ -152,7 +170,12 @@ function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVER
|
|
|
152
170
|
} else {
|
|
153
171
|
console.log(`โ
${absPath} - All changed lines are covered`);
|
|
154
172
|
}
|
|
173
|
+
// Display covered lines if option is enabled
|
|
174
|
+
const coveredLines = addedLines.filter(line => !fileUncovered.has(line));
|
|
155
175
|
console.log(` Total changed lines: ${addedLines.length}, Covered: ${coveredInDiff}, Uncovered: ${uncoveredInDiff.length}`);
|
|
176
|
+
if (SHOW_COVERED) {
|
|
177
|
+
console.log(` Covered lines: [${coveredLines.join(', ')}]`);
|
|
178
|
+
}
|
|
156
179
|
console.log(` Coverage for changed lines: ${((coveredInDiff / addedLines.length) * 100).toFixed(2)}%`);
|
|
157
180
|
});
|
|
158
181
|
|