mo3ta-coverage 1.0.0 โ†’ 1.0.2

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.
Files changed (4) hide show
  1. package/README.md +21 -13
  2. package/index.js +2 -1
  3. package/package.json +1 -1
  4. package/src/cli.js +29 -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 | 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` |
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: Base Branch: origin/main
53
+ Options:
54
+ Base Branch: origin/main
52
55
  LCOV Path: coverage/lcov.info
53
- Coverage Limit: 80
54
- ๐Ÿงช Running tests related to changed files:
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: 5, Covered: 5, Uncovered: 0
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: 85.71% limit is 80%
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mo3ta-coverage",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Run tests and report uncovered changed lines in a PR",
5
5
  "main": "index.js",
6
6
  "bin": {
package/src/cli.js CHANGED
@@ -1,20 +1,29 @@
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
- // log the options for debugging
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
 
@@ -28,28 +37,30 @@ function getChangedFiles(BASE_BRANCH) {
28
37
  function runRelatedTests(files) {
29
38
  if (files.length === 0) {
30
39
  console.log('โœ… No changed source files found.');
31
- return;
40
+
41
+ exit(0);
32
42
  }
33
43
 
34
- console.log('๐Ÿงช Running tests related to changed files:\n', files.join('\n'));
44
+ console.log('๐Ÿงช Running tests related to the changed files:\n', files.join('\n'));
35
45
 
36
46
  // filter files to only those that are JavaScript or TypeScript files
37
47
  files = getJsOnlyFiles(files);
38
48
 
39
49
  if (files.length === 0) {
40
50
  console.log('โœ… No relevant files found for testing.');
41
- return;
51
+
52
+ exit(0);
42
53
  }
43
54
 
44
55
  // log files to be tested
45
- console.log('Files to be tested:', files.join(', '));
56
+ console.log('Files to be tested:', files.join('\n'));
46
57
 
47
58
  const command = `npx jest --findRelatedTests --passWithNoTests --coverage ${files.join(' ')}`;
48
59
  try {
49
60
  execSync(command, { stdio: 'inherit' });
50
61
  } catch (err) {
51
- console.error('โŒ Some tests failed.');
52
- process.exit(1);
62
+ console.error('Error:', err.message || err);
63
+ exit(1);
53
64
  }
54
65
  }
55
66
 
@@ -57,7 +68,7 @@ function getJsOnlyFiles(files) {
57
68
  // Only include source code files, exclude test, config, and json files
58
69
  return files.filter(f => {
59
70
  // Exclude test files
60
- if (f.match(/(\.test|\.spec)\.(js|ts|jsx|tsx)$/)) return false;
71
+ // if (f.match(/(\.test|\.spec)\.(js|ts|jsx|tsx)$/)) return false;
61
72
  // Exclude config files
62
73
  if (f.match(/(jest|babel|webpack|tsconfig|eslint|prettier|rollup|vite|package)\.(js|ts|json)$/)) return false;
63
74
  // Exclude json files
@@ -104,7 +115,7 @@ function parseLcov(LCOV_PATH) {
104
115
  return files;
105
116
  }
106
117
 
107
- function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVERAGE_LIMIT) {
118
+ function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVERAGE_LIMIT, SHOW_COVERED) {
108
119
  if (!fs.existsSync(LCOV_PATH)) {
109
120
  console.error('โŒ lcov.info not found. Make sure coverage ran correctly.');
110
121
  process.exit(1);
@@ -152,7 +163,12 @@ function reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVER
152
163
  } else {
153
164
  console.log(`โœ… ${absPath} - All changed lines are covered`);
154
165
  }
166
+ // Display covered lines if option is enabled
167
+ const coveredLines = addedLines.filter(line => !fileUncovered.has(line));
155
168
  console.log(` Total changed lines: ${addedLines.length}, Covered: ${coveredInDiff}, Uncovered: ${uncoveredInDiff.length}`);
169
+ if (SHOW_COVERED) {
170
+ console.log(` Covered lines: [${coveredLines.join(', ')}]`);
171
+ }
156
172
  console.log(` Coverage for changed lines: ${((coveredInDiff / addedLines.length) * 100).toFixed(2)}%`);
157
173
  });
158
174