Skip to content

armgit / ECC

Public Imported from GitHub
0

The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.

fix(hooks): run Windows formatter shims in quality gate

Yann Roberto1cf47ab0e5e6
diff --git a/scripts/hooks/quality-gate.js b/scripts/hooks/quality-gate.js
index 37373b87c..dd5f5a0fb 100755
--- a/scripts/hooks/quality-gate.js
+++ b/scripts/hooks/quality-gate.js
@@ -17,6 +17,7 @@
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
+const { getLinterInvocation } = require('./pre-bash-commit-quality');
const { findProjectRoot, detectFormatter, resolveFormatterBin } = require('../lib/resolve-formatter');
@@ -31,10 +32,12 @@ const MAX_STDIN = 1024 * 1024;
* @returns {import('child_process').SpawnSyncReturns<string>}
*/
function exec(command, args, cwd = process.cwd()) {
- return spawnSync(command, args, {
+ // Reuse the Windows shim runner so .cmd formatters receive literal paths.
+ const invocation = getLinterInvocation(command, args);
+ return spawnSync(invocation.command, invocation.args, {
+ ...invocation.options,
cwd,
- encoding: 'utf8',
- env: process.env,
+ env: invocation.options.env || process.env,
timeout: 15000
});
}
diff --git a/tests/hooks/quality-gate.test.js b/tests/hooks/quality-gate.test.js
index cd3a05c4f..dfc75f1b4 100644
--- a/tests/hooks/quality-gate.test.js
+++ b/tests/hooks/quality-gate.test.js
@@ -8,8 +8,10 @@ const assert = require('assert');
const path = require('path');
const os = require('os');
const fs = require('fs');
+const { spawnSync } = require('child_process');
const qualityGate = require('../../scripts/hooks/quality-gate');
+const hookPath = path.resolve(__dirname, '../../scripts/hooks/quality-gate.js');
function test(name, fn) {
try {
@@ -155,5 +157,105 @@ if (test('returns original input for existing file with no formatter configured'
}
})) passed++; else failed++;
+// Exercise real executable shims: .cmd on Windows, executable shell scripts elsewhere.
+
+function withFormatterFixture(formatter, fallback, fn) {
+ const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'quality-gate project ')));
+ try {
+ const srcDir = path.join(root, 'source files');
+ const binDir = path.join(root, fallback ? 'runner bin' : 'node_modules/.bin');
+ fs.mkdirSync(srcDir, { recursive: true });
+ fs.mkdirSync(binDir, { recursive: true });
+ fs.writeFileSync(path.join(root, 'package.json'), '{}');
+ fs.writeFileSync(path.join(root, formatter === 'biome' ? 'biome.json' : '.prettierrc'), '{}');
+ const fileName = 'edited %ECC_TEST_LITERAL% !ECC_TEST_LITERAL! & file';
+ const filePath = path.join(srcDir, fileName + (formatter === 'biome' ? '.json' : '.js'));
+ const original = formatter === 'biome' ? '{"value":1}\n' : 'const value=1;\n';
+ fs.writeFileSync(filePath, original);
+ const capturePath = path.join(root, 'invocation.json');
+ const runnerPath = path.join(root, 'fake formatter.js');
+ fs.writeFileSync(runnerPath, [
+ "const fs = require('fs');",
+ "const path = require('path');",
+ 'const args = process.argv.slice(2);',
+ 'fs.writeFileSync(process.env.ECC_TEST_CAPTURE, JSON.stringify({ args, cwd: process.cwd() }));',
+ "if (args.includes('--write')) fs.appendFileSync(args.find(arg => path.isAbsolute(arg)), 'formatted\\n');",
+ 'process.exit(Number(process.env.ECC_TEST_EXIT || 0));'
+ ].join('\n'));
+ const binName = fallback ? 'npx' : formatter;
+ const shimPath = path.join(binDir, binName + (process.platform === 'win32' ? '.cmd' : ''));
+ const quoteSh = value => "'" + value.replace(/'/g, "'\\''") + "'";
+ const shim = process.platform === 'win32'
+ ? `@echo off\r\n"${process.execPath}" "${runnerPath}" %*\r\n`
+ : `#!/bin/sh\nexec ${quoteSh(process.execPath)} ${quoteSh(runnerPath)} "$@"\n`;
+ fs.writeFileSync(shimPath, shim, { mode: 0o755 });
+ const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => key.toUpperCase() !== 'PATH'));
+ Object.assign(env, {
+ PATH: binDir,
+ CLAUDE_PACKAGE_MANAGER: 'npm',
+ ECC_QUALITY_GATE_FIX: 'false',
+ ECC_QUALITY_GATE_STRICT: 'true',
+ ECC_TEST_CAPTURE: capturePath,
+ ECC_TEST_LITERAL: 'must-not-expand',
+ ECC_TEST_EXIT: '0'
+ });
+ const input = JSON.stringify({ tool_input: { file_path: filePath } });
+ const runHook = overrides => spawnSync(process.execPath, [hookPath], {
+ cwd: srcDir, input, encoding: 'utf8', timeout: 20000,
+ env: { ...env, ...overrides }
+ });
+ fn({ root, filePath, original, capturePath, input, runHook });
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true });
+ }
+}
+
+function assertInvocation(fixture, result, expectedArgs) {
+ assert.ifError(result.error);
+ assert.strictEqual(result.status, 0, result.stderr);
+ assert.strictEqual(result.stdout, fixture.input, 'Hook must preserve its input');
+ assert.ok(fs.existsSync(fixture.capturePath), 'Formatter shim was not executed');
+ assert.deepStrictEqual(JSON.parse(fs.readFileSync(fixture.capturePath, 'utf8')), {
+ args: expectedArgs, cwd: fixture.root
+ });
+}
+
+console.log('\nReal formatter subprocesses:');
+for (const formatter of ['prettier', 'biome']) {
+ for (const fix of [false, true]) {
+ if (test(`runs local ${formatter} in ${fix ? 'fix' : 'check'} mode with literal spaces and shell metacharacters`, () => {
+ withFormatterFixture(formatter, false, fixture => {
+ const result = fixture.runHook({ ECC_QUALITY_GATE_FIX: String(fix) });
+ const args = formatter === 'biome'
+ ? ['check', fixture.filePath, ...(fix ? ['--write'] : [])]
+ : [fix ? '--write' : '--check', fixture.filePath];
+ assertInvocation(fixture, result, args);
+ assert.strictEqual(result.stderr, '', 'Successful formatter should not warn');
+ assert.strictEqual(fs.readFileSync(fixture.filePath, 'utf8'), fixture.original + (fix ? 'formatted\n' : ''));
+ });
+ })) passed++; else failed++;
+ }
+ if (test(`runs ${formatter} through the package runner when no local formatter exists`, () => {
+ withFormatterFixture(formatter, true, fixture => {
+ const args = formatter === 'biome'
+ ? ['@biomejs/biome', 'check', fixture.filePath]
+ : ['prettier', '--check', fixture.filePath];
+ const result = fixture.runHook();
+ assertInvocation(fixture, result, args);
+ assert.strictEqual(result.stderr, '');
+ assert.strictEqual(fs.readFileSync(fixture.filePath, 'utf8'), fixture.original);
+ });
+ })) passed++; else failed++;
+}
+
+if (test('reports a real formatter failure in strict mode while preserving hook input', () => {
+ withFormatterFixture('prettier', false, fixture => {
+ const result = fixture.runHook({ ECC_TEST_EXIT: '2' });
+ assertInvocation(fixture, result, ['--check', fixture.filePath]);
+ assert.ok(result.stderr.includes(`[QualityGate] Prettier check failed for ${fixture.filePath}`));
+ assert.strictEqual(fs.readFileSync(fixture.filePath, 'utf8'), fixture.original);
+ });
+})) passed++; else failed++;
+
console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`);
process.exit(failed > 0 ? 1 : 0);