Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying stdin #360

Merged
merged 9 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/exec/__tests__/exec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see a test that shows what an action author would do. e.g. all js instead of the sh and cmd scripts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a js-specific test, although I admit I'm not sure exactly what you're asking. All of these tests spawn a childprocess using exec - there's no "all js". The test spawns a node subprocess with a node file that prints what it gets on stdin.

}

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(),
Expand Down
3 changes: 3 additions & 0 deletions packages/exec/__tests__/scripts/wait-for-input.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
set /p var=
echo %var%
3 changes: 3 additions & 0 deletions packages/exec/__tests__/scripts/wait-for-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var fs = require('fs')
var data = fs.readFileSync(0, 'utf-8')
process.stdout.write(data)
4 changes: 4 additions & 0 deletions packages/exec/__tests__/scripts/wait-for-input.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

read var
echo $var
3 changes: 3 additions & 0 deletions packages/exec/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions packages/exec/src/toolrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,14 @@ export class ToolRunner extends events.EventEmitter {
resolve(exitCode)
}
})

if (this.options.input) {
if (!cp.stdin) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Responded to this above: #360 (comment)

throw new Error('child process missing stdin')
}

cp.stdin.end(this.options.input)
}
})
}
}
Expand Down