3 Commits

Author SHA1 Message Date
Affaan Mustafa
a0cee21209 test: isolate package-manager dependent hooks and formatter tests 2026-03-30 02:01:01 -04:00
Affaan Mustafa
fbd91aeef4 fix: unblock shared CI regressions 2026-03-30 01:49:45 -04:00
Affaan Mustafa
d20378cfba fix: unblock unicode safety CI lint 2026-03-29 22:08:49 -04:00
7 changed files with 143 additions and 34 deletions

View File

@@ -50,9 +50,7 @@ const writeModeSkip = new Set([
path.normalize('tests/scripts/check-unicode-safety.test.js'), path.normalize('tests/scripts/check-unicode-safety.test.js'),
]); ]);
const dangerousInvisibleRe = const emojiRe = /(?:\p{Extended_Pictographic}|\p{Regional_Indicator})/gu;
/[\u200B-\u200D\u2060\uFEFF\u202A-\u202E\u2066-\u2069\uFE00-\uFE0F\u{E0100}-\u{E01EF}]/gu;
const emojiRe = /[\p{Extended_Pictographic}\p{Regional_Indicator}]/gu;
const allowedSymbolCodePoints = new Set([ const allowedSymbolCodePoints = new Set([
0x00A9, 0x00A9,
0x00AE, 0x00AE,
@@ -106,9 +104,31 @@ function isAllowedEmojiLikeSymbol(char) {
return allowedSymbolCodePoints.has(char.codePointAt(0)); return allowedSymbolCodePoints.has(char.codePointAt(0));
} }
function isDangerousInvisibleCodePoint(codePoint) {
return (
(codePoint >= 0x200B && codePoint <= 0x200D) ||
codePoint === 0x2060 ||
codePoint === 0xFEFF ||
(codePoint >= 0x202A && codePoint <= 0x202E) ||
(codePoint >= 0x2066 && codePoint <= 0x2069) ||
(codePoint >= 0xFE00 && codePoint <= 0xFE0F) ||
(codePoint >= 0xE0100 && codePoint <= 0xE01EF)
);
}
function stripDangerousInvisibleChars(text) {
let next = '';
for (const char of text) {
if (!isDangerousInvisibleCodePoint(char.codePointAt(0))) {
next += char;
}
}
return next;
}
function sanitizeText(text) { function sanitizeText(text) {
let next = text; let next = text;
next = next.replace(dangerousInvisibleRe, ''); next = stripDangerousInvisibleChars(next);
for (const [pattern, replacement] of targetedReplacements) { for (const [pattern, replacement] of targetedReplacements) {
next = next.replace(pattern, replacement); next = next.replace(pattern, replacement);
@@ -146,6 +166,28 @@ function collectMatches(text, regex, kind) {
return matches; return matches;
} }
function collectDangerousInvisibleMatches(text) {
const matches = [];
let index = 0;
for (const char of text) {
const codePoint = char.codePointAt(0);
if (isDangerousInvisibleCodePoint(codePoint)) {
const { line, column } = lineAndColumn(text, index);
matches.push({
kind: 'dangerous-invisible',
char,
codePoint: `U+${codePoint.toString(16).toUpperCase()}`,
line,
column,
});
}
index += char.length;
}
return matches;
}
const changedFiles = []; const changedFiles = [];
const violations = []; const violations = [];
@@ -172,7 +214,7 @@ for (const filePath of listFiles(repoRoot)) {
} }
const fileViolations = [ const fileViolations = [
...collectMatches(text, dangerousInvisibleRe, 'dangerous-invisible'), ...collectDangerousInvisibleMatches(text),
...collectMatches(text, emojiRe, 'emoji'), ...collectMatches(text, emojiRe, 'emoji'),
]; ];

View File

@@ -1221,9 +1221,14 @@ async function runTests() {
fs.writeFileSync(path.join(rootDir, '.prettierrc'), '{}'); fs.writeFileSync(path.join(rootDir, '.prettierrc'), '{}');
fs.writeFileSync(filePath, 'export const value = 1;\n'); fs.writeFileSync(filePath, 'export const value = 1;\n');
createCommandShim(binDir, 'npx', logFile); createCommandShim(binDir, 'npx', logFile);
const isolatedHome = path.join(testDir, 'isolated-home');
fs.mkdirSync(path.join(isolatedHome, '.claude'), { recursive: true });
const stdinJson = JSON.stringify({ tool_input: { file_path: filePath } }); const stdinJson = JSON.stringify({ tool_input: { file_path: filePath } });
const result = await runScript(path.join(scriptsDir, 'post-edit-format.js'), stdinJson, withPrependedPath(binDir)); const result = await runScript(path.join(scriptsDir, 'post-edit-format.js'), stdinJson, withPrependedPath(binDir, {
HOME: isolatedHome,
USERPROFILE: isolatedHome
}));
assert.strictEqual(result.code, 0, 'Should exit 0 for config-only repo'); assert.strictEqual(result.code, 0, 'Should exit 0 for config-only repo');
const logEntries = readCommandLog(logFile); const logEntries = readCommandLog(logFile);

View File

@@ -37,6 +37,33 @@ function cleanupTestDir(testDir) {
fs.rmSync(testDir, { recursive: true, force: true }); fs.rmSync(testDir, { recursive: true, force: true });
} }
function withIsolatedHome(fn) {
const isolatedHome = fs.mkdtempSync(path.join(os.tmpdir(), 'pm-home-'));
const originalHome = process.env.HOME;
const originalUserProfile = process.env.USERPROFILE;
process.env.HOME = isolatedHome;
process.env.USERPROFILE = isolatedHome;
try {
return fn(isolatedHome);
} finally {
if (originalHome !== undefined) {
process.env.HOME = originalHome;
} else {
delete process.env.HOME;
}
if (originalUserProfile !== undefined) {
process.env.USERPROFILE = originalUserProfile;
} else {
delete process.env.USERPROFILE;
}
fs.rmSync(isolatedHome, { recursive: true, force: true });
}
}
// Test suite // Test suite
function runTests() { function runTests() {
console.log('\n=== Testing package-manager.js ===\n'); console.log('\n=== Testing package-manager.js ===\n');
@@ -711,9 +738,11 @@ function runTests() {
const originalEnv = process.env.CLAUDE_PACKAGE_MANAGER; const originalEnv = process.env.CLAUDE_PACKAGE_MANAGER;
try { try {
delete process.env.CLAUDE_PACKAGE_MANAGER; delete process.env.CLAUDE_PACKAGE_MANAGER;
withIsolatedHome(() => {
const result = pm.getPackageManager({ projectDir: testDir }); const result = pm.getPackageManager({ projectDir: testDir });
assert.strictEqual(result.name, 'npm', 'Should default to npm'); assert.strictEqual(result.name, 'npm', 'Should default to npm');
assert.strictEqual(result.source, 'default'); assert.strictEqual(result.source, 'default');
});
} finally { } finally {
if (originalEnv !== undefined) { if (originalEnv !== undefined) {
process.env.CLAUDE_PACKAGE_MANAGER = originalEnv; process.env.CLAUDE_PACKAGE_MANAGER = originalEnv;

View File

@@ -58,6 +58,33 @@ function cleanupTmpDirs() {
tmpDirs.length = 0; tmpDirs.length = 0;
} }
function withIsolatedHome(fn) {
const isolatedHome = fs.mkdtempSync(path.join(os.tmpdir(), 'resolve-fmt-home-'));
const originalHome = process.env.HOME;
const originalUserProfile = process.env.USERPROFILE;
process.env.HOME = isolatedHome;
process.env.USERPROFILE = isolatedHome;
try {
return fn(isolatedHome);
} finally {
if (originalHome !== undefined) {
process.env.HOME = originalHome;
} else {
delete process.env.HOME;
}
if (originalUserProfile !== undefined) {
process.env.USERPROFILE = originalUserProfile;
} else {
delete process.env.USERPROFILE;
}
fs.rmSync(isolatedHome, { recursive: true, force: true });
}
}
function runTests() { function runTests() {
console.log('\n=== Testing resolve-formatter.js ===\n'); console.log('\n=== Testing resolve-formatter.js ===\n');
@@ -168,11 +195,13 @@ function runTests() {
run('resolveFormatterBin: falls back to npx for biome', () => { run('resolveFormatterBin: falls back to npx for biome', () => {
const root = makeTmpDir(); const root = makeTmpDir();
withIsolatedHome(() => {
const result = resolveFormatterBin(root, 'biome'); const result = resolveFormatterBin(root, 'biome');
const expectedBin = process.platform === 'win32' ? 'npx.cmd' : 'npx'; const expectedBin = process.platform === 'win32' ? 'npx.cmd' : 'npx';
assert.strictEqual(result.bin, expectedBin); assert.strictEqual(result.bin, expectedBin);
assert.deepStrictEqual(result.prefix, ['@biomejs/biome']); assert.deepStrictEqual(result.prefix, ['@biomejs/biome']);
}); });
});
run('resolveFormatterBin: uses local prettier binary when available', () => { run('resolveFormatterBin: uses local prettier binary when available', () => {
const root = makeTmpDir(); const root = makeTmpDir();
@@ -188,11 +217,13 @@ function runTests() {
run('resolveFormatterBin: falls back to npx for prettier', () => { run('resolveFormatterBin: falls back to npx for prettier', () => {
const root = makeTmpDir(); const root = makeTmpDir();
withIsolatedHome(() => {
const result = resolveFormatterBin(root, 'prettier'); const result = resolveFormatterBin(root, 'prettier');
const expectedBin = process.platform === 'win32' ? 'npx.cmd' : 'npx'; const expectedBin = process.platform === 'win32' ? 'npx.cmd' : 'npx';
assert.strictEqual(result.bin, expectedBin); assert.strictEqual(result.bin, expectedBin);
assert.deepStrictEqual(result.prefix, ['prettier']); assert.deepStrictEqual(result.prefix, ['prettier']);
}); });
});
run('resolveFormatterBin: returns null for unknown formatter', () => { run('resolveFormatterBin: returns null for unknown formatter', () => {
const root = makeTmpDir(); const root = makeTmpDir();

View File

@@ -116,8 +116,10 @@ if (
fs.mkdirSync(codexDir, { recursive: true }); fs.mkdirSync(codexDir, { recursive: true });
fs.writeFileSync(configPath, config); fs.writeFileSync(configPath, config);
const syncResult = runBash(syncScript, [], { HOME: homeDir, CODEX_HOME: codexDir }); const syncResult = runBash(syncScript, ['--update-mcp'], { HOME: homeDir, CODEX_HOME: codexDir });
assert.strictEqual(syncResult.status, 0, syncResult.stderr || syncResult.stdout); assert.strictEqual(syncResult.status, 0, `${syncResult.stdout}\n${syncResult.stderr}`);
const syncedConfig = fs.readFileSync(configPath, 'utf8');
assert.match(syncedConfig, /^\[mcp_servers\.context7\]$/m);
const checkResult = runBash(checkScript, [], { HOME: homeDir, CODEX_HOME: codexDir }); const checkResult = runBash(checkScript, [], { HOME: homeDir, CODEX_HOME: codexDir });
assert.strictEqual(checkResult.status, 0, checkResult.stderr || checkResult.stdout); assert.strictEqual(checkResult.status, 0, checkResult.stderr || checkResult.stdout);