1
0
Fork 0

Allow specifying stdin (#360)

* Allow specifying stdin
pull/417/head
Seth Vargo 2020-04-13 13:39:42 -04:00 committed by GitHub
parent 05e39f551d
commit c4b6011310
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 0 deletions

View File

@ -286,6 +286,52 @@ describe('@actions/exec', () => {
expect(stderrCalled).toBeTruthy()
})
it('Handles stdin shell', async () => {
let command: string
if (IS_WINDOWS) {
command = 'wait-for-input.cmd'
} else {
command = 'wait-for-input.sh'
}
const waitForInput: string = path.join(__dirname, 'scripts', command)
const _testExecOptions = getExecOptions()
_testExecOptions.listeners = {
stdout: (data: Buffer) => {
expect(data).toEqual(Buffer.from(`this is my input${os.EOL}`))
}
}
_testExecOptions.input = Buffer.from('this is my input')
const exitCode = await exec.exec(`"${waitForInput}"`, [], _testExecOptions)
expect(exitCode).toBe(0)
})
it('Handles stdin js', async () => {
const waitForInput: string = path.join(
__dirname,
'scripts',
'wait-for-input.js'
)
const _testExecOptions = getExecOptions()
_testExecOptions.listeners = {
stdout: (data: Buffer) => {
expect(data).toEqual(Buffer.from(`this is my input`))
}
}
_testExecOptions.input = Buffer.from('this is my input')
const nodePath = await io.which('node', true)
const exitCode = await exec.exec(nodePath, [waitForInput], _testExecOptions)
expect(exitCode).toBe(0)
})
it('Handles child process holding streams open', async function() {
const semaphorePath = path.join(
getTestTemp(),

View File

@ -0,0 +1,3 @@
@echo off
set /p var=
echo %var%

View File

@ -0,0 +1,3 @@
var fs = require('fs')
var data = fs.readFileSync(0, 'utf-8')
process.stdout.write(data)

View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
read var
echo $var

View File

@ -30,6 +30,9 @@ export interface ExecOptions {
/** optional. How long in ms to wait for STDIO streams to close after the exit event of the process before terminating. defaults to 10000 */
delay?: number
/** optional. input to write to the process on STDIN. */
input?: Buffer
/** optional. Listeners for output. Callback functions that will be called on these events */
listeners?: {
stdout?: (data: Buffer) => void

View File

@ -524,6 +524,14 @@ export class ToolRunner extends events.EventEmitter {
resolve(exitCode)
}
})
if (this.options.input) {
if (!cp.stdin) {
throw new Error('child process missing stdin')
}
cp.stdin.end(this.options.input)
}
})
}
}