-
Notifications
You must be signed in to change notification settings - Fork 20
/
version.test.ts
308 lines (293 loc) · 10.6 KB
/
version.test.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
const path = require('path');
const nock = require('nock');
const dataPath = path.join(__dirname, 'data');
import * as version from '../src/version';
describe('Pulling from multipage results with Link header', () => {
beforeEach(() => {
nock.disableNetConnect();
// Releases file contains version info for:
// 3.16.2, 3.16.1, 3.16.0, 3.16.0-rc4
// 3.15.6, 3.15.5, 3.15.1, 3.15.0
// 3.14.7, 3.14.1, 3.14.0
// 3.13.5
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.replyWithFile(200, path.join(dataPath, 'releases.json'), {
'Content-Type': 'application/json',
Link:
'<https://api.github.com/repos/Kitware/CMake/releases?page=2>; rel="next", <https://api.github.com/repos/Kitware/CMake/releases?page=2>; rel="last"',
});
// Releases file 2 contains version info for:
// 2.4.8, 2.6.4, 2.8.10.2, 2.8.12.2
// 3.0.0, 3.0.1, 3.0.2
// 3.1.0, 3.1.1, 3.1.2, 3.1.3
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.query({ page: 2 })
.replyWithFile(200, path.join(dataPath, 'releases2.json'), {
'Content-Type': 'application/json',
Link:
'<https://api.github.com/repos/Kitware/CMake/releases?page=1>; rel="first", <https://api.github.com/repos/Kitware/CMake/releases?page=1>; rel="prev"',
});
});
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
});
it('parses the latest version', async () => {
const version_info = await version.getAllVersionInfo();
const latest = version.getLatestMatching('', version_info);
expect(latest.name).toMatch(/3.16.2/);
});
it('selects an exact version for full release', async () => {
const version_info = await version.getAllVersionInfo();
const selected = version.getLatestMatching('3.15.5', version_info);
expect(selected.name).toMatch(/3.15.5/);
});
it('selects the latest version for provided minor release', async () => {
const version_info = await version.getAllVersionInfo();
const selected = version.getLatestMatching('3.15', version_info);
expect(selected.name).toMatch(/3.15.6/);
});
it('selects the latest version for provided minor release with x', async () => {
const version_info = await version.getAllVersionInfo();
const selected = version.getLatestMatching('3.15.x', version_info);
expect(selected.name).toMatch(/3.15.6/);
});
it('selects the latest version for provided major release with x', async () => {
const version_info = await version.getAllVersionInfo();
const selected = version.getLatestMatching('3.x', version_info);
expect(selected.name).toMatch(/3.16.2/);
});
it('throws on non-existent full version', async () => {
const version_info = await version.getAllVersionInfo();
expect(() => {
version.getLatestMatching('100.0.0', version_info);
}).toThrow('Unable to find version matching 100.0.0');
});
it('throws on non-existent part version', async () => {
const version_info = await version.getAllVersionInfo();
expect(() => {
version.getLatestMatching('100.0.x', version_info);
}).toThrow('Unable to find version matching 100.0');
});
it('select versions on second page', async () => {
const version_info = await version.getAllVersionInfo();
const selected = version.getLatestMatching('2.x', version_info);
expect(selected.name).toMatch(/2.8.12/);
});
it('ignores extra numbers in version', async () => {
const version_info = await version.getAllVersionInfo();
const selected = version.getLatestMatching('2.8.10', version_info);
expect(selected.name).toMatch(/2.8.10/);
});
});
describe('Pulling from multipage results without Link header', () => {
// When the Link header is not available, we still want to be able to parse
// all pages. This could be done by iterating over all possible pages until
// we get no further results.
beforeEach(() => {
nock.disableNetConnect();
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.replyWithFile(200, path.join(dataPath, 'releases.json'), {
'Content-Type': 'application/json',
});
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.query({ page: 2 })
.replyWithFile(200, path.join(dataPath, 'releases2.json'), {
'Content-Type': 'application/json',
});
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.query({ page: 3 })
.reply(200, []);
});
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
});
it('selects exact version from first page', async () => {
const version_info = await version.getAllVersionInfo();
const selected = version.getLatestMatching('3.15.5', version_info);
expect(selected.name).toMatch(/3.15.5/);
});
it('throws on non-existent full version', async () => {
const version_info = await version.getAllVersionInfo();
expect(() => {
version.getLatestMatching('100.0.0', version_info);
}).toThrow('Unable to find version matching 100.0.0');
});
it('selects versions on second page', async () => {
const version_info = await version.getAllVersionInfo();
const selected = version.getLatestMatching('2.x', version_info);
expect(selected.name).toMatch(/2.8.12/);
});
});
describe('When api token is required', () => {
beforeEach(() => {
nock('https://api.github.com', {
reqheaders: {
Authorization: 'token secret_token',
},
})
.get('/repos/Kitware/CMake/releases')
.replyWithFile(200, path.join(dataPath, 'releases.json'), {
'Content-Type': 'application/json',
});
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.replyWithError('Invalid API token');
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.query({ page: 2 })
.reply(200, []);
});
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
});
it('includes api token in request', async () => {
const version_info = await version.getAllVersionInfo('secret_token');
expect(version_info).toEqual(expect.arrayContaining([expect.anything()]));
});
it('passing empty token gives error', async () => {
await expect(version.getAllVersionInfo('')).rejects.toThrow(
'Invalid API token'
);
});
it('not passing token gives error', async () => {
await expect(version.getAllVersionInfo()).rejects.toThrow(
'Invalid API token'
);
});
});
describe('When using macos 3.19.2 release', () => {
const releases = [
{
tag_name: 'v3.19.2',
assets: [
{
name: 'cmake-3.19.2-Linux-x86_64.tar.gz',
browser_download_url:
'https://fakeaddress/cmake-3.19.2-Linux-x86_64.tar.gz',
},
{
name: 'cmake-3.19.2-macos-universal.dmg',
browser_download_url:
'https://fakeaddress.com/cmake-3.19.2-macos-universal.dmg',
},
{
name: 'cmake-3.19.2-macos-universal.tar.gz',
browser_download_url:
'https://fakeaddress.com/cmake-3.19.2-macos-universal.tar.gz',
},
],
},
];
beforeEach(() => {
nock.disableNetConnect();
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.reply(200, releases);
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.query({ page: 2 })
.reply(200, []);
});
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
});
it('correctly parses the version', async () => {
const version_info = await version.getAllVersionInfo();
const selected = version.getLatestMatching('3.x', version_info);
expect(selected.name).toMatch(/3.19.2/);
});
it('correctly parses the universal macos archive', async () => {
const version_info = await version.getAllVersionInfo();
const selected = version.getLatestMatching('3.x', version_info);
const assets = selected.assets;
const macos = assets.filter(
(a) => a.platform === 'darwin' && a.filetype === 'archive'
);
expect(macos.length).toBe(1);
const macosAsset = macos[0];
expect(macosAsset).toEqual({
name: 'cmake-3.19.2-macos-universal.tar.gz',
platform: 'darwin',
arch: 'x86_64',
filetype: 'archive',
url: 'https://fakeaddress.com/cmake-3.19.2-macos-universal.tar.gz',
});
});
});
describe('When providing multiple different archs', () => {
const releases = [
{
tag_name: 'v3.19.3',
assets: [
{
name: 'cmake-3.19.3-Linux-aarch64.tar.gz',
browser_download_url:
'https://fakeaddress.com/cmake-3.19.3-Linux-aarch64.tar.gz',
},
{
name: 'cmake-3.19.3-Linux-x86_64.tar.gz',
browser_download_url:
'https://fakeaddress.com/cmake-3.19.3-Linux-x86_64.tar.gz',
},
],
},
];
beforeEach(() => {
nock.disableNetConnect();
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.reply(200, releases);
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.query({ page: 2 })
.reply(200, []);
});
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
});
it('correctly parses the version', async () => {
const version_info = await version.getAllVersionInfo();
const selected = version.getLatestMatching('3.x', version_info);
expect(selected.name).toMatch(/3.19.3/);
});
it('correctly parses the x86 archive', async () => {
const version_info = await version.getAllVersionInfo();
const selected = version.getLatestMatching('3.x', version_info);
const assets = selected.assets;
const macos = assets.filter((a) => a.arch === 'x86_64');
expect(macos.length).toBe(1);
const macosAsset = macos[0];
expect(macosAsset).toEqual({
name: 'cmake-3.19.3-Linux-x86_64.tar.gz',
platform: 'linux',
arch: 'x86_64',
filetype: 'archive',
url: 'https://fakeaddress.com/cmake-3.19.3-Linux-x86_64.tar.gz',
});
});
it('correctly parses the aarch86 archive', async () => {
const version_info = await version.getAllVersionInfo();
const selected = version.getLatestMatching('3.x', version_info);
const assets = selected.assets;
const macos = assets.filter((a) => a.arch != 'x86_64');
expect(macos.length).toBe(1);
const macosAsset = macos[0];
expect(macosAsset).toEqual({
name: 'cmake-3.19.3-Linux-aarch64.tar.gz',
platform: 'linux',
arch: '',
filetype: 'archive',
url: 'https://fakeaddress.com/cmake-3.19.3-Linux-aarch64.tar.gz',
});
});
});