-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
merge.spec.ts
247 lines (226 loc) · 7.71 KB
/
merge.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
import {
RenovateConfig,
fs,
getConfig,
git,
mocked,
platform,
} from '../../../../test/util';
import * as _migrateAndValidate from '../../../config/migrate-validate';
import * as _migrate from '../../../config/migration';
import { initRepoCache } from '../../../util/cache/repository/init';
import {
checkForRepoConfigError,
detectRepoFileConfig,
mergeRenovateConfig,
} from './merge';
jest.mock('../../../util/fs');
jest.mock('../../../util/git');
const migrate = mocked(_migrate);
const migrateAndValidate = mocked(_migrateAndValidate);
let config: RenovateConfig;
beforeEach(() => {
jest.resetAllMocks();
config = getConfig();
config.errors = [];
config.warnings = [];
});
jest.mock('../../../config/migration');
jest.mock('../../../config/migrate-validate');
describe('workers/repository/init/merge', () => {
describe('detectRepoFileConfig()', () => {
beforeEach(async () => {
await initRepoCache({});
});
it('returns config if not found', async () => {
git.getFileList.mockResolvedValue(['package.json']);
fs.readLocalFile.mockResolvedValue('{}');
expect(await detectRepoFileConfig()).toEqual({});
});
it('uses package.json config if found', async () => {
git.getFileList.mockResolvedValue(['package.json']);
const pJson = JSON.stringify({
name: 'something',
renovate: {
prHourlyLimit: 10,
},
});
fs.readLocalFile.mockResolvedValue(pJson);
platform.getRawFile.mockResolvedValueOnce(pJson);
expect(await detectRepoFileConfig()).toEqual({
configFileName: 'package.json',
configFileParsed: { prHourlyLimit: 10 },
});
// get from repoCache
expect(await detectRepoFileConfig()).toEqual({
configFileName: 'package.json',
configFileParsed: { prHourlyLimit: 10 },
});
});
it('massages package.json renovate string', async () => {
git.getFileList.mockResolvedValue(['package.json']);
const pJson = JSON.stringify({
name: 'something',
renovate: 'github>renovatebot/renovate',
});
fs.readLocalFile.mockResolvedValue(pJson);
platform.getRawFile.mockResolvedValueOnce(pJson);
expect(await detectRepoFileConfig()).toEqual({
configFileName: 'package.json',
configFileParsed: { extends: ['github>renovatebot/renovate'] },
});
});
it('returns error if cannot parse', async () => {
git.getFileList.mockResolvedValue(['package.json', 'renovate.json']);
fs.readLocalFile.mockResolvedValue('cannot parse');
expect(await detectRepoFileConfig()).toEqual({
configFileName: 'renovate.json',
configFileParseError: {
validationError: 'Invalid JSON (parsing failed)',
validationMessage: 'Syntax error near cannot par',
},
});
});
it('throws error if duplicate keys', async () => {
git.getFileList.mockResolvedValue(['package.json', '.renovaterc']);
fs.readLocalFile.mockResolvedValue(
'{ "enabled": true, "enabled": false }'
);
expect(await detectRepoFileConfig()).toEqual({
configFileName: '.renovaterc',
configFileParseError: {
validationError: 'Duplicate keys in JSON',
validationMessage:
'"Syntax error: duplicated keys \\"enabled\\" near \\": false }"',
},
});
});
it('finds and parse renovate.json5', async () => {
const configFileRaw = `{
// this is json5 format
}`;
git.getFileList.mockResolvedValue(['package.json', 'renovate.json5']);
fs.readLocalFile.mockResolvedValue(configFileRaw);
expect(await detectRepoFileConfig()).toEqual({
configFileName: 'renovate.json5',
configFileParsed: {},
configFileRaw,
});
});
it('finds .github/renovate.json', async () => {
git.getFileList.mockResolvedValue([
'package.json',
'.github/renovate.json',
]);
fs.readLocalFile.mockResolvedValue('{}');
expect(await detectRepoFileConfig()).toEqual({
configFileName: '.github/renovate.json',
configFileParsed: {},
configFileRaw: '{}',
});
});
it('finds .gitlab/renovate.json', async () => {
git.getFileList.mockResolvedValue([
'package.json',
'.gitlab/renovate.json',
]);
fs.readLocalFile.mockResolvedValue('{}');
expect(await detectRepoFileConfig()).toEqual({
configFileName: '.gitlab/renovate.json',
configFileParsed: {},
configFileRaw: '{}',
});
});
it('finds .renovaterc.json', async () => {
git.getFileList.mockResolvedValue(['package.json', '.renovaterc.json']);
fs.readLocalFile.mockResolvedValue('{}');
platform.getRawFile.mockResolvedValueOnce('{"something":"new"}');
expect(await detectRepoFileConfig()).toEqual({
configFileName: '.renovaterc.json',
configFileParsed: {},
configFileRaw: '{}',
});
expect(await detectRepoFileConfig()).toEqual({
configFileName: '.renovaterc.json',
configFileParsed: {
something: 'new',
},
configFileRaw: '{"something":"new"}',
});
});
});
describe('checkForRepoConfigError', () => {
it('returns if no error', () => {
expect(checkForRepoConfigError({})).toBeUndefined();
});
it('throws on error', () => {
expect(() =>
checkForRepoConfigError({
configFileParseError: { validationError: '', validationMessage: '' },
})
).toThrow();
});
});
describe('mergeRenovateConfig()', () => {
beforeEach(() => {
migrate.migrateConfig.mockReturnValue({
isMigrated: false,
migratedConfig: {},
});
});
it('throws error if misconfigured', async () => {
git.getFileList.mockResolvedValue(['package.json', '.renovaterc.json']);
fs.readLocalFile.mockResolvedValue('{}');
migrateAndValidate.migrateAndValidate.mockResolvedValueOnce({
errors: [{ topic: 'dep', message: 'test error' }],
});
let e: Error | undefined;
try {
await mergeRenovateConfig(config);
} catch (err) {
e = err;
}
expect(e).toBeDefined();
expect(e?.toString()).toBe('Error: config-validation');
});
it('migrates nested config', async () => {
git.getFileList.mockResolvedValue(['renovate.json']);
fs.readLocalFile.mockResolvedValue('{}');
migrateAndValidate.migrateAndValidate.mockImplementation((_, c) => {
// We shouldn't see packageRules here (avoids #14827).
// (someday the validation should probably be reworked to know about `sourceUrl` from the repo config, but that day isn't today)
expect(c).not.toHaveProperty('packageRules');
return Promise.resolve({
...c,
warnings: [],
errors: [],
});
});
migrate.migrateConfig.mockImplementation((c) => ({
isMigrated: true,
migratedConfig: c,
}));
config.extends = [':automergeAll'];
config.packageRules = [{ extends: ['monorepo:react'] }];
const ret = await mergeRenovateConfig(config);
expect(ret).toMatchObject({
automerge: true,
packageRules: [
{
matchSourceUrlPrefixes: ['https://github.com/facebook/react'],
},
],
});
});
it('continues if no errors', async () => {
git.getFileList.mockResolvedValue(['package.json', '.renovaterc.json']);
fs.readLocalFile.mockResolvedValue('{}');
migrateAndValidate.migrateAndValidate.mockResolvedValue({
warnings: [],
errors: [],
});
config.extends = [':automergeDisabled'];
expect(await mergeRenovateConfig(config)).toBeDefined();
});
});
});