-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathindex.spec.ts
More file actions
286 lines (255 loc) · 9.88 KB
/
index.spec.ts
File metadata and controls
286 lines (255 loc) · 9.88 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import upath from 'upath';
import * as _decrypt from '../../../../config/decrypt';
import { CONFIG_PRESETS_INVALID } from '../../../../constants/error-messages';
import { getCustomEnv } from '../../../../util/env';
import { getParentDir, readSystemFile } from '../../../../util/fs';
import getArgv from './__fixtures__/argv';
import * as _hostRulesFromEnv from './host-rules-from-env';
import * as httpMock from '~test/http-mock';
vi.mock('../../../../modules/datasource/npm');
vi.mock('../../../../util/fs');
vi.mock('../../../../config/decrypt');
vi.mock('./host-rules-from-env');
const decrypt = vi.mocked(_decrypt);
const { hostRulesFromEnv } = vi.mocked(_hostRulesFromEnv);
describe('workers/global/config/parse/index', () => {
describe('.parseConfigs(env, defaultArgv)', () => {
let configParser: typeof import('.');
let defaultArgv: string[];
let defaultEnv: NodeJS.ProcessEnv;
beforeEach(async () => {
configParser = await vi.importActual('./index');
defaultArgv = getArgv();
defaultEnv = {
RENOVATE_CONFIG_FILE: upath.resolve(
__dirname,
'./__fixtures__/default.js',
),
};
});
it('supports token in env', async () => {
const env: NodeJS.ProcessEnv = { ...defaultEnv, RENOVATE_TOKEN: 'abc' };
const parsedConfig = await configParser.parseConfigs(env, defaultArgv);
expect(parsedConfig).toContainEntries([['token', 'abc']]);
});
it('supports token in CLI options', async () => {
defaultArgv = defaultArgv.concat([
'--token=abc',
'--pr-footer=custom',
'--log-context=123test',
]);
const parsedConfig = await configParser.parseConfigs(
defaultEnv,
defaultArgv,
);
expect(parsedConfig).toContainEntries([
['token', 'abc'],
['prFooter', 'custom'],
['logContext', '123test'],
]);
});
it('supports forceCli', async () => {
defaultArgv = defaultArgv.concat(['--force-cli=false']);
const env: NodeJS.ProcessEnv = {
...defaultEnv,
RENOVATE_TOKEN: 'abc',
};
const parsedConfig = await configParser.parseConfigs(env, defaultArgv);
expect(parsedConfig).toContainEntries([
['token', 'abc'],
['force', null],
]);
expect(parsedConfig).not.toContainKey('configFile');
});
it('sets customEnvVariables', async () => {
const env: NodeJS.ProcessEnv = {
...defaultEnv,
RENOVATE_TOKEN: 'abc',
RENOVATE_CUSTOM_ENV_VARIABLES: '{"customKey": "customValue"}',
};
await configParser.parseConfigs(env, defaultArgv);
const customEnvVars = getCustomEnv();
expect(customEnvVars).toEqual({
customKey: 'customValue',
});
});
it('supports config.force', async () => {
const configPath = upath.join(__dirname, '__fixtures__/with-force.js');
const env: NodeJS.ProcessEnv = {
...defaultEnv,
RENOVATE_CONFIG_FILE: configPath,
};
const parsedConfig = await configParser.parseConfigs(env, defaultArgv);
expect(parsedConfig).toContainEntries([
['token', 'abcdefg'],
[
'force',
{
schedule: null,
},
],
]);
});
it('reads private key from file', async () => {
const privateKeyPath = upath.join(__dirname, '__fixtures__/private.pem');
const privateKeyPathOld = upath.join(
__dirname,
'__fixtures__/private.pem',
);
const env: NodeJS.ProcessEnv = {
...defaultEnv,
RENOVATE_PRIVATE_KEY_PATH: privateKeyPath,
RENOVATE_PRIVATE_KEY_PATH_OLD: privateKeyPathOld,
};
const expected = await readSystemFile(privateKeyPath, 'utf8');
const parsedConfig = await configParser.parseConfigs(env, defaultArgv);
expect(parsedConfig.privateKey).toBeUndefined();
expect(decrypt.setPrivateKeys).toHaveBeenCalledWith(expected, undefined);
});
it('supports Bitbucket username/password', async () => {
defaultArgv = defaultArgv.concat([
'--platform=bitbucket',
'--username=user',
'--password=pass',
]);
const parsedConfig = await configParser.parseConfigs(
defaultEnv,
defaultArgv,
);
expect(parsedConfig).toContainEntries([
['platform', 'bitbucket'],
['username', 'user'],
['password', 'pass'],
]);
});
it('massages trailing slash into endpoint', async () => {
defaultArgv = defaultArgv.concat([
'--endpoint=https://github.renovatebot.com/api/v3',
]);
const parsed = await configParser.parseConfigs(defaultEnv, defaultArgv);
expect(parsed.endpoint).toBe('https://github.renovatebot.com/api/v3/');
});
it('parses global manager config', async () => {
defaultArgv = defaultArgv.concat(['--detect-global-manager-config=true']);
const parsed = await configParser.parseConfigs(defaultEnv, defaultArgv);
expect(parsed.npmrc).toBeNull();
});
it('parses host rules from env', async () => {
defaultArgv = defaultArgv.concat(['--detect-host-rules-from-env=true']);
hostRulesFromEnv.mockReturnValueOnce([{ matchHost: 'example.org' }]);
const parsed = await configParser.parseConfigs(defaultEnv, defaultArgv);
expect(parsed.hostRules).toContainEqual({ matchHost: 'example.org' });
});
it('env dryRun = true replaced to full', async () => {
const env: NodeJS.ProcessEnv = {
...defaultEnv,
RENOVATE_DRY_RUN: 'true',
};
const parsedConfig = await configParser.parseConfigs(env, defaultArgv);
expect(parsedConfig).toContainEntries([['dryRun', 'full']]);
});
it('cli dryRun = true replaced to full', async () => {
defaultArgv = defaultArgv.concat(['--dry-run=true']);
const parsed = await configParser.parseConfigs(defaultEnv, defaultArgv);
expect(parsed).toContainEntries([['dryRun', 'full']]);
});
it('resolves global presets', async () => {
// The remote preset defined in globalExtends of the config file
httpMock
.scope('http://example.com/')
.get('/config.json')
.reply(200, { repositories: ['g/r1', 'g/r2'], druRun: 'full' });
// Mock the default config file to return a preset
const defaultConfig = upath.join(__dirname, '__fixtures__/default.js');
vi.doMock(defaultConfig, () => ({
default: {
globalExtends: ['http://example.com/config.json', ':pinVersions'],
dryRun: 'extract',
},
}));
const parsedConfig = await configParser.parseConfigs(
defaultEnv,
defaultArgv,
);
vi.doUnmock(defaultConfig);
// Remote preset in globalExtends should be resolved
expect(parsedConfig).toContainEntries([
['repositories', ['g/r1', 'g/r2']],
]);
// :pinVersion in globalExtends should be resolved
expect(parsedConfig).toContainEntries([['rangeStrategy', 'pin']]);
// `globalExtends` should be an empty array after merging
expect(parsedConfig).toContainEntries([['globalExtends', []]]);
// `dryRun` from globalExtends should be overwritten with value defined in config file
expect(parsedConfig).toContainEntries([['dryRun', 'extract']]);
});
it('throws exception if global presets cannot be resolved', async () => {
httpMock
.scope('http://example.com/')
.get('/config.json')
.reply(404, 'Not Found');
await expect(
configParser.resolveGlobalExtends([
'http://example.com/config.json',
':pinVersions',
]),
).rejects.toThrow(CONFIG_PRESETS_INVALID);
});
it('cli dryRun replaced to full', async () => {
defaultArgv = defaultArgv.concat(['--dry-run']);
const parsed = await configParser.parseConfigs(defaultEnv, defaultArgv);
expect(parsed).toContainEntries([['dryRun', 'full']]);
});
it('env dryRun = false replaced to null', async () => {
const env: NodeJS.ProcessEnv = {
...defaultEnv,
RENOVATE_DRY_RUN: 'false',
};
const parsedConfig = await configParser.parseConfigs(env, defaultArgv);
expect(parsedConfig).toContainEntries([['dryRun', null]]);
});
it('cli dryRun = false replaced to null', async () => {
defaultArgv = defaultArgv.concat(['--dry-run=false']);
const parsed = await configParser.parseConfigs(defaultEnv, defaultArgv);
expect(parsed).toContainEntries([['dryRun', null]]);
});
it('only initializes the file when the env var LOG_FILE is properly set', async () => {
vi.doMock('../../../../../config.js', () => ({ default: {} }));
const env: NodeJS.ProcessEnv = {};
const parsedConfig = await configParser.parseConfigs(env, defaultArgv);
expect(parsedConfig).not.toContain([['logFile', 'someFile']]);
expect(getParentDir).not.toHaveBeenCalled();
});
it('massage onboardingNoDeps when autodiscover is false', async () => {
vi.doMock('../../../../../config.js', () => ({
default: {
onboardingNoDeps: 'auto',
autodiscover: false,
},
}));
const env: NodeJS.ProcessEnv = {};
const parsedConfig = await configParser.parseConfigs(env, defaultArgv);
expect(parsedConfig).toContainEntries([['onboardingNoDeps', 'enabled']]);
});
it('apply secrets to global config', async () => {
vi.doMock('../../../../../config.js', () => ({
default: {},
}));
const env: NodeJS.ProcessEnv = {
...defaultEnv,
RENOVATE_SECRETS: '{"SECRET_TOKEN": "secret_token"}',
RENOVATE_CUSTOM_ENV_VARIABLES:
'{"TOKEN": "{{ secrets.SECRET_TOKEN }}"}',
};
const parsedConfig = await configParser.parseConfigs(env, defaultArgv);
expect(parsedConfig).toMatchObject({
secrets: {
SECRET_TOKEN: 'secret_token',
},
customEnvVariables: {
TOKEN: 'secret_token',
},
});
});
});
});