diff --git a/.github/shared.yml b/.github/shared.yml index f82422a..24c48bb 100644 --- a/.github/shared.yml +++ b/.github/shared.yml @@ -17,9 +17,7 @@ definitions: dotnet-version: | 3.1.x 5.0.x - 6.x - 7.x - 8.x + 9.x setup-nuget: &setup-nuget name: Setup NuGet diff --git a/.github/workflows/continuous-delivery.yml b/.github/workflows/continuous-delivery.yml index 5febf10..2ebad17 100644 --- a/.github/workflows/continuous-delivery.yml +++ b/.github/workflows/continuous-delivery.yml @@ -36,9 +36,7 @@ jobs: dotnet-version: | 3.1.x 5.0.x - 6.x - 7.x - 8.x + 9.x - name: Restore run: dotnet restore @@ -97,9 +95,7 @@ jobs: dotnet-version: | 3.1.x 5.0.x - 6.x - 7.x - 8.x + 9.x - name: Publish run: | @@ -137,9 +133,7 @@ jobs: dotnet-version: | 3.1.x 5.0.x - 6.x - 7.x - 8.x + 9.x - name: Mutation Test run: | @@ -191,9 +185,7 @@ jobs: dotnet-version: | 3.1.x 5.0.x - 6.x - 7.x - 8.x + 9.x - name: Run Integration Tests run: | @@ -218,9 +210,7 @@ jobs: dotnet-version: | 3.1.x 5.0.x - 6.x - 7.x - 8.x + 9.x - name: Setup NuGet run: | diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 5647619..639f929 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -36,9 +36,7 @@ jobs: dotnet-version: | 3.1.x 5.0.x - 6.x - 7.x - 8.x + 9.x - name: Restore run: dotnet restore @@ -97,9 +95,7 @@ jobs: dotnet-version: | 3.1.x 5.0.x - 6.x - 7.x - 8.x + 9.x - name: Publish run: | @@ -137,9 +133,7 @@ jobs: dotnet-version: | 3.1.x 5.0.x - 6.x - 7.x - 8.x + 9.x - name: Mutation Test run: | @@ -191,9 +185,7 @@ jobs: dotnet-version: | 3.1.x 5.0.x - 6.x - 7.x - 8.x + 9.x - name: Run Integration Tests run: | diff --git a/.github/workflows/deploy-release.yml b/.github/workflows/deploy-release.yml index 215982d..04d0944 100644 --- a/.github/workflows/deploy-release.yml +++ b/.github/workflows/deploy-release.yml @@ -27,9 +27,7 @@ jobs: dotnet-version: | 3.1.x 5.0.x - 6.x - 7.x - 8.x + 9.x - name: Setup NuGet run: | @@ -98,9 +96,7 @@ jobs: dotnet-version: | 3.1.x 5.0.x - 6.x - 7.x - 8.x + 9.x - name: Publish run: | @@ -140,9 +136,7 @@ jobs: dotnet-version: | 3.1.x 5.0.x - 6.x - 7.x - 8.x + 9.x - name: Setup NuGet run: | diff --git a/Verlite.sln b/Verlite.sln index 93e02bb..d645ac6 100644 --- a/Verlite.sln +++ b/Verlite.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29509.3 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35514.174 d17.12 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D0F10406-C62D-49CE-A369-4CEDB84DC800}" ProjectSection(SolutionItems) = preProject @@ -9,6 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution .gitattributes = .gitattributes .gitignore = .gitignore Directory.Build.props = Directory.Build.props + global.json = global.json NuGet.config = NuGet.config README.md = README.md EndProjectSection diff --git a/global.json b/global.json new file mode 100644 index 0000000..10054b8 --- /dev/null +++ b/global.json @@ -0,0 +1,6 @@ +{ + "sdk": { + "version": "9.0.0", + "rollForward": "latestFeature" + } +} diff --git a/src/Verlite.CLI/Verlite.CLI.csproj b/src/Verlite.CLI/Verlite.CLI.csproj index 5082e9e..98e667e 100644 --- a/src/Verlite.CLI/Verlite.CLI.csproj +++ b/src/Verlite.CLI/Verlite.CLI.csproj @@ -12,7 +12,7 @@ verlite elf - net8.0 + net9.0 true Link false diff --git a/src/Verlite.Core/PublicAPI.Unshipped.txt b/src/Verlite.Core/PublicAPI.Unshipped.txt index 7dc5c58..79b781a 100644 --- a/src/Verlite.Core/PublicAPI.Unshipped.txt +++ b/src/Verlite.Core/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ #nullable enable +static Verlite.SemVer.CompareMetadata(string? left, string? right) -> int +static Verlite.SemVer.ComparePrerelease(string? left, string? right) -> int diff --git a/src/Verlite.Core/SemVer.cs b/src/Verlite.Core/SemVer.cs index e59b414..5837ef1 100644 --- a/src/Verlite.Core/SemVer.cs +++ b/src/Verlite.Core/SemVer.cs @@ -176,9 +176,61 @@ private static bool TryParseInt(string input, out int ret) /// Compares the prerelease taking into account ordinals within the string. /// /// -1 if precedes , 0 if they have the same precedence, and 1 if precedes the . - public static int ComparePrerelease(string left, string right) + public static int ComparePrerelease(string? left, string? right) { - int minLen = Math.Min(left.Length, right.Length); + if (left is null && right is null) + return 0; + if (left is null) + return 1; + if (right is null) + return -1; + + int minLen = Math.Min(left.Length, right!.Length); + + for (int i = 0; i < minLen; i++) + { + char l = left[i]; + char r = right[i]; + + if (char.IsDigit(l) && char.IsDigit(r)) + { + var (leftOrdinal, leftOrdinalLength) = SelectOrdinals(left.Substring(i)); + var (rightOrdinal, rightOrdinalLength) = SelectOrdinals(right.Substring(i)); + + int cmpOrdinal = leftOrdinal.CompareTo(rightOrdinal); + if (cmpOrdinal != 0) + return cmpOrdinal; + + Debug.Assert(leftOrdinalLength == rightOrdinalLength); + i += leftOrdinalLength - 1; + } + + if (l != r) + return l < r ? -1 : 1; + } + + return left.Length.CompareTo(right.Length) switch + { + 0 => 0, + <0 => -1, + >0 => 1, + }; + } + + /// + /// Compares the meta taking into account ordinals within the string. + /// + /// -1 if precedes , 0 if they have the same precedence, and 1 if precedes the . + public static int CompareMetadata(string? left, string? right) + { + if (left is null && right is null) + return 0; + if (left is null) + return -1; + if (right is null) + return 1; + + int minLen = Math.Min(left.Length, right!.Length); for (int i = 0; i < minLen; i++) { @@ -199,10 +251,15 @@ public static int ComparePrerelease(string left, string right) } if (l != r) - return l.CompareTo(r); + return l < r ? -1 : 1; } - return left.Length.CompareTo(right.Length); + return left.Length.CompareTo(right.Length) switch + { + 0 => 0, + < 0 => -1, + > 0 => 1, + }; } /// @@ -266,14 +323,11 @@ static bool areDifferent(int left, int right, out int result) if (areDifferent(Patch, other.Patch, out compareResult)) return compareResult; - if (Prerelease is null && other.Prerelease is null) - return 0; - else if (other.Prerelease is null) - return -1; - else if (Prerelease is null) - return 1; - else - return ComparePrerelease(Prerelease, other.Prerelease); + compareResult = ComparePrerelease(Prerelease, other.Prerelease); + if (compareResult != 0) + return compareResult; + + return CompareMetadata(BuildMetadata, other.BuildMetadata); } /// diff --git a/tests/IntegrationTests/tests/arg-escaping/src/TestEscaping/TestEscaping.csproj b/tests/IntegrationTests/tests/arg-escaping/src/TestEscaping/TestEscaping.csproj index ad9ac19..47021f7 100644 --- a/tests/IntegrationTests/tests/arg-escaping/src/TestEscaping/TestEscaping.csproj +++ b/tests/IntegrationTests/tests/arg-escaping/src/TestEscaping/TestEscaping.csproj @@ -2,7 +2,7 @@ Exe - net5.0 + net9.0 enable false diff --git a/tests/UnitTests/PrecedenceTests.cs b/tests/UnitTests/PrecedenceTests.cs index b093ad4..eb2ef78 100644 --- a/tests/UnitTests/PrecedenceTests.cs +++ b/tests/UnitTests/PrecedenceTests.cs @@ -46,5 +46,28 @@ public void PrereleaseComesBeforeRelease() new SemVer(1, 0, 0, "alpha.101").Should().BeLessThan(new SemVer(1, 0, 0, "alpha.102")); new SemVer(1, 0, 0, "alpha.aaa").Should().BeLessThan(new SemVer(1, 0, 0, "alpha.aab")); } + + [Fact] + public void OrderedMetaversions() + { + new SemVer(1, 0, 2, null, "abc").Should().BeGreaterThan(new SemVer(1, 0, 1)); + new SemVer(1, 0, 0, null, "abc").Should().BeGreaterThan(new SemVer(1, 0, 0)); + new SemVer(1, 0, 0, null, "abc").Should().BeGreaterThan(new SemVer(1, 0, 0)); + new SemVer(1, 0, 1, null, "abc").Should().BeGreaterThan(new SemVer(1, 0, 0)); + new SemVer(1, 0, 0, null, "abc").Should().BeLessThan(new SemVer(1, 0, 0, null, "alpha.0")); + new SemVer(1, 0, 0, null, "abc").Should().BeLessThan(new SemVer(1, 0, 0, null, "alpha.1")); + new SemVer(1, 0, 0, null, "abc.1").Should().BeLessThan(new SemVer(1, 0, 0, null, "alpha.2")); + new SemVer(1, 0, 0, null, "abc.2").Should().BeLessThan(new SemVer(1, 0, 0, null, "alpha.10")); + new SemVer(1, 0, 0, null, "abc.2.1").Should().BeLessThan(new SemVer(1, 0, 0, null, "alpha.100")); + new SemVer(1, 0, 0, null, "abc.101").Should().BeLessThan(new SemVer(1, 0, 0, null, "alpha.102")); + new SemVer(1, 0, 0, null, "abc.aaa").Should().BeLessThan(new SemVer(1, 0, 0, null, "alpha.aab")); + + new SemVer(1, 0, 0, null, "abc").Should().BeGreaterThan(new SemVer(1, 0, 0, "def")); + new SemVer(1, 0, 0, "def", "abc").Should().BeGreaterThan(new SemVer(1, 0, 0, "def")); + new SemVer(1, 0, 0, "def", "abc").Should().BeGreaterThan(new SemVer(1, 0, 0, "def")); + new SemVer(1, 0, 0, "def", "b").Should().BeGreaterThan(new SemVer(1, 0, 0, "def", "a")); + new SemVer(1, 0, 0, "def", "rev.13").Should().BeGreaterThan(new SemVer(1, 0, 0, "def", "rev.1")); + new SemVer(1, 0, 0, "def", "aa").Should().BeGreaterThan(new SemVer(1, 0, 0, "def", "a")); + } } } diff --git a/tests/UnitTests/UnitTests.csproj b/tests/UnitTests/UnitTests.csproj index 9c8fc8d..a8ac082 100644 --- a/tests/UnitTests/UnitTests.csproj +++ b/tests/UnitTests/UnitTests.csproj @@ -1,7 +1,7 @@ - net7.0 + net9.0 false diff --git a/tests/UnitTests/VersionTests.cs b/tests/UnitTests/VersionTests.cs index cc11c11..4619d56 100644 --- a/tests/UnitTests/VersionTests.cs +++ b/tests/UnitTests/VersionTests.cs @@ -87,13 +87,6 @@ public void CompreableWorks() (new SemVer(1, 0, 0).CompareTo(new SemVer(1, 0, 0, "alpha"))).Should().Be(1); } - [Fact] - public void CompreableBuildInfoIgnored() - { - (new SemVer(1, 0, 0, "alpha.1", "abc").CompareTo(new SemVer(1, 0, 0, "alpha.1", "def"))).Should().Be(0); - (new SemVer(1, 0, 0, "alpha.1", "abc").CompareTo(new SemVer(1, 0, 0, "alpha.2", "def"))).Should().Be(-1); - } - [Fact] public void CoreVersionFunctions() {