From aaaff77ef9976a8fcb770192914caabf27e7e987 Mon Sep 17 00:00:00 2001 From: haelyra <49814733+haelyra@users.noreply.github.com> Date: Tue, 25 Aug 2026 17:23:24 -0400 Subject: [PATCH] test(opencode): avoid path race in recovery fixture --- tests/lib/opencode-legacy-migration.test.js | 30 ++++++++++++++++----- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/tests/lib/opencode-legacy-migration.test.js b/tests/lib/opencode-legacy-migration.test.js index df7564e8e..c22499cf5 100644 --- a/tests/lib/opencode-legacy-migration.test.js +++ b/tests/lib/opencode-legacy-migration.test.js @@ -302,10 +302,13 @@ test('legacy cleanup never overwrites a file created during quarantine recovery' const destinationPath = path.join(targetRoot, 'managed.md'); let quarantinePath = null; let effectiveSafePath = destinationPath; + const openDescriptors = []; try { fs.mkdirSync(targetRoot, { recursive: true }); fs.writeFileSync(destinationPath, 'managed-old\n'); - const originalStat = fs.lstatSync(destinationPath, { bigint: true }); + const originalDescriptor = fs.openSync(destinationPath, 'r'); + openDescriptors.push(originalDescriptor); + const originalStat = fs.fstatSync(originalDescriptor, { bigint: true }); let injected = false; const fileSystem = new Proxy(fs, { get(target, property) { @@ -318,10 +321,14 @@ test('legacy cleanup never overwrites a file created during quarantine recovery' } if (property === 'lstatSync') { return (filePath, options) => { - const stat = fs.lstatSync(filePath, options); if (!injected && quarantinePath && filePath === quarantinePath) { injected = true; - fs.writeFileSync(effectiveSafePath, 'user-new\n', { flag: 'wx' }); + const quarantineDescriptor = fs.openSync(filePath, 'r'); + openDescriptors.push(quarantineDescriptor); + const stat = fs.fstatSync(quarantineDescriptor, options); + const userDescriptor = fs.openSync(effectiveSafePath, 'wx', 0o600); + openDescriptors.push(userDescriptor); + fs.writeFileSync(userDescriptor, 'user-new\n'); return new Proxy(stat, { get(statTarget, statProperty) { if (statProperty === 'ino') return statTarget.ino + 1n; @@ -330,7 +337,7 @@ test('legacy cleanup never overwrites a file created during quarantine recovery' }, }); } - return stat; + return fs.lstatSync(filePath, options); }; } const value = Reflect.get(target, property, target); @@ -350,9 +357,20 @@ test('legacy cleanup never overwrites a file created during quarantine recovery' return true; } ); - assert.strictEqual(fs.readFileSync(destinationPath, 'utf8'), 'user-new\n'); - assert.strictEqual(fs.readFileSync(quarantinePath, 'utf8'), 'managed-old\n'); + const destinationDescriptor = fs.openSync(destinationPath, 'r'); + openDescriptors.push(destinationDescriptor); + const retainedDescriptor = fs.openSync(quarantinePath, 'r'); + openDescriptors.push(retainedDescriptor); + assert.strictEqual(fs.readFileSync(destinationDescriptor, 'utf8'), 'user-new\n'); + assert.strictEqual(fs.readFileSync(retainedDescriptor, 'utf8'), 'managed-old\n'); } finally { + for (const descriptor of openDescriptors) { + try { + fs.closeSync(descriptor); + } catch (_error) { + // Best-effort fixture cleanup. + } + } fs.rmSync(homeDir, { recursive: true, force: true }); if (quarantinePath) { fs.rmSync(path.dirname(quarantinePath), { recursive: true, force: true });