@boyingliu01/xp-gate 0.38.73 → 0.38.75
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/bin/xp-gate.js +17 -1
- package/lib/__tests__/doctor-signature-stability.test.js +55 -0
- package/lib/__tests__/doctor.test.js +6 -1
- package/lib/__tests__/gate2-verdict.test.js +126 -0
- package/lib/__tests__/issue-566-sync-hooks-backup.test.js +23 -16
- package/lib/__tests__/jscpd-entry-resolution.test.js +143 -0
- package/lib/__tests__/tsx-resolution.test.js +89 -0
- package/lib/doctor.js +99 -3
- package/lib/gate-runner.js +275 -41
- package/lib/mattpocock-license-verdict.json +1 -1
- package/lib/tsx-resolve.js +48 -0
- package/package.json +1 -1
- package/plugins/claude-code/.claude-plugin/plugin.json +1 -1
- package/plugins/claude-code/skills/delphi-review/AGENTS.md +1 -1
- package/plugins/claude-code/skills/sprint-flow/AGENTS.md +1 -1
- package/plugins/claude-code/skills/test-specification-alignment/AGENTS.md +1 -1
- package/plugins/dsh/package.json +1 -1
- package/plugins/dsh/skills/delphi-review/AGENTS.md +1 -1
- package/plugins/dsh/skills/sprint-flow/AGENTS.md +1 -1
- package/plugins/dsh/skills/test-specification-alignment/AGENTS.md +1 -1
- package/plugins/opencode/package.json +1 -1
- package/plugins/opencode/skills/delphi-review/AGENTS.md +1 -1
- package/plugins/opencode/skills/sprint-flow/AGENTS.md +1 -1
- package/plugins/opencode/skills/test-specification-alignment/AGENTS.md +1 -1
- package/plugins/qoder/plugin.json +1 -1
- package/plugins/qoder/skills/delphi-review/AGENTS.md +1 -1
- package/plugins/qoder/skills/sprint-flow/AGENTS.md +1 -1
- package/plugins/qoder/skills/test-specification-alignment/AGENTS.md +1 -1
- package/principles/adapters/php.ts +407 -5
- package/skills/delphi-review/AGENTS.md +1 -1
- package/skills/sprint-flow/AGENTS.md +1 -1
- package/skills/test-specification-alignment/AGENTS.md +1 -1
package/bin/xp-gate.js
CHANGED
|
@@ -794,7 +794,23 @@ function handleAuditRecord(args, auditPath) {
|
|
|
794
794
|
function evalGateAuditModule(expr) {
|
|
795
795
|
const { execFileSync } = require('node:child_process');
|
|
796
796
|
const modulePath = path.join(__dirname, '..', 'lib', 'gate-audit.ts');
|
|
797
|
-
|
|
797
|
+
// Resolve tsx by searching upward instead of counting `..` segments (#817).
|
|
798
|
+
//
|
|
799
|
+
// Three `..` from `bin/` already reaches `<global>/node_modules`, so the old
|
|
800
|
+
// `path.join(__dirname, '..', '..', '..', 'node_modules', 'tsx', ...)` produced
|
|
801
|
+
// `<global>/node_modules/node_modules/tsx/dist/cli.mjs` -- a path that can never
|
|
802
|
+
// exist. `audit --check` then died with MODULE_NOT_FOUND on every global install,
|
|
803
|
+
// and it happens to be the command doctor recommends when audit evidence is
|
|
804
|
+
// missing, so the documented recovery path was the broken one.
|
|
805
|
+
const tsxCli = require('../lib/tsx-resolve').findTsxCliFrom(__dirname);
|
|
806
|
+
if (!tsxCli) {
|
|
807
|
+
// An install hint beats a MODULE_NOT_FOUND stack: the file is fine, the
|
|
808
|
+
// dependency is absent, and those need different actions from the user.
|
|
809
|
+
throw new Error(
|
|
810
|
+
'xp-gate: cannot locate tsx, which this command needs to load its TypeScript module. '
|
|
811
|
+
+ 'Install it with `npm i -g tsx`, or reinstall xp-gate (`npm i -g @boyingliu01/xp-gate`).',
|
|
812
|
+
);
|
|
813
|
+
}
|
|
798
814
|
const script = [
|
|
799
815
|
`import * as m from ${JSON.stringify(modulePath)};`,
|
|
800
816
|
`process.stdout.write(JSON.stringify(${expr}));`,
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @test REQ-doctor-signature 钩子归属判定不得依赖会漂移的描述性数字(#854)
|
|
3
|
+
* @intent 证明 `xp-gate doctor` 不再对完好钩子误报 Missing or not xp-gate。
|
|
4
|
+
* 修复前实测:SIGNATURES['pre-push'] 写的是 "Pre-push Hook — 8 gates:",
|
|
5
|
+
* 而实际横幅是 "Pre-push Hook: Gate 10 (Build Integrity) + M/..."。两者
|
|
6
|
+
* 从来没有匹配过,所以对任何正常安装这条检查恒为 FAIL —— 是误报,不是缺文件。
|
|
7
|
+
*
|
|
8
|
+
* 要点不是"把字符串改成现在这句",而是让签名不随门禁数量漂移:
|
|
9
|
+
* pre-commit 的签名如果把 "12 numbered gates" 也算进去,下次加一道门禁
|
|
10
|
+
* 就同样会恒 FAIL。签名只取稳定的身份前缀。
|
|
11
|
+
*
|
|
12
|
+
* @covers AC-DOCTOR-SIG-01, AC-DOCTOR-SIG-02, AC-DOCTOR-SIG-03
|
|
13
|
+
*/
|
|
14
|
+
const fs = require('node:fs');
|
|
15
|
+
const path = require('node:path');
|
|
16
|
+
const { getHookSignatures, isXpGateHookContent } = require('../doctor');
|
|
17
|
+
|
|
18
|
+
const REPO_ROOT = path.resolve(__dirname, '..', '..', '..', '..');
|
|
19
|
+
|
|
20
|
+
describe('#854 doctor hook signatures', () => {
|
|
21
|
+
it('recognises the real shipped pre-push hook', () => {
|
|
22
|
+
// End-to-end against the actual file. A signature that only matches a
|
|
23
|
+
// hand-written fixture in this test would still fail on every install.
|
|
24
|
+
const hookPath = path.join(REPO_ROOT, 'githooks', 'pre-push');
|
|
25
|
+
const content = fs.readFileSync(hookPath, 'utf8');
|
|
26
|
+
expect(isXpGateHookContent('pre-push', content)).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('recognises the real shipped pre-commit and post-merge hooks', () => {
|
|
30
|
+
for (const name of ['pre-commit', 'post-merge']) {
|
|
31
|
+
const content = fs.readFileSync(path.join(REPO_ROOT, 'githooks', name), 'utf8');
|
|
32
|
+
expect(isXpGateHookContent(name, content)).toBe(true);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('does not encode a gate COUNT in any signature', () => {
|
|
37
|
+
// A signature containing a number would break the moment a gate is added or
|
|
38
|
+
// removed, which is exactly how pre-push's "8 gates" rotted.
|
|
39
|
+
for (const [name, sig] of Object.entries(getHookSignatures())) {
|
|
40
|
+
expect(Number.isFinite(Number(sig.match(/\d+/)?.[0]))).toBe(false);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('still rejects a foreign hook that merely mentions xp-gate', () => {
|
|
45
|
+
// Loosening the signature must not make the check useless: the point of the
|
|
46
|
+
// check is to tell OUR hook from someone else's.
|
|
47
|
+
expect(isXpGateHookContent('pre-push', '#!/bin/bash\necho "some other tool"\n')).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('rejects an xp-gate hook placed under the wrong name', () => {
|
|
51
|
+
// Signatures are per-hook; a pre-commit file must not satisfy pre-push.
|
|
52
|
+
const preCommit = fs.readFileSync(path.join(REPO_ROOT, 'githooks', 'pre-commit'), 'utf8');
|
|
53
|
+
expect(isXpGateHookContent('pre-push', preCommit)).toBe(false);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
@@ -210,9 +210,14 @@ describe('doctor', () => {
|
|
|
210
210
|
|
|
211
211
|
function createXpGatePrePush(dir) {
|
|
212
212
|
fs.mkdirSync(dir, { recursive: true });
|
|
213
|
+
// Mirror the banner the shipped hook actually carries. The previous fixture used
|
|
214
|
+
// '# Pre-push Hook — 8 gates:', which no shipped hook has ever contained -- so
|
|
215
|
+
// these "healthy install" cases passed against a fixture while every real
|
|
216
|
+
// installation failed (#854). A fixture that disagrees with the product turns
|
|
217
|
+
// the test into a check on itself.
|
|
213
218
|
fs.writeFileSync(
|
|
214
219
|
path.join(dir, 'pre-push'),
|
|
215
|
-
'#!/bin/bash\n# Pre-push Hook
|
|
220
|
+
'#!/bin/bash\n# Pre-push Hook: Gate 10 (Build Integrity) + M (Mutation)\n'
|
|
216
221
|
);
|
|
217
222
|
}
|
|
218
223
|
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @test REQ-gate2-verdict 重复代码门禁必须能判定失败(#853 的第二半)
|
|
3
|
+
* @intent 修好 jscpd 入口探测只解决了"工具崩溃",没有解决"门禁从不失败"。
|
|
4
|
+
* 实测:jscpd 在不传 --threshold 时即使发现 66.7% 的重复也返回 exit 0,
|
|
5
|
+
* 所以 `runJscpd` 直接转发它的退出码,Gate 2 对任何仓库都恒为 PASS。
|
|
6
|
+
*
|
|
7
|
+
* 这和 #853 是同一个症状的两个阶段:修复前是 `spawnSync npx ENOENT`
|
|
8
|
+
* (工具跑不起来),修复后变成"跑起来了但从不拦截"。只修路径会让门禁
|
|
9
|
+
* 从"崩溃"变成"静默通过"——后者更危险,因为它看起来是绿的。
|
|
10
|
+
*
|
|
11
|
+
* @covers AC-GATE2-VERDICT-01, AC-GATE2-VERDICT-02, AC-GATE2-VERDICT-03
|
|
12
|
+
*/
|
|
13
|
+
const fs = require('node:fs');
|
|
14
|
+
const path = require('node:path');
|
|
15
|
+
const os = require('node:os');
|
|
16
|
+
|
|
17
|
+
const { buildJscpdArgs } = require('../gate-runner');
|
|
18
|
+
|
|
19
|
+
let tmpDir;
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'xpgate-gate2-'));
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
if (tmpDir && fs.existsSync(tmpDir)) {
|
|
27
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('#853 Gate 2 must be able to fail', () => {
|
|
32
|
+
it('passes a --threshold so jscpd can return non-zero', () => {
|
|
33
|
+
// Without this the gate can never block, whatever it finds.
|
|
34
|
+
const args = buildJscpdArgs({ targetPath: tmpDir });
|
|
35
|
+
const at = args.indexOf('--threshold');
|
|
36
|
+
expect(at).toBeGreaterThanOrEqual(0);
|
|
37
|
+
expect(Number(args[at + 1])).toBeGreaterThan(0);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('uses the same 15% the code-duplication principle rule uses', () => {
|
|
41
|
+
// Two gates judging "how much duplication is too much" must not disagree;
|
|
42
|
+
// a run that fails Gate 4 while passing Gate 2 teaches users the gates lie.
|
|
43
|
+
const args = buildJscpdArgs({ targetPath: tmpDir });
|
|
44
|
+
expect(args[args.indexOf('--threshold') + 1]).toBe('15');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('lets a project config own the threshold when it declares one', () => {
|
|
48
|
+
// jscpd reads `threshold` from its own config. Passing --threshold as well
|
|
49
|
+
// would override the project's value with ours, which is the same
|
|
50
|
+
// config-vs-CLI conflict that already cost the ignore list its entries.
|
|
51
|
+
fs.writeFileSync(
|
|
52
|
+
path.join(tmpDir, 'jscpd.conf.json'),
|
|
53
|
+
JSON.stringify({ threshold: 3, ignore: ['**/vendor/**'] }),
|
|
54
|
+
);
|
|
55
|
+
const args = buildJscpdArgs({ targetPath: tmpDir });
|
|
56
|
+
expect(args).toContain('--config');
|
|
57
|
+
expect(args).not.toContain('--threshold');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('honours a threshold declared in .jscpd.json, not just jscpd.conf.json', () => {
|
|
61
|
+
// jscpd reads several config filenames. Measured on jscpd 5.4.3 with a fixture
|
|
62
|
+
// at 66.7% duplication:
|
|
63
|
+
// .jscpd.json {threshold: 90} -> exit 0 (config respected)
|
|
64
|
+
// .jscpd.json {threshold: 90} + --threshold 15 -> exit 1 (CLI won)
|
|
65
|
+
// A project that sets 90 in .jscpd.json would have been overridden by our 15,
|
|
66
|
+
// silently taking the decision away from the project that declared it.
|
|
67
|
+
fs.writeFileSync(path.join(tmpDir, '.jscpd.json'), JSON.stringify({ threshold: 90 }));
|
|
68
|
+
const args = buildJscpdArgs({ targetPath: tmpDir });
|
|
69
|
+
expect(args).toContain('--config');
|
|
70
|
+
expect(args).not.toContain('--threshold');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('falls back to the default threshold when the config declares none', () => {
|
|
74
|
+
// A config that exists but says nothing about the threshold must still let the
|
|
75
|
+
// gate decide; otherwise Gate 2 would silently stop enforcing anything, which
|
|
76
|
+
// is the very defect this change exists to fix.
|
|
77
|
+
fs.writeFileSync(path.join(tmpDir, 'jscpd.conf.json'), JSON.stringify({ ignore: ['**/x/**'] }));
|
|
78
|
+
const args = buildJscpdArgs({ targetPath: tmpDir });
|
|
79
|
+
expect(args).toContain('--config');
|
|
80
|
+
expect(args[args.indexOf('--threshold') + 1]).toBe('15');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('ignores a DIRECTORY that happens to be named like a config file', () => {
|
|
84
|
+
// `--config <dir>` cannot work, so a stray directory must not be discovered as
|
|
85
|
+
// a config: it would turn the gate red for a reason that has nothing to do with
|
|
86
|
+
// duplication.
|
|
87
|
+
fs.mkdirSync(path.join(tmpDir, '.jscpd.json'));
|
|
88
|
+
const args = buildJscpdArgs({ targetPath: tmpDir });
|
|
89
|
+
expect(args).not.toContain('--config');
|
|
90
|
+
expect(args[args.indexOf('--threshold') + 1]).toBe('15');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('never applies the working directory config to an explicit target', () => {
|
|
94
|
+
// The stated rule is "an explicit --target decides". Measured before this was
|
|
95
|
+
// enforced: with only the xp-gate repo holding a jscpd.conf.json, a target with
|
|
96
|
+
// no config of its own still received `--config <xp-gate repo>/jscpd.conf.json`
|
|
97
|
+
// -- so `check 2 /other/repo` judged that repo by this repo's ignore list.
|
|
98
|
+
// The fixture keeps its own cwd config out of the picture by asserting on the
|
|
99
|
+
// config PATH rather than on presence.
|
|
100
|
+
const args = buildJscpdArgs({ targetPath: tmpDir });
|
|
101
|
+
const at = args.indexOf('--config');
|
|
102
|
+
if (at >= 0) {
|
|
103
|
+
expect(args[at + 1].startsWith(tmpDir)).toBe(true);
|
|
104
|
+
}
|
|
105
|
+
// A target with no config must never produce one of its own.
|
|
106
|
+
expect(args).not.toContain('--config');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('falls back to the default threshold when the config is malformed', () => {
|
|
110
|
+
// An unparseable config must not crash the gate or disable the threshold.
|
|
111
|
+
// Measured on jscpd 5.4.3: passing a corrupt --config makes jscpd exit 1 with
|
|
112
|
+
// "key must be a string at line 1 column 3" -- which the gate would report as
|
|
113
|
+
// "duplicate code found". A broken config must not read as a finding.
|
|
114
|
+
fs.writeFileSync(path.join(tmpDir, 'jscpd.conf.json'), '{ not valid json');
|
|
115
|
+
const args = buildJscpdArgs({ targetPath: tmpDir });
|
|
116
|
+
expect(args[args.indexOf('--threshold') + 1]).toBe('15');
|
|
117
|
+
expect(args).not.toContain('--config');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('does not pass a --config jscpd would reject', () => {
|
|
121
|
+
// Same rule, stated as the invariant: whatever --config we pass must parse.
|
|
122
|
+
fs.writeFileSync(path.join(tmpDir, 'jscpd.conf.json'), '{ not valid json');
|
|
123
|
+
const args = buildJscpdArgs({ targetPath: tmpDir });
|
|
124
|
+
expect(args).not.toContain('--config');
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -53,22 +53,29 @@ describe('doctor --sync-hooks keeps the previous version (#566)', () => {
|
|
|
53
53
|
* @param {{installedPrePush?: string}} [opts] content for the installed pre-push
|
|
54
54
|
*/
|
|
55
55
|
function makeLayout(opts = {}) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
56
|
+
// Wrap the fixture setup so a failure names the fixture that broke instead of
|
|
57
|
+
// surfacing later as "doctor reported the wrong thing". Without this, an
|
|
58
|
+
// unreadable tmpHome looks like a product defect in the assertion.
|
|
59
|
+
try {
|
|
60
|
+
const root = fs.mkdtempSync(path.join(tmpHome, `layout-${DESCRIBE_ID}-`));
|
|
61
|
+
const repoRoot = path.join(root, 'repo');
|
|
62
|
+
const repoHooks = path.join(repoRoot, 'githooks');
|
|
63
|
+
const installedHooks = path.join(root, 'installed-hooks');
|
|
64
|
+
fs.mkdirSync(repoHooks, { recursive: true });
|
|
65
|
+
fs.mkdirSync(installedHooks, { recursive: true });
|
|
66
|
+
|
|
67
|
+
// The canonical hook carries the xp-gate signature doctor looks for.
|
|
68
|
+
fs.writeFileSync(
|
|
69
|
+
path.join(repoHooks, 'pre-push'),
|
|
70
|
+
'#!/usr/bin/env bash\n# XP-Gate Pre-Push Hook\n# Pre-push Hook: Gate 10 (Build Integrity)\necho canonical\n',
|
|
71
|
+
);
|
|
72
|
+
if (opts.installedPrePush !== undefined) {
|
|
73
|
+
fs.writeFileSync(path.join(installedHooks, 'pre-push'), opts.installedPrePush);
|
|
74
|
+
}
|
|
75
|
+
return { root, repoRoot, repoHooks, installedHooks };
|
|
76
|
+
} catch (err) {
|
|
77
|
+
throw new Error(`fixture: could not build hook layout under ${tmpHome}: ${err.message}`);
|
|
70
78
|
}
|
|
71
|
-
return { root, repoRoot, repoHooks, installedHooks };
|
|
72
79
|
}
|
|
73
80
|
|
|
74
81
|
it('AC-599-13: overwriting an existing hook keeps a copy of the previous version', () => {
|
|
@@ -138,7 +145,7 @@ describe('doctor --sync-hooks keeps the previous version (#566)', () => {
|
|
|
138
145
|
});
|
|
139
146
|
|
|
140
147
|
it('AC-599-13: an identical file is not re-copied and leaves no backup', () => {
|
|
141
|
-
const canonical = '#!/usr/bin/env bash\n# XP-Gate Pre-Push Hook\n# Pre-push Hook
|
|
148
|
+
const canonical = '#!/usr/bin/env bash\n# XP-Gate Pre-Push Hook\n# Pre-push Hook: Gate 10 (Build Integrity)\necho canonical\n';
|
|
142
149
|
const { repoHooks, installedHooks } = makeLayout({ installedPrePush: canonical });
|
|
143
150
|
|
|
144
151
|
const { syncGlobalHooksFromRepo } = loadDoctor();
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @test REQ-jscpd-entry 重复代码门禁必须能在全局安装下找到 jscpd(#853)
|
|
3
|
+
* @intent 证明 Gate 2 在 npm 全局安装 + 干净 fixture 下不再恒失败。修复前实测:
|
|
4
|
+
* `npmGlobalRoots` 的元素已经以 `node_modules` 结尾,`runJscpd` 又拼了一次,
|
|
5
|
+
* 于是探测的是 `<global>/npm/node_modules/node_modules/jscpd/bin/jscpd`,
|
|
6
|
+
* 而真实路径是 `<global>/npm/node_modules/jscpd`。entry 恒为 undefined,
|
|
7
|
+
* 落到 `npx --no-install jscpd` fallback,在 Windows 上报 spawnSync ENOENT。
|
|
8
|
+
*
|
|
9
|
+
* 第二个缺陷叠加:jscpd 5.x 把入口从 `bin/jscpd` 换成了 `run-jscpd.js`
|
|
10
|
+
* (package.json 的 bin 字段为 `{"jscpd": "./run-jscpd.js"}`)。硬编码
|
|
11
|
+
* `bin/jscpd` 在 5.x 上即便路径拼对了也找不到。
|
|
12
|
+
*
|
|
13
|
+
* @covers AC-JSCPD-ENTRY-01, AC-JSCPD-ENTRY-02, AC-JSCPD-ENTRY-03, AC-JSCPD-ENTRY-04
|
|
14
|
+
*/
|
|
15
|
+
const fs = require('node:fs');
|
|
16
|
+
const path = require('node:path');
|
|
17
|
+
const os = require('node:os');
|
|
18
|
+
const { findJscpdEntry, buildGlobalPrefixes } = require('../gate-runner');
|
|
19
|
+
|
|
20
|
+
let tmpDir;
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'xpgate-jscpd-'));
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterEach(() => {
|
|
27
|
+
if (tmpDir && fs.existsSync(tmpDir)) {
|
|
28
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Lay down a fake jscpd install whose package.json declares the given bin entry.
|
|
34
|
+
*
|
|
35
|
+
* The real 5.4.3 layout is reproduced faithfully: `run-jscpd.js` at the package
|
|
36
|
+
* root, no `bin/jscpd`. Getting this wrong would make the test pass against a
|
|
37
|
+
* layout that does not exist and never catch the regression it exists for.
|
|
38
|
+
*/
|
|
39
|
+
function plantJscpd(packageDir, binField) {
|
|
40
|
+
try {
|
|
41
|
+
fs.mkdirSync(packageDir, { recursive: true });
|
|
42
|
+
fs.writeFileSync(
|
|
43
|
+
path.join(packageDir, 'package.json'),
|
|
44
|
+
JSON.stringify({ name: 'jscpd', version: '5.4.3', bin: binField }, null, 2),
|
|
45
|
+
);
|
|
46
|
+
for (const rel of Object.values(binField)) {
|
|
47
|
+
const target = path.join(packageDir, rel);
|
|
48
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
49
|
+
fs.writeFileSync(target, '// fake jscpd' + String.fromCharCode(10));
|
|
50
|
+
}
|
|
51
|
+
} catch (err) {
|
|
52
|
+
throw new Error(`fixture: could not plant jscpd at ${packageDir}: ${err.message}`);
|
|
53
|
+
}
|
|
54
|
+
return packageDir;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
describe('#853 jscpd entry resolution', () => {
|
|
58
|
+
it('finds jscpd 5.x whose entry is run-jscpd.js, not bin/jscpd', () => {
|
|
59
|
+
// The exact 5.4.3 declaration. Hardcoding `bin/jscpd` cannot satisfy this.
|
|
60
|
+
const pkg = plantJscpd(path.join(tmpDir, 'node_modules', 'jscpd'), { jscpd: './run-jscpd.js' });
|
|
61
|
+
const entry = findJscpdEntry({ searchRoots: [tmpDir] });
|
|
62
|
+
expect(entry).toBe(path.join(pkg, 'run-jscpd.js'));
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('finds a legacy jscpd whose entry is bin/jscpd', () => {
|
|
66
|
+
// 4.x layout must keep working; the fix must not trade one version for another.
|
|
67
|
+
const pkg = plantJscpd(path.join(tmpDir, 'node_modules', 'jscpd'), { jscpd: './bin/jscpd' });
|
|
68
|
+
const entry = findJscpdEntry({ searchRoots: [tmpDir] });
|
|
69
|
+
expect(entry).toBe(path.join(pkg, 'bin', 'jscpd'));
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('does not append node_modules to a root that already ends with it', () => {
|
|
73
|
+
// This is the #853 doubling. A global npm prefix IS `<...>/npm/node_modules`,
|
|
74
|
+
// so probing `<...>/npm/node_modules/node_modules/jscpd` can never succeed.
|
|
75
|
+
const globalPrefix = path.join(tmpDir, 'npm', 'node_modules');
|
|
76
|
+
const pkg = plantJscpd(path.join(globalPrefix, 'jscpd'), { jscpd: './run-jscpd.js' });
|
|
77
|
+
const entry = findJscpdEntry({ searchRoots: [globalPrefix] });
|
|
78
|
+
expect(entry).toBe(path.join(pkg, 'run-jscpd.js'));
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('follows an unusual bin field rather than assuming a filename', () => {
|
|
82
|
+
// A hardcoded filename silently breaks the next time upstream renames the
|
|
83
|
+
// entry point -- which is exactly how #853's second defect appeared.
|
|
84
|
+
const pkg = plantJscpd(path.join(tmpDir, 'node_modules', 'jscpd'), { jscpd: './dist/start.js' });
|
|
85
|
+
const entry = findJscpdEntry({ searchRoots: [tmpDir] });
|
|
86
|
+
expect(entry).toBe(path.join(pkg, 'dist', 'start.js'));
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('finds jscpd in a POSIX global prefix (<prefix>/lib/node_modules)', () => {
|
|
90
|
+
// `path.dirname(process.execPath)` is `<prefix>/bin` on Linux and macOS, and a
|
|
91
|
+
// global jscpd there lives in `<prefix>/lib/node_modules` -- NOT in
|
|
92
|
+
// `<prefix>/bin/node_modules`. #853's own scenario was Windows, but a fix that
|
|
93
|
+
// only works there would leave the same bug on every Unix install.
|
|
94
|
+
const prefix = path.join(tmpDir, 'prefix');
|
|
95
|
+
const pkg = plantJscpd(path.join(prefix, 'lib', 'node_modules', 'jscpd'), { jscpd: './run-jscpd.js' });
|
|
96
|
+
const entry = findJscpdEntry({ searchRoots: [prefix] });
|
|
97
|
+
expect(entry).toBe(path.join(pkg, 'run-jscpd.js'));
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('returns null when jscpd is genuinely absent', () => {
|
|
101
|
+
// "not installed" must stay distinguishable from "installed but unfindable":
|
|
102
|
+
// the first is a SKIP, the second was the bug.
|
|
103
|
+
expect(findJscpdEntry({ searchRoots: [tmpDir] })).toBeNull();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('never yields a relative prefix when the env vars are unset', () => {
|
|
107
|
+
// path.join('', 'npm') is the RELATIVE path 'npm'. Left in the list it would be
|
|
108
|
+
// resolved against the current directory, so an unset APPDATA/HOME would point
|
|
109
|
+
// the search at ./npm -- a directory that has nothing to do with any global
|
|
110
|
+
// install, and one an attacker-ish layout could populate.
|
|
111
|
+
const prefixes = buildGlobalPrefixes({ binDir: path.join(tmpDir, 'nodejs') });
|
|
112
|
+
expect(prefixes.length).toBeGreaterThan(0);
|
|
113
|
+
for (const p of prefixes) {
|
|
114
|
+
expect(path.isAbsolute(p)).toBe(true);
|
|
115
|
+
}
|
|
116
|
+
expect(prefixes).not.toContain('npm');
|
|
117
|
+
expect(prefixes).not.toContain('.npm-global');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('includes the APPDATA and home prefixes when those variables exist', () => {
|
|
121
|
+
const appData = path.join(tmpDir, 'appdata');
|
|
122
|
+
const home = path.join(tmpDir, 'home');
|
|
123
|
+
const prefixes = buildGlobalPrefixes({
|
|
124
|
+
binDir: path.join(tmpDir, 'nodejs'),
|
|
125
|
+
appData,
|
|
126
|
+
home,
|
|
127
|
+
});
|
|
128
|
+
expect(prefixes).toContain(path.join(appData, 'npm'));
|
|
129
|
+
expect(prefixes).toContain(path.join(home, '.npm-global'));
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('ignores a half-written install with no resolvable entry file', () => {
|
|
133
|
+
// A package.json whose bin target does not exist would be spawned and fail
|
|
134
|
+
// with a message about missing files, reading as a gate defect not an install one.
|
|
135
|
+
const pkgDir = path.join(tmpDir, 'node_modules', 'jscpd');
|
|
136
|
+
fs.mkdirSync(pkgDir, { recursive: true });
|
|
137
|
+
fs.writeFileSync(
|
|
138
|
+
path.join(pkgDir, 'package.json'),
|
|
139
|
+
JSON.stringify({ name: 'jscpd', bin: { jscpd: './run-jscpd.js' } }),
|
|
140
|
+
);
|
|
141
|
+
expect(findJscpdEntry({ searchRoots: [tmpDir] })).toBeNull();
|
|
142
|
+
});
|
|
143
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @test REQ-audit-cli 定位 tsx 时不得多拼一层 node_modules(#817)
|
|
3
|
+
* @intent 证明 `xp-gate audit --check` 在 npm 全局安装下不再 MODULE_NOT_FOUND。
|
|
4
|
+
* 修复前实测:`bin/xp-gate.js` 从 `bin/` 出发走三个 `..` 已经到达
|
|
5
|
+
* `<global>/node_modules`,再拼 `node_modules/tsx` 就变成
|
|
6
|
+
* `<global>/node_modules/node_modules/tsx/dist/cli.mjs`,必然不存在。
|
|
7
|
+
*
|
|
8
|
+
* 这不是"路径写错了"这么简单:`audit --check` 是 doctor 在缺审计证据时
|
|
9
|
+
* 推荐给用户的恢复路径,它一崩,用户就没有可用的恢复手段。
|
|
10
|
+
*
|
|
11
|
+
* @covers AC-TSX-RESOLVE-01, AC-TSX-RESOLVE-02, AC-TSX-RESOLVE-03
|
|
12
|
+
*/
|
|
13
|
+
const fs = require('node:fs');
|
|
14
|
+
const path = require('node:path');
|
|
15
|
+
const os = require('node:os');
|
|
16
|
+
const { findTsxCliFrom } = require('../tsx-resolve');
|
|
17
|
+
|
|
18
|
+
let tmpDir;
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'xpgate-tsxres-'));
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
if (tmpDir && fs.existsSync(tmpDir)) {
|
|
26
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Plant a fake `tsx/dist/cli.mjs` under `root` and return its absolute path.
|
|
32
|
+
*
|
|
33
|
+
* Failing to plant is a fixture defect, so the error names the fixture rather
|
|
34
|
+
* than letting it surface later as "the resolver returned undefined", which
|
|
35
|
+
* would be read as a resolver bug.
|
|
36
|
+
*/
|
|
37
|
+
function plantTsx(root) {
|
|
38
|
+
const cli = path.join(root, 'node_modules', 'tsx', 'dist', 'cli.mjs');
|
|
39
|
+
try {
|
|
40
|
+
fs.mkdirSync(path.dirname(cli), { recursive: true });
|
|
41
|
+
fs.writeFileSync(cli, '// fake tsx cli' + String.fromCharCode(10));
|
|
42
|
+
} catch (err) {
|
|
43
|
+
throw new Error(`fixture: could not plant tsx at ${cli}: ${err.message}`);
|
|
44
|
+
}
|
|
45
|
+
return cli;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
describe('#817 tsx resolution from the CLI entry point', () => {
|
|
49
|
+
it('finds the sibling tsx of a globally installed xp-gate', () => {
|
|
50
|
+
// Reproduce the real global layout:
|
|
51
|
+
// <prefix>/node_modules/@boyingliu01/xp-gate/bin
|
|
52
|
+
// <prefix>/node_modules/tsx/dist/cli.mjs
|
|
53
|
+
// The fix is to search UPWARD for node_modules rather than counting `..`.
|
|
54
|
+
const prefix = path.join(tmpDir, 'npm');
|
|
55
|
+
const pkgRoot = path.join(prefix, 'node_modules', '@boyingliu01', 'xp-gate');
|
|
56
|
+
fs.mkdirSync(path.join(pkgRoot, 'bin'), { recursive: true });
|
|
57
|
+
const expected = plantTsx(prefix);
|
|
58
|
+
const resolved = findTsxCliFrom(path.join(pkgRoot, 'bin'));
|
|
59
|
+
expect(resolved).toBe(expected);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('finds a tsx hoisted into the consumer project', () => {
|
|
63
|
+
// A local install hoists tsx next to the package under the same node_modules.
|
|
64
|
+
const project = path.join(tmpDir, 'project');
|
|
65
|
+
const pkgRoot = path.join(project, 'node_modules', '@boyingliu01', 'xp-gate');
|
|
66
|
+
fs.mkdirSync(path.join(pkgRoot, 'bin'), { recursive: true });
|
|
67
|
+
const expected = plantTsx(project);
|
|
68
|
+
expect(findTsxCliFrom(path.join(pkgRoot, 'bin'))).toBe(expected);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('never produces a doubled node_modules segment', () => {
|
|
72
|
+
// The exact #817 shape. Even when it resolves to nothing, the returned path
|
|
73
|
+
// must never contain node_modules/node_modules -- that is always a bug, and
|
|
74
|
+
// it is indistinguishable from "not installed" at the call site.
|
|
75
|
+
const prefix = path.join(tmpDir, 'npm');
|
|
76
|
+
const pkgRoot = path.join(prefix, 'node_modules', '@boyingliu01', 'xp-gate');
|
|
77
|
+
fs.mkdirSync(path.join(pkgRoot, 'bin'), { recursive: true });
|
|
78
|
+
const resolved = findTsxCliFrom(path.join(pkgRoot, 'bin'));
|
|
79
|
+
expect(resolved === null || !resolved.includes(`node_modules${path.sep}node_modules`)).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('returns null when tsx is genuinely absent, rather than a broken path', () => {
|
|
83
|
+
// The caller must be able to tell "not installed" (print an install hint)
|
|
84
|
+
// apart from "here is a path" (spawn it).
|
|
85
|
+
const pkgRoot = path.join(tmpDir, 'nowhere', 'bin');
|
|
86
|
+
fs.mkdirSync(pkgRoot, { recursive: true });
|
|
87
|
+
expect(findTsxCliFrom(pkgRoot)).toBeNull();
|
|
88
|
+
});
|
|
89
|
+
});
|
package/lib/doctor.js
CHANGED
|
@@ -62,14 +62,46 @@ function getPackageVersion() {
|
|
|
62
62
|
|
|
63
63
|
/**
|
|
64
64
|
* Signature strings used to verify file ownership.
|
|
65
|
+
*
|
|
66
|
+
* Each must be a STABLE identity anchor -- text that says "this file is xp-gate's
|
|
67
|
+
* <hook>" and does not describe how many gates it runs or what they are called.
|
|
68
|
+
* Descriptive banners rot: `pre-push` was keyed on `'Pre-push Hook — 8 gates:'`,
|
|
69
|
+
* while the hook's actual banner reads `'Pre-push Hook: Gate 10 (Build
|
|
70
|
+
* Integrity) + M/...'`. The two never matched, so `doctor` reported
|
|
71
|
+
* "Missing or not xp-gate" for every healthy installation -- a false alarm that
|
|
72
|
+
* `--fix` could not clear because there was nothing to fix (#854).
|
|
73
|
+
*
|
|
74
|
+
* The same trap sits in the sibling signatures: `pre-commit`'s banner continues
|
|
75
|
+
* " - 12 numbered gates (Gate 0–11)", so a signature that included that span
|
|
76
|
+
* would break the day a gate is added. Anchor before the count, not through it.
|
|
65
77
|
*/
|
|
66
78
|
const SIGNATURES = {
|
|
67
79
|
'pre-commit': 'OpenCode Quality Gates - Pre-Commit Hook',
|
|
68
|
-
'pre-push': 'Pre-push Hook
|
|
80
|
+
'pre-push': 'Pre-push Hook:',
|
|
69
81
|
'post-merge': 'XP-Gate VERSION sync hook',
|
|
70
82
|
'adapter-common.sh': 'detect_project_lang()'
|
|
71
83
|
};
|
|
72
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Contents that identify an xp-gate hook even when its banner wording changed.
|
|
87
|
+
*
|
|
88
|
+
* The banner is documentation and is expected to be edited; ownership should
|
|
89
|
+
* survive that. These are markers of the hook's actual machinery, which any
|
|
90
|
+
* xp-gate hook carries and no unrelated script would.
|
|
91
|
+
*/
|
|
92
|
+
const OWNERSHIP_MARKERS = [
|
|
93
|
+
'XP-Gate',
|
|
94
|
+
'xp-gate',
|
|
95
|
+
'_LE_CR', // the #546 line-ending self-check every shipped hook begins with
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
/** Banner prefixes per hook: how a hook announces its own identity. */
|
|
99
|
+
const BANNER_SELF_ID = {
|
|
100
|
+
'pre-commit': 'Pre-Commit Hook',
|
|
101
|
+
'pre-push': 'Pre-push Hook',
|
|
102
|
+
'post-merge': 'post-merge',
|
|
103
|
+
};
|
|
104
|
+
|
|
73
105
|
/**
|
|
74
106
|
* Hard timeout for all execSync calls in doctor diagnostics.
|
|
75
107
|
* Prevents hanging subprocesses (e.g., missing CLI tools on Windows)
|
|
@@ -248,6 +280,23 @@ function checkSpawnCapability(checks) {
|
|
|
248
280
|
return 0;
|
|
249
281
|
}
|
|
250
282
|
|
|
283
|
+
/**
|
|
284
|
+
* Read a hook file's text, or null when it is absent or unreadable.
|
|
285
|
+
*
|
|
286
|
+
* Returning null rather than throwing keeps the caller's decision in one place:
|
|
287
|
+
* absence is a FAIL, not an exception.
|
|
288
|
+
*
|
|
289
|
+
* @param {string} hookPath
|
|
290
|
+
* @returns {string|null}
|
|
291
|
+
*/
|
|
292
|
+
function readHookContent(hookPath) {
|
|
293
|
+
try {
|
|
294
|
+
return fs.readFileSync(hookPath, 'utf8');
|
|
295
|
+
} catch {
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
251
300
|
function isXpGateFile(filePath, signature) {
|
|
252
301
|
if (!fs.existsSync(filePath)) return false;
|
|
253
302
|
try {
|
|
@@ -258,6 +307,46 @@ function isXpGateFile(filePath, signature) {
|
|
|
258
307
|
}
|
|
259
308
|
}
|
|
260
309
|
|
|
310
|
+
/**
|
|
311
|
+
* The table of hook-ownership signatures, exposed so tests can assert the
|
|
312
|
+
* invariant that keeps #854 from recurring: no signature may embed a count.
|
|
313
|
+
*
|
|
314
|
+
* @returns {Record<string, string>}
|
|
315
|
+
*/
|
|
316
|
+
function getHookSignatures() {
|
|
317
|
+
return { ...SIGNATURES };
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Decide whether `content` is xp-gate's hook for `hookName`.
|
|
322
|
+
*
|
|
323
|
+
* Two things are checked, in order:
|
|
324
|
+
*
|
|
325
|
+
* 1. The hook's own signature, so a pre-commit file cannot satisfy pre-push.
|
|
326
|
+
* 2. A banner-independent ownership marker, but only when the file ALSO announces
|
|
327
|
+
* itself as this hook (`BANNER_SELF_ID`) and carries xp-gate machinery.
|
|
328
|
+
*
|
|
329
|
+
* The second exists because keying on a descriptive banner is what rotted: the
|
|
330
|
+
* banner is documentation and gets edited, while the check quietly turned every
|
|
331
|
+
* healthy install into "Missing or not xp-gate". Requiring a self-announcement is
|
|
332
|
+
* what keeps the looser test honest -- merely MENTIONING a hook name is not
|
|
333
|
+
* enough, and it has to be: `githooks/pre-commit` refers to `pre-push` twice in
|
|
334
|
+
* its comments, so a bare `content.includes(hookName)` accepted the wrong file.
|
|
335
|
+
*
|
|
336
|
+
* @param {string} hookName
|
|
337
|
+
* @param {string} content
|
|
338
|
+
* @returns {boolean}
|
|
339
|
+
*/
|
|
340
|
+
function isXpGateHookContent(hookName, content) {
|
|
341
|
+
if (typeof content !== 'string' || content.length === 0) return false;
|
|
342
|
+
const signature = SIGNATURES[hookName];
|
|
343
|
+
if (!signature) return false;
|
|
344
|
+
if (content.includes(signature)) return true;
|
|
345
|
+
const announcesItself = BANNER_SELF_ID[hookName];
|
|
346
|
+
if (!announcesItself || !content.includes(announcesItself)) return false;
|
|
347
|
+
return OWNERSHIP_MARKERS.some((marker) => content.includes(marker));
|
|
348
|
+
}
|
|
349
|
+
|
|
261
350
|
function getConfig() {
|
|
262
351
|
if (!fs.existsSync(CONFIG_FILE())) return null;
|
|
263
352
|
try {
|
|
@@ -1752,7 +1841,14 @@ function syncGlobalHooksFromRepo(ctx = {}) {
|
|
|
1752
1841
|
*/
|
|
1753
1842
|
function checkSingleHook(hooksDir, name, signature, label, checks) {
|
|
1754
1843
|
const hookPath = path.join(hooksDir, name);
|
|
1755
|
-
|
|
1844
|
+
// Read once and let the ownership check see the content: isXpGateHookContent
|
|
1845
|
+
// also accepts a banner-independent ownership marker, so a reworded banner no
|
|
1846
|
+
// longer turns every healthy install into "Missing or not xp-gate" (#854).
|
|
1847
|
+
const content = readHookContent(hookPath);
|
|
1848
|
+
const owned = content !== null
|
|
1849
|
+
? isXpGateHookContent(name, content)
|
|
1850
|
+
: isXpGateFile(hookPath, signature);
|
|
1851
|
+
if (!fs.existsSync(hookPath) || !owned) {
|
|
1756
1852
|
checks.push({ name: `${label}: ${name}`, status: 'FAIL', detail: 'Missing or not xp-gate' });
|
|
1757
1853
|
return 1;
|
|
1758
1854
|
}
|
|
@@ -3767,7 +3863,7 @@ function collectSkillDrift(skillDir, bundledFiles, bundledSkillDir, sha256) {
|
|
|
3767
3863
|
}
|
|
3768
3864
|
|
|
3769
3865
|
module.exports = {
|
|
3770
|
-
doctor, doctorDelphi, isXpGateFile, SIGNATURES,
|
|
3866
|
+
doctor, doctorDelphi, isXpGateFile, SIGNATURES, getHookSignatures, isXpGateHookContent,
|
|
3771
3867
|
fixVersionMismatch,
|
|
3772
3868
|
fixTemplateDirMismatch,
|
|
3773
3869
|
fixMissingHooks,
|