-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.spec.ts
247 lines (225 loc) · 8.3 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
/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */
import { updateLockedDependency } from '../..';
import { Fixtures } from '../../../../../../test/fixtures';
import * as httpMock from '../../../../../../test/http-mock';
import { clone } from '../../../../../util/clone';
import type { UpdateLockedConfig } from '../../../types';
const packageFileContent = Fixtures.get('package.json', './package-lock');
const lockFileContent = Fixtures.get('package-lock.json', './package-lock');
const lockFileV2Content = Fixtures.get(
'package-lock-v2.json',
'./package-lock'
);
const acceptsJson = Fixtures.getJson('accepts.json', './package-lock');
const expressJson = Fixtures.get('express.json', './common');
const mimeJson = Fixtures.get('mime.json', './package-lock');
const serveStaticJson = Fixtures.get('serve-static.json', './package-lock');
const sendJson = Fixtures.get('send.json', './package-lock');
const typeIsJson = Fixtures.getJson('type-is.json', './package-lock');
const bundledPackageJson = Fixtures.get(
'bundled.package.json',
'./package-lock'
);
const bundledPackageLockJson = Fixtures.get(
'bundled.package-lock.json',
'./package-lock'
);
describe('modules/manager/npm/update/locked-dependency/index', () => {
describe('updateLockedDependency()', () => {
let config: UpdateLockedConfig;
beforeEach(() => {
config = {
packageFile: 'package.json',
packageFileContent,
lockFile: 'package-lock.json',
lockFileContent,
depName: 'some-dep',
currentVersion: '1.0.0',
newVersion: '1.0.1',
};
});
it('validates filename', async () => {
expect(
await updateLockedDependency({ ...config, lockFile: 'yarn.lock' })
).toMatchObject({});
expect(
await updateLockedDependency({ ...config, lockFile: 'yarn.lock2' })
).toMatchObject({});
});
it('validates versions', async () => {
expect(
await updateLockedDependency({
...config,
newVersion: '^2.0.0',
})
).toMatchObject({});
});
it('returns null for unparseable files', async () => {
expect(
await updateLockedDependency({ ...config, lockFileContent: 'not json' })
).toMatchObject({});
});
it('rejects lockFileVersion 2', async () => {
expect(
await updateLockedDependency({
...config,
lockFileContent: lockFileContent.replace(': 1,', ': 2,'),
})
).toMatchObject({});
});
it('returns null if no locked deps', async () => {
expect(await updateLockedDependency(config)).toMatchObject({});
});
it('rejects null if no constraint found', async () => {
expect(
await updateLockedDependency({
...config,
lockFileContent: lockFileContent.replace('1.0.0', '10.0.0'),
depName: 'accepts',
currentVersion: '10.0.0',
newVersion: '11.0.0',
})
).toMatchObject({});
});
it('remediates in-range', async () => {
const res = await updateLockedDependency({
...config,
depName: 'mime',
currentVersion: '1.2.11',
newVersion: '1.2.12',
});
expect(
JSON.parse(res.files!['package-lock.json']).dependencies.mime.version
).toBe('1.2.12');
});
it('rejects in-range remediation if lockfile v2+', async () => {
const res = await updateLockedDependency({
...config,
lockFileContent: lockFileV2Content,
depName: 'mime',
currentVersion: '1.2.11',
newVersion: '1.2.12',
});
expect(res.status).toBe('unsupported');
});
it('fails to remediate if parent dep cannot support', async () => {
const acceptsModified = clone(acceptsJson);
acceptsModified.versions['2.0.0'] = {};
httpMock
.scope('https://registry.npmjs.org')
.get('/accepts')
.reply(200, acceptsModified);
httpMock
.scope('https://registry.npmjs.org')
.get('/express')
.reply(200, expressJson);
const res = await updateLockedDependency({
...config,
depName: 'accepts',
currentVersion: '1.0.0',
newVersion: '2.0.0',
});
expect(res).toMatchObject({});
});
it('remediates express', async () => {
config.depName = 'express';
config.currentVersion = '4.0.0';
config.newVersion = '4.1.0';
const res = await updateLockedDependency(config);
expect(res.files!['package.json']).toContain('"express": "4.1.0"');
const packageLock = JSON.parse(res.files!['package-lock.json']);
expect(packageLock.dependencies.express.version).toBe('4.1.0');
});
it('remediates lock file v2 express', async () => {
config.depName = 'express';
config.currentVersion = '4.0.0';
config.newVersion = '4.1.0';
config.lockFileContent = lockFileV2Content;
const res = await updateLockedDependency(config);
expect(res.files!['package.json']).toContain('"express": "4.1.0"');
const packageLock = JSON.parse(res.files!['package-lock.json']);
expect(packageLock.dependencies.express.version).toBe('4.1.0');
});
it('returns already-updated if already remediated exactly', async () => {
config.depName = 'mime';
config.currentVersion = '1.2.10';
config.newVersion = '1.2.11';
const res = await updateLockedDependency(config);
expect(res.status).toBe('already-updated');
});
it('returns already-updated if already v2 remediated exactly', async () => {
config.depName = 'mime';
config.currentVersion = '1.2.10';
config.newVersion = '1.2.11';
config.lockFileContent = lockFileV2Content;
const res = await updateLockedDependency(config);
expect(res.status).toBe('already-updated');
});
it('returns already-updated if already remediated higher', async () => {
config.depName = 'mime';
config.currentVersion = '1.2.9';
config.newVersion = '1.2.10';
config.allowHigherOrRemoved = true;
const res = await updateLockedDependency(config);
expect(res.status).toBe('already-updated');
});
it('returns already-updated if not found', async () => {
config.depName = 'notfound';
config.currentVersion = '1.2.9';
config.newVersion = '1.2.10';
config.allowHigherOrRemoved = true;
const res = await updateLockedDependency(config);
expect(res.status).toBe('already-updated');
});
it('returns update-failed if other, lower version found', async () => {
config.depName = 'mime';
config.currentVersion = '1.2.5';
config.newVersion = '1.2.15';
config.allowHigherOrRemoved = true;
const res = await updateLockedDependency(config);
expect(res.status).toBe('update-failed');
});
it('remediates mime', async () => {
config.depName = 'mime';
config.currentVersion = '1.2.11';
config.newVersion = '1.4.1';
httpMock
.scope('https://registry.npmjs.org')
.get('/mime')
.reply(200, mimeJson);
httpMock
.scope('https://registry.npmjs.org')
.get('/send')
.reply(200, sendJson);
httpMock
.scope('https://registry.npmjs.org')
.get('/serve-static')
.reply(200, serveStaticJson);
httpMock
.scope('https://registry.npmjs.org')
.get('/type-is')
.reply(200, typeIsJson);
const res = await updateLockedDependency(config);
const packageLock = JSON.parse(res.files!['package-lock.json']);
expect(packageLock.dependencies.mime.version).toBe('1.4.1');
expect(packageLock.dependencies.express.version).toBe('4.16.0');
});
it('fails remediation if cannot update parent', async () => {
config.depName = 'mime';
config.currentVersion = '1.2.11';
config.newVersion = '1.4.1';
config.allowParentUpdates = false;
const res = await updateLockedDependency(config);
expect(res.status).toBe('update-failed');
});
it('fails remediation if bundled', async () => {
config.depName = 'ansi-regex';
config.currentVersion = '3.0.0';
config.newVersion = '5.0.1';
config.packageFileContent = bundledPackageJson;
config.lockFileContent = bundledPackageLockJson;
const res = await updateLockedDependency(config);
expect(res.status).toBe('update-failed');
});
});
});