diff --git a/openless-all/app/scripts/ci-cargo-fixture-command.mjs b/openless-all/app/scripts/ci-cargo-fixture-command.mjs new file mode 100644 index 000000000..8bdbccca9 --- /dev/null +++ b/openless-all/app/scripts/ci-cargo-fixture-command.mjs @@ -0,0 +1,37 @@ +import { spawnSync } from 'node:child_process'; + +export function createCargoFixtureRunner( + directory, + env, + { platform = process.platform, spawn = spawnSync, report = console.error } = {}, +) { + let first = true; + return (args) => { + // The first isolated Windows test-profile link exceeded 30s on hosted CI. + // Give only that cold invocation 15s extra; do not retry or enlarge later runs. + const timeout = first && platform === 'win32' ? 45_000 : 30_000; + first = false; + const commandArgs = [...args, '--offline']; + const result = spawn('cargo', commandArgs, { + cwd: directory, + env, + encoding: 'utf8', + timeout, + }); + if (result.error) { + report( + `[cargo-fixture] ${JSON.stringify({ + args: commandArgs, + timeout, + code: result.error.code, + status: result.status, + signal: result.signal, + stdout: result.stdout, + stderr: result.stderr, + })}`, + ); + } + // Preserve the exact spawn result/error for expectSuccess and cleanup aggregation. + return result; + }; +} diff --git a/openless-all/app/scripts/ci-performance.test.mjs b/openless-all/app/scripts/ci-performance.test.mjs index 6f396423f..7806eef85 100644 --- a/openless-all/app/scripts/ci-performance.test.mjs +++ b/openless-all/app/scripts/ci-performance.test.mjs @@ -13,6 +13,7 @@ import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { load } from 'js-yaml'; import { withCargoFixtureCleanup } from './ci-cargo-fixture-cleanup.mjs'; +import { createCargoFixtureRunner } from './ci-cargo-fixture-command.mjs'; const workflow = (name) => load(readFileSync(new URL(`../../../.github/workflows/${name}`, import.meta.url), 'utf8')); @@ -145,13 +146,7 @@ ci-shared = { path = "../shared" } CARGO_PROFILE_TEST_DEBUG: '0', CARGO_TARGET_DIR: join(cargoScratch, 'host/target'), }; - const cargo = (args) => - spawnSync('cargo', [...args, '--offline'], { - cwd: cargoScratch, - env: cargoEnv, - encoding: 'utf8', - timeout: 30_000, - }); + const cargo = createCargoFixtureRunner(cargoScratch, cargoEnv); const expectSuccess = (result) => { assert.ifError(result.error); assert.equal(result.status, 0, result.stdout + result.stderr); diff --git a/openless-all/app/scripts/windows-cargo-fixture-command.test.mjs b/openless-all/app/scripts/windows-cargo-fixture-command.test.mjs new file mode 100644 index 000000000..cd792495c --- /dev/null +++ b/openless-all/app/scripts/windows-cargo-fixture-command.test.mjs @@ -0,0 +1,89 @@ +import assert from 'node:assert/strict'; +import { createCargoFixtureRunner } from './ci-cargo-fixture-command.mjs'; +import { withCargoFixtureCleanup } from './ci-cargo-fixture-cleanup.mjs'; + +// Inject spawn results: never start Cargo or any child process in this regression. +for (const platform of ['win32', 'darwin', 'linux']) { + const calls = []; + const messages = []; + const timeoutError = Object.assign(new Error('spawnSync cargo ETIMEDOUT'), { code: 'ETIMEDOUT' }); + const results = [ + { status: 0, stdout: 'compiled', stderr: '' }, + { + error: timeoutError, + status: null, + signal: 'SIGTERM', + stdout: 'partial compiler output', + stderr: 'fixture diagnostics', + }, + { status: 101, stdout: '', stderr: 'production diagnostics checked' }, + ]; + const directory = 'isolated-fixture'; + const env = { CARGO_TARGET_DIR: 'isolated-fixture/host/target' }; + const cargo = createCargoFixtureRunner(directory, env, { + platform, + spawn: (command, args, options) => { + calls.push({ command, args, options }); + return results[calls.length - 1]; + }, + report: (message) => messages.push(message), + }); + const args = ['test', '--manifest-path', 'host/Cargo.toml', '--lib', '--bins', '--no-run']; + assert.equal(cargo(args), results[0]); + assert.equal(cargo(['build']), results[1], 'a timeout must not be retried or replaced'); + assert.equal(cargo(['test']), results[2], 'a compiler failure must not turn into success'); + assert.equal(results[1].error, timeoutError); + assert.equal(calls.length, 3); + assert.deepEqual( + calls.map((call) => call.options.timeout), + platform === 'win32' ? [45_000, 30_000, 30_000] : [30_000, 30_000, 30_000], + ); + assert.deepEqual(calls[0].args, [...args, '--offline']); + assert.deepEqual(args, ['test', '--manifest-path', 'host/Cargo.toml', '--lib', '--bins', '--no-run']); + for (const call of calls) { + assert.equal(call.command, 'cargo'); + assert.equal(call.options.cwd, directory); + assert.equal(call.options.env, env); + assert.equal(call.options.encoding, 'utf8'); + assert.equal(call.args.at(-1), '--offline'); + assert.equal(call.options.shell, undefined, 'keep direct child-process ownership'); + } + assert.equal(messages.length, 1); + assert.deepEqual(JSON.parse(messages[0].slice('[cargo-fixture] '.length)), { + args: ['build', '--offline'], + timeout: 30_000, + code: 'ETIMEDOUT', + status: null, + signal: 'SIGTERM', + stdout: 'partial compiler output', + stderr: 'fixture diagnostics', + }); +} + +const firstTimeout = Object.assign(new Error('spawnSync cargo ETIMEDOUT'), { code: 'ETIMEDOUT' }); +const cleanupBusy = Object.assign(new Error('fixture still busy'), { code: 'EBUSY' }); +const diagnostics = []; +const coldCargo = createCargoFixtureRunner('isolated-fixture', {}, { + platform: 'win32', + spawn: () => ({ error: firstTimeout, status: null, stdout: '', stderr: 'cold link output' }), + report: (message) => diagnostics.push(message), +}); +assert.throws( + () => + withCargoFixtureCleanup( + 'isolated-fixture', + () => assert.ifError(coldCargo(['test']).error), + () => { + throw cleanupBusy; + }, + ), + (error) => { + assert.ok(error instanceof AggregateError); + assert.equal(error.errors[0].actual, firstTimeout); + assert.equal(error.cause, error.errors[0]); + assert.equal(error.errors[1], cleanupBusy); + return true; + }, +); +assert.equal(JSON.parse(diagnostics[0].slice('[cargo-fixture] '.length)).timeout, 45_000); +console.log('Windows Cargo fixture command contract passed (mock spawn only)');