parent
c729a5b7d2
commit
e9e281093b
|
@ -1,128 +0,0 @@
|
|||
var _ = require('lodash');
|
||||
var versionmath = require('ui/utils/versionmath');
|
||||
var expect = require('expect.js');
|
||||
var versions = [
|
||||
'1.1.12',
|
||||
'1.1.12',
|
||||
'1.1.12',
|
||||
'1.1.12',
|
||||
'0.90.0',
|
||||
'0.90.1',
|
||||
'1.0.0',
|
||||
'1.0',
|
||||
'1.2.3',
|
||||
'2.0.0',
|
||||
'2.0.1',
|
||||
'2.3.1'
|
||||
];
|
||||
|
||||
describe('version math (0.90.0 - 2.3.1)', function () {
|
||||
var methods = 'max,min,eq,is,lt,lte,gt,gte'.split(',');
|
||||
describe('methods', function () {
|
||||
it('should have ' + methods.join(', ') + ' methods', function () {
|
||||
_.each(methods, function (method) {
|
||||
expect(versionmath[method]).to.be.a(Function);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('min & max', function () {
|
||||
it('has a max of 2.3.1', function () {
|
||||
expect(versionmath.max(versions)).to.be('2.3.1');
|
||||
});
|
||||
|
||||
it('has a min of 0.90.0', function () {
|
||||
expect(versionmath.min(versions)).to.be('0.90.0');
|
||||
});
|
||||
});
|
||||
|
||||
describe('eq / lowest version', function () {
|
||||
it('should be true for 0.90.0', function () {
|
||||
expect(versionmath.eq('0.90.0', versions)).to.be(true);
|
||||
});
|
||||
|
||||
it('should be false for 1.0', function () {
|
||||
expect(versionmath.eq('1.0', versions)).to.be(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('gt / lowest version', function () {
|
||||
it('is > 0.20.3', function () {
|
||||
expect(versionmath.gt('0.20.3', versions)).to.be(true);
|
||||
});
|
||||
|
||||
it('is not > 0.90.0', function () {
|
||||
expect(versionmath.gt('0.90.0', versions)).to.be(false);
|
||||
});
|
||||
|
||||
it('is not > 1.0.0', function () {
|
||||
expect(versionmath.gt('1.0.0', versions)).to.be(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('gte / lowest version', function () {
|
||||
it('is >= 0.20.3', function () {
|
||||
expect(versionmath.gte('0.20.3', versions)).to.be(true);
|
||||
});
|
||||
|
||||
it('is >= 0.90.0', function () {
|
||||
expect(versionmath.gte('0.90.0', versions)).to.be(true);
|
||||
});
|
||||
|
||||
it('is not >= 1.0.0', function () {
|
||||
expect(versionmath.gte('1.0.0', versions)).to.be(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('lt / highest version', function () {
|
||||
it('is not < 0.20.3', function () {
|
||||
expect(versionmath.lt('0.20.3', versions)).to.be(false);
|
||||
});
|
||||
|
||||
it('is not < 2.3.1', function () {
|
||||
expect(versionmath.lt('2.3.1', versions)).to.be(false);
|
||||
});
|
||||
|
||||
it('is < 2.5', function () {
|
||||
expect(versionmath.lt('2.5', versions)).to.be(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('lte / highest version', function () {
|
||||
it('is not =< 0.20.3', function () {
|
||||
expect(versionmath.lte('0.20.3', versions)).to.be(false);
|
||||
});
|
||||
|
||||
it('is =< 2.3.1', function () {
|
||||
expect(versionmath.lte('2.3.1', versions)).to.be(true);
|
||||
});
|
||||
|
||||
it('is =< 2.5', function () {
|
||||
expect(versionmath.lte('2.5', versions)).to.be(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('is', function () {
|
||||
it('exactly, <, <=, >, >=', function () {
|
||||
expect(versionmath.is('0.90.0', versions)).to.be(true);
|
||||
expect(versionmath.is('0.20.0', versions)).to.be(false);
|
||||
|
||||
expect(versionmath.is('>0.20.0', versions)).to.be(true);
|
||||
expect(versionmath.is('>0.90.0', versions)).to.be(false);
|
||||
expect(versionmath.is('>0.90.1', versions)).to.be(false);
|
||||
|
||||
expect(versionmath.is('>=0.20.0', versions)).to.be(true);
|
||||
expect(versionmath.is('>=0.90.0', versions)).to.be(true);
|
||||
expect(versionmath.is('>=0.90.1', versions)).to.be(false);
|
||||
|
||||
expect(versionmath.is('<2.5', versions)).to.be(true);
|
||||
expect(versionmath.is('<2.3.1', versions)).to.be(false);
|
||||
expect(versionmath.is('<0.90.1', versions)).to.be(false);
|
||||
|
||||
expect(versionmath.is('<=2.5', versions)).to.be(true);
|
||||
expect(versionmath.is('<=2.3.1', versions)).to.be(true);
|
||||
expect(versionmath.is('<=0.90.1', versions)).to.be(false);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
|
@ -1,139 +0,0 @@
|
|||
define(function (require) {
|
||||
var _ = require('lodash');
|
||||
|
||||
function VersionMathException(message) {
|
||||
this.message = message;
|
||||
this.name = 'VersionMathException';
|
||||
}
|
||||
|
||||
// Get the max version in this cluster
|
||||
function max(versions) {
|
||||
return sortVersions(versions).pop();
|
||||
}
|
||||
|
||||
// Return the lowest version in the cluster
|
||||
function min(versions) {
|
||||
return sortVersions(versions).shift();
|
||||
}
|
||||
|
||||
// Sort versions from lowest to highest
|
||||
function sortVersions(versions) {
|
||||
if (!_.isArray(versions)) versions = [versions];
|
||||
|
||||
return _.uniq(versions).sort(function (a, b) {
|
||||
return compare(a, b) ? -1 : 1;
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
Takes a version string with one of the following optional comparison prefixes: >,>=,<.<=
|
||||
and evaluates if the cluster meets the requirement. If the prefix is omitted exact match
|
||||
is assumed
|
||||
*/
|
||||
function is(equation, versions) {
|
||||
var _versions = sortVersions(versions);
|
||||
var _v = equation;
|
||||
var _cf;
|
||||
|
||||
if (_v.charAt(0) === '>') {
|
||||
_cf = _v.charAt(1) === '=' ? gte(_v.slice(2), _versions) : gt(_v.slice(1), _versions);
|
||||
} else if (_v.charAt(0) === '<') {
|
||||
_cf = _v.charAt(1) === '=' ? lte(_v.slice(2), _versions) : lt(_v.slice(1), _versions);
|
||||
} else {
|
||||
_cf = eq(_v, _versions);
|
||||
}
|
||||
|
||||
return _cf;
|
||||
}
|
||||
|
||||
// check if lowest version in cluster = `version`
|
||||
function eq(version, versions) {
|
||||
var _versions = sortVersions(versions);
|
||||
return version === min(_versions) ? true : false;
|
||||
}
|
||||
|
||||
// version > lowest version in cluster?
|
||||
function gt(version, versions) {
|
||||
var _versions = sortVersions(versions);
|
||||
return version === min(_versions) ? false : gte(version, _versions);
|
||||
}
|
||||
|
||||
// version < highest version in cluster?
|
||||
function lt(version, versions) {
|
||||
var _versions = sortVersions(versions);
|
||||
return version === max(_versions) ? false : lte(version, _versions);
|
||||
}
|
||||
|
||||
// Check if the lowest version in the cluster is >= to `version`
|
||||
function gte(version, versions) {
|
||||
var _versions = sortVersions(versions);
|
||||
return compare(version, min(_versions));
|
||||
}
|
||||
|
||||
// Check if the highest version in the cluster is <= to `version`
|
||||
function lte(version, versions) {
|
||||
var _versions = sortVersions(versions);
|
||||
return compare(max(_versions), version);
|
||||
}
|
||||
|
||||
// Determine if a specific version meets the minimum requirement
|
||||
function compare(required, installed) {
|
||||
if (_.isUndefined(installed)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!required || !installed) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
var a = installed.split('.');
|
||||
var b = required.split('.');
|
||||
var i;
|
||||
|
||||
// leave suffixes as is ("RC1 or -SNAPSHOT")
|
||||
for (i = 0; i < Math.min(a.length, 3); ++i) {
|
||||
a[i] = Number(a[i]);
|
||||
}
|
||||
for (i = 0; i < Math.min(b.length, 3); ++i) {
|
||||
b[i] = Number(b[i]);
|
||||
}
|
||||
if (a.length === 2) {
|
||||
a[2] = 0;
|
||||
}
|
||||
|
||||
if (a[0] > b[0]) { return true; }
|
||||
if (a[0] < b[0]) { return false; }
|
||||
|
||||
if (a[1] > b[1]) { return true; }
|
||||
if (a[1] < b[1]) { return false; }
|
||||
|
||||
if (a[2] > b[2]) { return true; }
|
||||
if (a[2] < b[2]) { return false; }
|
||||
|
||||
if (a.length > 3) {
|
||||
// rc/beta suffix
|
||||
if (b.length <= 3) {
|
||||
return false;
|
||||
} // no suffix on b -> a<b
|
||||
return a[3] >= b[3];
|
||||
}
|
||||
if (b.length > 3) {
|
||||
// b has a suffix but a not -> a>b
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
min: min,
|
||||
max: max,
|
||||
is: is,
|
||||
eq: eq,
|
||||
gt: gt,
|
||||
gte: gte,
|
||||
lt: lt,
|
||||
lte: lte
|
||||
};
|
||||
});
|
Loading…
Reference in a new issue