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

deprecation(semver): deprecate SemVerRange, introduce Range #4161

Merged
merged 16 commits into from
Jan 14, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add test cases
  • Loading branch information
timreichen committed Jan 12, 2024
commit 3a0aa6d15a367e5971dd88090f889e999b2f3896
65 changes: 64 additions & 1 deletion semver/comparator_intersects_test.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import { parseComparator } from "./parse_comparator.ts";
import { comparatorIntersects } from "./comparator_intersects.ts";
import { rangeIntersects } from "./range_intersects.ts";

Deno.test("intersect", async (t) => {
Deno.test("comparatorIntersects() handles deprecated SemVerRange.ranges property", async (t) => {
const versions: [string, string, boolean][] = [
// One is a Version
["1.3.0", ">=1.3.0", true],
@@ -67,3 +67,66 @@ Deno.test("intersect", async (t) => {
});
}
});

Deno.test("comparatorIntersects()", async (t) => {
const versions: [string, string, boolean][] = [
// One is a Version
["1.3.0", ">=1.3.0", true],
["1.3.0", ">1.3.0", false],
[">=1.3.0", "1.3.0", true],
[">1.3.0", "1.3.0", false],

// Same direction increasing
[">1.3.0", ">1.2.0", true],
[">1.2.0", ">1.3.0", true],
[">=1.2.0", ">1.3.0", true],
[">1.2.0", ">=1.3.0", true],

// Same direction decreasing
["<1.3.0", "<1.2.0", true],
["<1.2.0", "<1.3.0", true],
["<=1.2.0", "<1.3.0", true],
["<1.2.0", "<=1.3.0", true],

// Different directions, same semver and inclusive operator
[">=1.3.0", "<=1.3.0", true],
[">=v1.3.0", "<=1.3.0", true],
[">=1.3.0", ">=1.3.0", true],
["<=1.3.0", "<=1.3.0", true],
["<=1.3.0", "<=v1.3.0", true],
[">1.3.0", "<=1.3.0", false],
[">=1.3.0", "<1.3.0", false],

// Opposite matching directions
[">1.0.0", "<2.0.0", true],
[">=1.0.0", "<2.0.0", true],
[">=1.0.0", "<=2.0.0", true],
[">1.0.0", "<=2.0.0", true],
["<=2.0.0", ">1.0.0", true],
["<=1.0.0", ">=2.0.0", false],
];
for (const v of versions) {
const comparator1 = parseComparator(v[0]);
const comparator2 = parseComparator(v[1]);
const expect = v[2];
await t.step({
name: `${v[0]} ${expect ? "∩" : "∁"} ${v[1]}`,
fn: () => {
const actual1 = comparatorIntersects(comparator1, comparator2);
const actual2 = comparatorIntersects(comparator2, comparator1);
const actual3 = rangeIntersects(
[[comparator1]],
[[comparator2]],
);
const actual4 = rangeIntersects(
[[comparator2]],
[[comparator1]],
);
assertEquals(actual1, expect);
assertEquals(actual2, expect);
assertEquals(actual3, expect);
assertEquals(actual4, expect);
},
});
}
});
21 changes: 20 additions & 1 deletion semver/comparator_test.ts
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import { parse } from "./parse.ts";
import { testRange } from "./test_range.ts";
import { parseComparator } from "./parse_comparator.ts";
import { comparatorFormat } from "./comparator_format.ts";
import { Comparator } from "https://deno.land/std@$STD_VERSION/semver/types.ts";

Deno.test({
name: "comparators",
@@ -161,7 +162,7 @@ Deno.test({
},
});

Deno.test("tostrings", function () {
Deno.test("comparatorFormat() handles semver inheritance", function () {
assertEquals(
comparatorFormat(parseComparator(">= v1.2.3")),
">=1.2.3",
@@ -171,3 +172,21 @@ Deno.test("tostrings", function () {
">=1.2.3-pre.1+b.2",
);
});

Deno.test("comparatorFormat() handles deprecated Comparator.semver property", function () {
const c1 = parseComparator(">= v1.2.3");
assertEquals(
comparatorFormat(
{ operator: c1.operator, semver: c1.semver } as Comparator,
),
">=1.2.3",
);
const c2 = parseComparator(">= v1.2.3-pre.1+b.2");

assertEquals(
comparatorFormat(
{ operator: c2.operator, semver: c2.semver } as Comparator,
),
">=1.2.3-pre.1+b.2",
);
});
68 changes: 68 additions & 0 deletions semver/format_range_test.ts
Original file line number Diff line number Diff line change
@@ -71,3 +71,71 @@ Deno.test({
}
},
});

Deno.test({
name: "formatRange() handles deprecated SemVerRange.ranges property",
fn: async (t) => {
const versions: [string, string][] = [
["1.0.0 - 2.0.0", ">=1.0.0 <=2.0.0"],
["1.0.0", "1.0.0"],
[">=*", "*"],
["", "*"],
["*", "*"],
[">=1.0.0", ">=1.0.0"],
[">1.0.0", ">1.0.0"],
["<=2.0.0", "<=2.0.0"],
["1", ">=1.0.0 <2.0.0"],
["<=2.0.0", "<=2.0.0"],
["<=2.0.0", "<=2.0.0"],
["<2.0.0", "<2.0.0"],
["<2.0.0", "<2.0.0"],
[">=0.1.97", ">=0.1.97"],
[">=0.1.97", ">=0.1.97"],
["0.1.20 || 1.2.4", "0.1.20||1.2.4"],
[">=0.2.3 || <0.0.1", ">=0.2.3||<0.0.1"],
[">=0.2.3 || <0.0.1", ">=0.2.3||<0.0.1"],
[">=0.2.3 || <0.0.1", ">=0.2.3||<0.0.1"],
["||", "*||*"],
["2.x.x", ">=2.0.0 <3.0.0"],
["1.2.x", ">=1.2.0 <1.3.0"],
["1.2.x || 2.x", ">=1.2.0 <1.3.0||>=2.0.0 <3.0.0"],
["1.2.x || 2.x", ">=1.2.0 <1.3.0||>=2.0.0 <3.0.0"],
["x", "*"],
["2.*.*", ">=2.0.0 <3.0.0"],
["1.2.*", ">=1.2.0 <1.3.0"],
["1.2.* || 2.*", ">=1.2.0 <1.3.0||>=2.0.0 <3.0.0"],
["2", ">=2.0.0 <3.0.0"],
["2.3", ">=2.3.0 <2.4.0"],
["~2.4", ">=2.4.0 <2.5.0"],
["~2.4", ">=2.4.0 <2.5.0"],
["~>3.2.1", ">=3.2.1 <3.3.0"],
["~1", ">=1.0.0 <2.0.0"],
["~>1", ">=1.0.0 <2.0.0"],
["~1.0", ">=1.0.0 <1.1.0"],
["^0", ">=0.0.0 <1.0.0"],
["^0.1", ">=0.1.0 <0.2.0"],
["^1.0", ">=1.0.0 <2.0.0"],
["^1.2", ">=1.2.0 <2.0.0"],
["^0.0.1", ">=0.0.1 <0.0.2"],
["^0.0.1-beta", ">=0.0.1-beta <0.0.2"],
["^0.1.2", ">=0.1.2 <0.2.0"],
["^1.2.3", ">=1.2.3 <2.0.0"],
["^1.2.3-beta.4", ">=1.2.3-beta.4 <2.0.0"],
["<1", "<1.0.0"],
[">=1", ">=1.0.0"],
["<1.2", "<1.2.0"],
["1", ">=1.0.0 <2.0.0"],
];

for (const [r, expected] of versions) {
await t.step({
name: `${r} -> ${expected}`,
fn: () => {
const range = parseRange(r);
const actual = formatRange({ ranges: range.ranges });
assertEquals(actual, expected);
},
});
}
},
});
89 changes: 88 additions & 1 deletion semver/range_intersects_test.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import { parseRange } from "./parse_range.ts";
import { rangeIntersects } from "./range_intersects.ts";

Deno.test({
name: "rangesIntersect",
name: "rangesIntersects()",
fn: async (t) => {
const versions: [string, string, boolean][] = [
["1.3.0 || <1.0.0 >2.0.0", "1.3.0 || <1.0.0 >2.0.0", true],
@@ -86,3 +86,90 @@ Deno.test({
}
},
});

Deno.test({
name: "rangesIntersects() handles deprecated SemVerRange.ranges property",
fn: async (t) => {
const versions: [string, string, boolean][] = [
["1.3.0 || <1.0.0 >2.0.0", "1.3.0 || <1.0.0 >2.0.0", true],
[">0.0.0", "<1.0.0 >2.0.0", false],
["<1.0.0 >2.0.0", ">1.4.0 <1.6.0", false],
["<1.0.0 >2.0.0", ">1.4.0 <1.6.0 || 2.0.0", false],
[">1.0.0 <=2.0.0", "2.0.0", true],
["<1.0.0 >=2.0.0", "2.1.0", false],
["<1.0.0 >=2.0.0", ">1.4.0 <1.6.0 || 2.0.0", false],
["1.5.x", "<1.5.0 || >=1.6.0", false],
["<1.5.0 || >=1.6.0", "1.5.x", false],
[
"<1.6.16 || >=1.7.0 <1.7.11 || >=1.8.0 <1.8.2",
">=1.6.16 <1.7.0 || >=1.7.11 <1.8.0 || >=1.8.2",
false,
],
[
"<=1.6.16 || >=1.7.0 <1.7.11 || >=1.8.0 <1.8.2",
">=1.6.16 <1.7.0 || >=1.7.11 <1.8.0 || >=1.8.2",
true,
],
[">=1.0.0", "<=1.0.0", true],
[">1.0.0 <1.0.0", "<=0.0.0", false],
["*", "0.0.1", true],
["*", ">=1.0.0", true],
["*", ">1.0.0", true],
["*", "~1.0.0", true],
["*", "<1.6.0", true],
["*", "<=1.6.0", true],
["1.*", "0.0.1", false],
["1.*", "2.0.0", false],
["1.*", "1.0.0", true],
["1.*", "<2.0.0", true],
["1.*", ">1.0.0", true],
["1.*", "<=1.0.0", true],
["1.*", "^1.0.0", true],
["1.0.*", "0.0.1", false],
["1.0.*", "<0.0.1", false],
["1.0.*", ">0.0.1", true],
["*", "1.3.0 || <1.0.0 >2.0.0", true],
["1.3.0 || <1.0.0 >2.0.0", "*", true],
["1.*", "1.3.0 || <1.0.0 >2.0.0", true],
["x", "0.0.1", true],
["x", ">=1.0.0", true],
["x", ">1.0.0", true],
["x", "~1.0.0", true],
["x", "<1.6.0", true],
["x", "<=1.6.0", true],
["1.x", "0.0.1", false],
["1.x", "2.0.0", false],
["1.x", "1.0.0", true],
["1.x", "<2.0.0", true],
["1.x", ">1.0.0", true],
["1.x", "<=1.0.0", true],
["1.x", "^1.0.0", true],
["1.0.x", "0.0.1", false],
["1.0.x", "<0.0.1", false],
["1.0.x", ">0.0.1", true],
["x", "1.3.0 || <1.0.0 >2.0.0", true],
["1.3.0 || <1.0.0 >2.0.0", "x", true],
["1.x", "1.3.0 || <1.0.0 >2.0.0", true],
["*", "*", true],
["x", "", true],
];

for (const [r1, r2, expected] of versions) {
await t.step({
name: `${r1} ∩ ${r2}`,
fn: () => {
const range1 = parseRange(r1);
const range2 = parseRange(r2);
const actual1 = rangeIntersects({ ranges: range1.ranges }, {
ranges: range2.ranges,
});
const actual2 = rangeIntersects({ ranges: range2.ranges }, {
ranges: range1.ranges,
});
assertEquals(actual1, expected);
assertEquals(actual2, expected);
},
});
}
},
});
78 changes: 77 additions & 1 deletion semver/range_min_test.ts
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import { INVALID } from "./constants.ts";
import type { SemVer } from "./types.ts";

Deno.test({
name: "rangeMin",
name: "rangeMin()",
fn: async (t) => {
const versions: [string, string | SemVer][] = [
// Stars
@@ -84,3 +84,79 @@ Deno.test({
}
},
});

Deno.test({
name: "rangeMin() handles deprecated SemVerRange.ranges property",
fn: async (t) => {
const versions: [string, string | SemVer][] = [
// Stars
["*", "0.0.0"],
["* || >=2", "0.0.0"],
[">=2 || *", "0.0.0"],
[">2 || *", "0.0.0"],

// equal
["1.0.0", "1.0.0"],
["1.0", "1.0.0"],
["1.0.x", "1.0.0"],
["1.0.*", "1.0.0"],
["1", "1.0.0"],
["1.x.x", "1.0.0"],
["1.x.x", "1.0.0"],
["1.*.x", "1.0.0"],
["1.x.*", "1.0.0"],
["1.x", "1.0.0"],
["1.*", "1.0.0"],
["=1.0.0", "1.0.0"],

// Tilde
["~1.1.1", "1.1.1"],
["~1.1.1-beta", "1.1.1-beta"],
["~1.1.1 || >=2", "1.1.1"],

// Caret
["^1.1.1", "1.1.1"],
["^1.1.1-beta", "1.1.1-beta"],
["^1.1.1 || >=2", "1.1.1"],

// '-' operator
["1.1.1 - 1.8.0", "1.1.1"],
["1.1 - 1.8.0", "1.1.0"],

// Less / less or equal
["<2", "0.0.0"],
["<0.0.0-beta", INVALID],
["<0.0.1-beta", "0.0.0"],
["<2 || >4", "0.0.0"],
[">4 || <2", "0.0.0"],
["<=2 || >=4", "0.0.0"],
[">=4 || <=2", "0.0.0"],
["<0.0.0-beta >0.0.0-alpha", INVALID],
[">0.0.0-alpha <0.0.0-beta", INVALID],

// Greater than or equal
[">=1.1.1 <2 || >=2.2.2 <2", "1.1.1"],
[">=2.2.2 <2 || >=1.1.1 <2", "1.1.1"],

// Greater than but not equal
[">1.0.0", "1.0.1"],
[">1.0.0-0", "1.0.0-1"],
[">1.0.0-beta", "1.0.0-beta.0"],
[">2 || >1.0.0", "1.0.1"],
[">2 || >1.0.0-0", "1.0.0-1"],
[">2 || >1.0.0-beta", "1.0.0-beta.0"],

// Impossible range
[">4 <3", INVALID],
];

for (const [a, b] of versions) {
await t.step(a, () => {
const range = parseRange(a);
const version = typeof b === "string" ? parse(b) : b;
const min = rangeMin({ ranges: range.ranges });
assert(eq(min, version), `${format(min)} != ${format(version)}`);
});
}
},
});
13 changes: 12 additions & 1 deletion semver/test_comparator_test.ts
Original file line number Diff line number Diff line change
@@ -4,8 +4,19 @@ import { assert } from "../assert/mod.ts";
import { parse } from "./parse.ts";
import { parseComparator } from "./parse_comparator.ts";
import { testComparator } from "./test_comparator.ts";
import { Comparator } from "./types.ts";

Deno.test("test", function () {
Deno.test("testComparator()", () => {
const c = parseComparator(">=1.2.3");
assert(testComparator(parse("1.2.4"), c));
});

Deno.test("testComparator() handles deprecated Comparator.semver property", () => {
const c = parseComparator(">=1.2.3");
assert(
testComparator(
parse("1.2.4"),
{ operator: c.operator, semver: c.semver } as Comparator,
),
);
});
Loading