|
1 | 1 | import * as cp from 'child_process'
|
2 | 2 | import * as path from 'path'
|
3 |
| -import {expect, test} from '@jest/globals' |
4 |
| - |
5 |
| -// shows how the runner will run a javascript action with env / stdout protocol |
6 |
| -test('test runs', () => { |
7 |
| - process.env['INPUT_SECRETS'] = '{"AAA": "123"}' |
8 |
| - const np = process.execPath |
9 |
| - const ip = path.join(__dirname, '..', 'lib', 'main.js') |
10 |
| - const options: cp.ExecFileSyncOptions = { |
11 |
| - env: process.env |
12 |
| - } |
13 |
| - console.log(cp.execFileSync(np, [ip], options).toString()) |
| 3 | +import {expect, jest, test} from '@jest/globals' |
| 4 | +import * as core from '@actions/core' |
| 5 | +import main from '../src/main' |
| 6 | + |
| 7 | +jest.mock('@actions/core') |
| 8 | + |
| 9 | +let mockedCore: jest.Mocked<typeof core> |
| 10 | + |
| 11 | +jest.mocked(core.debug).mockImplementation(s => console.log(`DEBUG: ${s}`)) |
| 12 | +jest.mocked(core.info).mockImplementation(s => console.log(`INFO: ${s}`)) |
| 13 | +jest.mocked(core.warning).mockImplementation(s => console.log(`WARNING: ${s}`)) |
| 14 | + |
| 15 | +function mockInputs(inputs: {[key: string]: string}) { |
| 16 | + jest.mocked(core.getInput).mockImplementation(s => inputs[s] || '') |
| 17 | +} |
| 18 | + |
| 19 | +describe('secrets-to-env-action', () => { |
| 20 | + let inputSecrets: {[key: string]: string} |
| 21 | + let newSecrets: {[key: string]: string} |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + inputSecrets = { |
| 25 | + MY_SECRET_1: 'VALUE_1', |
| 26 | + MY_SECRET_2: 'VALUE_2', |
| 27 | + my_low_secret_1: 'low_value_1' |
| 28 | + } |
| 29 | + |
| 30 | + newSecrets = {} |
| 31 | + jest |
| 32 | + .mocked(core.exportVariable) |
| 33 | + .mockImplementation((k, v) => (newSecrets[k] = v)) |
| 34 | + }) |
| 35 | + |
| 36 | + it('exports all variables', () => { |
| 37 | + mockInputs({ |
| 38 | + secrets: JSON.stringify(inputSecrets) |
| 39 | + }) |
| 40 | + main() |
| 41 | + expect(newSecrets).toEqual(inputSecrets) |
| 42 | + }) |
| 43 | + |
| 44 | + it('excludes variables (single)', () => { |
| 45 | + mockInputs({ |
| 46 | + secrets: JSON.stringify(inputSecrets), |
| 47 | + exclude: 'MY_SECRET_1' |
| 48 | + }) |
| 49 | + main() |
| 50 | + delete inputSecrets.MY_SECRET_1 |
| 51 | + expect(newSecrets).toEqual(inputSecrets) |
| 52 | + }) |
| 53 | + |
| 54 | + it('excludes variables (array)', () => { |
| 55 | + mockInputs({ |
| 56 | + secrets: JSON.stringify(inputSecrets), |
| 57 | + exclude: 'MY_SECRET_1,MY_SECRET_2,ignore' |
| 58 | + }) |
| 59 | + main() |
| 60 | + delete inputSecrets.MY_SECRET_1 |
| 61 | + delete inputSecrets.MY_SECRET_2 |
| 62 | + expect(newSecrets).toEqual(inputSecrets) |
| 63 | + }) |
| 64 | + |
| 65 | + it('excludes variables (regex)', () => { |
| 66 | + mockInputs({ |
| 67 | + secrets: JSON.stringify(inputSecrets), |
| 68 | + exclude: 'MY_SECRET_*,ignore' |
| 69 | + }) |
| 70 | + main() |
| 71 | + delete inputSecrets.MY_SECRET_1 |
| 72 | + delete inputSecrets.MY_SECRET_2 |
| 73 | + expect(newSecrets).toEqual(inputSecrets) |
| 74 | + }) |
| 75 | + |
| 76 | + it('includes variables (single)', () => { |
| 77 | + mockInputs({ |
| 78 | + secrets: JSON.stringify(inputSecrets), |
| 79 | + include: 'MY_SECRET_1' |
| 80 | + }) |
| 81 | + main() |
| 82 | + |
| 83 | + expect(newSecrets).toEqual({ |
| 84 | + MY_SECRET_1: inputSecrets.MY_SECRET_1 |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + it('includes variables (array)', () => { |
| 89 | + mockInputs({ |
| 90 | + secrets: JSON.stringify(inputSecrets), |
| 91 | + include: 'MY_SECRET_1, MY_SECRET_2, ignore' |
| 92 | + }) |
| 93 | + main() |
| 94 | + |
| 95 | + expect(newSecrets).toEqual({ |
| 96 | + MY_SECRET_1: inputSecrets.MY_SECRET_1, |
| 97 | + MY_SECRET_2: inputSecrets.MY_SECRET_2 |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + it('includes variables (regex)', () => { |
| 102 | + mockInputs({ |
| 103 | + secrets: JSON.stringify(inputSecrets), |
| 104 | + include: 'MY_SECRET_*' |
| 105 | + }) |
| 106 | + main() |
| 107 | + |
| 108 | + expect(newSecrets).toEqual({ |
| 109 | + MY_SECRET_1: inputSecrets.MY_SECRET_1, |
| 110 | + MY_SECRET_2: inputSecrets.MY_SECRET_2 |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + it('adds a prefix', () => { |
| 115 | + mockInputs({ |
| 116 | + secrets: JSON.stringify(inputSecrets), |
| 117 | + prefix: 'PREF_', |
| 118 | + include: 'MY_SECRET_1, MY_SECRET_2' |
| 119 | + }) |
| 120 | + main() |
| 121 | + |
| 122 | + expect(newSecrets).toEqual({ |
| 123 | + PREF_MY_SECRET_1: inputSecrets.MY_SECRET_1, |
| 124 | + PREF_MY_SECRET_2: inputSecrets.MY_SECRET_2 |
| 125 | + }) |
| 126 | + }) |
| 127 | + |
| 128 | + it('converts key (lower)', () => { |
| 129 | + mockInputs({ |
| 130 | + secrets: JSON.stringify(inputSecrets), |
| 131 | + include: 'MY_SECRET_1, MY_SECRET_2', |
| 132 | + convert: 'lower' |
| 133 | + }) |
| 134 | + main() |
| 135 | + |
| 136 | + expect(newSecrets).toEqual({ |
| 137 | + my_secret_1: inputSecrets.MY_SECRET_1, |
| 138 | + my_secret_2: inputSecrets.MY_SECRET_2 |
| 139 | + }) |
| 140 | + }) |
| 141 | + |
| 142 | + it('converts key (camel)', () => { |
| 143 | + mockInputs({ |
| 144 | + secrets: JSON.stringify(inputSecrets), |
| 145 | + include: 'MY_SECRET_1, MY_SECRET_2', |
| 146 | + convert: 'camel' |
| 147 | + }) |
| 148 | + main() |
| 149 | + |
| 150 | + expect(newSecrets).toEqual({ |
| 151 | + mySecret_1: inputSecrets.MY_SECRET_1, |
| 152 | + mySecret_2: inputSecrets.MY_SECRET_2 |
| 153 | + }) |
| 154 | + }) |
| 155 | + |
| 156 | + it('converts key (pascal)', () => { |
| 157 | + mockInputs({ |
| 158 | + secrets: JSON.stringify(inputSecrets), |
| 159 | + include: 'MY_SECRET_1, MY_SECRET_2', |
| 160 | + convert: 'pascal' |
| 161 | + }) |
| 162 | + main() |
| 163 | + |
| 164 | + expect(newSecrets).toEqual({ |
| 165 | + MySecret_1: inputSecrets.MY_SECRET_1, |
| 166 | + MySecret_2: inputSecrets.MY_SECRET_2 |
| 167 | + }) |
| 168 | + }) |
| 169 | + |
| 170 | + it('converts key (snake)', () => { |
| 171 | + mockInputs({ |
| 172 | + secrets: JSON.stringify(inputSecrets), |
| 173 | + include: 'MY_SECRET_1, MY_SECRET_2', |
| 174 | + convert: 'snake' |
| 175 | + }) |
| 176 | + main() |
| 177 | + |
| 178 | + expect(newSecrets).toEqual({ |
| 179 | + my_secret_1: inputSecrets.MY_SECRET_1, |
| 180 | + my_secret_2: inputSecrets.MY_SECRET_2 |
| 181 | + }) |
| 182 | + }) |
| 183 | + |
| 184 | + it('converts prefix', () => { |
| 185 | + mockInputs({ |
| 186 | + secrets: JSON.stringify(inputSecrets), |
| 187 | + include: 'MY_SECRET_1, MY_SECRET_2', |
| 188 | + prefix: 'PREFIX_', |
| 189 | + convert: 'snake', |
| 190 | + convert_prefix: 'true' |
| 191 | + }) |
| 192 | + main() |
| 193 | + |
| 194 | + expect(newSecrets).toEqual({ |
| 195 | + prefix_my_secret_1: inputSecrets.MY_SECRET_1, |
| 196 | + prefix_my_secret_2: inputSecrets.MY_SECRET_2 |
| 197 | + }) |
| 198 | + }) |
| 199 | + |
| 200 | + it('does not convert prefix', () => { |
| 201 | + mockInputs({ |
| 202 | + secrets: JSON.stringify(inputSecrets), |
| 203 | + include: 'MY_SECRET_1, MY_SECRET_2', |
| 204 | + prefix: 'PREFIX_', |
| 205 | + convert: 'snake', |
| 206 | + convert_prefix: 'false' |
| 207 | + }) |
| 208 | + main() |
| 209 | + |
| 210 | + expect(newSecrets).toEqual({ |
| 211 | + PREFIX_my_secret_1: inputSecrets.MY_SECRET_1, |
| 212 | + PREFIX_my_secret_2: inputSecrets.MY_SECRET_2 |
| 213 | + }) |
| 214 | + }) |
14 | 215 | })
|
0 commit comments