/
index.spec.ts
394 lines (388 loc) · 11.4 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
import { loadFixture } from '../../../../test/util';
import { getConfig } from '../../../config/defaults';
import * as _fs from '../../../util/fs';
import * as npmExtract from '.';
const fs: any = _fs;
// TODO: fix types
const defaultConfig = getConfig();
const input01Content = loadFixture('inputs/01.json', '..');
const input01GlobContent = loadFixture('inputs/01-glob.json', '..');
const workspacesContent = loadFixture('inputs/workspaces.json', '..');
const workspacesSimpleContent = loadFixture(
'inputs/workspaces-simple.json',
'..'
);
const vendorisedContent = loadFixture('is-object.json', '..');
const invalidNameContent = loadFixture('invalid-name.json', '..');
describe('manager/npm/extract/index', () => {
describe('.extractPackageFile()', () => {
beforeEach(() => {
fs.readLocalFile = jest.fn(() => null);
fs.localPathExists = jest.fn(() => false);
});
it('returns null if cannot parse', async () => {
const res = await npmExtract.extractPackageFile(
'not json',
'package.json',
defaultConfig
);
expect(res).toBeNull();
});
it('catches invalid names', async () => {
const res = await npmExtract.extractPackageFile(
invalidNameContent,
'package.json',
defaultConfig
);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('ignores vendorised package.json', async () => {
const res = await npmExtract.extractPackageFile(
vendorisedContent,
'package.json',
defaultConfig
);
expect(res).toBeNull();
});
it('throws error if non-root renovate config', async () => {
await expect(
npmExtract.extractPackageFile(
'{ "renovate": {} }',
'backend/package.json',
defaultConfig
)
).rejects.toThrow();
});
it('returns null if no deps', async () => {
const res = await npmExtract.extractPackageFile(
'{ "renovate": {} }',
'package.json',
defaultConfig
);
expect(res).toBeNull();
});
it('handles invalid', async () => {
const res = await npmExtract.extractPackageFile(
'{"dependencies": true, "devDependencies": []}',
'package.json',
defaultConfig
);
expect(res).toBeNull();
});
it('returns an array of dependencies', async () => {
const res = await npmExtract.extractPackageFile(
input01Content,
'package.json',
defaultConfig
);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('returns an array of dependencies with resolution comments', async () => {
const res = await npmExtract.extractPackageFile(
input01GlobContent,
'package.json',
defaultConfig
);
expect(res?.deps).toHaveLength(13);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('finds a lock file', async () => {
fs.readLocalFile = jest.fn((fileName) => {
if (fileName === 'yarn.lock') {
return '# yarn.lock';
}
return null;
});
const res = await npmExtract.extractPackageFile(
input01Content,
'package.json',
defaultConfig
);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('finds and filters .npmrc', async () => {
fs.readLocalFile = jest.fn((fileName) => {
if (fileName === '.npmrc') {
return 'save-exact = true\npackage-lock = false\n';
}
return null;
});
const res = await npmExtract.extractPackageFile(
input01Content,
'package.json',
{}
);
expect(res.npmrc).toBeDefined();
});
it('ignores .npmrc when config.npmrc is defined', async () => {
fs.readLocalFile = jest.fn((fileName) => {
if (fileName === '.npmrc') {
return 'some-npmrc\n';
}
return null;
});
const res = await npmExtract.extractPackageFile(
input01Content,
'package.json',
{ npmrc: 'some-configured-npmrc' }
);
expect(res.npmrc).toBeUndefined();
});
it('finds and filters .npmrc with variables', async () => {
fs.readLocalFile = jest.fn((fileName) => {
if (fileName === '.npmrc') {
// eslint-disable-next-line
return 'registry=https://registry.npmjs.org\n//registry.npmjs.org/:_authToken=${NPM_AUTH_TOKEN}\n';
}
return null;
});
const res = await npmExtract.extractPackageFile(
input01Content,
'package.json',
{}
);
expect(res.npmrc).toEqual('registry=https://registry.npmjs.org\n');
});
it('finds lerna', async () => {
fs.readLocalFile = jest.fn((fileName) => {
if (fileName === 'lerna.json') {
return '{}';
}
return null;
});
const res = await npmExtract.extractPackageFile(
input01Content,
'package.json',
defaultConfig
);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('finds "npmClient":"npm" in lerna.json', async () => {
fs.readLocalFile = jest.fn((fileName) => {
if (fileName === 'lerna.json') {
return '{ "npmClient": "npm" }';
}
return null;
});
const res = await npmExtract.extractPackageFile(
input01Content,
'package.json',
defaultConfig
);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('finds "npmClient":"yarn" in lerna.json', async () => {
fs.readLocalFile = jest.fn((fileName) => {
if (fileName === 'lerna.json') {
return '{ "npmClient": "yarn" }';
}
return null;
});
const res = await npmExtract.extractPackageFile(
input01Content,
'package.json',
defaultConfig
);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('finds simple yarn workspaces', async () => {
fs.readLocalFile = jest.fn((fileName) => {
if (fileName === 'lerna.json') {
return '{}';
}
return null;
});
const res = await npmExtract.extractPackageFile(
workspacesSimpleContent,
'package.json',
defaultConfig
);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('finds simple yarn workspaces with lerna.json and useWorkspaces: true', async () => {
fs.readLocalFile = jest.fn((fileName) => {
if (fileName === 'lerna.json') {
return '{"useWorkspaces": true}';
}
return null;
});
const res = await npmExtract.extractPackageFile(
workspacesSimpleContent,
'package.json',
defaultConfig
);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('finds complex yarn workspaces', async () => {
fs.readLocalFile = jest.fn((fileName) => {
if (fileName === 'lerna.json') {
return '{}';
}
return null;
});
const res = await npmExtract.extractPackageFile(
workspacesContent,
'package.json',
defaultConfig
);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('extracts engines', async () => {
const pJson = {
dependencies: {
angular: '1.6.0',
},
devDependencies: {
'@angular/cli': '1.6.0',
foo: '*',
bar: 'file:../foo/bar',
baz: '',
other: 'latest',
},
engines: {
atom: '>=1.7.0 <2.0.0',
node: '>= 8.9.2',
npm: '^8.0.0',
pnpm: '^1.2.0',
yarn: 'disabled',
vscode: '>=1.49.3',
},
main: 'index.js',
};
const pJsonStr = JSON.stringify(pJson);
const res = await npmExtract.extractPackageFile(
pJsonStr,
'package.json',
defaultConfig
);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('extracts volta', async () => {
const pJson = {
main: 'index.js',
engines: {
node: '8.9.2',
},
volta: {
node: '8.9.2',
yarn: '1.12.3',
npm: '5.9.0',
pnpm: '6.11.2',
},
};
const pJsonStr = JSON.stringify(pJson);
const res = await npmExtract.extractPackageFile(
pJsonStr,
'package.json',
defaultConfig
);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('extracts volta yarn unknown-version', async () => {
const pJson = {
main: 'index.js',
engines: {
node: '8.9.2',
},
volta: {
node: '8.9.2',
yarn: 'unknown',
},
};
const pJsonStr = JSON.stringify(pJson);
const res = await npmExtract.extractPackageFile(
pJsonStr,
'package.json',
defaultConfig
);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('extracts non-npmjs', async () => {
const pJson = {
dependencies: {
a: 'github:owner/a',
b: 'github:owner/b#master',
c: 'github:owner/c#v1.1.0',
d: 'github:owner/d#a7g3eaf',
e: 'github:owner/e#49b5aca613b33c5b626ae68c03a385f25c142f55',
f: 'owner/f#v2.0.0',
g: 'gitlab:owner/g#v1.0.0',
h: 'github:-hello/world#v1.0.0',
i: '@foo/bar#v2.0.0',
j: 'github:frank#v0.0.1',
k: 'github:owner/k#49b5aca',
l: 'github:owner/l.git#abcdef0',
m: 'https://github.com/owner/m.git#v1.0.0',
n: 'git+https://github.com/owner/n#v2.0.0',
},
};
const pJsonStr = JSON.stringify(pJson);
const res = await npmExtract.extractPackageFile(
pJsonStr,
'package.json',
defaultConfig
);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('extracts npm package alias', async () => {
fs.readLocalFile = jest.fn((fileName) => {
if (fileName === 'package-lock.json') {
return '{}';
}
return null;
});
const pJson = {
dependencies: {
a: 'npm:foo@1',
b: 'npm:@foo/bar@1.2.3',
c: 'npm:foo',
},
};
const pJsonStr = JSON.stringify(pJson);
const res = await npmExtract.extractPackageFile(
pJsonStr,
'package.json',
defaultConfig
);
// FIXME: explicit assert condition
expect(res).toMatchSnapshot();
});
it('sets skipInstalls false if Yarn zero-install is used', async () => {
fs.readLocalFile = jest.fn((fileName) => {
if (fileName === 'yarn.lock') {
return '# yarn.lock';
}
if (fileName === '.yarnrc.yml') {
return 'pnpEnableInlining: false';
}
return null;
});
fs.localPathExists = jest.fn(() => true);
const res = await npmExtract.extractPackageFile(
input01Content,
'package.json',
defaultConfig
);
expect(res).toMatchSnapshot();
});
});
describe('.postExtract()', () => {
it('runs', async () => {
await expect(npmExtract.postExtract([], false)).resolves.not.toThrow();
});
});
});