Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some problems comparing pre release versions #405

Merged
merged 1 commit into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/js_tests/wirecloud/VersionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,22 @@
}).toThrowError(TypeError);
});

it("should throw a TypeError if the passed version is not valid", function () {
expect(function () {
new Wirecloud.Version('invalid version');
}).toThrowError(TypeError);
describe("should throw a TypeError if the passed version is not valid", function () {
const test = (version) => {
it(version, () => {
expect(() => {
new Wirecloud.Version(version);
}).toThrowError(TypeError);
});
};

test('invalid version');
test('1abc');
test('1.01');
test('1.0a01');
test('1.0b01');
test('1.0rc01');
test('1.0rcc1');
});

});
Expand All @@ -65,7 +77,11 @@

it("(1.0 < 1.0.1)", test.bind(null, new Wirecloud.Version("1.0"), new Wirecloud.Version("1.0.1")));
it("(1.0a1 < 1.0)", test.bind(null, new Wirecloud.Version("1.0a1"), new Wirecloud.Version("1.0")));
it("(1.0a2 < 1.0a10)", test.bind(null, new Wirecloud.Version("1.0a2"), new Wirecloud.Version("1.0a10")));
it("(1.0a2 < 1.0b1)", test.bind(null, new Wirecloud.Version("1.0a2"), new Wirecloud.Version("1.0b1")));
it("(1.0b2 < 1.0b10)", test.bind(null, new Wirecloud.Version("1.0b2"), new Wirecloud.Version("1.0b10")));
it("(1.0b9 < 1.0rc1)", test.bind(null, new Wirecloud.Version("1.0b9"), new Wirecloud.Version("1.0rc1")));
it("(1.0rc2 < 1.0rc10)", test.bind(null, new Wirecloud.Version("1.0rc2"), new Wirecloud.Version("1.0rc10")));
it("(1.0rc9 < 1.0-dev)", test.bind(null, new Wirecloud.Version("1.0rc9"), new Wirecloud.Version("1.0-dev")));
it("(1.0-dev < 1.0)", test.bind(null, new Wirecloud.Version("1.0-dev"), new Wirecloud.Version("1.0")));
});
Expand All @@ -78,6 +94,8 @@
it("(1.0 == 1.0.0)", test.bind(null, new Wirecloud.Version("1.0"), new Wirecloud.Version("1.0.0")));
it("(1.0.0 == 1.0)", test.bind(null, new Wirecloud.Version("1.0.0"), new Wirecloud.Version("1.0")));
it("(1.0a1 == 1.0.0a1)", test.bind(null, new Wirecloud.Version("1.0a1"), new Wirecloud.Version("1.0.0a1")));
it("(1.0b1 == 1.0.0b1)", test.bind(null, new Wirecloud.Version("1.0b1"), new Wirecloud.Version("1.0.0b1")));
it("(1.0rc1 == 1.0.0rc1)", test.bind(null, new Wirecloud.Version("1.0rc1"), new Wirecloud.Version("1.0.0rc1")));
it("(1.0-dev == 1.0.0-dev)", test.bind(null, new Wirecloud.Version("1.0-dev"), new Wirecloud.Version("1.0.0-dev")));
it("(1.0-deva == 1.0.0-deva)", test.bind(null, new Wirecloud.Version("1.0-deva"), new Wirecloud.Version("1.0.0-deva")));
});
Expand All @@ -88,8 +106,11 @@
};

it("(1.0 > 0.9)", test.bind(null, new Wirecloud.Version("1.0"), new Wirecloud.Version("0.9")));
it("(1.0a10 > 1.0a9)", test.bind(null, new Wirecloud.Version("1.0a10"), new Wirecloud.Version("1.0a9")));
it("(1.0b1 > 1.0a9)", test.bind(null, new Wirecloud.Version("1.0b1"), new Wirecloud.Version("1.0a9")));
it("(1.0b10 > 1.0b9)", test.bind(null, new Wirecloud.Version("1.0b10"), new Wirecloud.Version("1.0b9")));
it("(1.0rc1 > 1.0b9)", test.bind(null, new Wirecloud.Version("1.0rc1"), new Wirecloud.Version("1.0b9")));
it("(1.0rc10 > 1.0rc9)", test.bind(null, new Wirecloud.Version("1.0rc10"), new Wirecloud.Version("1.0rc9")));
it("(1.0 > 1.0rc9)", test.bind(null, new Wirecloud.Version("1.0"), new Wirecloud.Version("1.0rc9")));
it("(1.0rc1 > 1.0rc1-dev)", test.bind(null, new Wirecloud.Version("1.0rc1"), new Wirecloud.Version("1.0rc1-dev")));
it("(1.0-dev > 1.0b9)", test.bind(null, new Wirecloud.Version("1.0-dev"), new Wirecloud.Version("1.0b9")));
Expand Down
30 changes: 20 additions & 10 deletions src/wirecloud/platform/static/js/wirecloud/Version.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

"use strict";

var VERSION_RE = /^((?:[1-9]\d*\.|0\.)*(?:[1-9]\d*|0))((?:a|b|rc)[1-9]\d*)?(-dev(.+)?)?$/; // Hangs if it doesn't match the RE
var VERSION_RE = /^((?:[1-9]\d*\.|0\.)*(?:[1-9]\d*|0))((a|b|rc)([1-9]\d*))?(-dev(.+)?)?$/; // Hangs if it doesn't match the RE

/**
* Creates a Version object. This kind of instance allows you to:
Expand All @@ -48,7 +48,7 @@
throw new TypeError(utils.interpolate(msg, {version: version}));
}

this.array = groups[1].split('.').map(function (x) { return parseInt(x, 10); });
this.array = groups[1].split('.').map((x) => {return parseInt(x, 10);});
/**
* Pre-version part of the version, `null` if this version has not a
* pre-version part.
Expand All @@ -59,13 +59,23 @@
* @type {String}
*/
this.pre_version = groups[2] != null ? groups[2] : null;
/**
* Pre-version part of the version in array format, `null` if this
* version has not a pre-version part.
*
* **Example**: ["a", 1]
*
* @name Wirecloud.Version#pre_version_array
* @type {Array}
*/
this.pre_version_array = groups[2] != null ? [groups[3], parseInt(groups[4], 10)] : null;
/**
* Indicates if this version is a development version
*
* @name Wirecloud.Version#dev
* @type {Boolean}
*/
this.dev = groups[3] != null;
this.dev = groups[5] != null;
/**
* Dev user part of the version, `null` if this version has not a
* dev user part.
Expand All @@ -75,7 +85,7 @@
* @name Wirecloud.Version#devtext
* @type {String}
*/
this.devtext = groups[4];
this.devtext = groups[6];
/**
* String representing this version.
*
Expand Down Expand Up @@ -129,18 +139,18 @@
}
}

pre_version1 = this.pre_version;
pre_version1 = this.pre_version_array;
if (pre_version1 == null) {
pre_version1 = "z";
pre_version1 = ["z", 1];
}
pre_version2 = version.pre_version;
pre_version2 = version.pre_version_array;
if (pre_version2 == null) {
pre_version2 = "z";
pre_version2 = ["z", 1];
}

if (pre_version1 < pre_version2) {
if (pre_version1[0] < pre_version2[0] || (pre_version1[0] === pre_version2[0]) && pre_version1[1] < pre_version2[1]) {
return -1;
} else if (pre_version1 > pre_version2) {
} else if (pre_version1[0] > pre_version2[0] || (pre_version1[0] === pre_version2[0]) && pre_version1[1] > pre_version2[1]) {
return 1;
} else {
if (this.dev === version.dev) {
Expand Down