From a1187b3358c96c1a217f8a51dc2f1f31b5f19e3a Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Mon, 16 Oct 2023 18:06:44 +0900 Subject: [PATCH] fix: versions prefixed with 'v' are parsed in strict mode (#58) --- src/SemanticVersioning/Version.cs | 2 +- .../AdvancedRanges.cs | 8 +-- .../ParseMajorMinorPatch.cs | 12 ++--- test/SemanticVersioning.Tests/SimpleRanges.cs | 14 +++--- .../StaticRangeMethods.cs | 50 +++++++++---------- .../VersionComparison.cs | 48 +++++++++--------- 6 files changed, 67 insertions(+), 67 deletions(-) diff --git a/src/SemanticVersioning/Version.cs b/src/SemanticVersioning/Version.cs index 807c1fa..ec6bd38 100644 --- a/src/SemanticVersioning/Version.cs +++ b/src/SemanticVersioning/Version.cs @@ -48,7 +48,7 @@ public class Version : IComparable, IComparable, IEquatable public bool IsPreRelease { get { return !string.IsNullOrEmpty(_preRelease); } } private static Regex strictRegex = new Regex(@"^ - \s*v? + \s* ([0-9]|[1-9][0-9]+) # major version \. ([0-9]|[1-9][0-9]+) # minor version diff --git a/test/SemanticVersioning.Tests/AdvancedRanges.cs b/test/SemanticVersioning.Tests/AdvancedRanges.cs index 26917d3..1363d47 100644 --- a/test/SemanticVersioning.Tests/AdvancedRanges.cs +++ b/test/SemanticVersioning.Tests/AdvancedRanges.cs @@ -117,7 +117,7 @@ public void TestHyphenRanges(string range, string[] comparatorStrings) [InlineData(">=*", "0.2.4")] [InlineData("", "1.0.0")] [InlineData("*", "1.2.3")] - [InlineData("*", "v1.2.3")] + [InlineData("*", "1.2.3")] [InlineData(">=1.0.0", "1.0.0")] [InlineData(">=1.0.0", "1.0.1")] [InlineData(">=1.0.0", "1.1.0")] @@ -138,7 +138,7 @@ public void TestHyphenRanges(string range, string[] comparatorStrings) [InlineData("<= 2.0.0", "0.2.9")] [InlineData("< 2.0.0", "1.9999.9999")] [InlineData("<\t2.0.0", "0.2.9")] - [InlineData(">=0.1.97", "v0.1.97")] + [InlineData(">=0.1.97", "0.1.97")] [InlineData(">=0.1.97", "0.1.97")] [InlineData("0.1.20 || 1.2.4", "1.2.4")] [InlineData(">=0.2.3 || <0.0.1", "0.0.0")] @@ -228,7 +228,7 @@ public void SatisfiedRanges(string rangeString, string versionString) [InlineData("<=2.0.0", "2.2.9", false)] [InlineData("<2.0.0", "2.9999.9999", false)] [InlineData("<2.0.0", "2.2.9", false)] - [InlineData(">=0.1.97", "v0.1.93", false)] + [InlineData(">=0.1.97", "0.1.93", false)] [InlineData(">=0.1.97", "0.1.93", false)] [InlineData("0.1.20 || 1.2.4", "1.2.3", false)] [InlineData(">=0.2.3 || <0.0.1", "0.0.3", false)] @@ -263,7 +263,7 @@ public void SatisfiedRanges(string rangeString, string versionString) [InlineData("^1.2.3", "1.2.2", false)] [InlineData("^1.2", "1.1.9", false)] [InlineData("^1.2.3", "2.0.0-pre", false)] - [InlineData("*", "v1.2.3-foo", false)] + [InlineData("*", "1.2.3-foo", false)] public void UnsatisfiedRanges(string rangeString, string versionString, bool loose) { var range = new Range(rangeString, loose); diff --git a/test/SemanticVersioning.Tests/ParseMajorMinorPatch.cs b/test/SemanticVersioning.Tests/ParseMajorMinorPatch.cs index 86051e1..0a209e7 100644 --- a/test/SemanticVersioning.Tests/ParseMajorMinorPatch.cs +++ b/test/SemanticVersioning.Tests/ParseMajorMinorPatch.cs @@ -10,9 +10,9 @@ public class ParseMajorMinorPatch [InlineData(" 1.2.3 ", 1, false)] [InlineData(" 2.2.3-4 ", 2, false)] [InlineData(" 3.2.3-pre ", 3, false)] - [InlineData("v5.2.3", 5, false)] - [InlineData(" v8.2.3 ", 8, false)] [InlineData("\t13.2.3", 13, false)] + [InlineData(" v8.2.3 ", 8, true)] + [InlineData("v5.2.3", 5, true)] [InlineData("=21.2.3", 21, true)] [InlineData("v=34.2.3", 34, true)] public void ParseMajorVersion(string versionString, int majorVersion, bool loose) @@ -26,9 +26,9 @@ public void ParseMajorVersion(string versionString, int majorVersion, bool loose [InlineData(" 1.2.3 ", 2, false)] [InlineData(" 2.2.3-4 ", 2, false)] [InlineData(" 3.2.3-pre ", 2, false)] - [InlineData("v5.2.3", 2, false)] - [InlineData(" v8.2.3 ", 2, false)] [InlineData("\t13.2.3", 2, false)] + [InlineData(" v8.2.3 ", 2, true)] + [InlineData("v5.2.3", 2, true)] [InlineData("=21.2.3", 2, true)] [InlineData("v=34.2.3", 2, true)] public void ParseMinorVersion(string versionString, int minorVersion, bool loose) @@ -42,9 +42,9 @@ public void ParseMinorVersion(string versionString, int minorVersion, bool loose [InlineData(" 1.2.3 ", 3, false)] [InlineData(" 2.2.3-4 ", 3, false)] [InlineData(" 3.2.3-pre ", 3, false)] - [InlineData("v5.2.3", 3, false)] - [InlineData(" v8.2.3 ", 3, false)] [InlineData("\t13.2.3", 3, false)] + [InlineData(" v8.2.3 ", 3, true)] + [InlineData("v5.2.3", 3, true)] [InlineData("=21.2.3", 3, true)] [InlineData("v=34.2.3", 3, true)] public void ParsePatchVersion(string versionString, int patchVersion, bool loose) diff --git a/test/SemanticVersioning.Tests/SimpleRanges.cs b/test/SemanticVersioning.Tests/SimpleRanges.cs index 76b1643..0502522 100644 --- a/test/SemanticVersioning.Tests/SimpleRanges.cs +++ b/test/SemanticVersioning.Tests/SimpleRanges.cs @@ -48,15 +48,15 @@ public void TestMaxSatisfying() public void TestStringMaxSatisfying() { var versions = new [] { - "=1.2.7", - "v1.2.8", - "v1.2.99", - "=1.2.6", - "v1.3.0", - "v1.1.0", + "1.2.7", + "1.2.8", + "1.2.99", + "1.2.6", + "1.3.0", + "1.1.0", }; var range = new Range(">=1.2.7 <1.3.0"); - Assert.Equal("v1.2.99", range.MaxSatisfying(versions)); + Assert.Equal("1.2.99", range.MaxSatisfying(versions)); } [Fact] diff --git a/test/SemanticVersioning.Tests/StaticRangeMethods.cs b/test/SemanticVersioning.Tests/StaticRangeMethods.cs index 0bc5a46..ab5f45c 100644 --- a/test/SemanticVersioning.Tests/StaticRangeMethods.cs +++ b/test/SemanticVersioning.Tests/StaticRangeMethods.cs @@ -11,14 +11,14 @@ public void TestMaxSatisfying() { var versions = new [] { "1.2.7", - "v1.2.8", - "v1.2.99", + "1.2.8", + "1.2.99", "1.2.6", - "v1.3.0", - "v1.1.0", + "1.3.0", + "1.1.0", }; var max = Range.MaxSatisfying(">=1.2.7 <1.3.0", versions); - Assert.Equal("v1.2.99", max); + Assert.Equal("1.2.99", max); } [Fact] @@ -26,17 +26,17 @@ public void TestSatisfying() { var versions = new [] { "1.2.7", - "v1.2.8", - "v1.2.99", + "1.2.8", + "1.2.99", "1.2.6", - "v1.3.0", - "v1.1.0", + "1.3.0", + "1.1.0", }; var satisfying = Range.Satisfying(">=1.2.7 <1.3.0", versions).ToArray(); Assert.Equal(3, satisfying.Count()); Assert.Contains("1.2.7", satisfying); - Assert.Contains("v1.2.8", satisfying); - Assert.Contains("v1.2.99", satisfying); + Assert.Contains("1.2.8", satisfying); + Assert.Contains("1.2.99", satisfying); } [Theory] @@ -53,15 +53,15 @@ public void TestMaxSatisfyingWithIncludePreRelease() { var versions = new [] { "1.2.7", - "v1.2.8", - "v1.2.98", - "v1.2.99-alpha.1", + "1.2.8", + "1.2.98", + "1.2.99-alpha.1", "1.2.6", - "v1.3.0", - "v1.1.0", + "1.3.0", + "1.1.0", }; var max = Range.MaxSatisfying(">=1.2.7 <1.3.0", versions, includePrerelease: true); - Assert.Equal("v1.2.99-alpha.1", max); + Assert.Equal("1.2.99-alpha.1", max); } [Fact] @@ -69,19 +69,19 @@ public void TestSatisfyingWithIncludePreRelease() { var versions = new [] { "1.2.7", - "v1.2.8-beta.2", - "v1.2.98", - "v1.2.99-alpha.1", + "1.2.8-beta.2", + "1.2.98", + "1.2.99-alpha.1", "1.2.6", - "v1.3.0", - "v1.1.0", + "1.3.0", + "1.1.0", }; var satisfying = Range.Satisfying(">=1.2.7 <1.3.0", versions, includePrerelease: true).ToArray(); Assert.Equal(4, satisfying.Count()); Assert.Contains("1.2.7", satisfying); - Assert.Contains("v1.2.98", satisfying); - Assert.Contains("v1.2.8-beta.2", satisfying); - Assert.Contains("v1.2.99-alpha.1", satisfying); + Assert.Contains("1.2.98", satisfying); + Assert.Contains("1.2.8-beta.2", satisfying); + Assert.Contains("1.2.99-alpha.1", satisfying); } [Theory] diff --git a/test/SemanticVersioning.Tests/VersionComparison.cs b/test/SemanticVersioning.Tests/VersionComparison.cs index 9103da4..ff8532e 100644 --- a/test/SemanticVersioning.Tests/VersionComparison.cs +++ b/test/SemanticVersioning.Tests/VersionComparison.cs @@ -157,18 +157,18 @@ public void CompareToDifferentType() [InlineData("0.10.0", "0.9.0")] [InlineData("0.99.0", "0.10.0")] [InlineData("2.0.0", "1.2.3")] - [InlineData("v0.0.0", "0.0.0-foo")] - [InlineData("v0.0.1", "0.0.0")] - [InlineData("v1.0.0", "0.9.9")] - [InlineData("v0.10.0", "0.9.0")] - [InlineData("v0.99.0", "0.10.0")] - [InlineData("v2.0.0", "1.2.3")] - [InlineData("0.0.0", "v0.0.0-foo")] - [InlineData("0.0.1", "v0.0.0")] - [InlineData("1.0.0", "v0.9.9")] - [InlineData("0.10.0", "v0.9.0")] - [InlineData("0.99.0", "v0.10.0")] - [InlineData("2.0.0", "v1.2.3")] + [InlineData("0.0.0", "0.0.0-foo")] + [InlineData("0.0.1", "0.0.0")] + [InlineData("1.0.0", "0.9.9")] + [InlineData("0.10.0", "0.9.0")] + [InlineData("0.99.0", "0.10.0")] + [InlineData("2.0.0", "1.2.3")] + [InlineData("0.0.0", "0.0.0-foo")] + [InlineData("0.0.1", "0.0.0")] + [InlineData("1.0.0", "0.9.9")] + [InlineData("0.10.0", "0.9.0")] + [InlineData("0.99.0", "0.10.0")] + [InlineData("2.0.0", "1.2.3")] [InlineData("1.2.3", "1.2.3-asdf")] [InlineData("1.2.3", "1.2.3-4")] [InlineData("1.2.3", "1.2.3-4-foo")] @@ -197,35 +197,35 @@ public void Comparisons(string v0s, string v1s) // Equality tests from npm/node-semver [Theory] - [InlineData("1.2.3", "v1.2.3", false)] + [InlineData("1.2.3", "1.2.3", false)] [InlineData("1.2.3", "=1.2.3", true)] - [InlineData("1.2.3", "v 1.2.3", true)] + [InlineData("1.2.3", " 1.2.3", true)] [InlineData("1.2.3", "= 1.2.3", true)] - [InlineData("1.2.3", " v1.2.3", false)] + [InlineData("1.2.3", " 1.2.3", false)] [InlineData("1.2.3", " =1.2.3", true)] [InlineData("1.2.3", " v 1.2.3", true)] [InlineData("1.2.3", " = 1.2.3", true)] - [InlineData("1.2.3-0", "v1.2.3-0", false)] + [InlineData("1.2.3-0", "1.2.3-0", false)] [InlineData("1.2.3-0", "=1.2.3-0", true)] - [InlineData("1.2.3-0", "v 1.2.3-0", true)] + [InlineData("1.2.3-0", " 1.2.3-0", true)] [InlineData("1.2.3-0", "= 1.2.3-0", true)] [InlineData("1.2.3-0", " v1.2.3-0", false)] [InlineData("1.2.3-0", " =1.2.3-0", true)] [InlineData("1.2.3-0", " v 1.2.3-0", true)] [InlineData("1.2.3-0", " = 1.2.3-0", true)] - [InlineData("1.2.3-1", "v1.2.3-1", false)] + [InlineData("1.2.3-1", "1.2.3-1", false)] [InlineData("1.2.3-1", "=1.2.3-1", true)] - [InlineData("1.2.3-1", "v 1.2.3-1", true)] + [InlineData("1.2.3-1", " 1.2.3-1", true)] [InlineData("1.2.3-1", "= 1.2.3-1", true)] - [InlineData("1.2.3-1", " v1.2.3-1", false)] + [InlineData("1.2.3-1", " 1.2.3-1", false)] [InlineData("1.2.3-1", " =1.2.3-1", true)] [InlineData("1.2.3-1", " v 1.2.3-1", true)] [InlineData("1.2.3-1", " = 1.2.3-1", true)] - [InlineData("1.2.3-beta", "v1.2.3-beta", false)] + [InlineData("1.2.3-beta", "1.2.3-beta", false)] [InlineData("1.2.3-beta", "=1.2.3-beta", true)] - [InlineData("1.2.3-beta", "v 1.2.3-beta", true)] + [InlineData("1.2.3-beta", " 1.2.3-beta", true)] [InlineData("1.2.3-beta", "= 1.2.3-beta", true)] - [InlineData("1.2.3-beta", " v1.2.3-beta", false)] + [InlineData("1.2.3-beta", " 1.2.3-beta", false)] [InlineData("1.2.3-beta", " =1.2.3-beta", true)] [InlineData("1.2.3-beta", " v 1.2.3-beta", true)] [InlineData("1.2.3-beta", " = 1.2.3-beta", true)] @@ -233,7 +233,7 @@ public void Comparisons(string v0s, string v1s) [InlineData("1.2.3+build", " = 1.2.3+otherbuild", true)] [InlineData("1.2.3-beta+build", "1.2.3-beta+otherbuild", false)] [InlineData("1.2.3+build", "1.2.3+otherbuild", false)] - [InlineData(" v1.2.3+build", "1.2.3+otherbuild", false)] + [InlineData(" 1.2.3+build", "1.2.3+otherbuild", false)] public void Equality(string v0s, string v1s, bool loose) { var v0 = new Version(v0s, false);