2022-07-29 13:51:45 -05:00
|
|
|
import assert from 'assert';
|
2021-11-09 14:38:44 -05:00
|
|
|
|
2022-07-29 13:51:45 -05:00
|
|
|
import { getVersion, sortVersionsAndFilterInvalid, tagVersion } from '../src/index';
|
2021-11-09 14:38:44 -05:00
|
|
|
|
2022-07-29 13:51:45 -05:00
|
|
|
describe('versions-utils', () => {
|
2021-11-09 14:38:44 -05:00
|
|
|
const dist = (version) => ({
|
|
|
|
tarball: `http://registry.org/npm_test/-/npm_test-${version}.tgz`,
|
|
|
|
shasum: `sha1-${version}`,
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getVersion', () => {
|
|
|
|
const metadata = {
|
|
|
|
'1.0.0': { dist: dist('1.0.0') },
|
|
|
|
'1.0.1': { dist: dist('1.0.1') },
|
|
|
|
'0.2.1-1': { dist: dist('0.2.1-1') },
|
|
|
|
'0.2.1-alpha': { dist: dist('0.2.1-alpha') },
|
|
|
|
'0.2.1-alpha.0': { dist: dist('0.2.1-alpha.0') },
|
|
|
|
};
|
|
|
|
|
|
|
|
test('should get the right version', () => {
|
|
|
|
expect(getVersion({ ...metadata } as any, '1.0.0')).toEqual({ dist: dist('1.0.0') });
|
|
|
|
expect(getVersion({ ...metadata } as any, 'v1.0.0')).toEqual({ dist: dist('1.0.0') });
|
|
|
|
expect(getVersion({ ...metadata } as any, 'v0.2.1-1')).toEqual({ dist: dist('0.2.1-1') });
|
|
|
|
expect(getVersion({ ...metadata } as any, '0.2.1-alpha')).toEqual({
|
|
|
|
dist: dist('0.2.1-alpha'),
|
|
|
|
});
|
|
|
|
expect(getVersion({ ...metadata } as any, '0.2.1-alpha.0')).toEqual({
|
|
|
|
dist: dist('0.2.1-alpha.0'),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return nothing on get non existing version', () => {
|
|
|
|
expect(getVersion({ ...metadata } as any, '0')).toBeUndefined();
|
|
|
|
expect(getVersion({ ...metadata } as any, '2.0.0')).toBeUndefined();
|
|
|
|
expect(getVersion({ ...metadata } as any, 'v2.0.0')).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return nothing on get invalid versions', () => {
|
|
|
|
expect(getVersion({ ...metadata } as any, undefined)).toBeUndefined();
|
|
|
|
expect(getVersion({ ...metadata } as any, null)).toBeUndefined();
|
|
|
|
expect(getVersion({ ...metadata } as any, 8)).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should handle no versions', () => {
|
|
|
|
expect(getVersion(undefined, undefined)).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-07-29 13:51:45 -05:00
|
|
|
describe('sortVersionsAndFilterInvalid', () => {
|
2021-11-09 14:38:44 -05:00
|
|
|
test('should sort versions', () => {
|
|
|
|
expect(sortVersionsAndFilterInvalid(['1.0.0', '5.0.0', '2.0.0'])).toEqual([
|
|
|
|
'1.0.0',
|
|
|
|
'2.0.0',
|
|
|
|
'5.0.0',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
test('should sort versions and filter out invalid', () => {
|
|
|
|
expect(sortVersionsAndFilterInvalid(['1.0.0', '5.0.0', '2.0.0', '', null])).toEqual([
|
|
|
|
'1.0.0',
|
|
|
|
'2.0.0',
|
|
|
|
'5.0.0',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-07-29 13:51:45 -05:00
|
|
|
describe('tagVersion', () => {
|
|
|
|
test('add new one', () => {
|
|
|
|
let pkg = {
|
|
|
|
versions: {},
|
|
|
|
'dist-tags': {},
|
|
|
|
};
|
2021-11-09 14:38:44 -05:00
|
|
|
|
2022-07-29 13:51:45 -05:00
|
|
|
// @ts-ignore
|
|
|
|
assert(tagVersion(pkg, '1.1.1', 'foo', {}));
|
|
|
|
assert.deepEqual(pkg, {
|
|
|
|
versions: {},
|
|
|
|
'dist-tags': { foo: '1.1.1' },
|
2021-11-09 14:38:44 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-07-29 13:51:45 -05:00
|
|
|
test('add (compat)', () => {
|
|
|
|
const x = {
|
|
|
|
versions: {},
|
|
|
|
'dist-tags': { foo: '1.1.0' },
|
2021-11-09 14:38:44 -05:00
|
|
|
};
|
|
|
|
|
2022-07-29 13:51:45 -05:00
|
|
|
// @ts-ignore
|
|
|
|
assert(tagVersion(x, '1.1.1', 'foo'));
|
|
|
|
assert.deepEqual(x, {
|
|
|
|
versions: {},
|
|
|
|
'dist-tags': { foo: '1.1.1' },
|
|
|
|
});
|
2021-11-09 14:38:44 -05:00
|
|
|
});
|
|
|
|
|
2022-07-29 13:51:45 -05:00
|
|
|
test('add fresh tag', () => {
|
|
|
|
let x = {
|
|
|
|
versions: {},
|
|
|
|
'dist-tags': { foo: '1.1.0' },
|
2021-11-09 14:38:44 -05:00
|
|
|
};
|
|
|
|
|
2022-07-29 13:51:45 -05:00
|
|
|
// @ts-ignore
|
|
|
|
assert(tagVersion(x, '1.1.1', 'foo'));
|
|
|
|
assert.deepEqual(x, {
|
|
|
|
versions: {},
|
|
|
|
'dist-tags': { foo: '1.1.1' },
|
|
|
|
});
|
2021-11-09 14:38:44 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|