2021-05-19 23:22:59 +00:00
|
|
|
import { strict as assert } from "assert";
|
2020-11-12 23:47:40 +00:00
|
|
|
import { Version } from "../src/version";
|
2020-05-17 22:37:33 +00:00
|
|
|
|
|
|
|
// Each row should be a list of compatible versions.
|
|
|
|
// Earlier entries in a row should be "greater than" later entries in the same row.
|
|
|
|
// Earlier rows should be "greater than" later rows.
|
|
|
|
// Rows should be incompatible with other rows.
|
|
|
|
const testMatrix = [
|
|
|
|
["3.6-pre1", "3.5.4", "3.2.3", "3"],
|
|
|
|
["2.5", "2.2.6", "2.2.4", "2.2.3"],
|
|
|
|
[
|
|
|
|
"1",
|
|
|
|
"1-rc.1",
|
|
|
|
"1-beta.11",
|
|
|
|
"1-beta.2",
|
2020-05-17 23:00:38 +00:00
|
|
|
"1-beta.01",
|
2020-05-17 22:37:33 +00:00
|
|
|
"1-beta",
|
|
|
|
"1-alpha.beta",
|
|
|
|
"1-alpha.1",
|
|
|
|
"1-alpha",
|
|
|
|
],
|
|
|
|
["0.1.2", "0.1.1-dev", "0.1"],
|
|
|
|
["0.0.2", "0.0.2-dev", "0.0.2+build"],
|
|
|
|
["0.0.1", "0.0.1-dev", "0.0.1-5", "0.0.1-2"],
|
|
|
|
];
|
|
|
|
|
2020-11-12 23:47:40 +00:00
|
|
|
function flatten<T>(arr: T[][]): T[] {
|
|
|
|
return arr.reduce((accumulator, value) => accumulator.concat(value), []);
|
2020-05-18 08:04:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 22:37:33 +00:00
|
|
|
describe("Version", function () {
|
|
|
|
describe("#from_semver()", function () {
|
|
|
|
it("should parse valid semver strings", function () {
|
|
|
|
assert.deepEqual(
|
2020-11-13 21:49:38 +00:00
|
|
|
Version.fromSemver("1.2"),
|
2020-11-13 21:44:09 +00:00
|
|
|
new Version(1, 2, 0, null, null)
|
2020-05-17 22:37:33 +00:00
|
|
|
);
|
|
|
|
assert.deepEqual(
|
2020-11-13 21:49:38 +00:00
|
|
|
Version.fromSemver("1.2.3"),
|
2020-11-13 21:44:09 +00:00
|
|
|
new Version(1, 2, 3, null, null)
|
2020-05-17 22:37:33 +00:00
|
|
|
);
|
2020-05-17 23:00:38 +00:00
|
|
|
assert.deepEqual(
|
2020-11-13 21:49:38 +00:00
|
|
|
Version.fromSemver("1.09.3"),
|
2020-11-13 21:44:09 +00:00
|
|
|
new Version(1, 9, 3, null, null)
|
2020-05-17 23:00:38 +00:00
|
|
|
);
|
2020-05-17 22:37:33 +00:00
|
|
|
assert.deepEqual(
|
2020-11-13 21:49:38 +00:00
|
|
|
Version.fromSemver("1.2.3-pr"),
|
2020-11-13 21:44:09 +00:00
|
|
|
new Version(1, 2, 3, ["pr"], null)
|
2020-05-17 22:37:33 +00:00
|
|
|
);
|
|
|
|
assert.deepEqual(
|
2020-11-13 21:49:38 +00:00
|
|
|
Version.fromSemver("1.2.3-pr1.pr2"),
|
2020-11-13 21:44:09 +00:00
|
|
|
new Version(1, 2, 3, ["pr1", "pr2"], null)
|
2020-05-17 22:37:33 +00:00
|
|
|
);
|
|
|
|
assert.deepEqual(
|
2020-11-13 21:49:38 +00:00
|
|
|
Version.fromSemver("1.2.3+build"),
|
2020-11-13 21:44:09 +00:00
|
|
|
new Version(1, 2, 3, null, ["build"])
|
2020-05-17 22:37:33 +00:00
|
|
|
);
|
|
|
|
assert.deepEqual(
|
2020-11-13 21:49:38 +00:00
|
|
|
Version.fromSemver("1.2.3+build1.build2"),
|
2020-11-13 21:44:09 +00:00
|
|
|
new Version(1, 2, 3, null, ["build1", "build2"])
|
2020-05-17 22:37:33 +00:00
|
|
|
);
|
|
|
|
assert.deepEqual(
|
2020-11-13 21:49:38 +00:00
|
|
|
Version.fromSemver("1-pr1.pr2+build1.build2"),
|
2020-05-17 22:37:33 +00:00
|
|
|
new Version(1, 0, 0, ["pr1", "pr2"], ["build1", "build2"])
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#is_compatible_with()", function () {
|
|
|
|
it("is compatible with similar versions", function () {
|
2020-11-12 23:47:40 +00:00
|
|
|
for (const test of testMatrix) {
|
|
|
|
for (const a of test) {
|
|
|
|
for (const b of test) {
|
2021-05-19 23:22:59 +00:00
|
|
|
assert(
|
2020-11-13 21:50:45 +00:00
|
|
|
Version.fromSemver(a).isCompatibleWith(
|
2020-11-13 21:49:38 +00:00
|
|
|
Version.fromSemver(b)
|
2020-05-17 22:37:33 +00:00
|
|
|
),
|
|
|
|
`${a} is compatible with ${b}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it("is not compatible with other versions", function () {
|
2020-11-12 23:47:40 +00:00
|
|
|
for (const test of testMatrix) {
|
|
|
|
for (const a of test) {
|
|
|
|
for (const otherTest of testMatrix) {
|
2023-02-18 12:05:56 +00:00
|
|
|
if (test === otherTest) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-12 23:47:40 +00:00
|
|
|
for (const b of otherTest) {
|
2021-05-19 23:22:59 +00:00
|
|
|
assert(
|
|
|
|
!Version.fromSemver(a).isCompatibleWith(
|
2020-11-13 21:49:38 +00:00
|
|
|
Version.fromSemver(b)
|
2020-05-17 22:37:33 +00:00
|
|
|
),
|
|
|
|
`${a} is not compatible with ${b}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#has_precedence_over()", function () {
|
|
|
|
it("returns true when it should", function () {
|
2020-05-18 08:04:29 +00:00
|
|
|
const tests = flatten(testMatrix);
|
2020-05-17 22:37:33 +00:00
|
|
|
for (let a = 0; a < tests.length; a++) {
|
|
|
|
for (let b = a + 1; b < tests.length; b++) {
|
|
|
|
if (
|
2023-02-20 14:19:18 +00:00
|
|
|
tests[a]!.indexOf("+") > -1 ||
|
|
|
|
tests[b]!.indexOf("+") > -1
|
2020-05-17 22:37:33 +00:00
|
|
|
) {
|
|
|
|
// Skip "builds" for purposes of this test.
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-19 23:22:59 +00:00
|
|
|
assert(
|
2023-02-20 14:19:18 +00:00
|
|
|
Version.fromSemver(tests[a]!).hasPrecedenceOver(
|
|
|
|
Version.fromSemver(tests[b]!)
|
2020-05-17 22:37:33 +00:00
|
|
|
),
|
|
|
|
`${tests[a]} has precedence over ${tests[b]}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it("returns false when it should", function () {
|
2020-05-18 08:04:29 +00:00
|
|
|
const tests = flatten(testMatrix).reverse();
|
2020-05-17 22:37:33 +00:00
|
|
|
for (let a = 0; a < tests.length; a++) {
|
|
|
|
for (let b = a + 1; b < tests.length; b++) {
|
|
|
|
if (
|
2023-02-20 14:19:18 +00:00
|
|
|
tests[a]!.indexOf("+") > -1 ||
|
|
|
|
tests[b]!.indexOf("+") > -1
|
2020-05-17 22:37:33 +00:00
|
|
|
) {
|
|
|
|
// Skip "builds" for purposes of this test.
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-19 23:22:59 +00:00
|
|
|
assert(
|
2023-02-20 14:19:18 +00:00
|
|
|
!Version.fromSemver(tests[a]!).hasPrecedenceOver(
|
|
|
|
Version.fromSemver(tests[b]!)
|
2020-05-17 22:37:33 +00:00
|
|
|
),
|
|
|
|
`${tests[a]} doesn't have precedence over ${tests[b]}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#is_equal()", function () {
|
|
|
|
it("returns true when it should", function () {
|
2020-05-18 08:04:29 +00:00
|
|
|
const tests = flatten(testMatrix);
|
2020-11-12 23:47:40 +00:00
|
|
|
for (const version of tests) {
|
2021-05-19 23:22:59 +00:00
|
|
|
assert(
|
2020-11-13 21:51:53 +00:00
|
|
|
Version.fromSemver(version).isEqual(
|
2020-11-13 21:49:38 +00:00
|
|
|
Version.fromSemver(version)
|
2020-05-17 22:37:33 +00:00
|
|
|
),
|
|
|
|
`${version} is equal to itself`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it("returns false when it should", function () {
|
2020-05-18 08:04:29 +00:00
|
|
|
const tests = flatten(testMatrix).reverse();
|
2020-05-17 22:37:33 +00:00
|
|
|
for (let a = 0; a < tests.length; a++) {
|
|
|
|
for (let b = a + 1; b < tests.length; b++) {
|
|
|
|
if (
|
2023-02-20 14:19:18 +00:00
|
|
|
tests[a]!.indexOf("+") > -1 ||
|
|
|
|
tests[b]!.indexOf("+") > -1 ||
|
|
|
|
tests[a]!.indexOf("-") > -1 ||
|
|
|
|
tests[b]!.indexOf("-") > -1
|
2020-05-17 22:37:33 +00:00
|
|
|
) {
|
|
|
|
// Skip "builds" and "identifiers" for purposes of this test.
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-19 23:22:59 +00:00
|
|
|
assert(
|
2023-02-20 14:19:18 +00:00
|
|
|
!Version.fromSemver(tests[a]!).isEqual(
|
|
|
|
Version.fromSemver(tests[b]!)
|
2020-05-17 22:37:33 +00:00
|
|
|
),
|
|
|
|
`${tests[a]} does not equal ${tests[b]}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#is_stable_or_compatible_prerelease()", function () {
|
|
|
|
it("returns true for own versions", function () {
|
2020-05-18 08:04:29 +00:00
|
|
|
const tests = flatten(testMatrix);
|
2020-11-12 23:47:40 +00:00
|
|
|
for (const version of tests) {
|
2021-05-19 23:22:59 +00:00
|
|
|
assert(
|
2020-11-13 21:52:30 +00:00
|
|
|
Version.fromSemver(version).isStableOrCompatiblePrerelease(
|
2020-11-13 21:49:38 +00:00
|
|
|
Version.fromSemver(version)
|
2020-05-17 22:37:33 +00:00
|
|
|
),
|
|
|
|
`${version} is compatible with itself`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it("returns true for compatible pre-releases", function () {
|
|
|
|
const tests = ["1.2.3", "1.2.3-alpha", "1.2.3-beta1.build2"];
|
2020-11-12 23:47:40 +00:00
|
|
|
for (const a of tests) {
|
|
|
|
for (const b of tests) {
|
2021-05-19 23:22:59 +00:00
|
|
|
assert(
|
2020-11-13 21:52:30 +00:00
|
|
|
Version.fromSemver(a).isStableOrCompatiblePrerelease(
|
2020-11-13 21:49:38 +00:00
|
|
|
Version.fromSemver(b)
|
2020-05-17 22:37:33 +00:00
|
|
|
),
|
|
|
|
`${a} is compatible with ${b}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it("returns false for incompatible pre-releases", function () {
|
|
|
|
const tests = ["1-dev", "1.2-alpha", "1.2.3-beta1.build2"];
|
2020-11-12 23:47:40 +00:00
|
|
|
for (const a of tests) {
|
|
|
|
for (const b of tests) {
|
2023-02-18 12:05:56 +00:00
|
|
|
if (a === b) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-19 23:22:59 +00:00
|
|
|
assert(
|
|
|
|
!Version.fromSemver(a).isStableOrCompatiblePrerelease(
|
2020-11-13 21:49:38 +00:00
|
|
|
Version.fromSemver(b)
|
2020-05-17 22:37:33 +00:00
|
|
|
),
|
|
|
|
`${a} is not compatible with ${b}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|