/
index.spec.ts
232 lines (204 loc) · 7.62 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
import { readFileSync } from 'fs';
import { fs } from '../../../test/util';
import { PackageDependency, PackageFile } from '../common';
import { extractPackage, resolveParents } from './extract';
import { extractAllPackageFiles, updateDependency } from './index';
jest.mock('../../util/fs');
const pomContent = readFileSync(
'lib/manager/maven/__fixtures__/simple.pom.xml',
'utf8'
);
const pomParent = readFileSync(
'lib/manager/maven/__fixtures__/parent.pom.xml',
'utf8'
);
const pomChild = readFileSync(
'lib/manager/maven/__fixtures__/child.pom.xml',
'utf8'
);
const origContent = readFileSync(
'lib/manager/maven/__fixtures__/grouping.pom.xml',
'utf8'
);
function selectDep(deps: PackageDependency[], name = 'org.example:quuz') {
return deps.find((dep) => dep.depName === name);
}
describe('manager/maven', () => {
describe('extractAllPackageFiles', () => {
it('should return empty if package has no content', async () => {
fs.readLocalFile.mockResolvedValueOnce(null);
const res = await extractAllPackageFiles({}, ['random.pom.xml']);
expect(res).toEqual([]);
});
it('should return empty for packages with invalid content', async () => {
fs.readLocalFile.mockResolvedValueOnce('invalid content');
const res = await extractAllPackageFiles({}, ['random.pom.xml']);
expect(res).toEqual([]);
});
it('should return package files info', async () => {
fs.readLocalFile.mockResolvedValueOnce(pomContent);
const packages = await extractAllPackageFiles({}, ['random.pom.xml']);
// windows path fix
for (const p of packages) {
if (p.parent) {
p.parent = p.parent.replace(/\\/g, '/');
}
}
expect(packages).toMatchSnapshot();
});
});
describe('updateDependency', () => {
it('should update an existing dependency', () => {
const newValue = '9.9.9.9-final';
const { deps } = extractPackage(pomContent);
const dep = selectDep(deps);
const upgrade = { ...dep, newValue };
const updatedContent = updateDependency({
fileContent: pomContent,
upgrade,
});
const updatedDep = selectDep(extractPackage(updatedContent).deps);
expect(updatedDep.currentValue).toEqual(newValue);
});
it('should update existing dependency defined via properties', () => {
const finder = ({ depName }: PackageDependency) =>
depName === 'org.example:quux';
const newValue = '9.9.9.9-final';
const packages = resolveParents([
extractPackage(pomParent, 'parent.pom.xml'),
extractPackage(pomChild, 'child.pom.xml'),
]);
const [{ deps }] = packages;
const dep = deps.find(finder);
const upgrade = { ...dep, newValue };
const updatedContent = updateDependency({
fileContent: pomParent,
upgrade,
});
const [updatedPkg] = resolveParents([
extractPackage(updatedContent, 'parent.pom.xml'),
extractPackage(pomChild, 'child.pom.xml'),
]);
const updatedDep = updatedPkg.deps.find(finder);
expect(updatedDep.registryUrls).toContain('http://example.com/');
expect(updatedDep.currentValue).toEqual(newValue);
});
it('should include registryUrls from parent pom files', async () => {
fs.readLocalFile
.mockResolvedValueOnce(pomParent)
.mockResolvedValueOnce(pomChild);
const packages = await extractAllPackageFiles({}, [
'parent.pom.xml',
'child.pom.xml',
]);
const urls = new Set([
'https://repo.maven.apache.org/maven2',
'http://example.com/',
'http://example.com/nexus/xyz',
]);
packages.forEach(({ deps }) => {
deps.forEach(({ registryUrls }) => {
const depUrls = new Set([...registryUrls]);
expect(depUrls).toEqual(urls);
});
});
});
it('should not touch content if new and old versions are equal', () => {
const newValue = '1.2.3';
const { deps } = extractPackage(pomContent);
const dep = selectDep(deps);
const upgrade = { ...dep, newValue };
const updatedContent = updateDependency({
fileContent: pomContent,
upgrade,
});
expect(pomContent).toBe(updatedContent);
});
it('should update to version of the latest dep in implicit group', async () => {
fs.readLocalFile.mockResolvedValueOnce(origContent);
const [{ deps }] = await extractAllPackageFiles({}, ['pom.xml']);
const dep1 = selectDep(deps, 'org.example:foo-1');
const upgrade1 = { ...dep1, newValue: '1.0.2' };
const dep2 = selectDep(deps, 'org.example:foo-2');
const upgrade2 = { ...dep2, newValue: '1.0.3' };
const updatedOutside = origContent.replace('1.0.0', '1.0.1');
expect(
updateDependency({ fileContent: origContent, upgrade: upgrade1 })
).toEqual(origContent.replace('1.0.0', '1.0.2'));
expect(
updateDependency({
fileContent: updatedOutside,
upgrade: upgrade1,
})
).toEqual(origContent.replace('1.0.0', '1.0.2'));
const updatedByPrevious = updateDependency({
fileContent: origContent,
upgrade: upgrade1,
});
expect(
updateDependency({
fileContent: updatedByPrevious,
upgrade: upgrade2,
})
).toEqual(origContent.replace('1.0.0', '1.0.3'));
expect(
updateDependency({
fileContent: updatedOutside,
upgrade: upgrade2,
})
).toEqual(origContent.replace('1.0.0', '1.0.3'));
expect(
updateDependency({ fileContent: origContent, upgrade: upgrade2 })
).toEqual(origContent.replace('1.0.0', '1.0.3'));
});
it('should return null for ungrouped deps if content was updated outside', async () => {
fs.readLocalFile.mockResolvedValueOnce(origContent);
const [{ deps }] = await extractAllPackageFiles({}, ['pom.xml']);
const dep = selectDep(deps, 'org.example:bar');
const upgrade = { ...dep, newValue: '2.0.2' };
const updatedOutside = origContent.replace('2.0.0', '2.0.1');
expect(
updateDependency({ fileContent: updatedOutside, upgrade })
).toBeNull();
});
it('should return null if current versions in content and upgrade are not same', () => {
const currentValue = '1.2.2';
const newValue = '1.2.4';
const { deps } = extractPackage(pomContent);
const dep = selectDep(deps);
const upgrade = { ...dep, currentValue, newValue };
const updatedContent = updateDependency({
fileContent: pomContent,
upgrade,
});
expect(updatedContent).toBeNull();
});
it('should update ranges', () => {
const newValue = '[1.2.3]';
const select = (depSet: PackageFile) =>
selectDep(depSet.deps, 'org.example:hard-range');
const oldContent = extractPackage(pomContent);
const dep = select(oldContent);
const upgrade = { ...dep, newValue };
const newContent = extractPackage(
updateDependency({ fileContent: pomContent, upgrade })
);
const newDep = select(newContent);
expect(newDep.currentValue).toEqual(newValue);
});
it('should preserve ranges', () => {
const newValue = '[1.0.0]';
const select = (depSet: PackageFile) =>
depSet && depSet.deps
? selectDep(depSet.deps, 'org.example:hard-range')
: null;
const oldContent = extractPackage(pomContent);
const dep = select(oldContent);
expect(dep).not.toBeNull();
const upgrade = { ...dep, newValue };
expect(updateDependency({ fileContent: pomContent, upgrade })).toEqual(
pomContent
);
});
});
});