From 6e1bcb10b6cd254dd6f411ca1a2d06b0aabfe816 Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Wed, 3 Jan 2024 22:00:13 +0300 Subject: [PATCH] Fixed wildcard range parsing Fixes #91 --- src/utils/versioning/version-range.ts | 2 +- .../utils/versioning/version-range.spec.ts | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/utils/versioning/version-range.ts b/src/utils/versioning/version-range.ts index cd479a0..45b9b8e 100644 --- a/src/utils/versioning/version-range.ts +++ b/src/utils/versioning/version-range.ts @@ -122,7 +122,7 @@ function intervalToSemver(range: string): string { /** * Regular expression for matching semver-like tags in version strings with optional patch version. */ -const SEMVER_OPTIONAL_PATCH_REGEX = /(\d+\.\d+)(\.\d+|\.[Xx])?([\w\-.+]*)/g; +const SEMVER_OPTIONAL_PATCH_REGEX = /((?:\d+|[Xx*])(?:\.\d+|\.[Xx*]))(\.\d+|\.[Xx*])?([\w\-.+]*)/g; /** * Ensures that a semver string has a patch version, adding ".0" if it is missing. diff --git a/tests/unit/utils/versioning/version-range.spec.ts b/tests/unit/utils/versioning/version-range.spec.ts index 443d0eb..a875a07 100644 --- a/tests/unit/utils/versioning/version-range.spec.ts +++ b/tests/unit/utils/versioning/version-range.spec.ts @@ -20,6 +20,40 @@ describe("parseVersionRange", () => { expect(versionRange.includes("2.0.1")).toBe(false); }); + test("parses a semver string with X-ranges correctly", () => { + const versionRange = parseVersionRange(">=1.2.x <2.0.X"); + + expect(versionRange).toBeDefined(); + expect(versionRange.toString()).toBe(">=1.2.x <2.0.X"); + + expect(versionRange.includes("1.2.0")).toBe(true); + expect(versionRange.includes("1.2.1")).toBe(true); + expect(versionRange.includes("1.2.2")).toBe(true); + expect(versionRange.includes("1.2.3")).toBe(true); + expect(versionRange.includes("1.2.4")).toBe(true); + expect(versionRange.includes("1.9999.9999")).toBe(true); + expect(versionRange.includes("2.0.0")).toBe(false); + expect(versionRange.includes("1.0.0")).toBe(false); + expect(versionRange.includes("2.0.1")).toBe(false); + }); + + test("parses a semver string with *-ranges correctly", () => { + const versionRange = parseVersionRange(">=1.2.* <2.0.*"); + + expect(versionRange).toBeDefined(); + expect(versionRange.toString()).toBe(">=1.2.* <2.0.*"); + + expect(versionRange.includes("1.2.0")).toBe(true); + expect(versionRange.includes("1.2.1")).toBe(true); + expect(versionRange.includes("1.2.2")).toBe(true); + expect(versionRange.includes("1.2.3")).toBe(true); + expect(versionRange.includes("1.2.4")).toBe(true); + expect(versionRange.includes("1.9999.9999")).toBe(true); + expect(versionRange.includes("2.0.0")).toBe(false); + expect(versionRange.includes("1.0.0")).toBe(false); + expect(versionRange.includes("2.0.1")).toBe(false); + }); + test("parses an interval notation string correctly", () => { const versionRange = parseVersionRange("[1.2.3,2.0.0)"); @@ -35,6 +69,40 @@ describe("parseVersionRange", () => { expect(versionRange.includes("2.0.1")).toBe(false); }); + test("parses an interval notation string with X-ranges correctly", () => { + const versionRange = parseVersionRange("[1.2.x,2.0.X)"); + + expect(versionRange).toBeDefined(); + expect(versionRange.toString()).toBe("[1.2.x,2.0.X)"); + + expect(versionRange.includes("1.2.0")).toBe(true); + expect(versionRange.includes("1.2.1")).toBe(true); + expect(versionRange.includes("1.2.2")).toBe(true); + expect(versionRange.includes("1.2.3")).toBe(true); + expect(versionRange.includes("1.2.4")).toBe(true); + expect(versionRange.includes("1.9999.9999")).toBe(true); + expect(versionRange.includes("2.0.0")).toBe(false); + expect(versionRange.includes("1.0.0")).toBe(false); + expect(versionRange.includes("2.0.1")).toBe(false); + }); + + test("parses an interval notation string with *-ranges correctly", () => { + const versionRange = parseVersionRange("[1.2.*,2.0.*)"); + + expect(versionRange).toBeDefined(); + expect(versionRange.toString()).toBe("[1.2.*,2.0.*)"); + + expect(versionRange.includes("1.2.0")).toBe(true); + expect(versionRange.includes("1.2.1")).toBe(true); + expect(versionRange.includes("1.2.2")).toBe(true); + expect(versionRange.includes("1.2.3")).toBe(true); + expect(versionRange.includes("1.2.4")).toBe(true); + expect(versionRange.includes("1.9999.9999")).toBe(true); + expect(versionRange.includes("2.0.0")).toBe(false); + expect(versionRange.includes("1.0.0")).toBe(false); + expect(versionRange.includes("2.0.1")).toBe(false); + }); + test("parses a string that mixes semver and interval notation correctly", () => { const versionRange = parseVersionRange(">=1.2.3 <2.0.0 || [2.0.0,3.0.0)");