mo3ta-coverage 1.0.18 → 1.0.21
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/AGENT.md +144 -0
- package/README.md +54 -7
- package/RECOMMENDED_USAGE.md +20 -5
- package/VIDEO_PLAN.md +316 -0
- package/docs/poster.png +0 -0
- package/docs/poster.svg +131 -0
- package/index.js +32 -1
- package/package.json +1 -1
- package/src/cli.js +150 -89
- package/src/cli.test.js +127 -15
- package/src/fileUtils.js +241 -9
- package/src/fileUtils.test.js +98 -1
package/docs/poster.svg
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="1600" height="900" viewBox="0 0 1600 900" role="img" aria-labelledby="title desc">
|
|
2
|
+
<title id="title">mo3ta-coverage — modes and features</title>
|
|
3
|
+
<desc id="desc">Poster listing the fast, smart, and full execution modes and the main features of mo3ta-coverage.</desc>
|
|
4
|
+
|
|
5
|
+
<defs>
|
|
6
|
+
<linearGradient id="bgGlow" x1="0" y1="0" x2="0" y2="1">
|
|
7
|
+
<stop offset="0%" stop-color="#10131a"/>
|
|
8
|
+
<stop offset="100%" stop-color="#0f1115"/>
|
|
9
|
+
</linearGradient>
|
|
10
|
+
<radialGradient id="topGlow" cx="50%" cy="0%" r="70%">
|
|
11
|
+
<stop offset="0%" stop-color="#60a5fa" stop-opacity="0.20"/>
|
|
12
|
+
<stop offset="100%" stop-color="#60a5fa" stop-opacity="0"/>
|
|
13
|
+
</radialGradient>
|
|
14
|
+
<radialGradient id="greenGlow" cx="100%" cy="100%" r="60%">
|
|
15
|
+
<stop offset="0%" stop-color="#22c55e" stop-opacity="0.10"/>
|
|
16
|
+
<stop offset="100%" stop-color="#22c55e" stop-opacity="0"/>
|
|
17
|
+
</radialGradient>
|
|
18
|
+
<pattern id="grid" width="52" height="52" patternUnits="userSpaceOnUse">
|
|
19
|
+
<path d="M52 0H0V52" fill="none" stroke="#ffffff" stroke-opacity="0.035" stroke-width="1"/>
|
|
20
|
+
</pattern>
|
|
21
|
+
<filter id="cardShadow" x="-20%" y="-20%" width="140%" height="160%">
|
|
22
|
+
<feDropShadow dx="0" dy="18" stdDeviation="24" flood-color="#000000" flood-opacity="0.45"/>
|
|
23
|
+
</filter>
|
|
24
|
+
<style>
|
|
25
|
+
.sans { font-family: Inter, -apple-system, "Helvetica Neue", Arial, sans-serif; }
|
|
26
|
+
.mono { font-family: "JetBrains Mono", "SF Mono", Menlo, Consolas, monospace; }
|
|
27
|
+
.muted { fill: #9aa0a6; }
|
|
28
|
+
.bright { fill: #f4f7fb; }
|
|
29
|
+
.accent { fill: #60a5fa; }
|
|
30
|
+
</style>
|
|
31
|
+
</defs>
|
|
32
|
+
|
|
33
|
+
<!-- background -->
|
|
34
|
+
<rect width="1600" height="900" fill="url(#bgGlow)"/>
|
|
35
|
+
<rect width="1600" height="900" fill="url(#grid)"/>
|
|
36
|
+
<rect width="1600" height="900" fill="url(#topGlow)"/>
|
|
37
|
+
<rect width="1600" height="900" fill="url(#greenGlow)"/>
|
|
38
|
+
|
|
39
|
+
<!-- header -->
|
|
40
|
+
<g class="sans">
|
|
41
|
+
<g transform="translate(72, 78)">
|
|
42
|
+
<rect x="0" y="-26" width="286" height="46" rx="23" fill="#60a5fa" fill-opacity="0.09" stroke="#60a5fa" stroke-opacity="0.42"/>
|
|
43
|
+
<circle cx="28" cy="-3" r="5" fill="#22c55e"/>
|
|
44
|
+
<text x="46" y="4" class="mono" font-size="15" letter-spacing="3.2" fill="#60a5fa">PR COVERAGE · SMART MODE</text>
|
|
45
|
+
</g>
|
|
46
|
+
|
|
47
|
+
<text x="72" y="188" class="bright" font-size="76" font-weight="800" letter-spacing="-3.2">mo3ta-coverage</text>
|
|
48
|
+
<text x="74" y="238" class="muted" font-size="28" letter-spacing="-0.4">Changed code. Related specs. Real coverage.</text>
|
|
49
|
+
</g>
|
|
50
|
+
|
|
51
|
+
<!-- modes -->
|
|
52
|
+
<g class="sans">
|
|
53
|
+
<text x="72" y="308" class="mono" font-size="15" letter-spacing="3.4" fill="#60a5fa">MODES</text>
|
|
54
|
+
|
|
55
|
+
<!-- fast -->
|
|
56
|
+
<g filter="url(#cardShadow)">
|
|
57
|
+
<rect x="72" y="330" width="460" height="196" rx="18" fill="#1a1c22" stroke="#2a2d35"/>
|
|
58
|
+
</g>
|
|
59
|
+
<text x="102" y="382" class="mono" font-size="15" letter-spacing="2.4" fill="#38bdf8">FAST</text>
|
|
60
|
+
<text x="102" y="425" class="bright" font-size="26" font-weight="700">Direct mapped tests</text>
|
|
61
|
+
<text x="102" y="461" class="muted" font-size="18">Naming convention + direct import scan.</text>
|
|
62
|
+
<text x="102" y="489" class="muted" font-size="18">No dependency traversal.</text>
|
|
63
|
+
<text x="102" y="518" class="mono" font-size="15" fill="#7dd3fc">--mode fast</text>
|
|
64
|
+
|
|
65
|
+
<!-- smart -->
|
|
66
|
+
<g filter="url(#cardShadow)">
|
|
67
|
+
<rect x="562" y="330" width="460" height="196" rx="18" fill="#1a1c22" stroke="#60a5fa" stroke-opacity="0.85"/>
|
|
68
|
+
</g>
|
|
69
|
+
<rect x="878" y="352" width="118" height="30" rx="15" fill="#60a5fa" fill-opacity="0.14" stroke="#60a5fa" stroke-opacity="0.45"/>
|
|
70
|
+
<text x="895" y="372" class="mono" font-size="12" letter-spacing="1.6" fill="#60a5fa">DEFAULT</text>
|
|
71
|
+
<text x="592" y="382" class="mono" font-size="15" letter-spacing="2.4" fill="#60a5fa">SMART</text>
|
|
72
|
+
<text x="592" y="425" class="bright" font-size="26" font-weight="700">Naming + reverse imports</text>
|
|
73
|
+
<text x="592" y="461" class="muted" font-size="18">Finds specs through barrels and wrappers.</text>
|
|
74
|
+
<text x="592" y="489" class="muted" font-size="18">Depth-limited by --depth (default 2).</text>
|
|
75
|
+
<text x="592" y="518" class="mono" font-size="15" fill="#93c5fd">--mode smart --depth 2</text>
|
|
76
|
+
|
|
77
|
+
<!-- full -->
|
|
78
|
+
<g filter="url(#cardShadow)">
|
|
79
|
+
<rect x="1052" y="330" width="460" height="196" rx="18" fill="#1a1c22" stroke="#2a2d35"/>
|
|
80
|
+
</g>
|
|
81
|
+
<text x="1082" y="382" class="mono" font-size="15" letter-spacing="2.4" fill="#a78bfa">FULL</text>
|
|
82
|
+
<text x="1082" y="425" class="bright" font-size="26" font-weight="700">Jest finds related tests</text>
|
|
83
|
+
<text x="1082" y="461" class="muted" font-size="18">Passes changed files to Jest directly.</text>
|
|
84
|
+
<text x="1082" y="489" class="muted" font-size="18">Unbounded --findRelatedTests walk.</text>
|
|
85
|
+
<text x="1082" y="518" class="mono" font-size="15" fill="#c4b5fd">--mode full</text>
|
|
86
|
+
</g>
|
|
87
|
+
|
|
88
|
+
<!-- features -->
|
|
89
|
+
<g class="sans">
|
|
90
|
+
<text x="72" y="598" class="mono" font-size="15" letter-spacing="3.4" fill="#60a5fa">FEATURES</text>
|
|
91
|
+
|
|
92
|
+
<!-- column 1 -->
|
|
93
|
+
<g transform="translate(72, 638)">
|
|
94
|
+
<circle cx="6" cy="-6" r="4" fill="#22c55e"/>
|
|
95
|
+
<text x="26" y="0" font-size="21" class="bright">Detects changed JS/TS files for all authors</text>
|
|
96
|
+
|
|
97
|
+
<circle cx="6" cy="42" r="4" fill="#22c55e"/>
|
|
98
|
+
<text x="26" y="48" font-size="21" class="bright">Runs only the tests related to the change</text>
|
|
99
|
+
|
|
100
|
+
<circle cx="6" cy="90" r="4" fill="#22c55e"/>
|
|
101
|
+
<text x="26" y="96" font-size="21" class="bright">Maps specs by name and by import graph</text>
|
|
102
|
+
|
|
103
|
+
<circle cx="6" cy="138" r="4" fill="#22c55e"/>
|
|
104
|
+
<text x="26" y="144" font-size="21" class="bright">Scans selected files with --files</text>
|
|
105
|
+
</g>
|
|
106
|
+
|
|
107
|
+
<!-- column 2 -->
|
|
108
|
+
<g transform="translate(876, 638)">
|
|
109
|
+
<circle cx="6" cy="-6" r="4" fill="#22c55e"/>
|
|
110
|
+
<text x="26" y="0" font-size="21" class="bright">Checks changed-line coverage from lcov.info</text>
|
|
111
|
+
|
|
112
|
+
<circle cx="6" cy="42" r="4" fill="#22c55e"/>
|
|
113
|
+
<text x="26" y="48" font-size="21" class="bright">Fails below the --limit threshold</text>
|
|
114
|
+
|
|
115
|
+
<circle cx="6" cy="90" r="4" fill="#22c55e"/>
|
|
116
|
+
<text x="26" y="96" font-size="21" class="bright">Reports every uncovered changed line</text>
|
|
117
|
+
|
|
118
|
+
<circle cx="6" cy="138" r="4" fill="#22c55e"/>
|
|
119
|
+
<text x="26" y="144" font-size="21" class="bright">Works locally and in CI</text>
|
|
120
|
+
</g>
|
|
121
|
+
</g>
|
|
122
|
+
|
|
123
|
+
<!-- footer command -->
|
|
124
|
+
<rect x="72" y="790" width="1456" height="62" rx="14" fill="#14161c" stroke="#2a2d35"/>
|
|
125
|
+
<text x="102" y="829" class="mono" font-size="19" fill="#9aa0a6">$</text>
|
|
126
|
+
<text x="124" y="829" class="mono" font-size="19" fill="#e6e6e6">npx mo3ta-coverage</text>
|
|
127
|
+
<text x="334" y="829" class="mono" font-size="19" fill="#60a5fa">--mode smart</text>
|
|
128
|
+
<text x="482" y="829" class="mono" font-size="19" fill="#9aa0a6">--base origin/main</text>
|
|
129
|
+
<text x="706" y="829" class="mono" font-size="19" fill="#9aa0a6">--limit 80</text>
|
|
130
|
+
<text x="1476" y="829" class="mono" font-size="14" fill="#22c55e" text-anchor="end">changed lines only</text>
|
|
131
|
+
</svg>
|
package/index.js
CHANGED
|
@@ -35,6 +35,9 @@ program
|
|
|
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
37
|
.option('--mode <mode>', 'Execution mode: fast, smart, or full', 'smart')
|
|
38
|
+
.option('--depth <number>', 'Smart mode max reverse-dependency depth (1 = direct importers, 2 = also importers of importers)', '2')
|
|
39
|
+
.option('--files <files...>', 'Scan only these files instead of detecting changed files (space- or comma-separated)')
|
|
40
|
+
.option('--filesFrom <path>', 'Read the list of files to scan from a newline-separated file')
|
|
38
41
|
.enablePositionalOptions(); // used to allow using same options in subcommands
|
|
39
42
|
|
|
40
43
|
|
|
@@ -43,6 +46,7 @@ program
|
|
|
43
46
|
.description('Set or get configuration options')
|
|
44
47
|
.option('--base <branch>', 'Set default base branch')
|
|
45
48
|
.option('--mode <mode>', 'Set default mode (fast/smart/full)')
|
|
49
|
+
.option('--depth <number>', 'Set default smart-mode depth (1-10)')
|
|
46
50
|
.action((opts) => {
|
|
47
51
|
let config = readConfig();
|
|
48
52
|
|
|
@@ -66,6 +70,17 @@ program
|
|
|
66
70
|
updated = true;
|
|
67
71
|
console.log(`Default mode set to: ${config.mode}`);
|
|
68
72
|
}
|
|
73
|
+
|
|
74
|
+
if (opts.depth !== undefined) {
|
|
75
|
+
const normalizedDepth = Number.parseInt(opts.depth, 10);
|
|
76
|
+
if (!Number.isInteger(normalizedDepth) || normalizedDepth < 1 || normalizedDepth > 10) {
|
|
77
|
+
console.error(`Invalid depth: ${opts.depth}. Expected an integer between 1 and 10.`);
|
|
78
|
+
process.exit(1);
|
|
79
|
+
}
|
|
80
|
+
config.depth = normalizedDepth;
|
|
81
|
+
updated = true;
|
|
82
|
+
console.log(`Default smart-mode depth set to: ${config.depth}`);
|
|
83
|
+
}
|
|
69
84
|
if (updated) {
|
|
70
85
|
writeConfig(config);
|
|
71
86
|
} else {
|
|
@@ -79,6 +94,9 @@ program
|
|
|
79
94
|
program.action((opts) => {
|
|
80
95
|
const config = readConfig();
|
|
81
96
|
const options = program.opts();
|
|
97
|
+
|
|
98
|
+
const modeFromCli = program.getOptionValueSource('mode') === 'cli';
|
|
99
|
+
const depthFromCli = program.getOptionValueSource('depth') === 'cli';
|
|
82
100
|
|
|
83
101
|
// Use config base if not provided in CLI
|
|
84
102
|
if (!options.base && config.base) {
|
|
@@ -87,7 +105,7 @@ program.action((opts) => {
|
|
|
87
105
|
options.base = 'origin/main';
|
|
88
106
|
}
|
|
89
107
|
|
|
90
|
-
if (!
|
|
108
|
+
if (!modeFromCli && config.mode) {
|
|
91
109
|
options.mode = config.mode;
|
|
92
110
|
}
|
|
93
111
|
|
|
@@ -97,6 +115,19 @@ program.action((opts) => {
|
|
|
97
115
|
process.exit(1);
|
|
98
116
|
}
|
|
99
117
|
|
|
118
|
+
if (options.mode === 'smart') {
|
|
119
|
+
if (!depthFromCli && config.depth !== undefined) {
|
|
120
|
+
options.depth = String(config.depth);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const depth = Number.parseInt(options.depth, 10);
|
|
124
|
+
if (!Number.isInteger(depth) || depth < 1 || depth > 10) {
|
|
125
|
+
console.error(`Invalid depth: ${options.depth}. Expected an integer between 1 and 10.`);
|
|
126
|
+
process.exit(1);
|
|
127
|
+
}
|
|
128
|
+
options.depth = depth;
|
|
129
|
+
}
|
|
130
|
+
|
|
100
131
|
console.log(`Final options: ${JSON.stringify(options, null, 2)}`);
|
|
101
132
|
|
|
102
133
|
main(options);
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -3,7 +3,13 @@ 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 {
|
|
6
|
+
import {
|
|
7
|
+
getJsOnlyFiles,
|
|
8
|
+
separateSourceAndTestFiles,
|
|
9
|
+
findTestFilesForSource,
|
|
10
|
+
findTestFilesByImport,
|
|
11
|
+
findRelatedTestFilesBySource
|
|
12
|
+
} from './fileUtils.js';
|
|
7
13
|
|
|
8
14
|
export function main(options) {
|
|
9
15
|
try {
|
|
@@ -15,11 +21,23 @@ export function main(options) {
|
|
|
15
21
|
const COVERAGE_LIMIT = parseFloat(options.limit);
|
|
16
22
|
const SHOW_COVERED = options.showCovered === true || options.showCovered === 'true';
|
|
17
23
|
const MODE = options.mode;
|
|
24
|
+
const DEPTH = Number.parseInt(options.depth, 10) || 2;
|
|
18
25
|
|
|
19
|
-
console.log(`Options:\nBase Branch: ${BASE_BRANCH} \nLCOV Path: ${LCOV_PATH} \nCoverage Limit: ${COVERAGE_LIMIT} \nShow Covered Lines: ${SHOW_COVERED} \nMode: ${MODE}`);
|
|
26
|
+
console.log(`Options:\nBase Branch: ${BASE_BRANCH} \nLCOV Path: ${LCOV_PATH} \nCoverage Limit: ${COVERAGE_LIMIT} \nShow Covered Lines: ${SHOW_COVERED} \nMode: ${MODE} \nDepth: ${DEPTH}`);
|
|
20
27
|
|
|
21
|
-
const
|
|
22
|
-
|
|
28
|
+
const explicitFiles = getExplicitFiles(options);
|
|
29
|
+
let changedFiles;
|
|
30
|
+
|
|
31
|
+
if (explicitFiles.length > 0) {
|
|
32
|
+
// A base branch is still needed to compute the changed lines of each file.
|
|
33
|
+
verifyBaseBranchExists(BASE_BRANCH);
|
|
34
|
+
console.log(`Scanning ${explicitFiles.length} explicitly provided file(s) (git changed-file detection skipped):\n${explicitFiles.join('\n')}`);
|
|
35
|
+
changedFiles = explicitFiles;
|
|
36
|
+
} else {
|
|
37
|
+
changedFiles = getChangedFiles(BASE_BRANCH);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
runRelatedTests(changedFiles, MODE, DEPTH);
|
|
23
41
|
reportUncoveredChangedLines(changedFiles, LCOV_PATH, BASE_BRANCH, COVERAGE_LIMIT, SHOW_COVERED);
|
|
24
42
|
} catch (error) {
|
|
25
43
|
console.error('❌ An unexpected error occurred:', error.message || error);
|
|
@@ -28,17 +46,83 @@ export function main(options) {
|
|
|
28
46
|
}
|
|
29
47
|
|
|
30
48
|
|
|
31
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Normalizes a user-provided file list into repo-relative paths.
|
|
51
|
+
* Accepts an array or a comma-separated string, trims entries, removes
|
|
52
|
+
* duplicates, converts absolute paths under `root` to relative ones, and
|
|
53
|
+
* drops dependency/build artifacts.
|
|
54
|
+
* @param {string|string[]} input
|
|
55
|
+
* @param {string} [root]
|
|
56
|
+
* @returns {string[]}
|
|
57
|
+
*/
|
|
58
|
+
export function normalizeExplicitFiles(input, root = process.cwd()) {
|
|
59
|
+
const values = Array.isArray(input) ? input : [input];
|
|
60
|
+
|
|
61
|
+
const files = values
|
|
62
|
+
.flatMap(value => String(value ?? '').split(','))
|
|
63
|
+
.map(file => file.trim())
|
|
64
|
+
.filter(Boolean)
|
|
65
|
+
.map(file => {
|
|
66
|
+
const absolute = path.resolve(root, file);
|
|
67
|
+
const relative = path.relative(root, absolute);
|
|
68
|
+
// Keep paths outside the repo root as provided.
|
|
69
|
+
if (relative.startsWith('..') || path.isAbsolute(relative)) return file;
|
|
70
|
+
return relative;
|
|
71
|
+
})
|
|
72
|
+
.map(file => file.replace(/\\/g, '/'))
|
|
73
|
+
.filter(file => !file.match(/(^|\/)(node_modules|coverage|dist|build)\/|(^|\/)\.git\//));
|
|
74
|
+
|
|
75
|
+
return Array.from(new Set(files));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Reads a newline-separated list of files (for example the output of
|
|
80
|
+
* `git diff --name-only`) from disk.
|
|
81
|
+
* @param {string} listPath
|
|
82
|
+
* @param {string} [root]
|
|
83
|
+
* @returns {string[]}
|
|
84
|
+
*/
|
|
85
|
+
export function readExplicitFilesFromFile(listPath, root = process.cwd()) {
|
|
86
|
+
const absolute = path.resolve(root, listPath);
|
|
87
|
+
const content = fs.readFileSync(absolute, 'utf8');
|
|
88
|
+
return normalizeExplicitFiles(content.split(/\r?\n/), root);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Resolves the files to scan from CLI options. Returns an empty array when
|
|
93
|
+
* the user did not provide an explicit list.
|
|
94
|
+
* @param {{files?: string|string[], filesFrom?: string}} options
|
|
95
|
+
* @returns {string[]}
|
|
96
|
+
*/
|
|
97
|
+
export function getExplicitFiles(options = {}) {
|
|
98
|
+
const files = [];
|
|
99
|
+
|
|
100
|
+
if (options.files !== undefined && options.files !== null && options.files !== false) {
|
|
101
|
+
files.push(...normalizeExplicitFiles(options.files));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (options.filesFrom) {
|
|
105
|
+
files.push(...readExplicitFilesFromFile(options.filesFrom));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return Array.from(new Set(files));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function getChangedFiles(BASE_BRANCH) {
|
|
32
112
|
// Check if BASE_BRANCH exists
|
|
33
113
|
verifyBaseBranchExists(BASE_BRANCH);
|
|
34
114
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
git diff --name-only --diff-filter=AM
|
|
38
|
-
git diff --cached --name-only --diff-filter=AM
|
|
39
|
-
) | sort -u`;
|
|
40
|
-
|
|
115
|
+
// Compare the merge base with HEAD so every file changed on this branch is
|
|
116
|
+
// included, regardless of which author committed it.
|
|
41
117
|
try {
|
|
118
|
+
const mergeBase = execSync(`git merge-base HEAD ${BASE_BRANCH}`).toString().trim();
|
|
119
|
+
|
|
120
|
+
const command = `(
|
|
121
|
+
git diff --name-only --diff-filter=AM ${mergeBase}..HEAD
|
|
122
|
+
git diff --name-only --diff-filter=AM
|
|
123
|
+
git diff --cached --name-only --diff-filter=AM
|
|
124
|
+
) | sort -u`;
|
|
125
|
+
|
|
42
126
|
const output = execSync(command).toString();
|
|
43
127
|
const changedFiles = output
|
|
44
128
|
.split('\n')
|
|
@@ -53,15 +137,18 @@ function getChangedFiles(BASE_BRANCH) {
|
|
|
53
137
|
}
|
|
54
138
|
}
|
|
55
139
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return changedTestFiles;
|
|
59
|
-
}
|
|
140
|
+
function runJest(command) {
|
|
141
|
+
console.log(` Jest command: ${command}`);
|
|
60
142
|
|
|
61
|
-
|
|
143
|
+
try {
|
|
144
|
+
execSync(command, { stdio: 'inherit' });
|
|
145
|
+
} catch (err) {
|
|
146
|
+
console.error('❌ Tests failed:', err.message || err);
|
|
147
|
+
exit(1);
|
|
148
|
+
}
|
|
62
149
|
}
|
|
63
150
|
|
|
64
|
-
function runRelatedTests(files, mode) {
|
|
151
|
+
function runRelatedTests(files, mode, depth = 2) {
|
|
65
152
|
if (files.length === 0) {
|
|
66
153
|
console.log('✅ No changed source files found.');
|
|
67
154
|
exit(0);
|
|
@@ -84,32 +171,51 @@ function runRelatedTests(files, mode) {
|
|
|
84
171
|
testFiles.forEach(file => console.log(` - ${file}`));
|
|
85
172
|
}
|
|
86
173
|
|
|
87
|
-
//
|
|
174
|
+
// Full mode delegates discovery to Jest, so pass the changed files themselves.
|
|
175
|
+
if (mode === 'full') {
|
|
176
|
+
const jestTargets = [...sourceFiles, ...testFiles];
|
|
177
|
+
|
|
178
|
+
if (jestTargets.length === 0) {
|
|
179
|
+
console.log('\n⚠️ No changed source or test files found to run.');
|
|
180
|
+
exit(0);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
console.log(`\n📝 Test strategy: full (Jest --findRelatedTests, unbounded traversal)`);
|
|
184
|
+
console.log(` Running tests related to ${sourceFiles.length} source file(s) + ${testFiles.length} changed test(s)`);
|
|
185
|
+
console.log(` ⚠️ If this expands to too many tests, switch to --mode smart`);
|
|
186
|
+
console.log(' Files passed to Jest:');
|
|
187
|
+
jestTargets.forEach(file => console.log(` - ${file}`));
|
|
188
|
+
|
|
189
|
+
runJest(`npx jest --findRelatedTests --passWithNoTests --coverage ${jestTargets.join(' ')}`);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Fast: naming-convention mapping with a direct-import fallback.
|
|
194
|
+
// Smart: naming-convention mapping plus a depth-limited reverse-dependency walk.
|
|
88
195
|
const testFilesToRun = new Set(testFiles);
|
|
89
|
-
const mappedTests = new Map(); // Track which tests map to which source files
|
|
90
196
|
|
|
91
197
|
sourceFiles.forEach(sourceFile => {
|
|
92
|
-
let relatedTests
|
|
93
|
-
let matchType
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
198
|
+
let relatedTests;
|
|
199
|
+
let matchType;
|
|
200
|
+
|
|
201
|
+
if (mode === 'smart') {
|
|
202
|
+
relatedTests = findRelatedTestFilesBySource(sourceFile, { maxDepth: depth });
|
|
203
|
+
matchType = `naming + import graph, depth ≤ ${depth}`;
|
|
204
|
+
} else {
|
|
205
|
+
relatedTests = findTestFilesForSource(sourceFile);
|
|
206
|
+
matchType = 'naming convention';
|
|
207
|
+
|
|
208
|
+
if (relatedTests.length === 0) {
|
|
209
|
+
relatedTests = findTestFilesByImport(sourceFile);
|
|
210
|
+
if (relatedTests.length > 0) {
|
|
211
|
+
matchType = 'import scan';
|
|
212
|
+
}
|
|
101
213
|
}
|
|
102
214
|
}
|
|
103
215
|
|
|
104
216
|
if (relatedTests.length > 0) {
|
|
105
217
|
console.log(` ✓ ${sourceFile} → ${relatedTests.length} test file(s) (${matchType})`);
|
|
106
|
-
relatedTests.forEach(test =>
|
|
107
|
-
testFilesToRun.add(test);
|
|
108
|
-
if (!mappedTests.has(test)) {
|
|
109
|
-
mappedTests.set(test, []);
|
|
110
|
-
}
|
|
111
|
-
mappedTests.get(test).push(sourceFile);
|
|
112
|
-
});
|
|
218
|
+
relatedTests.forEach(test => testFilesToRun.add(test));
|
|
113
219
|
} else {
|
|
114
220
|
console.log(` ⚠ ${sourceFile} → No test file found`);
|
|
115
221
|
}
|
|
@@ -124,65 +230,20 @@ function runRelatedTests(files, mode) {
|
|
|
124
230
|
}
|
|
125
231
|
|
|
126
232
|
console.log(`\n🧪 Running ${finalTestFiles.length} test file(s):\n`, finalTestFiles.join('\n'));
|
|
233
|
+
console.log(' Files passed to Jest:');
|
|
234
|
+
finalTestFiles.forEach(file => console.log(` - ${file}`));
|
|
127
235
|
|
|
128
|
-
if (mode === '
|
|
129
|
-
console.log(`\n📝 Test strategy:
|
|
236
|
+
if (mode === 'smart') {
|
|
237
|
+
console.log(`\n📝 Test strategy: smart (naming + reverse imports, depth ≤ ${depth})`);
|
|
130
238
|
console.log(` Test files: ${finalTestFiles.length}`);
|
|
131
|
-
console.log(` Source files detected: ${sourceFiles.length}`);
|
|
132
|
-
console.log(' Files passed to Jest:');
|
|
133
|
-
finalTestFiles.forEach(file => console.log(` - ${file}`));
|
|
134
|
-
const command = `npx jest --passWithNoTests --coverage ${finalTestFiles.join(' ')}`;
|
|
135
|
-
console.log(` Jest command: ${command}`);
|
|
136
|
-
|
|
137
|
-
try {
|
|
138
|
-
execSync(command, { stdio: 'inherit' });
|
|
139
|
-
} catch (err) {
|
|
140
|
-
console.error('❌ Tests failed:', err.message || err);
|
|
141
|
-
exit(1);
|
|
142
|
-
}
|
|
143
|
-
} else if (mode === 'smart') {
|
|
144
|
-
// Smart mode is intentionally scoped to the changed files in the diff.
|
|
145
|
-
// Mapped tests can include unchanged files (especially when a source file
|
|
146
|
-
// is imported by a broad test suite), which makes smart mode unexpectedly
|
|
147
|
-
// run the whole suite. Only changed test files belong in this mode.
|
|
148
|
-
const changedTestFiles = selectTestFilesForMode(mode, testFiles, finalTestFiles);
|
|
149
|
-
if (changedTestFiles.length === 0) {
|
|
150
|
-
console.log('\n⚠️ No changed test files found to run in smart mode.');
|
|
151
|
-
console.log('Change a test file or use --mode full to run related tests for changed source files.');
|
|
152
|
-
exit(0);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
console.log(`\n📝 Test strategy: smart (Run only changed test files, no Jest dependency traversal)`);
|
|
156
|
-
console.log(` Test files: ${changedTestFiles.length}`);
|
|
157
239
|
console.log(` Source files for coverage: ${sourceFiles.length}`);
|
|
158
|
-
console.log(' Files passed to Jest:');
|
|
159
|
-
changedTestFiles.forEach(file => console.log(` - ${file}`));
|
|
160
|
-
const command = `npx jest --passWithNoTests --coverage ${changedTestFiles.join(' ')}`;
|
|
161
|
-
console.log(` Jest command: ${command}`);
|
|
162
|
-
|
|
163
|
-
try {
|
|
164
|
-
execSync(command, { stdio: 'inherit' });
|
|
165
|
-
} catch (err) {
|
|
166
|
-
console.error('❌ Tests failed:', err.message || err);
|
|
167
|
-
exit(1);
|
|
168
|
-
}
|
|
169
240
|
} else {
|
|
170
|
-
console.log(`\n📝 Test strategy:
|
|
171
|
-
console.log(`
|
|
172
|
-
console.log(`
|
|
173
|
-
console.log(` ⚠️ If this expands to too many tests, switch to --mode smart`);
|
|
174
|
-
console.log(' Test files passed to Jest:');
|
|
175
|
-
finalTestFiles.forEach(file => console.log(` - ${file}`));
|
|
176
|
-
const command = `npx jest --findRelatedTests --passWithNoTests --coverage ${finalTestFiles.join(' ')}`;
|
|
177
|
-
console.log(` Jest command: ${command}`);
|
|
178
|
-
|
|
179
|
-
try {
|
|
180
|
-
execSync(command, { stdio: 'inherit' });
|
|
181
|
-
} catch (err) {
|
|
182
|
-
console.error('❌ Tests failed:', err.message || err);
|
|
183
|
-
exit(1);
|
|
184
|
-
}
|
|
241
|
+
console.log(`\n📝 Test strategy: fast (Direct execution, no Jest dependency traversal)`);
|
|
242
|
+
console.log(` Test files: ${finalTestFiles.length}`);
|
|
243
|
+
console.log(` Source files detected: ${sourceFiles.length}`);
|
|
185
244
|
}
|
|
245
|
+
|
|
246
|
+
runJest(`npx jest --passWithNoTests --coverage ${finalTestFiles.join(' ')}`);
|
|
186
247
|
}
|
|
187
248
|
|
|
188
249
|
function getChangedLines(filePath, BASE_BRANCH) {
|