mo3ta-coverage 1.0.4 โ 1.0.6
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/.github/workflows/publish.yml +29 -0
- package/README.md +2 -1
- package/index.js +2 -1
- package/package.json +5 -2
- package/src/cli.js +16 -10
- package/src/cli.test.js +76 -7
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
name: Publish Package
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
branches:
|
|
5
|
+
- main
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
steps:
|
|
11
|
+
- name: Checkout code
|
|
12
|
+
uses: actions/checkout@v2
|
|
13
|
+
|
|
14
|
+
- name: Set up Node.js
|
|
15
|
+
uses: actions/setup-node@v2
|
|
16
|
+
with:
|
|
17
|
+
node-version: '16'
|
|
18
|
+
|
|
19
|
+
- name: Install dependencies
|
|
20
|
+
run: npm install
|
|
21
|
+
|
|
22
|
+
# - name: Run tests
|
|
23
|
+
# run: npm test
|
|
24
|
+
|
|
25
|
+
- name: Configure npm authentication
|
|
26
|
+
run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc
|
|
27
|
+
|
|
28
|
+
- name: Publish package
|
|
29
|
+
run: npm publish
|
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
## Usage
|
|
15
15
|
|
|
16
16
|
```sh
|
|
17
|
-
mo3ta-coverage --base origin/main --lcov coverage/lcov.info --limit 80 --showCovered
|
|
17
|
+
mo3ta-coverage --base origin/main --lcov coverage/lcov.info --limit 80 --showCovered --runTestFilesOnly
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
## CLI Options
|
|
@@ -25,6 +25,7 @@ mo3ta-coverage --base origin/main --lcov coverage/lcov.info --limit 80 --showCov
|
|
|
25
25
|
| `--lcov` | Path to lcov.info file generated by test coverage tools | `coverage/lcov.info` |
|
|
26
26
|
| `--limit` | Minimum required coverage percentage for all changed lines | `80` |
|
|
27
27
|
| `--showCovered` | Show covered changed lines in the output | `false` |
|
|
28
|
+
| `--runTestFilesOnly` | Run test files only, skipping other files | `false` |
|
|
28
29
|
|
|
29
30
|
## How It Works
|
|
30
31
|
|
package/index.js
CHANGED
|
@@ -10,7 +10,8 @@ program
|
|
|
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
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
|
+
.option('--showCovered', 'Show covered changed lines in the output', false)
|
|
14
|
+
.option('--runTestFilesOnly', 'Run test files only, skipping other files', false);
|
|
14
15
|
|
|
15
16
|
program.parse(process.argv);
|
|
16
17
|
const options = program.opts();
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mo3ta-coverage",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Run tests and report uncovered changed lines in a PR",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
7
7
|
"mo3ta-coverage": "./index.js"
|
|
8
8
|
},
|
|
9
|
-
"scripts": {
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "jest --coverage",
|
|
11
|
+
"build": "babel index.js -o dist/index.js"
|
|
12
|
+
},
|
|
10
13
|
"author": "Mahmoud Mabrok",
|
|
11
14
|
"license": "MIT",
|
|
12
15
|
"type": "module",
|
package/src/cli.js
CHANGED
|
@@ -18,11 +18,12 @@ export function main(options) {
|
|
|
18
18
|
const LCOV_PATH = options.lcov;
|
|
19
19
|
const COVERAGE_LIMIT = parseFloat(options.limit);
|
|
20
20
|
const SHOW_COVERED = options.showCovered === true || options.showCovered === 'true';
|
|
21
|
+
const RUN_TESTS_ONLY = options.runTestsOnly === true || options.runTestsOnly === 'true';
|
|
21
22
|
|
|
22
|
-
console.log(`Options:\nBase Branch: ${BASE_BRANCH} \nLCOV Path: ${LCOV_PATH} \nCoverage Limit: ${COVERAGE_LIMIT} \nShow Covered Lines: ${SHOW_COVERED}`);
|
|
23
|
+
console.log(`Options:\nBase Branch: ${BASE_BRANCH} \nLCOV Path: ${LCOV_PATH} \nCoverage Limit: ${COVERAGE_LIMIT} \nShow Covered Lines: ${SHOW_COVERED} \nRun Tests Only: ${RUN_TESTS_ONLY}`);
|
|
23
24
|
|
|
24
25
|
const changedFiles = getChangedFiles(BASE_BRANCH);
|
|
25
|
-
runRelatedTests(changedFiles);
|
|
26
|
+
runRelatedTests(changedFiles, RUN_TESTS_ONLY);
|
|
26
27
|
reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVERAGE_LIMIT, SHOW_COVERED);
|
|
27
28
|
|
|
28
29
|
}
|
|
@@ -41,17 +42,22 @@ function getChangedFiles(BASE_BRANCH) {
|
|
|
41
42
|
.filter(f => f.trim().length > 0 && f.match(/\.(js|ts|jsx|tsx)$/));
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
function runRelatedTests(files) {
|
|
45
|
+
function runRelatedTests(files, runTestsOnly) {
|
|
45
46
|
if (files.length === 0) {
|
|
46
47
|
console.log('โ
No changed source files found.');
|
|
47
48
|
|
|
48
49
|
exit(0);
|
|
49
50
|
}
|
|
50
51
|
|
|
52
|
+
let runRelatedFilesArgs = '';
|
|
53
|
+
if (!runTestsOnly) {
|
|
54
|
+
runRelatedFilesArgs = '--findRelatedTests';
|
|
55
|
+
}
|
|
56
|
+
|
|
51
57
|
console.log('๐งช Running tests related to the changed files:\n', files.join('\n'));
|
|
52
58
|
|
|
53
59
|
// filter files to only those that are JavaScript or TypeScript files
|
|
54
|
-
files = getJsOnlyFiles(files);
|
|
60
|
+
files = getJsOnlyFiles(files, runTestsOnly);
|
|
55
61
|
|
|
56
62
|
if (files.length === 0) {
|
|
57
63
|
console.log('โ
No relevant files found for testing.');
|
|
@@ -60,9 +66,9 @@ function runRelatedTests(files) {
|
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
// log files to be tested
|
|
63
|
-
console.log('
|
|
69
|
+
console.log('\nFiles to be tested:', files.join('\n'));
|
|
64
70
|
|
|
65
|
-
const command = `npx jest
|
|
71
|
+
const command = `npx jest ${runRelatedFilesArgs} --passWithNoTests --coverage ${files.join(' ')}`;
|
|
66
72
|
try {
|
|
67
73
|
execSync(command, { stdio: 'inherit' });
|
|
68
74
|
} catch (err) {
|
|
@@ -71,11 +77,11 @@ function runRelatedTests(files) {
|
|
|
71
77
|
}
|
|
72
78
|
}
|
|
73
79
|
|
|
74
|
-
function getJsOnlyFiles(files) {
|
|
80
|
+
function getJsOnlyFiles(files, runTestsOnly) {
|
|
75
81
|
// Only include source code files, exclude test, config, and json files
|
|
76
82
|
return files.filter(f => {
|
|
77
83
|
// Exclude test files
|
|
78
|
-
if (f.match(/(\.test|\.spec)\.(js|ts|jsx|tsx)$/)) return false;
|
|
84
|
+
if (f.match(/(\.test|\.spec)\.(js|ts|jsx|tsx)$/)) return false || runTestsOnly;
|
|
79
85
|
// Exclude config files
|
|
80
86
|
if (f.match(/(jest|babel|webpack|tsconfig|eslint|prettier|rollup|vite|package)\.(js|ts|json)$/)) return false;
|
|
81
87
|
// Exclude json files
|
|
@@ -139,7 +145,7 @@ function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVER
|
|
|
139
145
|
return;
|
|
140
146
|
}
|
|
141
147
|
|
|
142
|
-
console.log('Files with changed lines:', changedFiles.join('
|
|
148
|
+
console.log('Files with changed lines:', changedFiles.join('\n'));
|
|
143
149
|
|
|
144
150
|
let totalChanged = 0;
|
|
145
151
|
let totalUncovered = 0;
|
|
@@ -182,6 +188,6 @@ function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVER
|
|
|
182
188
|
// Cumulative coverage check
|
|
183
189
|
if (totalChanged > 0) {
|
|
184
190
|
const overallCoverage = (((totalChanged - totalUncovered) / totalChanged) * 100).toFixed(2);
|
|
185
|
-
console.log(`\n๐ข Overall coverage for all changed lines: ${overallCoverage}% limit is ${COVERAGE_LIMIT}
|
|
191
|
+
console.log(`\n๐ข Overall coverage for all changed lines: ${overallCoverage}% limit is ${COVERAGE_LIMIT}% with Uncovered: ${totalUncovered} (${totalChanged})`);
|
|
186
192
|
}
|
|
187
193
|
}
|
package/src/cli.test.js
CHANGED
|
@@ -1,10 +1,79 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { main } from './cli';
|
|
2
|
+
import * as child_process from 'child_process';
|
|
3
|
+
import * as fs from 'fs';
|
|
4
|
+
import * as path from 'path';
|
|
2
5
|
|
|
3
|
-
describe('CLI functionality', () => {
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
jest.mock('child_process');
|
|
8
|
+
jest.mock('fs');
|
|
9
|
+
jest.mock('path');
|
|
10
|
+
|
|
11
|
+
describe('main', () => {
|
|
12
|
+
let originalExit;
|
|
13
|
+
let exitMock;
|
|
14
|
+
let consoleLogSpy;
|
|
15
|
+
let consoleErrorSpy;
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
// Mock process.exit
|
|
19
|
+
originalExit = process.exit;
|
|
20
|
+
exitMock = jest.fn();
|
|
21
|
+
process.exit = exitMock;
|
|
22
|
+
|
|
23
|
+
// Mock console
|
|
24
|
+
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
|
|
25
|
+
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
26
|
+
|
|
27
|
+
// Reset mocks
|
|
28
|
+
jest.clearAllMocks();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
process.exit = originalExit;
|
|
33
|
+
consoleLogSpy.mockRestore();
|
|
34
|
+
consoleErrorSpy.mockRestore();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should log options and call getChangedFiles, runRelatedTests, and reportUncoveredChangedLines', () => {
|
|
38
|
+
// Mock git check
|
|
39
|
+
child_process.execSync.mockImplementationOnce(() => 'true');
|
|
40
|
+
// Mock getChangedFiles
|
|
41
|
+
const fakeFiles = ['src/foo.js', 'src/bar.ts'];
|
|
42
|
+
// Mock execSync for getChangedFiles
|
|
43
|
+
child_process.execSync.mockImplementationOnce(() => fakeFiles.join('\n'));
|
|
44
|
+
// Mock execSync for runRelatedTests (jest run)
|
|
45
|
+
child_process.execSync.mockImplementationOnce(() => {});
|
|
46
|
+
// Mock fs.existsSync for lcov
|
|
47
|
+
fs.existsSync.mockReturnValue(true);
|
|
48
|
+
// Mock fs.readFileSync for lcov parsing
|
|
49
|
+
fs.readFileSync.mockReturnValue(
|
|
50
|
+
'SF:/abs/path/src/foo.js\nDA:1,0\nDA:2,1\nend_of_record\nSF:/abs/path/src/bar.ts\nDA:3,0\nend_of_record\n'
|
|
51
|
+
);
|
|
52
|
+
// Mock path.resolve to identity for simplicity
|
|
53
|
+
path.resolve.mockImplementation(x => '/abs/path/' + x);
|
|
54
|
+
|
|
55
|
+
// Mock getChangedLines to return some lines
|
|
56
|
+
jest.spyOn(child_process, 'execSync').mockImplementation((cmd) => {
|
|
57
|
+
if (cmd.includes('git diff')) {
|
|
58
|
+
return Buffer.from('@@ -0,0 +1,2 @@\n+foo\n+bar\n');
|
|
59
|
+
}
|
|
60
|
+
if (cmd.includes('git rev-parse')) {
|
|
61
|
+
return Buffer.from('true');
|
|
62
|
+
}
|
|
63
|
+
if (cmd.includes('git log') || cmd.includes('git diff --name-only')) {
|
|
64
|
+
return Buffer.from(fakeFiles.join('\n'));
|
|
65
|
+
}
|
|
66
|
+
return Buffer.from('');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
main({ base: 'main', lcov: 'lcov.info', limit: '80', showCovered: true });
|
|
70
|
+
|
|
71
|
+
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Options:'));
|
|
72
|
+
// expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Base Branch: main'));
|
|
73
|
+
// expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('LCOV Path: lcov.info'));
|
|
74
|
+
// expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Coverage Limit: 80'));
|
|
75
|
+
// expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Show Covered Lines: true'));
|
|
76
|
+
// expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Files with changed lines:'));
|
|
77
|
+
// expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Overall coverage for all changed lines:'));
|
|
9
78
|
});
|
|
10
|
-
});
|
|
79
|
+
});
|