mo3ta-coverage 1.2.1 → 1.3.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.
@@ -0,0 +1,89 @@
1
+ import fs from 'fs';
2
+ import os from 'os';
3
+ import path from 'path';
4
+ import { execSync, spawnSync } from 'child_process';
5
+
6
+ const CLI = path.resolve('index.js');
7
+
8
+ function git(repo, command) {
9
+ execSync(command, { cwd: repo, stdio: 'pipe' });
10
+ }
11
+
12
+ function runCli(repo, args) {
13
+ return spawnSync(process.execPath, [CLI, ...args], {
14
+ cwd: repo,
15
+ encoding: 'utf8'
16
+ });
17
+ }
18
+
19
+ describe('CLI process behavior', () => {
20
+ let root;
21
+
22
+ beforeEach(() => {
23
+ root = fs.mkdtempSync(path.join(os.tmpdir(), 'mo3ta-cli-'));
24
+ });
25
+
26
+ afterEach(() => {
27
+ fs.rmSync(root, { recursive: true, force: true });
28
+ });
29
+
30
+ it('prints help with the --dryRun example', () => {
31
+ const result = runCli(root, ['--help']);
32
+
33
+ expect(result.status).toBe(0);
34
+ expect(result.stdout).toContain('--dryRun true --files src/a.js,src/b.ts --base main');
35
+ expect(result.stdout).not.toContain('--dry-run');
36
+ });
37
+
38
+ it('dryRun prints only the selected test path on stdout', () => {
39
+ git(root, 'git init -b main -q');
40
+ git(root, 'git config user.email test@example.com');
41
+ git(root, 'git config user.name Test');
42
+ fs.writeFileSync(path.join(root, 'README.md'), 'base\n');
43
+ git(root, 'git add README.md && git commit -q -m base');
44
+ fs.mkdirSync(path.join(root, 'src'));
45
+ fs.writeFileSync(path.join(root, 'src', 'a.js'), 'module.exports = 1;\n');
46
+ fs.writeFileSync(path.join(root, 'src', 'a.test.js'), 'test("a", () => expect(1).toBe(1));\n');
47
+
48
+ const result = runCli(root, ['--dryRun', 'true', '--files', 'src/a.js', '--base', 'main', '--mode', 'fast']);
49
+
50
+ expect(result.status).toBe(0);
51
+ expect(result.stdout.trim()).toBe('src/a.test.js');
52
+ expect(result.stderr).not.toMatch(/^src\/a\.test\.js$/m);
53
+ });
54
+
55
+ it('exits 1 with the existing message when the directory is not a git repo', () => {
56
+ const result = runCli(root, ['--base', 'main']);
57
+
58
+ expect(result.status).toBe(1);
59
+ expect(result.stderr).toContain('❌ Not a git repository. Please run this tool inside a git repo.');
60
+ });
61
+
62
+ it('exits 1 with the existing message when the base ref is missing', () => {
63
+ git(root, 'git init -b main -q');
64
+ git(root, 'git config user.email test@example.com');
65
+ git(root, 'git config user.name Test');
66
+ fs.writeFileSync(path.join(root, 'README.md'), 'base\n');
67
+ git(root, 'git add README.md && git commit -q -m base');
68
+
69
+ const result = runCli(root, ['--base', 'origin/missing']);
70
+
71
+ expect(result.status).toBe(1);
72
+ expect(result.stderr).toContain('❌ Base branch "origin/missing" does not exist or is invalid. try git fetch to update your local branches.');
73
+ });
74
+
75
+ it('exits 1 with a parse error when .mo3ta-coverage.json is not valid JSON', () => {
76
+ git(root, 'git init -b main -q');
77
+ git(root, 'git config user.email test@example.com');
78
+ git(root, 'git config user.name Test');
79
+ fs.writeFileSync(path.join(root, 'README.md'), 'base\n');
80
+ git(root, 'git add README.md && git commit -q -m base');
81
+ fs.writeFileSync(path.join(root, '.mo3ta-coverage.json'), '{');
82
+
83
+ const result = runCli(root, ['config']);
84
+
85
+ expect(result.status).toBe(1);
86
+ expect(result.stderr).toContain('Could not parse');
87
+ expect(result.stderr).toContain('.mo3ta-coverage.json');
88
+ });
89
+ });
package/src/fileUtils.js CHANGED
@@ -105,8 +105,24 @@ function getTestFileCandidates(sourceFile) {
105
105
  * @param {string} sourceFile - Path to source file
106
106
  * @returns {string[]} - Array of potential test file paths
107
107
  */
108
- export function findTestFilesForSource(sourceFile) {
109
- return getTestFileCandidates(sourceFile).filter(testFile => fs.existsSync(testFile));
108
+ export function findTestFilesForSource(sourceFile, root = process.cwd()) {
109
+ const candidates = getTestFileCandidates(sourceFile);
110
+ // The default (cwd) path stays relative. Callers and tests observe the
111
+ // exact path strings passed to existsSync; resolving them would change that.
112
+ if (path.resolve(root) === process.cwd()) {
113
+ return candidates.filter(testFile => fs.existsSync(testFile));
114
+ }
115
+ return candidates.filter(testFile => fs.existsSync(path.resolve(root, testFile)));
116
+ }
117
+
118
+ /**
119
+ * Drops cached directory walks. The CLI keeps the cache for one process;
120
+ * long-running callers (the MCP server) clear it between tool calls.
121
+ */
122
+ export function clearScanCaches() {
123
+ cachedAllTestFiles = null;
124
+ cachedAllJsFiles = null;
125
+ cachedReverseImportIndex = null;
110
126
  }
111
127
 
112
128
  /**
package/src/gitUtils.js CHANGED
@@ -1,24 +1,55 @@
1
1
  import { execFileSync } from 'child_process';
2
2
  import { exit } from 'process';
3
3
 
4
- export function verifyGitRepoExists() {
4
+ /**
5
+ * Checks that `cwd` is inside a git work tree.
6
+ * Returns a result instead of exiting so callers (the CLI and the MCP server)
7
+ * can decide how to surface the failure.
8
+ * @param {string} [cwd]
9
+ * @returns {{ok: true} | {ok: false, message: string}}
10
+ */
11
+ export function checkGitRepository(cwd = process.cwd()) {
5
12
  try {
6
- execFileSync('git', ['rev-parse', '--is-inside-work-tree'], { stdio: 'ignore' });
13
+ execFileSync('git', ['rev-parse', '--is-inside-work-tree'], { stdio: 'ignore', cwd });
14
+ return { ok: true };
7
15
  } catch (err) {
8
- console.error(`❌ Not a git repository. Please run this tool inside a git repo. ${err.message}`);
16
+ return {
17
+ ok: false,
18
+ message: `Not a git repository. Please run this tool inside a git repo. ${err.message}`
19
+ };
20
+ }
21
+ }
22
+
23
+ export function verifyGitRepoExists(cwd = process.cwd()) {
24
+ const result = checkGitRepository(cwd);
25
+ if (!result.ok) {
26
+ console.error(`❌ ${result.message}`);
9
27
  exit(1);
10
28
  }
11
29
  }
12
30
 
13
- export function verifyBaseBranchExists(BASE_BRANCH) {
31
+ /**
32
+ * Checks that `baseRef` resolves to a commit. The ref is passed as one
33
+ * argument (never through a shell) and git `--verify` rejects option-like
34
+ * values such as `--upload-pack=...`.
35
+ * @param {string} baseRef
36
+ * @param {string} [cwd]
37
+ * @returns {{ok: true} | {ok: false, message: string}}
38
+ */
39
+ export function checkBaseRef(baseRef, cwd = process.cwd()) {
40
+ const message = `Base branch "${baseRef}" does not exist or is invalid. try git fetch to update your local branches.`;
14
41
  try {
15
- // Accept any valid git ref (branch, tag, or revision like origin/main, HEAD~1).
16
- // Git is invoked with an argument array (never through a shell) and the value
17
- // is checked with --verify, so a hostile value such as `--upload-pack=cmd`
18
- // or `a; rm -rf ~` is rejected by git as an invalid ref instead of running.
19
- execFileSync('git', ['rev-parse', '--verify', '--quiet', `${BASE_BRANCH}^{commit}`], { stdio: 'ignore' });
20
- } catch (err) {
21
- console.error(`❌ Base branch "${BASE_BRANCH}" does not exist or is invalid. try git fetch to update your local branches.`);
42
+ execFileSync('git', ['rev-parse', '--verify', '--quiet', `${baseRef}^{commit}`], { stdio: 'ignore', cwd });
43
+ return { ok: true };
44
+ } catch {
45
+ return { ok: false, message };
46
+ }
47
+ }
48
+
49
+ export function verifyBaseBranchExists(BASE_BRANCH, cwd = process.cwd()) {
50
+ const result = checkBaseRef(BASE_BRANCH, cwd);
51
+ if (!result.ok) {
52
+ console.error(`❌ ${result.message}`);
22
53
  exit(1);
23
54
  }
24
55
  }