Add File Commands (#571)
* Add File Commands * pr updates w/ feedback * run format * fix lint/format * slight update with an example in the docs * pr feedback
This commit is contained in:
1 parent
73fbf4efc7
commit
4e6fd9c7f7
5 files changed
+137
-29
No files matched your search
+79
-13
@@ -1,3 +1,4 @@
|
|||||||
|
import * as fs from 'fs'
|
||||||
import * as os from 'os'
|
import * as os from 'os'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import * as core from '../src/core'
|
import * as core from '../src/core'
|
||||||
@@ -20,27 +21,34 @@ const testEnvVars = {
|
|||||||
INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces',
|
INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces',
|
||||||
|
|
||||||
// Save inputs
|
// Save inputs
|
||||||
STATE_TEST_1: 'state_val'
|
STATE_TEST_1: 'state_val',
|
||||||
|
|
||||||
|
// File Commands
|
||||||
|
GITHUB_PATH: '',
|
||||||
|
GITHUB_ENV: ''
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('@actions/core', () => {
|
describe('@actions/core', () => {
|
||||||
beforeEach(() => {
|
beforeAll(() => {
|
||||||
for (const key in testEnvVars)
|
const filePath = path.join(__dirname, `test`)
|
||||||
process.env[key] = testEnvVars[key as keyof typeof testEnvVars]
|
if (!fs.existsSync(filePath)) {
|
||||||
|
fs.mkdirSync(filePath)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
for (const key in testEnvVars) {
|
||||||
|
process.env[key] = testEnvVars[key as keyof typeof testEnvVars]
|
||||||
|
}
|
||||||
process.stdout.write = jest.fn()
|
process.stdout.write = jest.fn()
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
it('legacy exportVariable produces the correct command and sets the env', () => {
|
||||||
for (const key in testEnvVars) Reflect.deleteProperty(testEnvVars, key)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('exportVariable produces the correct command and sets the env', () => {
|
|
||||||
core.exportVariable('my var', 'var val')
|
core.exportVariable('my var', 'var val')
|
||||||
assertWriteCalls([`::set-env name=my var::var val${os.EOL}`])
|
assertWriteCalls([`::set-env name=my var::var val${os.EOL}`])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('exportVariable escapes variable names', () => {
|
it('legacy exportVariable escapes variable names', () => {
|
||||||
core.exportVariable('special char var \r\n,:', 'special val')
|
core.exportVariable('special char var \r\n,:', 'special val')
|
||||||
expect(process.env['special char var \r\n,:']).toBe('special val')
|
expect(process.env['special char var \r\n,:']).toBe('special val')
|
||||||
assertWriteCalls([
|
assertWriteCalls([
|
||||||
@@ -48,28 +56,68 @@ describe('@actions/core', () => {
|
|||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('exportVariable escapes variable values', () => {
|
it('legacy exportVariable escapes variable values', () => {
|
||||||
core.exportVariable('my var2', 'var val\r\n')
|
core.exportVariable('my var2', 'var val\r\n')
|
||||||
expect(process.env['my var2']).toBe('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}`])
|
assertWriteCalls([`::set-env name=my var2::var val%0D%0A${os.EOL}`])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('exportVariable handles boolean inputs', () => {
|
it('legacy exportVariable handles boolean inputs', () => {
|
||||||
core.exportVariable('my var', true)
|
core.exportVariable('my var', true)
|
||||||
assertWriteCalls([`::set-env name=my var::true${os.EOL}`])
|
assertWriteCalls([`::set-env name=my var::true${os.EOL}`])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('exportVariable handles number inputs', () => {
|
it('legacy exportVariable handles number inputs', () => {
|
||||||
core.exportVariable('my var', 5)
|
core.exportVariable('my var', 5)
|
||||||
assertWriteCalls([`::set-env name=my var::5${os.EOL}`])
|
assertWriteCalls([`::set-env name=my var::5${os.EOL}`])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('exportVariable produces the correct command and sets the env', () => {
|
||||||
|
const command = 'ENV'
|
||||||
|
createFileCommandFile(command)
|
||||||
|
core.exportVariable('my var', 'var val')
|
||||||
|
verifyFileCommand(
|
||||||
|
command,
|
||||||
|
`my var<<_GitHubActionsFileCommandDelimeter_${os.EOL}var val${os.EOL}_GitHubActionsFileCommandDelimeter_${os.EOL}`
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('exportVariable handles boolean inputs', () => {
|
||||||
|
const command = 'ENV'
|
||||||
|
createFileCommandFile(command)
|
||||||
|
core.exportVariable('my var', true)
|
||||||
|
verifyFileCommand(
|
||||||
|
command,
|
||||||
|
`my var<<_GitHubActionsFileCommandDelimeter_${os.EOL}true${os.EOL}_GitHubActionsFileCommandDelimeter_${os.EOL}`
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('exportVariable handles number inputs', () => {
|
||||||
|
const command = 'ENV'
|
||||||
|
createFileCommandFile(command)
|
||||||
|
core.exportVariable('my var', 5)
|
||||||
|
verifyFileCommand(
|
||||||
|
command,
|
||||||
|
`my var<<_GitHubActionsFileCommandDelimeter_${os.EOL}5${os.EOL}_GitHubActionsFileCommandDelimeter_${os.EOL}`
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
it('setSecret produces the correct command', () => {
|
it('setSecret produces the correct command', () => {
|
||||||
core.setSecret('secret val')
|
core.setSecret('secret val')
|
||||||
assertWriteCalls([`::add-mask::secret val${os.EOL}`])
|
assertWriteCalls([`::add-mask::secret val${os.EOL}`])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('prependPath produces the correct commands and sets the env', () => {
|
it('prependPath produces the correct commands and sets the env', () => {
|
||||||
|
const command = 'PATH'
|
||||||
|
createFileCommandFile(command)
|
||||||
|
core.addPath('myPath')
|
||||||
|
expect(process.env['PATH']).toBe(
|
||||||
|
`myPath${path.delimiter}path1${path.delimiter}path2`
|
||||||
|
)
|
||||||
|
verifyFileCommand(command, `myPath${os.EOL}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('legacy prependPath produces the correct commands and sets the env', () => {
|
||||||
core.addPath('myPath')
|
core.addPath('myPath')
|
||||||
expect(process.env['PATH']).toBe(
|
expect(process.env['PATH']).toBe(
|
||||||
`myPath${path.delimiter}path1${path.delimiter}path2`
|
`myPath${path.delimiter}path1${path.delimiter}path2`
|
||||||
@@ -259,3 +307,21 @@ function assertWriteCalls(calls: string[]): void {
|
|||||||
expect(process.stdout.write).toHaveBeenNthCalledWith(i + 1, calls[i])
|
expect(process.stdout.write).toHaveBeenNthCalledWith(i + 1, calls[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createFileCommandFile(command: string): void {
|
||||||
|
const filePath = path.join(__dirname, `test/${command}`)
|
||||||
|
process.env[`GITHUB_${command}`] = filePath
|
||||||
|
fs.appendFileSync(filePath, '', {
|
||||||
|
encoding: 'utf8'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function verifyFileCommand(command: string, expectedContents: string): void {
|
||||||
|
const filePath = path.join(__dirname, `test/${command}`)
|
||||||
|
const contents = fs.readFileSync(filePath, 'utf8')
|
||||||
|
try {
|
||||||
|
expect(contents).toEqual(expectedContents)
|
||||||
|
} finally {
|
||||||
|
fs.unlinkSync(filePath)
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-13
@@ -1,4 +1,5 @@
|
|||||||
import * as os from 'os'
|
import * as os from 'os'
|
||||||
|
import {toCommandValue} from './utils'
|
||||||
|
|
||||||
// For internal use, subject to change.
|
// For internal use, subject to change.
|
||||||
|
|
||||||
@@ -76,19 +77,6 @@ class Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sanitizes an input into a string so it can be passed into issueCommand safely
|
|
||||||
* @param input input to sanitize into a string
|
|
||||||
*/
|
|
||||||
export function toCommandValue(input: any): string {
|
|
||||||
if (input === null || input === undefined) {
|
|
||||||
return ''
|
|
||||||
} else if (typeof input === 'string' || input instanceof String) {
|
|
||||||
return input as string
|
|
||||||
}
|
|
||||||
return JSON.stringify(input)
|
|
||||||
}
|
|
||||||
|
|
||||||
function escapeData(s: any): string {
|
function escapeData(s: any): string {
|
||||||
return toCommandValue(s)
|
return toCommandValue(s)
|
||||||
.replace(/%/g, '%25')
|
.replace(/%/g, '%25')
|
||||||
|
|||||||
+18
-3
@@ -1,4 +1,6 @@
|
|||||||
import {issue, issueCommand, toCommandValue} from './command'
|
import {issue, issueCommand} from './command'
|
||||||
|
import {issueCommand as issueFileCommand} from './file-command'
|
||||||
|
import {toCommandValue} from './utils'
|
||||||
|
|
||||||
import * as os from 'os'
|
import * as os from 'os'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
@@ -39,7 +41,15 @@ export enum ExitCode {
|
|||||||
export function exportVariable(name: string, val: any): void {
|
export function exportVariable(name: string, val: any): void {
|
||||||
const convertedVal = toCommandValue(val)
|
const convertedVal = toCommandValue(val)
|
||||||
process.env[name] = convertedVal
|
process.env[name] = convertedVal
|
||||||
issueCommand('set-env', {name}, convertedVal)
|
|
||||||
|
const filePath = process.env['GITHUB_ENV'] || ''
|
||||||
|
if (filePath) {
|
||||||
|
const delimiter = '_GitHubActionsFileCommandDelimeter_'
|
||||||
|
const commandValue = `${name}<<${delimiter}${os.EOL}${convertedVal}${os.EOL}${delimiter}`
|
||||||
|
issueFileCommand('ENV', commandValue)
|
||||||
|
} else {
|
||||||
|
issueCommand('set-env', {name}, convertedVal)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -55,7 +65,12 @@ export function setSecret(secret: string): void {
|
|||||||
* @param inputPath
|
* @param inputPath
|
||||||
*/
|
*/
|
||||||
export function addPath(inputPath: string): void {
|
export function addPath(inputPath: string): void {
|
||||||
issueCommand('add-path', {}, inputPath)
|
const filePath = process.env['GITHUB_PATH'] || ''
|
||||||
|
if (filePath) {
|
||||||
|
issueFileCommand('PATH', inputPath)
|
||||||
|
} else {
|
||||||
|
issueCommand('add-path', {}, inputPath)
|
||||||
|
}
|
||||||
process.env['PATH'] = `${inputPath}${path.delimiter}${process.env['PATH']}`
|
process.env['PATH'] = `${inputPath}${path.delimiter}${process.env['PATH']}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
// For internal use, subject to change.
|
||||||
|
|
||||||
|
// We use any as a valid input type
|
||||||
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
|
|
||||||
|
import * as fs from 'fs'
|
||||||
|
import * as os from 'os'
|
||||||
|
import {toCommandValue} from './utils'
|
||||||
|
|
||||||
|
export function issueCommand(command: string, message: any): void {
|
||||||
|
const filePath = process.env[`GITHUB_${command}`]
|
||||||
|
if (!filePath) {
|
||||||
|
throw new Error(
|
||||||
|
`Unable to find environment variable for file command ${command}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (!fs.existsSync(filePath)) {
|
||||||
|
throw new Error(`Missing file at path: ${filePath}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.appendFileSync(filePath, `${toCommandValue(message)}${os.EOL}`, {
|
||||||
|
encoding: 'utf8'
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
// We use any as a valid input type
|
||||||
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitizes an input into a string so it can be passed into issueCommand safely
|
||||||
|
* @param input input to sanitize into a string
|
||||||
|
*/
|
||||||
|
export function toCommandValue(input: any): string {
|
||||||
|
if (input === null || input === undefined) {
|
||||||
|
return ''
|
||||||
|
} else if (typeof input === 'string' || input instanceof String) {
|
||||||
|
return input as string
|
||||||
|
}
|
||||||
|
return JSON.stringify(input)
|
||||||
|
}
|
||||||
Reference in new issue
Block a user