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 2 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
21 changes: 21 additions & 0 deletions packages/exec/__tests__/exec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,27 @@ describe('@actions/exec', () => {
expect(stderrCalled).toBeTruthy()
})

it('Handles stdin', async () => {
const waitForInput: string = path.join(
__dirname,
'scripts',
'wait-for-input.sh'
)

const _testExecOptions = getExecOptions()

_testExecOptions.listeners = {
stdout: (data: Buffer) => {
expect(data).toEqual(Buffer.from('this is my input\n'))
}
}

_testExecOptions.stdin = Buffer.from('this is my input')

const exitCode = await exec.exec(`"${waitForInput}"`, [], _testExecOptions)
expect(exitCode).toBe(0)
})

it('Handles child process holding streams open', async function() {
const semaphorePath = path.join(
getTestTemp(),
Expand Down
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. */
stdin?: Buffer
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 kept going back-and-forth about whether to accept a Buffer or Stream here. I think Buffer is a better API for most users, but it's not quite as flexible as giving users the ability to specify a stream.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree that buffer is the better api for most users. Its simple and should work for most use cases here. If we end up implementing more complicated scenarios with streams in the future, we can always support passing in a stream or a buffer mutually exclusive. We some use cases for that scenario

@bryanmacfarlane , FYI.

Copy link
Member

Choose a reason for hiding this comment

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

Are we sure this is a buffer? subprocess.stdin advertises it as a writable stream. Another reason I wanted to see a pure js test instead of the script. https://nodejs.org/api/child_process.html#child_process_subprocess_stdin

Copy link
Collaborator

Choose a reason for hiding this comment

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

The input we are allowing a user to pass in here is a buffer.

We write the contents of that buffer to the process's StdIn Stream.

Very similar to how node does this with the input option you outlined below, which also takes a string and various other inputs.
https://nodejs.org/api/child_process.html#child_process_child_process_execfilesync_file_args_options

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry I just realized node also has input option. Should we just pass the value through and let node handle writing to the stream?

Copy link
Contributor

Choose a reason for hiding this comment

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

...that said, seems like would be an implementation detail and wouldn't change behavior exposed by this PR. So i think this PR is OK the way it is (incremental)

Copy link
Collaborator

Choose a reason for hiding this comment

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

input doesn't appear to be available for any of the async process creation function. In particular we use spawn. https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

Copy link
Contributor

Choose a reason for hiding this comment

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

oh weird, didnt realize it was only on the sync functions. makes sense now why we dont pass thru :)


/** optional. Listeners for output. Callback functions that will be called on these events */
listeners?: {
stdout?: (data: Buffer) => void
Expand Down
10 changes: 10 additions & 0 deletions packages/exec/src/toolrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,16 @@ export class ToolRunner extends events.EventEmitter {
resolve(exitCode)
}
})

if (this.options.stdin) {
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.setDefaultEncoding('utf-8')
cp.stdin.write(this.options.stdin)
cp.stdin.end()
}
})
}
}
Expand Down