From f528bc2d18d5df841f6319a5a0eaa94f08210b40 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 28 Jul 2024 19:13:02 +0900 Subject: [PATCH] actions/exec: add `timeout` option to `exec` function Fixes #1777 --- packages/exec/__tests__/exec.test.ts | 9 +++++++++ packages/exec/src/interfaces.ts | 3 +++ packages/exec/src/toolrunner.ts | 1 + 3 files changed, 13 insertions(+) diff --git a/packages/exec/__tests__/exec.test.ts b/packages/exec/__tests__/exec.test.ts index 1f83ce4aac..bd075d1e42 100644 --- a/packages/exec/__tests__/exec.test.ts +++ b/packages/exec/__tests__/exec.test.ts @@ -820,6 +820,15 @@ describe('@actions/exec', () => { expect(stdout).toBe('©') }) + it('allows to specify timeouts on running the child process', async () => { + const options = getExecOptions() + // The `cat` command will be killed after 100ms + options.timeout = 100 + await expect(exec.exec('cat', [], options)).rejects.toThrowError( + 'failed with exit code null' + ) + }) + if (IS_WINDOWS) { it('Exec roots relative tool path using process.cwd (Windows path separator)', async () => { let exitCode: number diff --git a/packages/exec/src/interfaces.ts b/packages/exec/src/interfaces.ts index c86080c4f5..4cf24ad186 100644 --- a/packages/exec/src/interfaces.ts +++ b/packages/exec/src/interfaces.ts @@ -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. Timeout in ms for the child process */ + timeout?: number + /** optional. input to write to the process on STDIN. */ input?: Buffer diff --git a/packages/exec/src/toolrunner.ts b/packages/exec/src/toolrunner.ts index a6ca34a2da..7d66611d68 100644 --- a/packages/exec/src/toolrunner.ts +++ b/packages/exec/src/toolrunner.ts @@ -374,6 +374,7 @@ export class ToolRunner extends events.EventEmitter { const result = {} result.cwd = options.cwd result.env = options.env + result.timeout = options.timeout result['windowsVerbatimArguments'] = options.windowsVerbatimArguments || this._isCmdFile() if (options.windowsVerbatimArguments) {