generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add regex support to include/exclude (replaces starts_with)
Also: - Adds proper unit tests
- Loading branch information
Showing
10 changed files
with
419 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: 'build-test' | ||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- main | ||
- 'releases/*' | ||
|
||
jobs: | ||
build-test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- run: | | ||
npm install | ||
- run: | | ||
npm run all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,215 @@ | ||
import * as cp from 'child_process' | ||
import * as path from 'path' | ||
import {expect, test} from '@jest/globals' | ||
|
||
// shows how the runner will run a javascript action with env / stdout protocol | ||
test('test runs', () => { | ||
process.env['INPUT_SECRETS'] = '{"AAA": "123"}' | ||
const np = process.execPath | ||
const ip = path.join(__dirname, '..', 'lib', 'main.js') | ||
const options: cp.ExecFileSyncOptions = { | ||
env: process.env | ||
} | ||
console.log(cp.execFileSync(np, [ip], options).toString()) | ||
import {expect, jest, test} from '@jest/globals' | ||
import * as core from '@actions/core' | ||
import main from '../src/main' | ||
|
||
jest.mock('@actions/core') | ||
|
||
let mockedCore: jest.Mocked<typeof core> | ||
|
||
jest.mocked(core.debug).mockImplementation(s => console.log(`DEBUG: ${s}`)) | ||
jest.mocked(core.info).mockImplementation(s => console.log(`INFO: ${s}`)) | ||
jest.mocked(core.warning).mockImplementation(s => console.log(`WARNING: ${s}`)) | ||
|
||
function mockInputs(inputs: {[key: string]: string}) { | ||
jest.mocked(core.getInput).mockImplementation(s => inputs[s] || '') | ||
} | ||
|
||
describe('secrets-to-env-action', () => { | ||
let inputSecrets: {[key: string]: string} | ||
let newSecrets: {[key: string]: string} | ||
|
||
beforeEach(() => { | ||
inputSecrets = { | ||
MY_SECRET_1: 'VALUE_1', | ||
MY_SECRET_2: 'VALUE_2', | ||
my_low_secret_1: 'low_value_1' | ||
} | ||
|
||
newSecrets = {} | ||
jest | ||
.mocked(core.exportVariable) | ||
.mockImplementation((k, v) => (newSecrets[k] = v)) | ||
}) | ||
|
||
it('exports all variables', () => { | ||
mockInputs({ | ||
secrets: JSON.stringify(inputSecrets) | ||
}) | ||
main() | ||
expect(newSecrets).toEqual(inputSecrets) | ||
}) | ||
|
||
it('excludes variables (single)', () => { | ||
mockInputs({ | ||
secrets: JSON.stringify(inputSecrets), | ||
exclude: 'MY_SECRET_1' | ||
}) | ||
main() | ||
delete inputSecrets.MY_SECRET_1 | ||
expect(newSecrets).toEqual(inputSecrets) | ||
}) | ||
|
||
it('excludes variables (array)', () => { | ||
mockInputs({ | ||
secrets: JSON.stringify(inputSecrets), | ||
exclude: 'MY_SECRET_1,MY_SECRET_2,ignore' | ||
}) | ||
main() | ||
delete inputSecrets.MY_SECRET_1 | ||
delete inputSecrets.MY_SECRET_2 | ||
expect(newSecrets).toEqual(inputSecrets) | ||
}) | ||
|
||
it('excludes variables (regex)', () => { | ||
mockInputs({ | ||
secrets: JSON.stringify(inputSecrets), | ||
exclude: 'MY_SECRET_*,ignore' | ||
}) | ||
main() | ||
delete inputSecrets.MY_SECRET_1 | ||
delete inputSecrets.MY_SECRET_2 | ||
expect(newSecrets).toEqual(inputSecrets) | ||
}) | ||
|
||
it('includes variables (single)', () => { | ||
mockInputs({ | ||
secrets: JSON.stringify(inputSecrets), | ||
include: 'MY_SECRET_1' | ||
}) | ||
main() | ||
|
||
expect(newSecrets).toEqual({ | ||
MY_SECRET_1: inputSecrets.MY_SECRET_1 | ||
}) | ||
}) | ||
|
||
it('includes variables (array)', () => { | ||
mockInputs({ | ||
secrets: JSON.stringify(inputSecrets), | ||
include: 'MY_SECRET_1, MY_SECRET_2, ignore' | ||
}) | ||
main() | ||
|
||
expect(newSecrets).toEqual({ | ||
MY_SECRET_1: inputSecrets.MY_SECRET_1, | ||
MY_SECRET_2: inputSecrets.MY_SECRET_2 | ||
}) | ||
}) | ||
|
||
it('includes variables (regex)', () => { | ||
mockInputs({ | ||
secrets: JSON.stringify(inputSecrets), | ||
include: 'MY_SECRET_*' | ||
}) | ||
main() | ||
|
||
expect(newSecrets).toEqual({ | ||
MY_SECRET_1: inputSecrets.MY_SECRET_1, | ||
MY_SECRET_2: inputSecrets.MY_SECRET_2 | ||
}) | ||
}) | ||
|
||
it('adds a prefix', () => { | ||
mockInputs({ | ||
secrets: JSON.stringify(inputSecrets), | ||
prefix: 'PREF_', | ||
include: 'MY_SECRET_1, MY_SECRET_2' | ||
}) | ||
main() | ||
|
||
expect(newSecrets).toEqual({ | ||
PREF_MY_SECRET_1: inputSecrets.MY_SECRET_1, | ||
PREF_MY_SECRET_2: inputSecrets.MY_SECRET_2 | ||
}) | ||
}) | ||
|
||
it('converts key (lower)', () => { | ||
mockInputs({ | ||
secrets: JSON.stringify(inputSecrets), | ||
include: 'MY_SECRET_1, MY_SECRET_2', | ||
convert: 'lower' | ||
}) | ||
main() | ||
|
||
expect(newSecrets).toEqual({ | ||
my_secret_1: inputSecrets.MY_SECRET_1, | ||
my_secret_2: inputSecrets.MY_SECRET_2 | ||
}) | ||
}) | ||
|
||
it('converts key (camel)', () => { | ||
mockInputs({ | ||
secrets: JSON.stringify(inputSecrets), | ||
include: 'MY_SECRET_1, MY_SECRET_2', | ||
convert: 'camel' | ||
}) | ||
main() | ||
|
||
expect(newSecrets).toEqual({ | ||
mySecret_1: inputSecrets.MY_SECRET_1, | ||
mySecret_2: inputSecrets.MY_SECRET_2 | ||
}) | ||
}) | ||
|
||
it('converts key (pascal)', () => { | ||
mockInputs({ | ||
secrets: JSON.stringify(inputSecrets), | ||
include: 'MY_SECRET_1, MY_SECRET_2', | ||
convert: 'pascal' | ||
}) | ||
main() | ||
|
||
expect(newSecrets).toEqual({ | ||
MySecret_1: inputSecrets.MY_SECRET_1, | ||
MySecret_2: inputSecrets.MY_SECRET_2 | ||
}) | ||
}) | ||
|
||
it('converts key (snake)', () => { | ||
mockInputs({ | ||
secrets: JSON.stringify(inputSecrets), | ||
include: 'MY_SECRET_1, MY_SECRET_2', | ||
convert: 'snake' | ||
}) | ||
main() | ||
|
||
expect(newSecrets).toEqual({ | ||
my_secret_1: inputSecrets.MY_SECRET_1, | ||
my_secret_2: inputSecrets.MY_SECRET_2 | ||
}) | ||
}) | ||
|
||
it('converts prefix', () => { | ||
mockInputs({ | ||
secrets: JSON.stringify(inputSecrets), | ||
include: 'MY_SECRET_1, MY_SECRET_2', | ||
prefix: 'PREFIX_', | ||
convert: 'snake', | ||
convert_prefix: 'true' | ||
}) | ||
main() | ||
|
||
expect(newSecrets).toEqual({ | ||
prefix_my_secret_1: inputSecrets.MY_SECRET_1, | ||
prefix_my_secret_2: inputSecrets.MY_SECRET_2 | ||
}) | ||
}) | ||
|
||
it('does not convert prefix', () => { | ||
mockInputs({ | ||
secrets: JSON.stringify(inputSecrets), | ||
include: 'MY_SECRET_1, MY_SECRET_2', | ||
prefix: 'PREFIX_', | ||
convert: 'snake', | ||
convert_prefix: 'false' | ||
}) | ||
main() | ||
|
||
expect(newSecrets).toEqual({ | ||
PREFIX_my_secret_1: inputSecrets.MY_SECRET_1, | ||
PREFIX_my_secret_2: inputSecrets.MY_SECRET_2 | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.