Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pr updates w/ feedback
  • Loading branch information
thboop committed Sep 22, 2020
commit 54459256d2be76ac23cd8f13cff6fc5a33eb0b90
32 changes: 4 additions & 28 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ echo "FOO=BAR" >> $GITHUB_ENV

Running `$FOO` in a future step will now return `BAR`

For multiline strings, you may use the [heredoc syntax](https://tldp.org/LDP/abs/html/here-docs.html) with your choice of delimeter. In the below example, we use `EOF`
For multiline strings, you may use the heredoc style syntax with your choice of delimeter. In the below example, we use `EOF`
```
steps:
- name: Set the value
Expand All @@ -157,31 +157,7 @@ steps:
echo 'EOF' >> $GITHUB_ENV
```

This would set the value of the `JSON_RESPONSE` env variable to:

```
{
"slideshow": {
"author": "Yours Truly",
"date": "date of publication",
"slides": [
{
"title": "Wake up to WonderWidgets!",
"type": "all"
},
{
"items": [
"Why <em>WonderWidgets</em> are great",
"Who <em>buys</em> WonderWidgets"
],
"title": "Overview",
"type": "all"
}
],
"title": "Sample Slide Show"
}
}
```
This would set the value of the `JSON_RESPONSE` env variable to the value of the curl response.

This is wrapped by the core `exportVariable` method which sets for future steps but also updates the variable for this step.

Expand All @@ -194,10 +170,10 @@ export function exportVariable(name: string, val: string): void {}
To prepend a string to PATH write to the file located at `GITHUB_PATH` or use the equivalent `actions/core` function

```sh
echo "foo=bar" >> $GITHUB_PATH
echo "/Users/test/.nvm/versions/node/v12.18.3/bin" >> $GITHUB_PATH
```

Running `$PATH` in a future step will now return `BAR:{Previous Path}`;
Running `$PATH` in a future step will now return `/Users/test/.nvm/versions/node/v12.18.3/bin:{Previous Path}`;

This is wrapped by the core addPath method:
```javascript
Expand Down
56 changes: 47 additions & 9 deletions packages/core/__tests__/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ const testEnvVars = {
'my secret': '',
'special char secret \r\n];': '',
'my secret2': '',
PATH: `path1${path.delimiter}path2`,
'PATH': `path1${path.delimiter}path2`,

// Set inputs
INPUT_MY_INPUT: 'val',
INPUT_MISSING: '',
'INPUT_MY_INPUT': 'val',
'INPUT_MISSING': '',
'INPUT_SPECIAL_CHARS_\'\t"\\': '\'\t"\\ response ',
INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces',
'INPUT_MULTIPLE_SPACES_VARIABLE': 'I have multiple spaces',

// Save inputs
STATE_TEST_1: 'state_val'
'STATE_TEST_1': 'state_val',

// File Commands
'GITHUB_PATH': '',
'GITHUB_ENV': ''
}

describe('@actions/core', () => {
Expand All @@ -34,13 +38,39 @@ describe('@actions/core', () => {

beforeEach(() => {
for (const key in testEnvVars)
process.env[key] = testEnvVars[key as keyof typeof testEnvVars]

{
process.env[key] = (testEnvVars as { [key: string]: any })[key] as string
}
process.stdout.write = jest.fn()
})

afterEach(() => {
for (const key in testEnvVars) Reflect.deleteProperty(testEnvVars, key)
it('legacy exportVariable produces the correct command and sets the env', () => {
core.exportVariable('my var', 'var val')
assertWriteCalls([`::set-env name=my var::var val${os.EOL}`])
})

it('legacy exportVariable escapes variable names', () => {
core.exportVariable('special char var \r\n,:', 'special val')
expect(process.env['special char var \r\n,:']).toBe('special val')
assertWriteCalls([
`::set-env name=special char var %0D%0A%2C%3A::special val${os.EOL}`
])
})

it('legacy exportVariable escapes variable values', () => {
core.exportVariable('my var2', 'var val\r\n')
expect(process.env['my var2']).toBe('var val\r\n')
assertWriteCalls([`::set-env name=my var2::var val%0D%0A${os.EOL}`])
})

it('legacy exportVariable handles boolean inputs', () => {
core.exportVariable('my var', true)
assertWriteCalls([`::set-env name=my var::true${os.EOL}`])
})

it('legacy exportVariable handles number inputs', () => {
core.exportVariable('my var', 5)
assertWriteCalls([`::set-env name=my var::5${os.EOL}`])
})

it('exportVariable produces the correct command and sets the env', () => {
Expand Down Expand Up @@ -88,6 +118,14 @@ describe('@actions/core', () => {
verifyFileCommand(command, `myPath${os.EOL}`)
})

it('legacy 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')
})
Expand Down
26 changes: 22 additions & 4 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,18 @@ export enum ExitCode {
export function exportVariable(name: string, val: any): void {
const convertedVal = toCommandValue(val)
process.env[name] = convertedVal
const delimiter = '_GitHubActionsFileCommandDelimeter_'
const commandValue = `${name}<<${delimiter}${os.EOL}${convertedVal}${os.EOL}${delimiter}`
issueFileCommand('ENV', commandValue)

var filePath = process.env["GITHUB_ENV"] || ""
if (filePath && filePath.length > 0)
{
const delimiter = '_GitHubActionsFileCommandDelimeter_'
const commandValue = `${name}<<${delimiter}${os.EOL}${convertedVal}${os.EOL}${delimiter}`
issueFileCommand('ENV', commandValue)
}
else
{
issueCommand('set-env', {name}, convertedVal)
}
}

/**
Expand All @@ -59,7 +68,16 @@ export function setSecret(secret: string): void {
* @param inputPath
*/
export function addPath(inputPath: string): void {
issueFileCommand('PATH', inputPath)

var filePath = process.env["GITHUB_PATH"] || ""
if (filePath && filePath.length > 0)
{
issueFileCommand('PATH', inputPath)
}
else
{
issueCommand('add-path', {}, inputPath)
}
process.env['PATH'] = `${inputPath}${path.delimiter}${process.env['PATH']}`
}

Expand Down