diff --git a/__tests__/lib.test.ts b/__tests__/lib.test.ts index b1f638c..49f4206 100644 --- a/__tests__/lib.test.ts +++ b/__tests__/lib.test.ts @@ -1,5 +1,6 @@ import {ExitCode} from '@actions/exit' import * as os from 'os' +import * as path from 'path' import * as core from '../src/core' /* eslint-disable @typescript-eslint/unbound-method */ @@ -11,6 +12,7 @@ const testEnvVars = { 'my secret': '', 'special char secret \r\n];': '', 'my secret2': '', + PATH: `path1${path.delimiter}path2`, // Set inputs INPUT_MY_INPUT: 'val', @@ -78,6 +80,14 @@ describe('@actions/core', () => { ]) }) + it('prependPath produces the correct commands and sets the env', () => { + core.addPath('myPath') + expect(process.env['PATH']).toBe( + `myPath${path.delimiter}path1${path.delimiter}path2` + ) + assertWriteCalls([`##[add-path]myPath${os.EOL}`]) + }) + it('getInput gets non-required input', () => { expect(core.getInput('my input')).toBe('val') }) diff --git a/src/core.ts b/src/core.ts index f63ef1c..7e3d7e0 100644 --- a/src/core.ts +++ b/src/core.ts @@ -1,6 +1,8 @@ import {ExitCode} from '@actions/exit' import {issue, issueCommand} from './command' +import * as path from 'path' + /** * Interface for getInput options */ @@ -33,6 +35,15 @@ export function exportSecret(name: string, val: string): void { issueCommand('set-secret', {}, val) } +/** + * Prepends inputPath to the PATH (for this action and future actions) + * @param inputPath + */ +export function addPath(inputPath: string): void { + issueCommand('add-path', {}, inputPath) + process.env['PATH'] = `${inputPath}${path.delimiter}${process.env['PATH']}` +} + /** * Gets the value of an input. The value is also trimmed. *