-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.spec.ts
255 lines (221 loc) · 7.94 KB
/
index.spec.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
252
253
254
255
import { mocked } from '../../../test/util';
import { getOptions } from '../../config/options';
import * as _execUtils from '../exec/utils';
import * as template from '.';
jest.mock('../exec/utils');
const execUtils = mocked(_execUtils);
describe('util/template/index', () => {
beforeEach(() => {
execUtils.getChildEnv.mockReturnValue({
CUSTOM_FOO: 'foo',
HOME: '/root',
});
});
it('returns empty string if cannot compile', () => {
expect(template.safeCompile('{{abc', {})).toBe('');
});
it('has valid exposed config options', () => {
const allOptions = getOptions().map((option) => option.name);
const missingOptions = template.exposedConfigOptions.filter(
(option) => !allOptions.includes(option),
);
expect(missingOptions).toEqual([]);
});
it('filters out disallowed fields', () => {
const userTemplate =
'{{#if isFoo}}foo{{/if}}{{platform}} token = "{{token}}"';
const input = {
isFoo: true,
platform: 'github',
token: '123test ',
releases: [{ token: '123test' }],
logJSON: { token: '123test' },
};
const output = template.compile(userTemplate, input);
expect(output).toBe('github token = ""');
});
it('containsString', () => {
const userTemplate =
"{{#if (containsString platform 'git')}}True{{else}}False{{/if}}";
const input = { platform: 'github' };
const output = template.compile(userTemplate, input, false);
expect(output).toContain('True');
});
it('not containsString', () => {
const userTemplate =
"{{#if (containsString platform 'hub')}}True{{else}}False{{/if}}";
const input = { platform: 'gitlab' };
const output = template.compile(userTemplate, input);
expect(output).toContain('False');
});
it('and returns true when all parameters are true', () => {
const userTemplate =
'{{#if (and isMajor isSingleVersion isReplacement)}}True{{else}}False{{/if}}';
const input = { isMajor: true, isSingleVersion: true, isReplacement: true };
const output = template.compile(userTemplate, input);
expect(output).toContain('True');
});
it('and returns false when at least one parameter is false', () => {
const userTemplate =
'{{#if (and isMajor isPatch isGithub)}}True{{else}}False{{/if}}';
const input = { isMajor: true, isPatch: false, isReplacement: true };
const output = template.compile(userTemplate, input);
expect(output).toContain('False');
});
it('or returns true when at least one is true', () => {
const userTemplate =
'{{#if (or isMajor isPatch isReplacement)}}True{{else}}False{{/if}}';
const input = { isMajor: false, isPatch: true, isReplacement: false };
const output = template.compile(userTemplate, input);
expect(output).toContain('True');
});
it('or returns false when all are false', () => {
const userTemplate =
'{{#if (or isMajor isPatch isReplacement)}}True{{else}}False{{/if}}';
const input = { isMajor: false, isPatch: false, isReplacement: false };
const output = template.compile(userTemplate, input);
expect(output).toContain('False');
});
it('string to pretty JSON ', () => {
const userTemplate =
'{{{ stringToPrettyJSON \'{"some":{"fancy":"json"}}\'}}}';
const output = template.compile(userTemplate, undefined as never);
expect(output).toMatchSnapshot();
});
it('lowercase', () => {
const userTemplate = "{{{ lowercase 'FOO'}}}";
const output = template.compile(userTemplate, undefined as never);
expect(output).toBe('foo');
});
it('has access to basic environment variables (basicEnvVars)', () => {
const userTemplate = 'HOME is {{env.HOME}}';
const output = template.compile(userTemplate, {});
expect(output).toBe('HOME is /root');
});
it('and has access to custom variables (customEnvVariables)', () => {
const userTemplate = 'CUSTOM_FOO is {{env.CUSTOM_FOO}}';
const output = template.compile(userTemplate, {});
expect(output).toBe('CUSTOM_FOO is foo');
});
it('replace', () => {
const userTemplate =
"{{ replace '[a-z]+\\.github\\.com' 'ghc' depName }}{{ replace 'some' 'other' depType }}";
const output = template.compile(userTemplate, {
depName: 'some.github.com/dep',
});
expect(output).toBe('ghc/dep');
});
describe('proxyCompileInput', () => {
const allowedField = 'body';
const allowedArrayField = 'prBodyNotes';
const forbiddenField = 'foobar';
type TestCompileInput = Record<
typeof allowedField | typeof allowedArrayField | typeof forbiddenField,
unknown
>;
const compileInput: TestCompileInput = {
[allowedField]: 'allowed',
[allowedArrayField]: ['allowed'],
[forbiddenField]: 'forbidden',
};
it('accessing allowed fields', () => {
const p = template.proxyCompileInput(compileInput);
expect(p[allowedField]).toBe('allowed');
expect(p[allowedArrayField]).toStrictEqual(['allowed']);
expect(p[forbiddenField]).toBeUndefined();
});
it('supports object nesting', () => {
const proxy = template.proxyCompileInput({
[allowedField]: compileInput,
});
const obj = proxy[allowedField] as TestCompileInput;
expect(obj[allowedField]).toBe('allowed');
expect(obj[forbiddenField]).toBeUndefined();
});
it('supports array nesting', () => {
const proxy = template.proxyCompileInput({
[allowedField]: [compileInput],
});
const arr = proxy[allowedField] as TestCompileInput[];
const obj = arr[0];
expect(obj[allowedField]).toBe('allowed');
expect(obj[allowedArrayField]).toStrictEqual(['allowed']);
expect(obj[forbiddenField]).toBeUndefined();
});
});
describe('containsTemplate', () => {
it('supports null', () => {
expect(template.containsTemplates(null, 'logJSON')).toBeFalse();
});
it('contains template', () => {
expect(
template.containsTemplates(
'{{#if logJSON}}{{logJSON}}{{/if}}',
'logJSON',
),
).toBeTrue();
expect(
template.containsTemplates(
'{{#with logJSON.hasReleaseNotes as | hasNotes |}}{{hasNotes}}{{/if}}',
'logJSON',
),
).toBeTrue();
expect(
template.containsTemplates(
'{{#if logJSON.hasReleaseNotes}}has notes{{/if}}',
'logJSON',
),
).toBeTrue();
});
it('does not contain template', () => {
expect(template.containsTemplates('{{body}}', ['logJSON'])).toBeFalse();
});
});
describe('percent encoding', () => {
it('encodes values', () => {
const output = template.compile(
'{{{encodeURIComponent "@fsouza/prettierd"}}}',
undefined as never,
);
expect(output).toBe('%40fsouza%2Fprettierd');
});
it('decodes values', () => {
const output = template.compile(
'{{{decodeURIComponent "%40fsouza/prettierd"}}}',
undefined as never,
);
expect(output).toBe('@fsouza/prettierd');
});
});
describe('equals', () => {
it('equals', () => {
const output = template.compile(
'{{#if (equals datasource "git-refs")}}https://github.com/{{packageName}}{{else}}{{packageName}}{{/if}}',
{
datasource: 'git-refs',
packageName: 'renovatebot/renovate',
},
);
expect(output).toBe('https://github.com/renovatebot/renovate');
});
it('not equals', () => {
const output = template.compile(
'{{#if (equals datasource "git-refs")}}https://github.com/{{packageName}}{{else}}{{packageName}}{{/if}}',
{
datasource: 'github-releases',
packageName: 'renovatebot/renovate',
},
);
expect(output).toBe('renovatebot/renovate');
});
it('not strict equals', () => {
const output = template.compile(
'{{#if (equals newMajor "3")}}equals{{else}}not equals{{/if}}',
{
newMajor: 3,
},
);
expect(output).toBe('not equals');
});
});
});