forked from renovatebot/renovate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.ts
441 lines (365 loc) · 14.6 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import { exec as _exec } from 'child_process';
import * as _os from 'os';
import fsExtra from 'fs-extra';
import tmp, { DirectoryResult } from 'tmp-promise';
import * as upath from 'upath';
import { envMock, mockExecAll } from '../../../test/execUtil';
import { getName, replacingSerializer } from '../../../test/util';
import * as _util from '../../util';
import { BinarySource } from '../../util/exec/common';
import * as _docker from '../../util/exec/docker';
import * as _env from '../../util/exec/env';
import * as _fs from '../../util/fs';
import { ExtractConfig } from '../common';
import { ifSystemSupportsGradle } from './__testutil__/gradle';
import { GRADLE_DEPENDENCY_REPORT_FILENAME } from './gradle-updates-report';
import * as _manager from '.';
const fixtures = 'lib/manager/gradle/__fixtures__';
const standardUpdatesReport = () =>
fsExtra.readFile(`${fixtures}/updatesReport.json`, 'utf8');
const emptyUpdatesReport = () =>
fsExtra.readFile(`${fixtures}/updatesReportEmpty.json`, 'utf8');
const multiProjectUpdatesReport = () =>
fsExtra.readFile(`${fixtures}/MultiProjectUpdatesReport.json`, 'utf8');
const baseConfig = {
gradle: {
timeout: 60,
},
};
const gradleOutput = {
stdout: 'gradle output',
stderr: '',
};
function resetMocks() {
jest.resetAllMocks();
jest.resetModules();
}
async function setupMocks() {
resetMocks();
jest.mock('child_process');
jest.mock('../../util/exec/env');
jest.mock('../../util/fs');
jest.mock('os');
const os: jest.Mocked<typeof _os> = require('os');
const fs: jest.Mocked<typeof _fs> = require('../../util/fs');
const env: jest.Mocked<typeof _env> = require('../../util/exec/env');
const exec: jest.Mock<typeof _exec> = require('child_process').exec;
const util: jest.Mocked<typeof _util> = require('../../util');
fs.readLocalFile.mockResolvedValue(`
dependency 'foo:foo:1.2.3'
dependency "bar:bar:3.4.5"
`);
env.getChildProcessEnv.mockReturnValue(envMock.basic);
await util.setUtilConfig(baseConfig);
return [require('.'), exec, util, os];
}
describe(getName(__filename), () => {
describe('extractPackageFile', () => {
let manager: typeof _manager;
let exec: jest.Mock<typeof _exec>;
let util: jest.Mocked<typeof _util>;
let os: jest.Mocked<typeof _os>;
let docker: typeof _docker;
let config: ExtractConfig;
beforeAll(async () => {
[manager, exec, util, os] = await setupMocks();
docker = require('../../util/exec/docker');
});
afterAll(resetMocks);
beforeEach(async () => {
exec.mockReset();
docker.resetPrefetchedImages();
os.platform.mockReturnValue('linux');
const gradleDir = await tmp.dir({ unsafeCleanup: true });
config = { ...baseConfig, localDir: gradleDir.path };
expect.addSnapshotSerializer(
replacingSerializer(upath.toUnix(gradleDir.path), 'localDir')
);
});
async function initializeWorkingDir(
addGradleWrapper: boolean,
updatesReport: Promise<string> | string | null,
dir: string = config.localDir
) {
if (addGradleWrapper) {
await fsExtra.copy(`${fixtures}/gradle-wrappers/6`, dir);
}
if (updatesReport) {
await fsExtra.writeFile(
`${dir}/${GRADLE_DEPENDENCY_REPORT_FILENAME}`,
await updatesReport
);
}
}
it('should return gradle dependencies', async () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
await initializeWorkingDir(true, standardUpdatesReport());
const dependencies = await manager.extractAllPackageFiles(config, [
'build.gradle',
'subproject/build.gradle',
]);
expect(dependencies).toMatchSnapshot();
expect(execSnapshots).toMatchSnapshot();
});
it('should return gradle.kts dependencies', async () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
await initializeWorkingDir(true, standardUpdatesReport());
const dependencies = await manager.extractAllPackageFiles(config, [
'build.gradle.kts',
'subproject/build.gradle.kts',
]);
expect(dependencies).toMatchSnapshot();
expect(execSnapshots).toMatchSnapshot();
});
it('should return empty if there are no dependencies', async () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
await initializeWorkingDir(true, emptyUpdatesReport());
const dependencies = await manager.extractAllPackageFiles(config, [
'build.gradle',
]);
expect(dependencies).toEqual([]);
expect(execSnapshots).toMatchSnapshot();
});
it('should return empty if there is no dependency report', async () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
await initializeWorkingDir(true, null);
const dependencies = await manager.extractAllPackageFiles(config, [
'build.gradle',
]);
expect(dependencies).toEqual([]);
expect(execSnapshots).toMatchSnapshot();
});
it('should return empty if renovate report is invalid', async () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
await initializeWorkingDir(
true,
`
Invalid JSON]
`
);
const dependencies = await manager.extractAllPackageFiles(config, [
'build.gradle',
]);
expect(dependencies).toEqual([]);
expect(execSnapshots).toMatchSnapshot();
});
it('should use repositories only for current project', async () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
await initializeWorkingDir(true, multiProjectUpdatesReport());
const dependencies = await manager.extractAllPackageFiles(config, [
'build.gradle',
]);
expect(dependencies).toMatchSnapshot();
expect(execSnapshots).toMatchSnapshot();
});
it('should execute gradlew when available', async () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
await initializeWorkingDir(true, standardUpdatesReport());
await manager.extractAllPackageFiles(config, ['build.gradle']);
expect(execSnapshots).toMatchSnapshot();
});
it('should execute gradlew.bat when available on Windows', async () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
await initializeWorkingDir(true, standardUpdatesReport());
os.platform.mockReturnValue('win32');
await manager.extractAllPackageFiles(config, ['build.gradle']);
expect(execSnapshots).toMatchSnapshot();
});
it('should execute gradle if gradlew is not available', async () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
await initializeWorkingDir(false, standardUpdatesReport());
await manager.extractAllPackageFiles(config, ['build.gradle']);
expect(execSnapshots).toMatchSnapshot();
});
it('should return null and gradle should not be executed if no root build.gradle', async () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
await initializeWorkingDir(false, null);
const packageFiles = ['foo/build.gradle'];
expect(
await manager.extractAllPackageFiles(config, packageFiles)
).toBeNull();
expect(exec).toHaveBeenCalledTimes(0);
expect(execSnapshots).toMatchSnapshot();
});
it('should return gradle dependencies for build.gradle in subdirectories if there is gradlew in the same directory', async () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
await initializeWorkingDir(true, standardUpdatesReport());
await fsExtra.mkdirs(`${config.localDir}/foo`);
await initializeWorkingDir(
true,
standardUpdatesReport(),
`${config.localDir}/foo`
);
const dependencies = await manager.extractAllPackageFiles(config, [
'foo/build.gradle',
]);
expect(dependencies).toMatchSnapshot();
expect(execSnapshots).toMatchSnapshot();
});
it('should configure the renovate report plugin', async () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
await initializeWorkingDir(true, standardUpdatesReport());
await manager.extractAllPackageFiles(config, ['build.gradle']);
await expect(
fsExtra.access(
`${config.localDir}/renovate-plugin.gradle`,
fsExtra.constants.F_OK
)
).resolves.toBeUndefined();
expect(execSnapshots).toMatchSnapshot();
});
it('should use docker if required', async () => {
const configWithDocker = { binarySource: BinarySource.Docker, ...config };
jest.spyOn(docker, 'removeDanglingContainers').mockResolvedValueOnce();
await util.setUtilConfig(configWithDocker);
await initializeWorkingDir(false, standardUpdatesReport());
const execSnapshots = mockExecAll(exec, gradleOutput);
await manager.extractAllPackageFiles(configWithDocker, ['build.gradle']);
expect(execSnapshots).toMatchSnapshot();
});
it('should use docker even if gradlew is available', async () => {
const configWithDocker = { binarySource: BinarySource.Docker, ...config };
jest.spyOn(docker, 'removeDanglingContainers').mockResolvedValueOnce();
await util.setUtilConfig(configWithDocker);
await initializeWorkingDir(true, standardUpdatesReport());
const execSnapshots = mockExecAll(exec, gradleOutput);
await manager.extractAllPackageFiles(configWithDocker, ['build.gradle']);
expect(execSnapshots).toMatchSnapshot();
});
it('should use docker even if gradlew.bat is available on Windows', async () => {
const configWithDocker = { binarySource: BinarySource.Docker, ...config };
jest.spyOn(docker, 'removeDanglingContainers').mockResolvedValueOnce();
await util.setUtilConfig(configWithDocker);
os.platform.mockReturnValue('win32');
await initializeWorkingDir(true, standardUpdatesReport());
const execSnapshots = mockExecAll(exec, gradleOutput);
await manager.extractAllPackageFiles(configWithDocker, ['build.gradle']);
expect(execSnapshots).toMatchSnapshot();
});
});
describe('updateDependency', () => {
let manager: typeof _manager;
let exec: jest.Mock<typeof _exec>;
beforeAll(async () => {
[manager, exec] = await setupMocks();
});
afterAll(resetMocks);
it('should update an existing module dependency', async () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
const buildGradleContent = await fsExtra.readFile(
`${fixtures}/build.gradle.example1`,
'utf8'
);
// prettier-ignore
const upgrade = {
depGroup: 'cglib', name: 'cglib-nodep', version: '3.1', newValue: '3.2.8',
};
const buildGradleContentUpdated = manager.updateDependency({
fileContent: buildGradleContent,
upgrade,
});
expect(buildGradleContent).not.toMatch('cglib:cglib-nodep:3.2.8');
expect(buildGradleContentUpdated).toMatch('cglib:cglib-nodep:3.2.8');
expect(buildGradleContentUpdated).not.toMatch('cglib:cglib-nodep:3.1');
expect(execSnapshots).toMatchSnapshot();
});
it('should update an existing plugin dependency', () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
const buildGradleContent = `
plugins {
id "com.github.ben-manes.versions" version "0.20.0"
}
`;
const upgrade = {
depGroup: 'com.github.ben-manes.versions',
name: 'com.github.ben-manes.versions.gradle.plugin',
version: '0.20.0',
newValue: '0.21.0',
};
const buildGradleContentUpdated = manager.updateDependency({
fileContent: buildGradleContent,
upgrade,
});
expect(buildGradleContent).not.toMatch(
'id "com.github.ben-manes.versions" version "0.21.0"'
);
expect(buildGradleContentUpdated).toMatch(
'id "com.github.ben-manes.versions" version "0.21.0"'
);
expect(buildGradleContentUpdated).not.toMatch(
'id "com.github.ben-manes.versions" version "0.20.0"'
);
expect(execSnapshots).toMatchSnapshot();
});
it('should update an existing plugin dependency with Kotlin DSL', () => {
const execSnapshots = mockExecAll(exec, gradleOutput);
const buildGradleContent = `
plugins {
id("com.github.ben-manes.versions") version "0.20.0"
}
`;
const upgrade = {
depGroup: 'com.github.ben-manes.versions',
name: 'com.github.ben-manes.versions.gradle.plugin',
version: '0.20.0',
newValue: '0.21.0',
};
const buildGradleContentUpdated = manager.updateDependency({
fileContent: buildGradleContent,
upgrade,
});
expect(buildGradleContent).not.toMatch(
'id("com.github.ben-manes.versions") version "0.21.0"'
);
expect(buildGradleContentUpdated).toMatch(
'id("com.github.ben-manes.versions") version "0.21.0"'
);
expect(buildGradleContentUpdated).not.toMatch(
'id("com.github.ben-manes.versions") version "0.20.0"'
);
expect(execSnapshots).toMatchSnapshot();
});
});
ifSystemSupportsGradle(6).describe('executeGradle integration', () => {
const SUCCESS_FILE_NAME = 'success.indicator';
let workingDir: DirectoryResult;
let testRunConfig: ExtractConfig;
let successFile: string;
const manager = require('.');
beforeEach(async () => {
workingDir = await tmp.dir({ unsafeCleanup: true });
successFile = '';
testRunConfig = { ...baseConfig, localDir: workingDir.path };
await fsExtra.copy(`${fixtures}/minimal-project`, workingDir.path);
await fsExtra.copy(`${fixtures}/gradle-wrappers/6`, workingDir.path);
const mockPluginContent = `
allprojects {
tasks.register("renovate") {
doLast {
new File('${SUCCESS_FILE_NAME}').write 'success'
}
}
}`;
await fsExtra.writeFile(
`${workingDir.path}/renovate-plugin.gradle`,
mockPluginContent
);
successFile = `${workingDir.path}/${SUCCESS_FILE_NAME}`;
});
it('executes an executable gradle wrapper', async () => {
const gradlew = await fsExtra.stat(`${workingDir.path}/gradlew`);
await manager.executeGradle(testRunConfig, workingDir.path, gradlew);
await expect(fsExtra.readFile(successFile, 'utf8')).resolves.toBe(
'success'
);
}, 120000);
it('executes a not-executable gradle wrapper', async () => {
await fsExtra.chmod(`${workingDir.path}/gradlew`, '444');
const gradlew = await fsExtra.stat(`${workingDir.path}/gradlew`);
await manager.executeGradle(testRunConfig, workingDir.path, gradlew);
await expect(fsExtra.readFile(successFile, 'utf8')).resolves.toBe(
'success'
);
}, 120000);
});
});