diff --git a/changelog.d/fix-pregate-hidden-folder-tests.md b/changelog.d/fix-pregate-hidden-folder-tests.md new file mode 100644 index 000000000..90649df71 --- /dev/null +++ b/changelog.d/fix-pregate-hidden-folder-tests.md @@ -0,0 +1,4 @@ +--- +important: blocker +--- +- **/ship's test lane now runs test files under hidden folders such as `.github/scripts/` instead of failing on them.** The lane passed each selected test to `bun test` as a bare relative path. Bun 1.4 reads a bare path as a name filter, and that search skips hidden folders, so `bun test .github/scripts/x.test.mjs` found nothing, exited 1, and the pregate reported `PREGATE_LANE_FAILED` on a green test. That blocked /ship on any gbrain PR touching `.github/scripts/`. Every relative path now reaches `bun test` as an explicit path (`./` added unless it already starts with `./`, `../` or `/`): the local lane, the small-machine remote gate command, the docs-only reader tests and /ship's base-red check all share one helper with the CI carry's per-file command, which already did this. A failing test in a hidden folder still fails the lane. diff --git a/lib/base-red.ts b/lib/base-red.ts index 442fb92bf..48c8e57af 100644 --- a/lib/base-red.ts +++ b/lib/base-red.ts @@ -19,6 +19,7 @@ import { type BaseRedPolicy } from './ship-policy'; import { git, sha256, writeJsonAtomic } from '../crew/lib/ship-queue-gate'; import { runBashSync } from './script-invocation'; import { resolveStateRoot } from './state-root'; +import { testFileArgs } from './test-paths'; export const PAUSE_HOURS = 24; @@ -83,7 +84,7 @@ export function runAt(repoRoot: string, sha: string, policy: BaseRedPolicy, file const s = runner(dir, policy.setup, policy.timeout_sec); if (s.status !== 0) return { status: s.status, out: s.out, error: `setup exited ${s.status ?? s.error ?? 'unknown'}`, setupFailed: true }; } - return runner(dir, policy.select.split('{files}').join(files.map(quote).join(' ')), policy.timeout_sec); + return runner(dir, policy.select.split('{files}').join(testFileArgs(policy.select, files).map(quote).join(' ')), policy.timeout_sec); } finally { git(repoRoot, ['worktree', 'remove', '--force', dir]); fs.rmSync(dir, { recursive: true, force: true }); diff --git a/lib/ci-carry.ts b/lib/ci-carry.ts index fd5c49f21..23f1b205d 100644 --- a/lib/ci-carry.ts +++ b/lib/ci-carry.ts @@ -30,6 +30,7 @@ import * as fs from 'node:fs'; import * as path from 'node:path'; import { pollDelayMs } from './gh-poll'; +import { explicitPath } from './test-paths'; import { normalPath, runChanged, runInventory, runReceiptFields, uncoveredFiles, verifyRun, type ApiRun, type RunDeps, type RunEvidence, type RunProfile, type RunSource } from './run-evidence'; export type CarryRefusal = 'dirty_tree' | 'no_profile' | 'no_run' | 'no_verified_run' | 'tree_mismatch' | 'no_tests' @@ -231,7 +232,7 @@ export function recordCiResult(stderr: string): { runs?: string; tests?: string; /** The per_file runner command for `files`: `{files}` replaced by the paths, each anchored with ./ (Bun reads a bare relative path as a substring filter) and single-quoted for bash. */ export const perFileCommand = (template: string, files: string[]): string => - template.replaceAll('{files}', files.map(f => `'${(/^(\/|\.\/)/.test(f) ? f : `./${f}`).replace(/'/g, `'\\''`)}'`).join(' ')); + template.replaceAll('{files}', files.map(f => `'${explicitPath(f).replace(/'/g, `'\\''`)}'`).join(' ')); /** `: ` errors for `ship_lanes_from`; empty when valid. Shared with lib/pregate/config.ts. */ export function shipLanesErrors(raw: unknown, profileNames: readonly string[]): Array<{ path: string; message: string }> { diff --git a/lib/pregate/checks/lanes.ts b/lib/pregate/checks/lanes.ts index 3ea583511..219e33735 100644 --- a/lib/pregate/checks/lanes.ts +++ b/lib/pregate/checks/lanes.ts @@ -43,6 +43,7 @@ import { cargoModel, type CargoModel } from '../rust'; import { adapterFor, readRange, STALL_IDLE, supervise, type Counter, type Supervised } from '../supervise'; import { ranFiles, verifyFromRun, type RunProfile } from '../../run-evidence'; import { matchesAny } from '../../ship-policy'; +import { testFileArgs } from '../../test-paths'; import type { CheckDef, CheckResult, PregateContext, RemoteObligation } from '../types'; /** The runner a `.py`-only selection gets; it needs the pytest module importable by python3 (`pytestMissing`). */ @@ -366,7 +367,7 @@ const quote = (f: string) => `'${f.replace(/'/g, `'\\''`)}'`; async function runOnce(ctx: PregateContext, env: NodeJS.ProcessEnv, g: RunGroup, files: string[], log: string, counter: Counter): Promise { // playwright test reads each file argument as a case-insensitive regex: escape it and anchor it to a path segment, so `a[1].spec.ts` runs that file, not `a1.spec.ts`. - const args = /(? `(?:^|/)${f.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}$`) : files; + const args = /(? `(?:^|/)${f.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}$`) : testFileArgs(g.runner, files); const joined = args.map(quote).join(' '); const run = g.runner.replace('{files}', () => joined); const command = g.cwd ? `(cd ${quote(g.cwd)} && ${run})` : run; @@ -460,7 +461,7 @@ const GSTACK_ROOT = path.resolve(import.meta.dir, '..', '..', '..'); function remoteObligation(ctx: PregateContext, sel: Selection): RemoteObligation { const declared = ctx.config.remote_gate; const runner = detectRunner(ctx, sel.gateTests); - const command = declared ?? `${GSTACK_ROOT}/scripts/ubicloud/ubi-runner.sh run --src ${quote(ctx.repoRoot)} -- ${quote(runner ? runner.replace('{files}', sel.gateTests.map(quote).join(' ')) : ' ')}`; + const command = declared ?? `${GSTACK_ROOT}/scripts/ubicloud/ubi-runner.sh run --src ${quote(ctx.repoRoot)} -- ${quote(runner ? runner.replace('{files}', testFileArgs(runner, sel.gateTests).map(quote).join(' ')) : ' ')}`; const [workflow, job] = declared ? REMOTE_GATE_LANE.split('/') as [string, string] : ['test_backend', ctx.testBackend]; return { lane: `${workflow}/${job}`, platform: 'remote', workflow, job, tests: sel.gateTests, receipt: null, command }; } diff --git a/lib/pregate/docs-only-lane.ts b/lib/pregate/docs-only-lane.ts index b10c5c979..cbb434837 100644 --- a/lib/pregate/docs-only-lane.ts +++ b/lib/pregate/docs-only-lane.ts @@ -11,6 +11,7 @@ import * as path from 'node:path'; import { bunIsolationArgs } from '../bun-isolation'; import { addedLines, brokenLinks } from '../doc-links'; import { classifyDocsOnly } from '../docs-only'; +import { explicitPath } from '../test-paths'; import type { CheckResult, PregateContext } from './types'; /** The lanes result for a docs-only change, or null when the change is not docs-only (or cannot be classified). */ @@ -34,8 +35,8 @@ export function docsOnlyLane(ctx: PregateContext, base: CheckResult): CheckResul const done = (detail: string): CheckResult => ({ ...base, lines, status: 'pass', remote: [], detail }); if (!tests.length) return done(`docs-only: ${r.files.length} file(s); links on added lines resolve; no docs reader tests; the test suites are not owed`); if (!ctx.allowRepoCommands) return { ...base, lines, status: 'incomplete', code: 'REPO_COMMANDS_NOT_ALLOWED', detail: `docs-only: ${tests.length} docs reader test file(s) selected but not run (pass --allow-repo-commands)`, fix: 'rerun with --allow-repo-commands (what /ship passes)' }; - const run = spawnSync(process.execPath, [...bunIsolationArgs(), 'test', '--timeout=30000', ...tests], { cwd: ctx.repoRoot, encoding: 'utf8', timeout: 600_000, maxBuffer: 64 * 1024 * 1024 }); + const run = spawnSync(process.execPath, [...bunIsolationArgs(), 'test', '--timeout=30000', ...tests.map(explicitPath)], { cwd: ctx.repoRoot, encoding: 'utf8', timeout: 600_000, maxBuffer: 64 * 1024 * 1024 }); const tail = `${run.stdout ?? ''}${run.stderr ?? ''}`.trim().split('\n').slice(-3).join(' | '); - if (run.status !== 0) return { ...base, lines: [...lines, tail], status: 'fail', cause: 'diff', code: 'PREGATE_LANE_FAILED', detail: `docs-only: docs reader tests failed (${run.error ? run.error.message : `exit ${run.status}`})`, fix: `bun test ${tests.join(' ')}` }; + if (run.status !== 0) return { ...base, lines: [...lines, tail], status: 'fail', cause: 'diff', code: 'PREGATE_LANE_FAILED', detail: `docs-only: docs reader tests failed (${run.error ? run.error.message : `exit ${run.status}`})`, fix: `bun test ${tests.map(explicitPath).join(' ')}` }; return done(`docs-only: ${r.files.length} file(s); links on added lines resolve; ${tests.length} docs reader test file(s) passed; the test suites are not owed`); } diff --git a/lib/pregate/supervise.ts b/lib/pregate/supervise.ts index 37cc3210e..49331283d 100644 --- a/lib/pregate/supervise.ts +++ b/lib/pregate/supervise.ts @@ -23,9 +23,10 @@ import { spawn, spawnSync } from 'node:child_process'; import * as fs from 'node:fs'; import { bashCommandInvocation } from '../script-invocation'; +import { BUN_TEST } from '../test-paths'; export type ProgressAdapter = 'bun'; -export const PROGRESS_ADAPTERS: Record = { bun: /(?:^|[\s;&|(])bun\s+test\b/ }; +export const PROGRESS_ADAPTERS: Record = { bun: BUN_TEST }; export interface Counter { done: number; total: number } diff --git a/lib/test-paths.ts b/lib/test-paths.ts new file mode 100644 index 000000000..a7484d28b --- /dev/null +++ b/lib/test-paths.ts @@ -0,0 +1,15 @@ +/** + * Test file arguments for `bun test`. Bun reads a bare relative path as a name filter, and that filter's search skips + * hidden folders, so `bun test .github/scripts/x.test.mjs` finds nothing and exits 1 (GST-2640, Bun 1.4.4). A path that + * starts with `./`, `../` or `/` is read as a path. Bun still drops a missing path from a run of several files without + * an error, so a caller rejects a missing file before Bun runs. + */ + +/** A command that runs `bun test` as a whole command word, with or without Bun's own flags before `test`. */ +export const BUN_TEST = /(?:^|[\s;&|(])bun(?:\s+--?[\w-]+(?:=\S+)?)*\s+test\b/; + +/** `file` as a path Bun reads as a path: `./` added unless it already starts with `./`, `../` or `/`. */ +export const explicitPath = (file: string): string => (/^\.{0,2}\//.test(file) ? file : `./${file}`); + +/** The file arguments for `command`: explicit paths when it runs `bun test`, the files unchanged for any other runner. */ +export const testFileArgs = (command: string, files: readonly string[]): string[] => (BUN_TEST.test(command) ? files.map(explicitPath) : [...files]); diff --git a/test/pregate-hidden-folder-paths.test.ts b/test/pregate-hidden-folder-paths.test.ts new file mode 100644 index 000000000..3252a13ae --- /dev/null +++ b/test/pregate-hidden-folder-paths.test.ts @@ -0,0 +1,113 @@ +/** + * GST-2640 (gbrain #3965): the lanes check ran `bun test {files}` with bare repo-relative paths. Bun 1.4.4 reads a bare + * path as a name filter whose search skips hidden folders, so a selected test under .github/scripts/ ran nothing, Bun + * exited 1 and the lane failed with PREGATE_LANE_FAILED on a green test. The fixture repo has its test under + * .github/scripts/; the failing control proves the lane still judges that test instead of passing it unrun. + */ +import { afterEach, describe, expect, test } from 'bun:test'; +import * as fs from 'node:fs'; +import * as path from 'node:path'; +import { buildContext } from '../lib/pregate/context'; +import { runPregate } from '../lib/pregate/index'; +import { explicitPath, testFileArgs } from '../lib/test-paths'; +import { runAt } from '../lib/base-red'; +import { perFileCommand } from '../lib/ci-carry'; +import { execFileSync } from 'node:child_process'; +import { cleanup, commit, makeFixtureRepo, type FixtureRepo } from './helpers/pregate-fixture'; + +const repos: FixtureRepo[] = []; +afterEach(() => { while (repos.length) cleanup(repos.pop()!); }); + +const TEST = '.github/scripts/select.test.mjs'; +const GREEN = "import { expect, test } from 'bun:test';\nimport { pick } from './select.mjs';\ntest('picks the first lane', () => expect(pick(['a', 'b'])).toBe('a'));\n"; +const RED = "import { expect, test } from 'bun:test';\nimport { pick } from './select.mjs';\ntest('picks the last lane', () => expect(pick(['a', 'b'])).toBe('b'));\n"; +const DETECTED = { $schema: 'https://github.com/garrytan/gstack/docs/pregate.md#pregate-v1', dependencies: { '.github/scripts/**': [TEST], 'docs/**': [], 'README.md': [] } }; +const DECLARED = { ...DETECTED, runner: 'bun test {files}' }; + +async function lanes(policy: Record, files: Record, unlink: string[] = [], machine: { cores: number; testBackend: string } = { cores: 64, testBackend: 'local' }) { + const repo = makeFixtureRepo({ pregate: policy, baseFiles: { 'bun.lock': '' } }); + repos.push(repo); + commit(repo, { '.github/scripts/select.mjs': 'export const pick = (lanes) => lanes[0];\n', ...files }, 'add the lane selector'); + for (const rel of unlink) fs.rmSync(path.join(repo.work, rel)); + const loaded = buildContext({ repoRoot: repo.work, base: 'main', policyFrom: 'base', allowRepoCommands: true, env: { ...process.env, ...repo.env }, progress: () => {}, ...machine } as never); + if (!('ctx' in loaded)) throw new Error('fixture context refused'); + const file = await runPregate(loaded.ctx, { stage: 'tests', only: ['lanes'] }); + return file.checks.find(c => c.id === 'lanes')!; +} + +describe('explicit test paths for bun test', () => { + // Value: protects=every relative test path reaches bun test as a path; fails_when=a bare path (Bun's name filter) or a doubled ./ is passed; + // why_new=GST-2640; seam=the pure helper the lanes, remote-gate, docs-only, base-red and CI-carry commands share + test('adds ./ to a bare relative path, keeps explicit ones, and leaves other runners alone', () => { + expect(['.github/scripts/x.test.mjs', 'test/a.test.ts', './b.test.ts', '../c.test.ts', '/abs/d.test.ts'].map(explicitPath)) + .toEqual(['./.github/scripts/x.test.mjs', './test/a.test.ts', './b.test.ts', '../c.test.ts', '/abs/d.test.ts']); + expect(testFileArgs('bun test --timeout=30000 {files}', ['.github/x.test.ts'])).toEqual(['./.github/x.test.ts']); + expect(testFileArgs('bun --no-env-file --config=/dev/null test {files}', ['.github/x.test.ts'])).toEqual(['./.github/x.test.ts']); + expect(testFileArgs('npx vitest run {files}', ['.github/x.test.ts'])).toEqual(['.github/x.test.ts']); + }); + + // Value: protects=perFileCommand single-quotes each path after anchoring it with one ./; fails_when=quoting runs before the prefix, ./ doubles, or an absolute path gains ./; + // why_new=no test called perFileCommand after it moved to explicitPath; seam=none; no_claim=bash execution of the command (string only) + test('perFileCommand anchors and quotes per-file paths', () => { + expect(perFileCommand('bun test {files}', [".github/it's.test.ts", './a.test.ts', '/abs/b.test.ts'])) + .toBe("bun test './.github/it'\\''s.test.ts' './a.test.ts' '/abs/b.test.ts'"); + }); +}); + +describe.skipIf(process.platform === 'win32')('lanes check on a test under a hidden folder', () => { + // Value: protects=a green test under .github/ passes the lane, with the runner detected from bun.lock or declared; + // fails_when=bun test gets the bare path, runs nothing and exits 1 (PREGATE_LANE_FAILED); + // why_new=GST-2640, gbrain #3965 blocked /ship on bun 1.4.4; seam=runPregate --only lanes on a fixture repo that runs bun test + for (const [name, policy] of [['detected', DETECTED], ['declared', DECLARED]] as const) { + test(`a green hidden-folder test passes the lane (${name} runner)`, async () => { + const result = await lanes(policy, { [TEST]: GREEN }); + expect(result.lines).toContainEqual(expect.stringContaining("bun test './.github/scripts/select.test.mjs'")); + // The fixture's windows.yml claims every test, so the green local lane leaves a windows obligation instead of a bare pass. + expect(result).toMatchObject({ status: 'requires-remote', detail: '1 tests ran in 1 file(s), exit 0; 1 requires-remote obligation(s)' }); + }, 120_000); + } + + // Value: protects=the remote-gate command a small machine records carries ./-prefixed bun paths; fails_when=remoteObligation drops testFileArgs and records the bare .github path; + // why_new=only the local lane was tested; seam=none (runPregate on a 4-core ubicloud-backend context); no_claim=the remote VM run itself + test('a small machine records the remote-gate command with ./ paths', async () => { + const result = await lanes(DETECTED, { [TEST]: GREEN }, [], { cores: 4, testBackend: 'ubicloud' }); + expect(result.status).toBe('requires-remote'); + const run = result.lines?.find(l => l.startsWith('run: ')) ?? ''; + expect(run).toContain('./.github/scripts/select.test.mjs'); + expect(run).not.toMatch(/'\.github\/scripts/); + }, 120_000); + + // Value: protects=the lane still runs and judges a hidden-folder test; fails_when=the fix passes the lane without running the test + test('a failing hidden-folder test still fails the lane', async () => { + const result = await lanes(DETECTED, { [TEST]: RED }); + expect(result).toMatchObject({ status: 'fail', code: 'PREGATE_LANE_FAILED', detail: expect.stringContaining('1 failed of 1 ran') }); + }, 120_000); + + // Value: protects=AGENTS.md's rule that a missing test file never reaches Bun, which drops a missing ./ path beside a present one + // without an error; fails_when=selection hands bun test a path the working tree no longer has + test('a declared test file missing from the working tree is never passed to bun test', async () => { + const gone = '.github/scripts/gone.test.mjs'; + const result = await lanes({ ...DETECTED, dependencies: { ...DETECTED.dependencies, '.github/scripts/**': [TEST, gone] } }, { [TEST]: GREEN, [gone]: GREEN }, [gone]); + expect(result.lines).toContain("ran: bun test './.github/scripts/select.test.mjs'"); + expect(result.lines?.join('\n')).not.toContain('gone.test.mjs'); + }, 120_000); +}); + +describe.skipIf(process.platform === 'win32')('base-red select anchors bun test paths', () => { + // Value: protects=runAt hands a bun select ./-prefixed, quoted paths; fails_when=runAt drops testFileArgs and passes the bare .github path; + // why_new=no test reached runAt's select string; seam=none (runAt's existing Runner parameter); no_claim=bun execution (recording runner) + test('runAt passes ./-prefixed paths to a bun select', () => { + const repo = makeFixtureRepo({ pregate: DETECTED }); + repos.push(repo); + const prev = process.env.GSTACK_STATE_ROOT; + process.env.GSTACK_STATE_ROOT = path.join(repo.work, '..', 'state'); + try { + const seen: string[] = []; + const sha = execFileSync('git', ['-C', repo.work, 'rev-parse', 'HEAD'], { encoding: 'utf8', timeout: 10_000 }).trim(); + runAt(repo.work, sha, { select: 'bun test {files}', timeout_sec: 30 } as never, [TEST, 'test/a.test.ts'], (_d, cmd) => { seen.push(cmd); return { status: 0, out: '' }; }); + expect(seen).toEqual(["bun test './.github/scripts/select.test.mjs' './test/a.test.ts'"]); + } finally { + if (prev === undefined) delete process.env.GSTACK_STATE_ROOT; else process.env.GSTACK_STATE_ROOT = prev; + } + }); +});