Skip to content

Commit

Permalink
fix(core): improve handling of non-semver versions
Browse files Browse the repository at this point in the history
Refs #14
  • Loading branch information
JamieMason committed Jan 7, 2019
1 parent 68f9401 commit 9e1176a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 19 deletions.
80 changes: 63 additions & 17 deletions src/lib/version.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@ import { getNewest, getVersionRange, sortBySemver } from './version';
describe('getNewest', () => {
it('returns the newest version from an array of versions', () => {
const a = ['<1.0.0'];
const b = _.shuffle(a.concat('<=1.0.0'));
const c = _.shuffle(b.concat('1.0.0'));
const d = _.shuffle(c.concat('~1.0.0'));
const e = _.shuffle(d.concat('1.x.x'));
const f = _.shuffle(e.concat('^1.0.0'));
const g = _.shuffle(f.concat('>=1.0.0'));
const h = _.shuffle(g.concat('>1.0.0'));
const i = _.shuffle(h.concat('*'));
const b = _.shuffle([...a, '<=1.0.0']);
const c = _.shuffle([...b, '1.0.0']);
const d = _.shuffle([...c, '~1.0.0']);
const e = _.shuffle([...d, '1.x.x']);
const f = _.shuffle([...e, '^1.0.0']);
const g = _.shuffle([...f, '>=1.0.0']);
const h = _.shuffle([...g, '>1.0.0']);
const i = _.shuffle([...h, '*']);
const j = _.shuffle([...i, 'http://asdf.com/asdf.tar.gz']);
const k = _.shuffle([...j, 'file:../foo/bar']);
const l = _.shuffle([...k, 'latest']);
const m = _.shuffle([...l, 'git+ssh://git@github.com:npm/cli.git#v1.0.27']);
const n = _.shuffle([...m, 'git+ssh://git@github.com:npm/cli#semver:^5.0']);
const o = _.shuffle([...n, 'git+https://isaacs@github.com/npm/cli.git']);
const p = _.shuffle([...o, 'git://github.com/npm/cli.git#v1.0.27']);
const q = _.shuffle([...p, 'expressjs/express']);
const r = _.shuffle([...q, 'mochajs/mocha#4727d357ea']);
const s = _.shuffle([...r, 'user/repo#feature/branch']);
// valid semver
expect(getNewest(a)).toEqual('<1.0.0');
expect(getNewest(b)).toEqual('<=1.0.0');
expect(getNewest(c)).toEqual('1.0.0');
Expand All @@ -21,27 +32,62 @@ describe('getNewest', () => {
expect(getNewest(g)).toEqual('>=1.0.0');
expect(getNewest(h)).toEqual('>1.0.0');
expect(getNewest(i)).toEqual('*');
// invalid semver
expect(getNewest(j)).toEqual('*');
expect(getNewest(k)).toEqual('*');
expect(getNewest(l)).toEqual('*');
expect(getNewest(m)).toEqual('*');
expect(getNewest(n)).toEqual('*');
expect(getNewest(o)).toEqual('*');
expect(getNewest(p)).toEqual('*');
expect(getNewest(q)).toEqual('*');
expect(getNewest(r)).toEqual('*');
expect(getNewest(s)).toEqual('*');
});
});

describe('getVersionRange', () => {
it('gets the version range from a semver dependency', () => {
expect(getVersionRange('*')).toEqual('*');
expect(getVersionRange('>1.0.0')).toEqual('>');
expect(getVersionRange('>=1.0.0')).toEqual('>=');
expect(getVersionRange('^1.0.0')).toEqual('^');
expect(getVersionRange('~1.0.0')).toEqual('~');
expect(getVersionRange('1.x.x')).toEqual('.x');
expect(getVersionRange('1.0.0')).toEqual('');
expect(getVersionRange('<=1.0.0')).toEqual('<=');
expect(getVersionRange('<1.0.0')).toEqual('<');
[
['*', '*'],
['>1.0.0', '>'],
['>=1.0.0', '>='],
['^1.0.0', '^'],
['~1.0.0', '~'],
['1.x.x', '.x'],
['1.0.0', ''],
['<=1.0.0', '<='],
['<1.0.0', '<'],
['http://asdf.com/asdf.tar.gz', ''],
['file:../foo/bar', ''],
['latest', ''],
['git+ssh://git@github.com:npm/cli.git#v1.0.27', ''],
['git+ssh://git@github.com:npm/cli#semver:^5.0', ''],
['git+https://isaacs@github.com/npm/cli.git', ''],
['git://github.com/npm/cli.git#v1.0.27', ''],
['expressjs/express"', ''],
['mochajs/mocha#4727d357ea"', ''],
['user/repo#feature/branch', '']
].forEach(([version, expected]) => {
expect(getVersionRange(version)).toEqual(expected);
});
});
});

describe('sortBySemver', () => {
it('returns an ordered array of semver versions', () => {
_.times(10, () => {
const unordered = [
'http://asdf.com/asdf.tar.gz',
'file:../foo/bar',
'latest',
'git+ssh://git@github.com:npm/cli.git#v1.0.27',
'git+ssh://git@github.com:npm/cli#semver:^5.0',
'git+https://isaacs@github.com/npm/cli.git',
'git://github.com/npm/cli.git#v1.0.27',
'expressjs/express',
'mochajs/mocha#4727d357ea',
'user/repo#feature/branch',
'^1.0.0',
'^1.0.1',
'^1.0.0',
Expand Down
13 changes: 11 additions & 2 deletions src/lib/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import _ = require('lodash');
import semver = require('semver');
import { GREATER, LESSER, SAME, SEMVER_ORDER } from '../constants';

const isSemver = (version: string) =>
version === '*' ||
version.search(/^(<|<=|~|\^|>=|>)?([0-9]+|x)\.([0-9]+|x)\.([0-9]+|x)/) !== -1;

export const getNewest = (versions: string[]): string => {
const sorted = sortBySemver(versions);
return sorted[sorted.length - 1];
Expand All @@ -10,12 +14,17 @@ export const getNewest = (versions: string[]): string => {
export const getVersionNumber = (version: string): string =>
version.slice(version.search(/[0-9]/), version.length);

export const getVersionRange = (version: string): string =>
version.includes('.x') ? '.x' : version.split(/[0-9]/)[0];
export const getVersionRange = (version: string): string => {
if (isSemver(version)) {
return version.includes('.x') ? '.x' : version.split(/[0-9]/)[0];
}
return '';
};

export const sortBySemver = (versions: string[]): string[] =>
versions
.concat()
.filter(isSemver)
.sort()
.sort((a: string, b: string) => {
if (a === '*') {
Expand Down

0 comments on commit 9e1176a

Please sign in to comment.