-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathlistPackageVersions.test.ts
More file actions
402 lines (356 loc) · 18.1 KB
/
listPackageVersions.test.ts
File metadata and controls
402 lines (356 loc) · 18.1 KB
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
395
396
397
398
399
400
401
402
import { describe, expect, it, jest } from '@jest/globals';
import { listPackageVersions, listPackageVersionsByTag } from '../../packageManager/listPackageVersions';
import type { NpmOptions } from '../../types/NpmOptions';
import { initNpmMock } from '../../__fixtures__/mockNpm';
import { makePackageInfos, type PartialPackageInfos } from '../../__fixtures__/packageInfos';
import type { RepoOptions } from '../../types/BeachballOptions';
import { getParsedOptions } from '../../options/getOptions';
import { _npmShowProperties } from '../../packageManager/getNpmPackageInfo';
jest.mock('../../packageManager/npm');
// jest.mock('npm-registry-fetch');
//
// These tests cover aspects of listPackageVersions without a real registry.
// getNpmPackageInfo tests cover real usage of npm-registry-fetch.
//
describe('list npm versions', () => {
/** Mock the `npm show` command for `npmAsync` calls. This also handles cleanup after each test. */
const npmMock = initNpmMock();
const registry = 'https://fake';
const timeout = 1500;
// const commonOptions = { registry, timeout };
const commonArgs = ['show', '--registry', registry, '--json'];
describe('listPackageVersions', () => {
const npmOptions: NpmOptions = {
registry,
timeout,
path: undefined,
npmReadConcurrency: 2,
};
it('succeeds with nothing to do', async () => {
const versions = await listPackageVersions([], npmOptions);
expect(versions).toEqual({});
expect(npmMock.mock).not.toHaveBeenCalled();
// expect(npmMock.mockFetchJson).not.toHaveBeenCalled();
});
it('returns versions for one package', async () => {
npmMock.setRegistryData({ foo: { versions: ['1.0.0', '1.0.1'] } });
const versions = await listPackageVersions(['foo'], npmOptions);
expect(versions).toEqual({ foo: ['1.0.0', '1.0.1'] });
expect(npmMock.mock).toHaveBeenCalledTimes(1);
expect(npmMock.mock).toHaveBeenCalledWith([...commonArgs, 'foo', ..._npmShowProperties], expect.anything());
// expect(npmMock.mock).not.toHaveBeenCalled();
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(1);
// expect(npmMock.mockFetchJson).toHaveBeenCalledWith('/foo', expect.objectContaining(commonOptions));
});
it('returns empty versions array for missing package', async () => {
npmMock.setRegistryData({});
const versions = await listPackageVersions(['foo'], npmOptions);
expect(versions).toEqual({ foo: [] });
expect(npmMock.mock).toHaveBeenCalledTimes(1);
expect(npmMock.mock).toHaveBeenCalledWith([...commonArgs, 'foo', ..._npmShowProperties], expect.anything());
// expect(npmMock.mock).not.toHaveBeenCalled();
// expect(npmMock.mockFetchJson).toHaveBeenCalledWith('/foo', expect.objectContaining(commonOptions));
});
it('returns versions for multiple packages', async () => {
const packages = 'abcdefghij'.split('');
const showData = Object.fromEntries(packages.map((x, i) => [x, { versions: [`${i}.0.0`, `${i}.0.1`] }]));
npmMock.setRegistryData(showData);
const versions = await listPackageVersions(packages, npmOptions);
const expectedVerions = Object.fromEntries(Object.entries(showData).map(([k, v]) => [k, v.versions]));
expect(versions).toEqual(expectedVerions);
expect(npmMock.mock).toHaveBeenCalledTimes(packages.length);
// expect(npmMock.mock).not.toHaveBeenCalled();
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(packages.length);
});
it('returns versions for multiple packages with some missing', async () => {
npmMock.setRegistryData({ foo: { versions: ['1.0.0', '1.0.1'] } });
const versions = await listPackageVersions(['foo', 'bar'], npmOptions);
expect(versions).toEqual({ foo: ['1.0.0', '1.0.1'], bar: [] });
expect(npmMock.mock).toHaveBeenCalledTimes(2);
// expect(npmMock.mock).not.toHaveBeenCalled();
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(2);
});
it('respects password auth args', async () => {
npmMock.setRegistryData({ foo: { versions: ['1.0.0', '1.0.1'] } });
const versions = await listPackageVersions(['foo'], { ...npmOptions, authType: 'password', token: 'pass' });
expect(versions).toEqual({ foo: ['1.0.0', '1.0.1'] });
expect(npmMock.mock).toHaveBeenCalledWith(
[...commonArgs, '--//fake:_password=pass', 'foo', ..._npmShowProperties],
expect.anything()
);
// expect(npmMock.mockFetchJson).toHaveBeenCalledWith(
// '/foo',
// expect.objectContaining({ ...commonOptions, '//fake:_password': 'pass' })
// );
});
it('respects token auth args', async () => {
npmMock.setRegistryData({ foo: { versions: ['1.0.0', '1.0.1'] } });
const versions = await listPackageVersions(['foo'], { ...npmOptions, authType: 'authtoken', token: 'pass' });
expect(versions).toEqual({ foo: ['1.0.0', '1.0.1'] });
expect(npmMock.mock).toHaveBeenCalledWith(
[...commonArgs, '--//fake:_authToken=pass', 'foo', ..._npmShowProperties],
expect.anything()
);
// expect(npmMock.mockFetchJson).toHaveBeenCalledWith(
// '/foo',
// expect.objectContaining({ ...commonOptions, '//fake:_authToken': 'pass' })
// );
});
});
describe('listPackageVersionsByTag', () => {
function getOptionsAndPackages(params: {
packages: PartialPackageInfos;
/** CLI options which override any package-specific options */
extraArgv?: string[];
/** Options to override the defaults */
repoOptions?: Partial<RepoOptions>;
}) {
const parsedOptions = getParsedOptions({
argv: ['node', 'beachball', ...(params.extraArgv || [])],
cwd: '',
testRepoOptions: {
registry,
timeout,
npmReadConcurrency: 2,
...params.repoOptions,
},
});
const packageInfos = makePackageInfos(params.packages, parsedOptions.cliOptions);
return { options: parsedOptions.options, packages: Object.values(packageInfos) };
}
describe('defaults and repo options', () => {
it('succeeds with no packages', async () => {
const { packages, options } = getOptionsAndPackages({ packages: {} });
expect(await listPackageVersionsByTag(packages, options)).toEqual({});
expect(npmMock.mock).not.toHaveBeenCalled();
// expect(npmMock.mockFetchJson).not.toHaveBeenCalled();
});
it('returns latest tag by default', async () => {
npmMock.setRegistryData({ foo: { 'dist-tags': { latest: '1.0.0', beta: '2.0.0-beta' } } });
const { packages, options } = getOptionsAndPackages({
packages: { foo: {} },
});
// currently this is how the default to "latest" works in realistic scenarios
expect(options).toMatchObject({ tag: '', defaultNpmTag: 'latest' });
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({ foo: '1.0.0' });
expect(npmMock.mock).toHaveBeenCalledTimes(1);
expect(npmMock.mock).toHaveBeenCalledWith([...commonArgs, 'foo', ..._npmShowProperties], expect.anything());
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(1);
// expect(npmMock.mockFetchJson).toHaveBeenCalledWith('/foo', expect.objectContaining(commonOptions));
// should not use npm CLI wrapper
// expect(npmMock.mock).not.toHaveBeenCalled();
});
it('returns requested tag from repo options', async () => {
npmMock.setRegistryData({
foo: { 'dist-tags': { latest: '1.0.0', beta: '2.0.0-beta' } },
bar: { 'dist-tags': { latest: '1.0.0', beta: '3.0.0-beta' } },
});
const { packages, options } = getOptionsAndPackages({
packages: { foo: {}, bar: {} },
repoOptions: { tag: 'beta' },
});
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({ foo: '2.0.0-beta', bar: '3.0.0-beta' });
expect(npmMock.mock).toHaveBeenCalledTimes(2);
expect(npmMock.mock).toHaveBeenCalledWith([...commonArgs, 'foo', ..._npmShowProperties], expect.anything());
expect(npmMock.mock).toHaveBeenCalledWith([...commonArgs, 'bar', ..._npmShowProperties], expect.anything());
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(2);
// expect(npmMock.mockFetchJson).toHaveBeenCalledWith('/foo', expect.objectContaining(commonOptions));
// expect(npmMock.mockFetchJson).toHaveBeenCalledWith('/bar', expect.objectContaining(commonOptions));
});
it('returns versions for many packages', async () => {
const packages = 'abcdefghij'.split('');
const showData = Object.fromEntries(packages.map((x, i) => [x, { 'dist-tags': { latest: `${i}.0.0` } }]));
npmMock.setRegistryData(showData);
const { packages: packageInfos, options } = getOptionsAndPackages({
packages: Object.fromEntries(packages.map(x => [x, {}])),
repoOptions: { tag: 'latest' },
});
expect(await listPackageVersionsByTag(packageInfos, options)).toEqual(
Object.fromEntries(Object.entries(showData).map(([k, v]) => [k, v['dist-tags'].latest]))
);
expect(npmMock.mock).toHaveBeenCalledTimes(packages.length);
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(packages.length);
});
it('returns empty if no dist-tags available', async () => {
npmMock.setRegistryData({});
const { packages, options } = getOptionsAndPackages({
packages: { foo: {} },
});
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({});
expect(npmMock.mock).toHaveBeenCalledTimes(1);
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(1);
});
it('returns empty if no matching dist-tags available', async () => {
npmMock.setRegistryData({ foo: { 'dist-tags': { latest: '1.0.0', beta: '2.0.0-beta' } } });
const { packages, options } = getOptionsAndPackages({
packages: { foo: {} },
repoOptions: { tag: 'missing' },
});
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({});
expect(npmMock.mock).toHaveBeenCalledTimes(1);
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(1);
});
it("omits packages that don't exist in registry", async () => {
npmMock.setRegistryData({ foo: { 'dist-tags': { latest: '1.0.0' } } });
const { packages, options } = getOptionsAndPackages({
packages: { foo: {}, bar: {} },
});
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({ foo: '1.0.0' });
expect(npmMock.mock).toHaveBeenCalledTimes(2);
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(2);
});
it('does nothing if both tag and defaultNpmTag are empty', async () => {
const { packages, options } = getOptionsAndPackages({
packages: { foo: {} },
repoOptions: { tag: '', defaultNpmTag: '' },
});
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({});
expect(npmMock.mock).not.toHaveBeenCalled();
// expect(npmMock.mockFetchJson).not.toHaveBeenCalled();
});
});
describe('package options', () => {
it('uses per-package tag option', async () => {
npmMock.setRegistryData({
foo: { 'dist-tags': { latest: '1.0.0', beta: '2.0.0-beta' } },
bar: { 'dist-tags': { latest: '1.0.0', beta: '3.0.0-beta' } },
});
const { packages, options } = getOptionsAndPackages({
packages: {
foo: { beachball: { tag: 'beta', defaultNpmTag: 'nope' } },
bar: {},
},
});
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({ foo: '2.0.0-beta', bar: '1.0.0' });
expect(npmMock.mock).toHaveBeenCalledTimes(2);
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(2);
});
it('falls back to package defaultNpmTag if tag is unset', async () => {
npmMock.setRegistryData({
foo: { 'dist-tags': { latest: '1.0.0', beta: '2.0.0-beta' } },
bar: { 'dist-tags': { latest: '1.0.0', beta: '3.0.0-beta' } },
});
const { packages, options } = getOptionsAndPackages({
packages: { foo: { beachball: { defaultNpmTag: 'beta' } }, bar: {} },
});
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({ foo: '2.0.0-beta', bar: '1.0.0' });
expect(npmMock.mock).toHaveBeenCalledTimes(2);
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(2);
});
it('does nothing if package override tag and defaultNpmTag are empty', async () => {
const { packages, options } = getOptionsAndPackages({
packages: { foo: { beachball: { tag: '', defaultNpmTag: '' } } },
repoOptions: { tag: 'latest', defaultNpmTag: 'latest' },
});
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({});
expect(npmMock.mock).not.toHaveBeenCalled();
// expect(npmMock.mockFetchJson).not.toHaveBeenCalled();
});
});
describe('CLI options override', () => {
// it's expected that token is only specified as a CLI arg
it('respects token auth args', async () => {
npmMock.setRegistryData({ foo: { 'dist-tags': { latest: '1.0.0', beta: '2.0.0-beta' } } });
const { packages, options } = getOptionsAndPackages({
packages: { foo: {} },
extraArgv: ['--token', 'pass'],
});
// authtoken is the default auth type
expect(options).toMatchObject({ authType: 'authtoken', token: 'pass' });
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({ foo: '1.0.0' });
expect(npmMock.mock).toHaveBeenCalledWith(
[...commonArgs, '--//fake:_authToken=pass', 'foo', ..._npmShowProperties],
expect.anything()
);
// expect(npmMock.mockFetchJson).toHaveBeenCalledWith(
// '/foo',
// expect.objectContaining({ ...commonOptions, '//fake:_authToken': 'pass' })
// );
});
it('respects password auth args', async () => {
npmMock.setRegistryData({ foo: { 'dist-tags': { latest: '1.0.0', beta: '2.0.0-beta' } } });
const { packages, options } = getOptionsAndPackages({
packages: { foo: {} },
extraArgv: ['--auth-type', 'password', '--token', 'pass'],
});
expect(options).toMatchObject({ authType: 'password', token: 'pass' });
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({ foo: '1.0.0' });
expect(npmMock.mock).toHaveBeenCalledWith(
[...commonArgs, '--//fake:_password=pass', 'foo', ..._npmShowProperties],
expect.anything()
);
// expect(npmMock.mockFetchJson).toHaveBeenCalledWith(
// '/foo',
// expect.objectContaining({ ...commonOptions, '//fake:_password': 'pass' })
// );
});
// This full scenario uses code outside listPackageVersionsByTag, but it's good to cover realistically
it('overrides repo tag with CLI tag', async () => {
npmMock.setRegistryData({
foo: { 'dist-tags': { latest: '1.0.0', alpha: '1.5.0-alpha', beta: '2.0.0-beta' } },
});
const { packages, options } = getOptionsAndPackages({
packages: { foo: {} },
repoOptions: { tag: 'alpha' },
extraArgv: ['--tag', 'beta'],
});
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({ foo: '2.0.0-beta' });
expect(npmMock.mock).toHaveBeenCalledTimes(1);
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(1);
});
it('overrides package tag with CLI tag', async () => {
npmMock.setRegistryData({
foo: { 'dist-tags': { latest: '1.0.0', alpha: '1.5.0-alpha', beta: '2.0.0-beta' } },
});
const { packages, options } = getOptionsAndPackages({
packages: { foo: { beachball: { tag: 'alpha' } } },
extraArgv: ['--tag', 'beta'], // CLI args should take precedence
});
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({ foo: '2.0.0-beta' });
expect(npmMock.mock).toHaveBeenCalledTimes(1);
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(1);
});
it('overrides package tag with CLI tag', async () => {
npmMock.setRegistryData({
foo: { 'dist-tags': { latest: '1.0.0', alpha: '1.5.0-alpha', beta: '2.0.0-beta' } },
bar: { 'dist-tags': { latest: '1.0.0', beta: '3.0.0-beta' } },
});
const { packages, options } = getOptionsAndPackages({
packages: { foo: { beachball: { tag: 'alpha' } }, bar: {} },
extraArgv: ['--tag', 'beta'], // CLI args should take precedence
});
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({ foo: '2.0.0-beta', bar: '3.0.0-beta' });
expect(npmMock.mock).toHaveBeenCalledTimes(2);
// expect(npmMock.mock).not.toHaveBeenCalled();
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(2);
});
it('overrides empty package tag and defaultNpmTag with CLI tag', async () => {
npmMock.setRegistryData({
foo: { 'dist-tags': { latest: '1.0.0', beta: '2.0.0-beta' } },
});
const { packages, options } = getOptionsAndPackages({
packages: { foo: { beachball: { tag: '', defaultNpmTag: '' } } },
extraArgv: ['--tag', 'beta'],
});
const versions = await listPackageVersionsByTag(packages, options);
expect(versions).toEqual({ foo: '2.0.0-beta' });
expect(npmMock.mock).toHaveBeenCalledTimes(1);
// expect(npmMock.mockFetchJson).toHaveBeenCalledTimes(1);
});
});
});
});