From 156ae3b48626c8054892c2710d9106e4e00087bb Mon Sep 17 00:00:00 2001 From: gengjiawen Date: Wed, 30 Sep 2026 16:28:54 +0800 Subject: [PATCH] test: avoid EBUSY removing renamed host executable "addon works with renamed host executable" copied node to a fixed path in the temp directory and unlinked it right after running it. On Windows the just-exited executable can still be locked, which intermittently failed the test with "EBUSY: resource busy or locked, unlink ...notnode.exe". Use a fresh temp directory and remove it in a finally block with retries, like the other cleanups in the test suite. --- test/test-addon.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/test/test-addon.js b/test/test-addon.js index 878dcc415a..2ee313aeef 100644 --- a/test/test-addon.js +++ b/test/test-addon.js @@ -139,21 +139,26 @@ describe('addon', function () { it('addon works with renamed host executable', async function () { this.timeout(platformTimeout(1, { win32: 5 })) - const notNodePath = path.join(os.tmpdir(), 'notnode' + path.extname(process.execPath)) + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'node-gyp-notnode-')) + const notNodePath = path.join(tempDir, 'notnode' + path.extname(process.execPath)) fs.copyFileSync(process.execPath, notNodePath) - const cmd = [nodeGyp, 'rebuild', '-C', addonPath, '--loglevel=verbose'] - const [err, stdout, logLines] = await execFile(cmd) - if (err) { - console.log('-- build stdout (MSBuild/make output) --') - console.log(stdout) - console.log('-- build stderr (gyp logs) --') - console.log(logLines.join('\n')) + try { + const cmd = [nodeGyp, 'rebuild', '-C', addonPath, '--loglevel=verbose'] + const [err, stdout, logLines] = await execFile(cmd) + if (err) { + console.log('-- build stdout (MSBuild/make output) --') + console.log(stdout) + console.log('-- build stderr (gyp logs) --') + console.log(logLines.join('\n')) + } + const lastLine = logLines[logLines.length - 1] + assert.strictEqual(err, null) + assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok') + assert.strictEqual(runHello(notNodePath), 'world') + } finally { + // Windows may keep the just-exited executable locked briefly (EBUSY) + fs.rmSync(tempDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 1000 }) } - const lastLine = logLines[logLines.length - 1] - assert.strictEqual(err, null) - assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok') - assert.strictEqual(runHello(notNodePath), 'world') - fs.unlinkSync(notNodePath) }) })