-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
core.test.ts
251 lines (205 loc) · 7.43 KB
/
core.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import * as os from 'os'
import * as path from 'path'
import * as core from '../src/core'
/* eslint-disable @typescript-eslint/unbound-method */
const testEnvVars = {
'my var': '',
'special char var \r\n];': '',
'my var2': '',
'my secret': '',
'special char secret \r\n];': '',
'my secret2': '',
PATH: `path1${path.delimiter}path2`,
// Set inputs
INPUT_MY_INPUT: 'val',
INPUT_MISSING: '',
'INPUT_SPECIAL_CHARS_\'\t"\\': '\'\t"\\ response ',
INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces',
// Save inputs
STATE_TEST_1: 'state_val'
}
describe('@actions/core', () => {
beforeEach(() => {
for (const key in testEnvVars)
process.env[key] = testEnvVars[key as keyof typeof testEnvVars]
process.stdout.write = jest.fn()
})
afterEach(() => {
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')
assertWriteCalls([`::set-env name=my var::var val${os.EOL}`])
})
it('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('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('exportVariable handles boolean inputs', () => {
core.exportVariable('my var', true)
assertWriteCalls([`::set-env name=my var::true${os.EOL}`])
})
it('exportVariable handles number inputs', () => {
core.exportVariable('my var', 5)
assertWriteCalls([`::set-env name=my var::5${os.EOL}`])
})
it('setSecret produces the correct command', () => {
core.setSecret('secret val')
assertWriteCalls([`::add-mask::secret val${os.EOL}`])
})
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')
})
it('getInput gets required input', () => {
expect(core.getInput('my input', {required: true})).toBe('val')
})
it('getInput throws on missing required input', () => {
expect(() => core.getInput('missing', {required: true})).toThrow(
'Input required and not supplied: missing'
)
})
it('getInput does not throw on missing non-required input', () => {
expect(core.getInput('missing', {required: false})).toBe('')
})
it('getInput is case insensitive', () => {
expect(core.getInput('My InPuT')).toBe('val')
})
it('getInput handles special characters', () => {
expect(core.getInput('special chars_\'\t"\\')).toBe('\'\t"\\ response')
})
it('getInput handles multiple spaces', () => {
expect(core.getInput('multiple spaces variable')).toBe(
'I have multiple spaces'
)
})
it('setOutput produces the correct command', () => {
core.setOutput('some output', 'some value')
assertWriteCalls([`::set-output name=some output::some value${os.EOL}`])
})
it('setOutput handles bools', () => {
core.setOutput('some output', false)
assertWriteCalls([`::set-output name=some output::false${os.EOL}`])
})
it('setOutput handles numbers', () => {
core.setOutput('some output', 1.01)
assertWriteCalls([`::set-output name=some output::1.01${os.EOL}`])
})
it('setFailed sets the correct exit code and failure message', () => {
core.setFailed('Failure message')
expect(process.exitCode).toBe(core.ExitCode.Failure)
assertWriteCalls([`::error::Failure message${os.EOL}`])
})
it('setFailed escapes the failure message', () => {
core.setFailed('Failure \r\n\nmessage\r')
expect(process.exitCode).toBe(core.ExitCode.Failure)
assertWriteCalls([`::error::Failure %0D%0A%0Amessage%0D${os.EOL}`])
})
it('setFailed handles Error', () => {
const message = 'this is my error message'
core.setFailed(new Error(message))
expect(process.exitCode).toBe(core.ExitCode.Failure)
assertWriteCalls([`::error::Error: ${message}${os.EOL}`])
})
it('error sets the correct error message', () => {
core.error('Error message')
assertWriteCalls([`::error::Error message${os.EOL}`])
})
it('error escapes the error message', () => {
core.error('Error message\r\n\n')
assertWriteCalls([`::error::Error message%0D%0A%0A${os.EOL}`])
})
it('error handles an error object', () => {
const message = 'this is my error message'
core.error(new Error(message))
assertWriteCalls([`::error::Error: ${message}${os.EOL}`])
})
it('warning sets the correct message', () => {
core.warning('Warning')
assertWriteCalls([`::warning::Warning${os.EOL}`])
})
it('warning escapes the message', () => {
core.warning('\r\nwarning\n')
assertWriteCalls([`::warning::%0D%0Awarning%0A${os.EOL}`])
})
it('warning handles an error object', () => {
const message = 'this is my error message'
core.warning(new Error(message))
assertWriteCalls([`::warning::Error: ${message}${os.EOL}`])
})
it('startGroup starts a new group', () => {
core.startGroup('my-group')
assertWriteCalls([`::group::my-group${os.EOL}`])
})
it('endGroup ends new group', () => {
core.endGroup()
assertWriteCalls([`::endgroup::${os.EOL}`])
})
it('group wraps an async call in a group', async () => {
const result = await core.group('mygroup', async () => {
process.stdout.write('in my group\n')
return true
})
expect(result).toBe(true)
assertWriteCalls([
`::group::mygroup${os.EOL}`,
'in my group\n',
`::endgroup::${os.EOL}`
])
})
it('debug sets the correct message', () => {
core.debug('Debug')
assertWriteCalls([`::debug::Debug${os.EOL}`])
})
it('debug escapes the message', () => {
core.debug('\r\ndebug\n')
assertWriteCalls([`::debug::%0D%0Adebug%0A${os.EOL}`])
})
it('saveState produces the correct command', () => {
core.saveState('state_1', 'some value')
assertWriteCalls([`::save-state name=state_1::some value${os.EOL}`])
})
it('saveState handles numbers', () => {
core.saveState('state_1', 1)
assertWriteCalls([`::save-state name=state_1::1${os.EOL}`])
})
it('saveState handles bools', () => {
core.saveState('state_1', true)
assertWriteCalls([`::save-state name=state_1::true${os.EOL}`])
})
it('getState gets wrapper action state', () => {
expect(core.getState('TEST_1')).toBe('state_val')
})
it('isDebug check debug state', () => {
const current = process.env['RUNNER_DEBUG']
try {
delete process.env.RUNNER_DEBUG
expect(core.isDebug()).toBe(false)
process.env['RUNNER_DEBUG'] = '1'
expect(core.isDebug()).toBe(true)
} finally {
process.env['RUNNER_DEBUG'] = current
}
})
})
// Assert that process.stdout.write calls called only with the given arguments.
function assertWriteCalls(calls: string[]): void {
expect(process.stdout.write).toHaveBeenCalledTimes(calls.length)
for (let i = 0; i < calls.length; i++) {
expect(process.stdout.write).toHaveBeenNthCalledWith(i + 1, calls[i])
}
}