/
artifacts.spec.ts
315 lines (306 loc) · 10.2 KB
/
artifacts.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
import { exec as _exec } from 'child_process';
import { join } from 'upath';
import { envMock, mockExecAll } from '../../../test/exec-util';
import { env, fs, git, mocked, partial } from '../../../test/util';
import { setAdminConfig } from '../../config/admin';
import {
PLATFORM_TYPE_GITHUB,
PLATFORM_TYPE_GITLAB,
} from '../../constants/platforms';
import * as _datasource from '../../datasource';
import * as datasourcePackagist from '../../datasource/packagist';
import { setUtilConfig } from '../../util';
import { BinarySource } from '../../util/exec/common';
import * as docker from '../../util/exec/docker';
import { StatusResult } from '../../util/git';
import * as hostRules from '../../util/host-rules';
import * as composer from './artifacts';
jest.mock('child_process');
jest.mock('../../util/exec/env');
jest.mock('../../../lib/datasource');
jest.mock('../../util/fs');
jest.mock('../../util/git');
const exec: jest.Mock<typeof _exec> = _exec as any;
const datasource = mocked(_datasource);
const config = {
// `join` fixes Windows CI
localDir: join('/tmp/github/some/repo'),
cacheDir: join('/tmp/renovate/cache'),
composerIgnorePlatformReqs: true,
ignoreScripts: false,
};
const repoStatus = partial<StatusResult>({
modified: [],
not_added: [],
deleted: [],
});
describe('.updateArtifacts()', () => {
beforeEach(async () => {
jest.resetAllMocks();
jest.resetModules();
env.getChildProcessEnv.mockReturnValue(envMock.basic);
await setUtilConfig(config);
docker.resetPrefetchedImages();
hostRules.clear();
setAdminConfig({ allowScripts: false });
});
it('returns if no composer.lock found', async () => {
expect(
await composer.updateArtifacts({
packageFileName: 'composer.json',
updatedDeps: [],
newPackageFileContent: '{}',
config,
})
).toBeNull();
});
it('returns null if unchanged', async () => {
fs.readLocalFile.mockResolvedValueOnce('Current composer.lock' as any);
const execSnapshots = mockExecAll(exec);
fs.readLocalFile.mockReturnValueOnce('Current composer.lock' as any);
git.getRepoStatus.mockResolvedValue(repoStatus);
setAdminConfig({ allowScripts: true });
expect(
await composer.updateArtifacts({
packageFileName: 'composer.json',
updatedDeps: [],
newPackageFileContent: '{}',
config,
})
).toBeNull();
expect(execSnapshots).toMatchSnapshot();
});
it('uses hostRules to set COMPOSER_AUTH', async () => {
hostRules.add({
hostType: PLATFORM_TYPE_GITHUB,
hostName: 'api.github.com',
token: 'github-token',
});
hostRules.add({
hostType: PLATFORM_TYPE_GITLAB,
hostName: 'gitlab.com',
token: 'gitlab-token',
});
hostRules.add({
hostType: datasourcePackagist.id,
hostName: 'packagist.renovatebot.com',
username: 'some-username',
password: 'some-password',
});
hostRules.add({
hostType: datasourcePackagist.id,
endpoint: 'https://artifactory.yyyyyyy.com/artifactory/api/composer/',
username: 'some-other-username',
password: 'some-other-password',
});
hostRules.add({
hostType: datasourcePackagist.id,
username: 'some-other-username',
password: 'some-other-password',
});
fs.readLocalFile.mockResolvedValueOnce('Current composer.lock' as any);
const execSnapshots = mockExecAll(exec);
fs.readLocalFile.mockReturnValueOnce('Current composer.lock' as any);
const authConfig = {
...config,
registryUrls: ['https://packagist.renovatebot.com'],
};
git.getRepoStatus.mockResolvedValue(repoStatus);
expect(
await composer.updateArtifacts({
packageFileName: 'composer.json',
updatedDeps: [],
newPackageFileContent: '{}',
config: authConfig,
})
).toBeNull();
expect(execSnapshots).toMatchSnapshot();
});
it('returns updated composer.lock', async () => {
fs.readLocalFile.mockResolvedValueOnce('Current composer.lock' as any);
const execSnapshots = mockExecAll(exec);
fs.readLocalFile.mockReturnValueOnce('New composer.lock' as any);
git.getRepoStatus.mockResolvedValue({
...repoStatus,
modified: ['composer.lock'],
});
expect(
await composer.updateArtifacts({
packageFileName: 'composer.json',
updatedDeps: [],
newPackageFileContent: '{}',
config,
})
).not.toBeNull();
expect(execSnapshots).toMatchSnapshot();
});
it('supports vendor directory update', async () => {
const foo = join('vendor/foo/Foo.php');
const bar = join('vendor/bar/Bar.php');
const baz = join('vendor/baz/Baz.php');
fs.localPathExists.mockResolvedValueOnce(true);
fs.readLocalFile.mockResolvedValueOnce('Current composer.lock' as any);
const execSnapshots = mockExecAll(exec);
git.getRepoStatus.mockResolvedValueOnce({
...repoStatus,
modified: ['composer.lock', foo],
not_added: [bar],
deleted: [baz],
});
fs.readLocalFile.mockResolvedValueOnce('New composer.lock' as any);
fs.readLocalFile.mockResolvedValueOnce('Foo' as any);
fs.readLocalFile.mockResolvedValueOnce('Bar' as any);
fs.getSiblingFileName.mockReturnValueOnce('vendor' as any);
const res = await composer.updateArtifacts({
packageFileName: 'composer.json',
updatedDeps: [],
newPackageFileContent: '{}',
config,
});
expect(res).not.toBeNull();
expect(res?.map(({ file }) => file)).toEqual([
{ contents: 'New composer.lock', name: 'composer.lock' },
{ contents: 'Foo', name: foo },
{ contents: 'Bar', name: bar },
{ contents: baz, name: '|delete|' },
]);
expect(execSnapshots).toMatchSnapshot();
});
it('performs lockFileMaintenance', async () => {
fs.readLocalFile.mockResolvedValueOnce('Current composer.lock' as any);
const execSnapshots = mockExecAll(exec);
fs.readLocalFile.mockReturnValueOnce('New composer.lock' as any);
git.getRepoStatus.mockResolvedValue({
...repoStatus,
modified: ['composer.lock'],
});
expect(
await composer.updateArtifacts({
packageFileName: 'composer.json',
updatedDeps: [],
newPackageFileContent: '{}',
config: {
...config,
isLockFileMaintenance: true,
},
})
).not.toBeNull();
expect(execSnapshots).toMatchSnapshot();
});
it('supports docker mode', async () => {
jest.spyOn(docker, 'removeDanglingContainers').mockResolvedValueOnce();
await setUtilConfig({ ...config, binarySource: BinarySource.Docker });
fs.readLocalFile.mockResolvedValueOnce('Current composer.lock' as any);
const execSnapshots = mockExecAll(exec);
fs.readLocalFile.mockReturnValueOnce('New composer.lock' as any);
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [
{ version: '1.10.0' },
{ version: '1.10.17' },
{ version: '2.0.0' },
{ version: '2.0.7' },
],
});
git.getRepoStatus.mockResolvedValue({
...repoStatus,
modified: ['composer.lock'],
});
expect(
await composer.updateArtifacts({
packageFileName: 'composer.json',
updatedDeps: [],
newPackageFileContent: '{}',
config: { ...config, constraints: { composer: '^1.10.0' } },
})
).not.toBeNull();
expect(execSnapshots).toMatchSnapshot();
});
it('supports global mode', async () => {
fs.readLocalFile.mockResolvedValueOnce('Current composer.lock' as any);
const execSnapshots = mockExecAll(exec);
fs.readLocalFile.mockReturnValueOnce('New composer.lock' as any);
git.getRepoStatus.mockResolvedValue({
...repoStatus,
modified: ['composer.lock'],
});
expect(
await composer.updateArtifacts({
packageFileName: 'composer.json',
updatedDeps: [],
newPackageFileContent: '{}',
config: {
...config,
binarySource: BinarySource.Global,
},
})
).not.toBeNull();
expect(execSnapshots).toMatchSnapshot();
});
it('catches errors', async () => {
fs.readLocalFile.mockResolvedValueOnce('Current composer.lock' as any);
fs.writeLocalFile.mockImplementationOnce(() => {
throw new Error('not found');
});
expect(
await composer.updateArtifacts({
packageFileName: 'composer.json',
updatedDeps: [],
newPackageFileContent: '{}',
config,
})
).toMatchSnapshot();
});
it('catches unmet requirements errors', async () => {
fs.readLocalFile.mockResolvedValueOnce('Current composer.lock' as any);
fs.writeLocalFile.mockImplementationOnce(() => {
throw new Error(
'fooYour requirements could not be resolved to an installable set of packages.bar'
);
});
expect(
await composer.updateArtifacts({
packageFileName: 'composer.json',
updatedDeps: [],
newPackageFileContent: '{}',
config,
})
).toMatchSnapshot();
});
it('throws for disk space', async () => {
fs.readLocalFile.mockResolvedValueOnce('Current composer.lock' as any);
fs.writeLocalFile.mockImplementationOnce(() => {
throw new Error(
'vendor/composer/07fe2366/sebastianbergmann-php-code-coverage-c896779/src/Report/Html/Renderer/Template/js/d3.min.js: write error (disk full?). Continue? (y/n/^C) '
);
});
await expect(
composer.updateArtifacts({
packageFileName: 'composer.json',
updatedDeps: [],
newPackageFileContent: '{}',
config,
})
).rejects.toThrow();
});
it('disables ignorePlatformReqs', async () => {
fs.readLocalFile.mockResolvedValueOnce('Current composer.lock' as any);
const execSnapshots = mockExecAll(exec);
fs.readLocalFile.mockReturnValueOnce('New composer.lock' as any);
git.getRepoStatus.mockResolvedValue({
...repoStatus,
modified: ['composer.lock'],
});
expect(
await composer.updateArtifacts({
packageFileName: 'composer.json',
updatedDeps: [],
newPackageFileContent: '{}',
config: {
...config,
composerIgnorePlatformReqs: false,
},
})
).not.toBeNull();
expect(execSnapshots).toMatchSnapshot();
});
});