1
0
Fork 0

Exec: throw error when cwd option does not exist (#793)

* Exec: throw error when cwd option does not exist

* Simplify promise rejection
pull/801/head
Luke Tomlinson 2021-05-07 16:12:40 -04:00 committed by GitHub
parent fecf6cdd59
commit 09e59b9a5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -538,6 +538,22 @@ describe('@actions/exec', () => {
expect(output.trim()).toBe(`args[0]: "hello"${os.EOL}args[1]: "world"`) 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 () => { it('Exec roots relative tool path using rooted options.cwd', async () => {
let command: string let command: string
if (IS_WINDOWS) { if (IS_WINDOWS) {

View File

@ -414,7 +414,7 @@ export class ToolRunner extends events.EventEmitter {
// otherwise verify it exists (add extension on Windows if necessary) // otherwise verify it exists (add extension on Windows if necessary)
this.toolPath = await io.which(this.toolPath, true) 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(`exec tool: ${this.toolPath}`)
this._debug('arguments:') this._debug('arguments:')
for (const arg of this.args) { for (const arg of this.args) {
@ -433,6 +433,10 @@ export class ToolRunner extends events.EventEmitter {
this._debug(message) this._debug(message)
}) })
if (this.options.cwd && !(await ioUtil.exists(this.options.cwd))) {
return reject(new Error(`The cwd: ${this.options.cwd} does not exist!`))
}
const fileName = this._getSpawnFileName() const fileName = this._getSpawnFileName()
const cp = child.spawn( const cp = child.spawn(
fileName, fileName,