fix: improve exec stream tests cross-platform handling

- Update spawn-wait-for-file.js to use proper stdio inheritance
- Add small delay before exit to ensure child process inherits handles
- Simplify test code to use the helper script instead of shell commands
This commit is contained in:
Salman Muin Kayser Chishti
2025-12-08 12:27:43 +00:00
parent d97deb1f60
commit 894f77901e
2 changed files with 38 additions and 44 deletions
@@ -1,51 +1,39 @@
const childProcess = require('child_process')
const path = require('path')
function parseArgs() {
const result = {}
for (const arg of process.argv.slice(2)) {
const equalsIndex = arg.indexOf('=')
if (equalsIndex === -1) {
continue
}
const key = arg.slice(0, equalsIndex)
const value = arg.slice(equalsIndex + 1)
result[key] = value
// Parse args
const args = {}
for (const arg of process.argv.slice(2)) {
const idx = arg.indexOf('=')
if (idx !== -1) {
args[arg.slice(0, idx)] = arg.slice(idx + 1)
}
return result
}
const args = parseArgs()
const filePath = args.file
if (!filePath) {
throw new Error('file is not specified')
}
// Spawn wait-for-file.js with inherited stdio
// This creates a grandchild process that holds the stdio handles open
// after this process (the child) exits
const waitScript = path.join(__dirname, 'wait-for-file.js')
const waitArgs = [waitScript, `file=${filePath}`]
const child = childProcess.spawn(process.execPath, [waitScript, `file=${filePath}`], {
stdio: ['ignore', 'inherit', 'inherit'],
detached: process.platform !== 'win32'
})
// Spawn with inherited stdio and detached on Unix, non-detached on Windows
// This keeps the streams open after parent exits
const isWindows = process.platform === 'win32'
const spawnOptions = {
stdio: 'inherit',
detached: !isWindows
}
// On Windows, we need to hide the window
if (isWindows) {
spawnOptions.windowsHide = true
}
const waitProcess = childProcess.spawn(process.execPath, waitArgs, spawnOptions)
// Unref so parent doesn't wait for child
waitProcess.unref()
// Don't wait for child to exit
child.unref()
// Handle optional stderr output (must happen BEFORE we exit)
if (args.stderr === 'true') {
process.stderr.write('hi')
}
const exitCode = args.exitCode ? parseInt(args.exitCode, 10) : 0
process.exit(exitCode)
// Small delay to ensure child has started and inherited handles
setTimeout(() => {
const exitCode = args.exitCode ? parseInt(args.exitCode, 10) : 0
process.exit(exitCode)
}, 50)