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:
@@ -11,7 +11,7 @@ import * as io from '@actions/io'
|
|||||||
/* eslint-disable @typescript-eslint/unbound-method */
|
/* eslint-disable @typescript-eslint/unbound-method */
|
||||||
|
|
||||||
const IS_WINDOWS = process.platform === 'win32'
|
const IS_WINDOWS = process.platform === 'win32'
|
||||||
const SPAWN_WAIT_FOR_FILE = path.join(
|
const SPAWN_WAIT_SCRIPT = path.join(
|
||||||
__dirname,
|
__dirname,
|
||||||
'scripts',
|
'scripts',
|
||||||
'spawn-wait-for-file.js'
|
'spawn-wait-for-file.js'
|
||||||
@@ -379,9 +379,11 @@ describe('@actions/exec', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const args = [SPAWN_WAIT_FOR_FILE, `file=${semaphorePath}`]
|
const exitCode = await exec.exec(
|
||||||
|
`"${nodePath}"`,
|
||||||
const exitCode = await exec.exec(`"${nodePath}"`, args, _testExecOptions)
|
[SPAWN_WAIT_SCRIPT, `file=${semaphorePath}`],
|
||||||
|
_testExecOptions
|
||||||
|
)
|
||||||
|
|
||||||
expect(exitCode).toBe(0)
|
expect(exitCode).toBe(0)
|
||||||
expect(
|
expect(
|
||||||
@@ -408,10 +410,12 @@ describe('@actions/exec', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const args = [SPAWN_WAIT_FOR_FILE, `file=${semaphorePath}`, 'exitCode=123']
|
|
||||||
|
|
||||||
await exec
|
await exec
|
||||||
.exec(`"${nodePath}"`, args, _testExecOptions)
|
.exec(
|
||||||
|
`"${nodePath}"`,
|
||||||
|
[SPAWN_WAIT_SCRIPT, `file=${semaphorePath}`, 'exitCode=123'],
|
||||||
|
_testExecOptions
|
||||||
|
)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
throw new Error('Should not have succeeded')
|
throw new Error('Should not have succeeded')
|
||||||
})
|
})
|
||||||
@@ -446,10 +450,12 @@ describe('@actions/exec', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const args = [SPAWN_WAIT_FOR_FILE, `file=${semaphorePath}`, 'stderr=true']
|
|
||||||
|
|
||||||
await exec
|
await exec
|
||||||
.exec(`"${nodePath}"`, args, _testExecOptions)
|
.exec(
|
||||||
|
`"${nodePath}"`,
|
||||||
|
[SPAWN_WAIT_SCRIPT, `file=${semaphorePath}`, 'stderr=true'],
|
||||||
|
_testExecOptions
|
||||||
|
)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
throw new Error('Should not have succeeded')
|
throw new Error('Should not have succeeded')
|
||||||
})
|
})
|
||||||
@@ -466,7 +472,7 @@ describe('@actions/exec', () => {
|
|||||||
).toBe(1)
|
).toBe(1)
|
||||||
|
|
||||||
fs.unlinkSync(semaphorePath)
|
fs.unlinkSync(semaphorePath)
|
||||||
})
|
}, 10000)
|
||||||
|
|
||||||
it('Exec roots relative tool path using unrooted options.cwd', async () => {
|
it('Exec roots relative tool path using unrooted options.cwd', async () => {
|
||||||
let exitCode: number
|
let exitCode: number
|
||||||
|
|||||||
@@ -1,51 +1,39 @@
|
|||||||
const childProcess = require('child_process')
|
const childProcess = require('child_process')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
|
||||||
function parseArgs() {
|
// Parse args
|
||||||
const result = {}
|
const args = {}
|
||||||
for (const arg of process.argv.slice(2)) {
|
for (const arg of process.argv.slice(2)) {
|
||||||
const equalsIndex = arg.indexOf('=')
|
const idx = arg.indexOf('=')
|
||||||
if (equalsIndex === -1) {
|
if (idx !== -1) {
|
||||||
continue
|
args[arg.slice(0, idx)] = arg.slice(idx + 1)
|
||||||
}
|
|
||||||
const key = arg.slice(0, equalsIndex)
|
|
||||||
const value = arg.slice(equalsIndex + 1)
|
|
||||||
result[key] = value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const args = parseArgs()
|
|
||||||
const filePath = args.file
|
const filePath = args.file
|
||||||
if (!filePath) {
|
if (!filePath) {
|
||||||
throw new Error('file is not specified')
|
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 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
|
// Don't wait for child to exit
|
||||||
// This keeps the streams open after parent exits
|
child.unref()
|
||||||
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()
|
|
||||||
|
|
||||||
|
// Handle optional stderr output (must happen BEFORE we exit)
|
||||||
if (args.stderr === 'true') {
|
if (args.stderr === 'true') {
|
||||||
process.stderr.write('hi')
|
process.stderr.write('hi')
|
||||||
}
|
}
|
||||||
|
|
||||||
const exitCode = args.exitCode ? parseInt(args.exitCode, 10) : 0
|
// Small delay to ensure child has started and inherited handles
|
||||||
process.exit(exitCode)
|
setTimeout(() => {
|
||||||
|
const exitCode = args.exitCode ? parseInt(args.exitCode, 10) : 0
|
||||||
|
process.exit(exitCode)
|
||||||
|
}, 50)
|
||||||
|
|||||||
Reference in New Issue
Block a user