toolrunner should which tool before invoking (#220)
This commit is contained in:
1 parent
021d032160
commit
42ec8de762
6 files changed
+243
-6
No files matched your search
@@ -48,13 +48,10 @@ await exec.exec('node', ['index.js', 'foo=bar'], options);
|
|||||||
|
|
||||||
#### Exec tools not in the PATH
|
#### Exec tools not in the PATH
|
||||||
|
|
||||||
You can use it in conjunction with the `which` function from `@actions/io` to execute tools that are not in the PATH:
|
You can specify the full path for tools not in the PATH:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const exec = require('@actions/exec');
|
const exec = require('@actions/exec');
|
||||||
const io = require('@actions/io');
|
|
||||||
|
|
||||||
const pythonPath: string = await io.which('python', true)
|
await exec.exec('"/path/to/my-tool"', ['arg1']);
|
||||||
|
|
||||||
await exec.exec(`"${pythonPath}"`, ['main.py']);
|
|
||||||
```
|
```
|
||||||
@@ -121,6 +121,38 @@ describe('@actions/exec', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('Runs exec successfully with command from PATH', async () => {
|
||||||
|
const execOptions = getExecOptions()
|
||||||
|
const outStream = new StringStream()
|
||||||
|
execOptions.outStream = outStream
|
||||||
|
let output = ''
|
||||||
|
execOptions.listeners = {
|
||||||
|
stdout: (data: Buffer) => {
|
||||||
|
output += data.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let exitCode = 1
|
||||||
|
let tool: string
|
||||||
|
let args: string[]
|
||||||
|
if (IS_WINDOWS) {
|
||||||
|
tool = 'cmd'
|
||||||
|
args = ['/c', 'echo', 'hello']
|
||||||
|
} else {
|
||||||
|
tool = 'sh'
|
||||||
|
args = ['-c', 'echo hello']
|
||||||
|
}
|
||||||
|
|
||||||
|
exitCode = await exec.exec(tool, args, execOptions)
|
||||||
|
|
||||||
|
expect(exitCode).toBe(0)
|
||||||
|
const rootedTool = await io.which(tool, true)
|
||||||
|
expect(outStream.getContents().split(os.EOL)[0]).toBe(
|
||||||
|
`[command]${rootedTool} ${args.join(' ')}`
|
||||||
|
)
|
||||||
|
expect(output.trim()).toBe(`hello`)
|
||||||
|
})
|
||||||
|
|
||||||
it('Exec fails with error on bad call', async () => {
|
it('Exec fails with error on bad call', async () => {
|
||||||
const _testExecOptions = getExecOptions()
|
const _testExecOptions = getExecOptions()
|
||||||
|
|
||||||
@@ -418,6 +450,134 @@ describe('@actions/exec', () => {
|
|||||||
fs.unlinkSync(semaphorePath)
|
fs.unlinkSync(semaphorePath)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('Exec roots relative tool path using unrooted options.cwd', async () => {
|
||||||
|
let exitCode: number
|
||||||
|
let command: string
|
||||||
|
if (IS_WINDOWS) {
|
||||||
|
command = './print-args-cmd' // let ToolRunner resolve the extension
|
||||||
|
} else {
|
||||||
|
command = './print-args-sh.sh'
|
||||||
|
}
|
||||||
|
const execOptions = getExecOptions()
|
||||||
|
execOptions.cwd = 'scripts'
|
||||||
|
const outStream = new StringStream()
|
||||||
|
execOptions.outStream = outStream
|
||||||
|
let output = ''
|
||||||
|
execOptions.listeners = {
|
||||||
|
stdout: (data: Buffer) => {
|
||||||
|
output += data.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const originalCwd = process.cwd()
|
||||||
|
try {
|
||||||
|
process.chdir(__dirname)
|
||||||
|
exitCode = await exec.exec(`${command} hello world`, [], execOptions)
|
||||||
|
} catch (err) {
|
||||||
|
process.chdir(originalCwd)
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(exitCode).toBe(0)
|
||||||
|
const toolPath = path.resolve(
|
||||||
|
__dirname,
|
||||||
|
execOptions.cwd,
|
||||||
|
`${command}${IS_WINDOWS ? '.cmd' : ''}`
|
||||||
|
)
|
||||||
|
if (IS_WINDOWS) {
|
||||||
|
expect(outStream.getContents().split(os.EOL)[0]).toBe(
|
||||||
|
`[command]${process.env.ComSpec} /D /S /C "${toolPath} hello world"`
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
expect(outStream.getContents().split(os.EOL)[0]).toBe(
|
||||||
|
`[command]${toolPath} hello world`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
expect(output.trim()).toBe(`args[0]: "hello"${os.EOL}args[1]: "world"`)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Exec roots relative tool path using rooted options.cwd', async () => {
|
||||||
|
let command: string
|
||||||
|
if (IS_WINDOWS) {
|
||||||
|
command = './print-args-cmd' // let ToolRunner resolve the extension
|
||||||
|
} else {
|
||||||
|
command = './print-args-sh.sh'
|
||||||
|
}
|
||||||
|
const execOptions = getExecOptions()
|
||||||
|
execOptions.cwd = path.join(__dirname, 'scripts')
|
||||||
|
const outStream = new StringStream()
|
||||||
|
execOptions.outStream = outStream
|
||||||
|
let output = ''
|
||||||
|
execOptions.listeners = {
|
||||||
|
stdout: (data: Buffer) => {
|
||||||
|
output += data.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const exitCode = await exec.exec(`${command} hello world`, [], execOptions)
|
||||||
|
|
||||||
|
expect(exitCode).toBe(0)
|
||||||
|
const toolPath = path.resolve(
|
||||||
|
__dirname,
|
||||||
|
execOptions.cwd,
|
||||||
|
`${command}${IS_WINDOWS ? '.cmd' : ''}`
|
||||||
|
)
|
||||||
|
if (IS_WINDOWS) {
|
||||||
|
expect(outStream.getContents().split(os.EOL)[0]).toBe(
|
||||||
|
`[command]${process.env.ComSpec} /D /S /C "${toolPath} hello world"`
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
expect(outStream.getContents().split(os.EOL)[0]).toBe(
|
||||||
|
`[command]${toolPath} hello world`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
expect(output.trim()).toBe(`args[0]: "hello"${os.EOL}args[1]: "world"`)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Exec roots relative tool path using process.cwd', async () => {
|
||||||
|
let exitCode: number
|
||||||
|
let command: string
|
||||||
|
if (IS_WINDOWS) {
|
||||||
|
command = 'scripts/print-args-cmd' // let ToolRunner resolve the extension
|
||||||
|
} else {
|
||||||
|
command = 'scripts/print-args-sh.sh'
|
||||||
|
}
|
||||||
|
const execOptions = getExecOptions()
|
||||||
|
const outStream = new StringStream()
|
||||||
|
execOptions.outStream = outStream
|
||||||
|
let output = ''
|
||||||
|
execOptions.listeners = {
|
||||||
|
stdout: (data: Buffer) => {
|
||||||
|
output += data.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const originalCwd = process.cwd()
|
||||||
|
try {
|
||||||
|
process.chdir(__dirname)
|
||||||
|
exitCode = await exec.exec(`${command} hello world`, [], execOptions)
|
||||||
|
} catch (err) {
|
||||||
|
process.chdir(originalCwd)
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(exitCode).toBe(0)
|
||||||
|
const toolPath = path.resolve(
|
||||||
|
__dirname,
|
||||||
|
`${command}${IS_WINDOWS ? '.cmd' : ''}`
|
||||||
|
)
|
||||||
|
if (IS_WINDOWS) {
|
||||||
|
expect(outStream.getContents().split(os.EOL)[0]).toBe(
|
||||||
|
`[command]${process.env.ComSpec} /D /S /C "${toolPath} hello world"`
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
expect(outStream.getContents().split(os.EOL)[0]).toBe(
|
||||||
|
`[command]${toolPath} hello world`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
expect(output.trim()).toBe(`args[0]: "hello"${os.EOL}args[1]: "world"`)
|
||||||
|
})
|
||||||
|
|
||||||
if (IS_WINDOWS) {
|
if (IS_WINDOWS) {
|
||||||
// Win specific quoting tests
|
// Win specific quoting tests
|
||||||
it('execs .exe with verbatim args (Windows)', async () => {
|
it('execs .exe with verbatim args (Windows)', async () => {
|
||||||
@@ -572,6 +732,42 @@ describe('@actions/exec', () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('execs .cmd from path (Windows)', async () => {
|
||||||
|
// this test validates whether a .cmd is resolved from the PATH when the extension is not specified
|
||||||
|
const cmd = 'print-args-cmd' // note, not print-args-cmd.cmd
|
||||||
|
const cmdPath = path.join(__dirname, 'scripts', `${cmd}.cmd`)
|
||||||
|
const args: string[] = ['my arg 1', 'my arg 2']
|
||||||
|
const outStream = new StringStream()
|
||||||
|
let output = ''
|
||||||
|
const options = {
|
||||||
|
outStream: <stream.Writable>outStream,
|
||||||
|
listeners: {
|
||||||
|
stdout: (data: Buffer) => {
|
||||||
|
output += data.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const originalPath = process.env['Path']
|
||||||
|
try {
|
||||||
|
process.env['Path'] = `${originalPath};${path.dirname(cmdPath)}`
|
||||||
|
const exitCode = await exec.exec(`${cmd}`, args, options)
|
||||||
|
expect(exitCode).toBe(0)
|
||||||
|
expect(outStream.getContents().split(os.EOL)[0]).toBe(
|
||||||
|
`[command]${
|
||||||
|
process.env.ComSpec
|
||||||
|
} /D /S /C "${cmdPath} "my arg 1" "my arg 2""`
|
||||||
|
)
|
||||||
|
expect(output.trim()).toBe(
|
||||||
|
'args[0]: "<quote>my arg 1<quote>"\r\n' +
|
||||||
|
'args[1]: "<quote>my arg 2<quote>"'
|
||||||
|
)
|
||||||
|
} catch (err) {
|
||||||
|
process.env['Path'] = originalPath
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
it('execs .cmd with arg quoting (Windows)', async () => {
|
it('execs .cmd with arg quoting (Windows)', async () => {
|
||||||
// this test validates .cmd quoting rules are applied, not the default libuv rules
|
// this test validates .cmd quoting rules are applied, not the default libuv rules
|
||||||
const cmdPath = path.join(
|
const cmdPath = path.join(
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal
|
||||||
|
set index=0
|
||||||
|
|
||||||
|
:check_arg
|
||||||
|
set arg=%1
|
||||||
|
if not defined arg goto :eof
|
||||||
|
set "arg=%arg:"=<quote>%"
|
||||||
|
echo args[%index%]: "%arg%"
|
||||||
|
set /a index=%index%+1
|
||||||
|
shift
|
||||||
|
goto check_arg
|
||||||
Executable
+11
@@ -0,0 +1,11 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# store arguments in a special array
|
||||||
|
args=("$@")
|
||||||
|
# get number of elements
|
||||||
|
ELEMENTS=${#args[@]}
|
||||||
|
|
||||||
|
# echo each element
|
||||||
|
for (( i=0;i<$ELEMENTS;i++)); do
|
||||||
|
echo "args[$i]: \"${args[${i}]}\""
|
||||||
|
done
|
||||||
+1
-1
@@ -32,7 +32,7 @@
|
|||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/actions/toolkit/issues"
|
"url": "https://github.com/actions/toolkit/issues"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"dependencies": {
|
||||||
"@actions/io": "^1.0.1"
|
"@actions/io": "^1.0.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
import * as os from 'os'
|
import * as os from 'os'
|
||||||
import * as events from 'events'
|
import * as events from 'events'
|
||||||
import * as child from 'child_process'
|
import * as child from 'child_process'
|
||||||
|
import * as path from 'path'
|
||||||
import * as stream from 'stream'
|
import * as stream from 'stream'
|
||||||
import * as im from './interfaces'
|
import * as im from './interfaces'
|
||||||
|
import * as io from '@actions/io'
|
||||||
|
import * as ioUtil from '@actions/io/lib/io-util'
|
||||||
|
|
||||||
/* eslint-disable @typescript-eslint/unbound-method */
|
/* eslint-disable @typescript-eslint/unbound-method */
|
||||||
|
|
||||||
@@ -392,6 +395,24 @@ export class ToolRunner extends events.EventEmitter {
|
|||||||
* @returns number
|
* @returns number
|
||||||
*/
|
*/
|
||||||
async exec(): Promise<number> {
|
async exec(): Promise<number> {
|
||||||
|
// root the tool path if it is unrooted and contains relative pathing
|
||||||
|
if (
|
||||||
|
!ioUtil.isRooted(this.toolPath) &&
|
||||||
|
(this.toolPath.includes('/') ||
|
||||||
|
(IS_WINDOWS && this.toolPath.includes('\\')))
|
||||||
|
) {
|
||||||
|
// prefer options.cwd if it is specified, however options.cwd may also need to be rooted
|
||||||
|
this.toolPath = path.resolve(
|
||||||
|
process.cwd(),
|
||||||
|
this.options.cwd || process.cwd(),
|
||||||
|
this.toolPath
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the tool is only a file name, then resolve it from the PATH
|
||||||
|
// 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>((resolve, reject) => {
|
||||||
this._debug(`exec tool: ${this.toolPath}`)
|
this._debug(`exec tool: ${this.toolPath}`)
|
||||||
this._debug('arguments:')
|
this._debug('arguments:')
|
||||||
|
|||||||
Reference in new issue
Block a user