diff --git a/scripts/ci/check-spec-freshness.js b/scripts/ci/check-spec-freshness.js index cb74b3507f2..f5c2a0b73b5 100644 --- a/scripts/ci/check-spec-freshness.js +++ b/scripts/ci/check-spec-freshness.js @@ -25,6 +25,7 @@ * - git is always invoked through execFileSync with an argv array. Paths are * passed after `--`; the date is validated to YYYY-MM-DD first and passed * as a single argument. Nothing from spec Markdown reaches a shell. + * Literal pathspecs ensure Git looks at the same target we validated. * - the project root must not be a symlink; every enforced target is resolved * through fs.realpathSync and must stay inside the real project root. * - shallow clones report UNVERIFIED — incomplete history is never @@ -203,12 +204,19 @@ function gitProbe() { return gitState; } +// Git rejects literal mode when other global pathspec settings are active. +// These queries are always exact; keep other inherited Git settings intact. +const conflictingPathspecOptions = new Set(['GIT_GLOB_PATHSPECS', 'GIT_NOGLOB_PATHSPECS', 'GIT_ICASE_PATHSPECS']); +const gitHistoryEnv = Object.fromEntries( + Object.entries(process.env).filter(([name]) => !conflictingPathspecOptions.has(name.toUpperCase())) +); + /** stdout of `git log --format=%H --after= -- ` (empty when none). */ function gitHashesAfter(date, anchorPath) { const out = execFileSync( 'git', - ['log', '--format=%H', `--after=${date}`, '--', anchorPath], - { encoding: 'utf8', cwd: projectRoot, stdio: ['pipe', 'pipe', 'pipe'] } + ['--literal-pathspecs', 'log', '--format=%H', `--after=${date}`, '--', anchorPath], + { encoding: 'utf8', cwd: projectRoot, env: gitHistoryEnv, stdio: ['pipe', 'pipe', 'pipe'] } ); return out.trim().split('\n').filter(Boolean); } @@ -216,8 +224,8 @@ function gitHashesAfter(date, anchorPath) { function gitAllHashes(anchorPath) { const out = execFileSync( 'git', - ['log', '--format=%H', '--', anchorPath], - { encoding: 'utf8', cwd: projectRoot, stdio: ['pipe', 'pipe', 'pipe'] } + ['--literal-pathspecs', 'log', '--format=%H', '--', anchorPath], + { encoding: 'utf8', cwd: projectRoot, env: gitHistoryEnv, stdio: ['pipe', 'pipe', 'pipe'] } ); return out.trim().split('\n').filter(Boolean); } diff --git a/tests/ci/check-spec-freshness.test.js b/tests/ci/check-spec-freshness.test.js index 927b0db72da..2b0e9e49d17 100644 --- a/tests/ci/check-spec-freshness.test.js +++ b/tests/ci/check-spec-freshness.test.js @@ -190,6 +190,54 @@ describe('check-spec-freshness (v1, file-level)', () => { }); }); + describe('literal enforced paths', () => { + it('finds changes to a tracked file with brackets in its name', () => { + const repo = track(initRepo()); + commitFile(repo, 'src/[id].js', 'v1\n', daysAgo(5)); + commitFile(repo, 'src/[id].js', 'v2\n', daysAgo(1)); + writeSpec(repo, 'route', { lastVerified: daysAgo(3), anchors: ['src/[id].js::run'] }); + + const result = runChecker(repo); + assert.strictEqual(result.exitCode, 1); + assert.strictEqual(parseJson(result).specs[0].status, 'STALE'); + }); + + it('queries the exact path even with inherited pathspec options', () => { + const repo = track(initRepo()); + commitFile(repo, 'src/[id].js', 'v1\n', daysAgo(5)); + commitFile(repo, 'src/[id].js', 'v2\n', daysAgo(1)); + writeSpec(repo, 'route', { lastVerified: daysAgo(3), anchors: ['src/[id].js::run'] }); + + for (const variable of ['GIT_GLOB_PATHSPECS', 'GIT_NOGLOB_PATHSPECS', 'GIT_ICASE_PATHSPECS']) { + const result = runChecker(repo, { [variable]: '1' }); + assert.strictEqual(result.exitCode, 1, variable + ': ' + result.stderr); + assert.strictEqual(parseJson(result).specs[0].status, 'STALE'); + } + }); + + it('ignores decoy changes when the literal bracketed path is unchanged', () => { + const repo = track(initRepo()); + commitFile(repo, 'src/[id].js', 'unchanged\n', daysAgo(5)); + commitFile(repo, 'src/i.js', 'unrelated\n', daysAgo(1)); + writeSpec(repo, 'route', { lastVerified: daysAgo(3), anchors: ['src/[id].js::run'] }); + + const result = runChecker(repo); + assert.strictEqual(result.exitCode, 0); + assert.strictEqual(parseJson(result).specs[0].status, 'FRESH'); + }); + + it('does not use another file as evidence for an uncommitted bracketed path', () => { + const repo = track(initRepo()); + commitFile(repo, 'src/i.js', 'unrelated\n', daysAgo(5)); + fs.writeFileSync(path.join(repo, 'src/[id].js'), 'uncommitted\n'); + writeSpec(repo, 'route', { lastVerified: daysAgo(3), anchors: ['src/[id].js::run'] }); + + const result = runChecker(repo); + assert.strictEqual(result.exitCode, 0); + assert.strictEqual(parseJson(result).specs[0].status, 'UNVERIFIED'); + }); + }); + describe('verdicts on a real repo with history', () => { // Timeline: src/lib.js committed 300 days ago (v1) and 50 days ago (v2). let repo;