Skip to content

Commit

Permalink
Exec: throw error when cwd option does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
luketomlinson committed May 7, 2021
1 parent 3491e2e commit ae2eb2e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
16 changes: 16 additions & 0 deletions packages/exec/__tests__/exec.test.ts
Expand Up @@ -538,6 +538,22 @@ describe('@actions/exec', () => {
expect(output.trim()).toBe(`args[0]: "hello"${os.EOL}args[1]: "world"`)
})

it('Exec roots throws friendly error when bad cwd is specified', async () => {
const execOptions = getExecOptions()
execOptions.cwd = 'nonexistent/path'

await expect(exec.exec('ls', ['-all'], execOptions)).rejects.toThrowError(
`The cwd: ${execOptions.cwd} does not exist!`
)
})

it('Exec roots does not throw when valid cwd is provided', async () => {
const execOptions = getExecOptions()
execOptions.cwd = './'

await expect(exec.exec('ls', ['-all'], execOptions)).resolves.toBe(0)
})

it('Exec roots relative tool path using rooted options.cwd', async () => {
let command: string
if (IS_WINDOWS) {
Expand Down
Binary file added packages/exec/actions-exec-1.0.4.tgz
Binary file not shown.
14 changes: 13 additions & 1 deletion packages/exec/src/toolrunner.ts
Expand Up @@ -414,13 +414,17 @@ export class ToolRunner extends events.EventEmitter {
// otherwise verify it exists (add extension on Windows if necessary)
this.toolPath = await io.which(this.toolPath, true)

return new Promise<number>((resolve, reject) => {
return new Promise<number>(async (resolve, reject) => {
this._debug(`exec tool: ${this.toolPath}`)
this._debug('arguments:')
for (const arg of this.args) {
this._debug(` ${arg}`)
}

this.on('error', (error: Error) => {
reject(error)
})

const optionsNonNull = this._cloneExecOptions(this.options)
if (!optionsNonNull.silent && optionsNonNull.outStream) {
optionsNonNull.outStream.write(
Expand All @@ -433,6 +437,14 @@ export class ToolRunner extends events.EventEmitter {
this._debug(message)
})

if (this.options.cwd && !(await ioUtil.exists(this.options.cwd))) {
this.emit(
'error',
new Error(`The cwd: ${this.options.cwd} does not exist!`)
)
return
}

const fileName = this._getSpawnFileName()
const cp = child.spawn(
fileName,
Expand Down

0 comments on commit ae2eb2e

Please sign in to comment.