From 5416bc2a4cfaf7cb7cf21e4817a138cceb694024 Mon Sep 17 00:00:00 2001 From: appergb <192432815+appergb@users.noreply.github.com> Date: Fri, 27 Nov 2026 22:20:58 +0800 Subject: [PATCH] test(ci): retry Cargo fixture cleanup without masking failures --- .../app/scripts/ci-cargo-fixture-cleanup.mjs | 28 ++++++++ .../app/scripts/ci-performance.test.mjs | 7 +- .../windows-cargo-fixture-cleanup.test.mjs | 66 +++++++++++++++++++ 3 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 openless-all/app/scripts/ci-cargo-fixture-cleanup.mjs create mode 100644 openless-all/app/scripts/windows-cargo-fixture-cleanup.test.mjs diff --git a/openless-all/app/scripts/ci-cargo-fixture-cleanup.mjs b/openless-all/app/scripts/ci-cargo-fixture-cleanup.mjs new file mode 100644 index 000000000..1113df5be --- /dev/null +++ b/openless-all/app/scripts/ci-cargo-fixture-cleanup.mjs @@ -0,0 +1,28 @@ +import { rmSync } from 'node:fs'; + +export function withCargoFixtureCleanup(directory, run, remove = rmSync) { + let failed = false; + let failure; + try { + return run(); + } catch (error) { + failed = true; + failure = error; + throw error; + } finally { + try { + // Retry the observed Windows busy removals only in this Cargo fixture. + // Use Node's bounded retry budget; exhaustion still throws. + remove(directory, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }); + } catch (cleanupError) { + if (failed) { + throw new AggregateError( + [failure, cleanupError], + 'Cargo fixture failed and its temporary directory could not be removed', + { cause: failure }, + ); + } + throw cleanupError; + } + } +} diff --git a/openless-all/app/scripts/ci-performance.test.mjs b/openless-all/app/scripts/ci-performance.test.mjs index 042d40e47..6f396423f 100644 --- a/openless-all/app/scripts/ci-performance.test.mjs +++ b/openless-all/app/scripts/ci-performance.test.mjs @@ -12,6 +12,7 @@ import { import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { load } from 'js-yaml'; +import { withCargoFixtureCleanup } from './ci-cargo-fixture-cleanup.mjs'; const workflow = (name) => load(readFileSync(new URL(`../../../.github/workflows/${name}`, import.meta.url), 'utf8')); @@ -99,7 +100,7 @@ assert.equal( // macOS exposes /private/var through /var. Use one canonical path for relative // and absolute manifests so Cargo does not fingerprint two names for one source. const cargoScratch = realpathSync(mkdtempSync(join(tmpdir(), 'openless-cargo-reuse-'))); -try { +withCargoFixtureCleanup(cargoScratch, () => { for (const path of ['host/src', 'shared/src', 'backend/src']) { mkdirSync(join(cargoScratch, path), { recursive: true }); } @@ -205,9 +206,7 @@ ci-shared = { path = "../shared" } assert.ifError(broken.error); assert.notEqual(broken.status, 0); assert.match(broken.stderr, /production diagnostics checked/); -} finally { - rmSync(cargoScratch, { recursive: true, force: true }); -} +}); // Run the real Windows build shell with only npm stubbed. Check artifact-flow // decisions, not the spelling of the commands: one compile, optional rebundle, diff --git a/openless-all/app/scripts/windows-cargo-fixture-cleanup.test.mjs b/openless-all/app/scripts/windows-cargo-fixture-cleanup.test.mjs new file mode 100644 index 000000000..893dcdb73 --- /dev/null +++ b/openless-all/app/scripts/windows-cargo-fixture-cleanup.test.mjs @@ -0,0 +1,66 @@ +import assert from 'node:assert/strict'; +import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { withCargoFixtureCleanup } from './ci-cargo-fixture-cleanup.mjs'; + +// This regression never starts Cargo, a compiler, or any other child process. +const root = mkdtempSync(join(tmpdir(), 'openless-cargo-cleanup-contract-')); +const fixture = (name) => { + const directory = join(root, name); + mkdirSync(directory); + writeFileSync(join(directory, 'fixture.txt'), 'fixture'); + return directory; +}; +try { + const success = fixture('success'); + assert.equal(withCargoFixtureCleanup(success, () => 42), 42); + assert.equal(existsSync(success), false); + + const primary = new Error('fixture compilation or assertion failed'); + const failed = fixture('primary-failure'); + assert.throws( + () => + withCargoFixtureCleanup(failed, () => { + throw primary; + }), + (error) => error === primary, + ); + assert.equal(existsSync(failed), false, 'the original failure must still clean its fixture'); + + const cleanupError = Object.assign(new Error('fixture removal busy'), { code: 'EBUSY' }); + const cannotRemove = (directory, options) => { + assert.equal(directory.startsWith(root), true); + assert.deepEqual(options, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }); + // Simulate rmSync exhausting its own retry budget; do not emulate Windows locks. + throw cleanupError; + }; + const cleanupOnly = fixture('cleanup-failure'); + assert.throws( + () => withCargoFixtureCleanup(cleanupOnly, () => 42, cannotRemove), + (error) => error === cleanupError, + ); + assert.equal(existsSync(cleanupOnly), true); + + const both = fixture('both-failures'); + assert.throws( + () => + withCargoFixtureCleanup( + both, + () => { + throw primary; + }, + cannotRemove, + ), + (error) => { + assert.ok(error instanceof AggregateError); + assert.equal(error.cause, primary); + assert.deepEqual(error.errors, [primary, cleanupError]); + return true; + }, + ); + assert.equal(existsSync(both), true); +} finally { + rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }); +} +console.log('Windows Cargo fixture cleanup contract passed (no compilation)');