From cb6e2c96e148c1c2f8a44e19980334db74bf2041 Mon Sep 17 00:00:00 2001 From: "Luca v. d. W" Date: Sun, 14 Jan 2024 21:59:41 +0100 Subject: [PATCH] Support for single version in Forge ranges (#98) * Added test cases for forge style single version "range" * Made forge version range separator optional to allow single version * Verify closed end version range is treated as typo for open end * Interpret range as single version only in absence of separator Fixes #95 --------- Co-authored-by: Kir_Antipov <40232067+Kir-Antipov@users.noreply.github.com> --- src/utils/versioning/version-range.ts | 7 +++- .../utils/versioning/version-range.spec.ts | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/utils/versioning/version-range.ts b/src/utils/versioning/version-range.ts index 45b9b8e..c82aa2e 100644 --- a/src/utils/versioning/version-range.ts +++ b/src/utils/versioning/version-range.ts @@ -85,7 +85,7 @@ function mixedToSemver(range: string): string { /** * Regular expression for matching interval expressions in version range strings. */ -const INTERVAL_REGEX = /(?\[|\()\s*(?[^,\s]+)?\s*,\s*(?[^,\s\])]+)?\s*(?\]|\))/; +const INTERVAL_REGEX = /(?\[|\()\s*(?[^,\s]+)?\s*(?,)?\s*(?[^,\s\])]+)?\s*(?\]|\))/; /** * Converts an interval expression into a semver-compatible range expression. @@ -102,6 +102,7 @@ function intervalToSemver(range: string): string { const fromOperator = match.groups.from_bracket === "[" ? ">=" : ">"; const from = match.groups.from; + const separator = match.groups.separator; const toOperator = match.groups.to_bracket === "]" ? "<=" : "<"; const to = match.groups.to; if (!from && !to) { @@ -112,6 +113,10 @@ function intervalToSemver(range: string): string { return `${toOperator}${to}`; } + if (!separator) { + return from; + } + if (!to) { return `${fromOperator}${from}`; } diff --git a/tests/unit/utils/versioning/version-range.spec.ts b/tests/unit/utils/versioning/version-range.spec.ts index a875a07..1db699b 100644 --- a/tests/unit/utils/versioning/version-range.spec.ts +++ b/tests/unit/utils/versioning/version-range.spec.ts @@ -69,6 +69,48 @@ describe("parseVersionRange", () => { expect(versionRange.includes("2.0.1")).toBe(false); }); + test("parses an interval notation string with single version correctly", () => { + const versionRange = parseVersionRange("[1.2.3]"); + + expect(versionRange).toBeDefined(); + expect(versionRange.toString()).toBe("[1.2.3]"); + + expect(versionRange.includes("1.2.3")).toBe(true); + expect(versionRange.includes("1.2.4")).toBe(false); + expect(versionRange.includes("2.0.0")).toBe(false); + expect(versionRange.includes("1.0.0")).toBe(false); + expect(versionRange.includes("1.2.2")).toBe(false); + expect(versionRange.includes("2.0.1")).toBe(false); + }); + + test("parses an interval notation string with single version open end correctly", () => { + const versionRange = parseVersionRange("[1.2.3,)"); + + expect(versionRange).toBeDefined(); + expect(versionRange.toString()).toBe("[1.2.3,)"); + + expect(versionRange.includes("1.2.3")).toBe(true); + expect(versionRange.includes("1.2.4")).toBe(true); + expect(versionRange.includes("2.0.0")).toBe(true); + expect(versionRange.includes("2.0.1")).toBe(true); + expect(versionRange.includes("1.0.0")).toBe(false); + expect(versionRange.includes("1.2.2")).toBe(false); + }); + + test("parses an interval notation string with single version closed end as typo for open end", () => { + const versionRange = parseVersionRange("[1.2.3,]"); + + expect(versionRange).toBeDefined(); + expect(versionRange.toString()).toBe("[1.2.3,]"); + + expect(versionRange.includes("1.2.3")).toBe(true); + expect(versionRange.includes("1.2.4")).toBe(true); + expect(versionRange.includes("2.0.0")).toBe(true); + expect(versionRange.includes("2.0.1")).toBe(true); + expect(versionRange.includes("1.0.0")).toBe(false); + expect(versionRange.includes("1.2.2")).toBe(false); + }); + test("parses an interval notation string with X-ranges correctly", () => { const versionRange = parseVersionRange("[1.2.x,2.0.X)");