fix(releases): strip prerelease and build metadata before version compare

System.Version can't parse SemVer prerelease ("-alpha") or MinVer build
metadata ("+sha") suffixes, so an installed 1.0.2-alpha was treated as
unparseable. Reduce both sides to their numeric core before comparing.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
mika kuns
2026-05-30 09:38:57 +02:00
parent ce879f6f70
commit b84716ff9c
2 changed files with 17 additions and 7 deletions

View File

@@ -9,6 +9,14 @@ public class VersionComparerTests
[InlineData("v0.2.0", "0.1.0", true, false)]
[InlineData("0.2.0", "v0.1.0", true, false)]
[InlineData("1.0.0.0", "0.99.99.99", true, false)]
// Prerelease and build-metadata suffixes are stripped to the numeric core
// before comparing (System.Version can't parse them otherwise).
[InlineData("0.2.0-beta", "0.1.0", true, false)]
[InlineData("0.2.0", "0.1.0-alpha", true, false)]
[InlineData("1.2.0", "1.0.2-alpha", true, false)] // real-world: installed 1.0.2-alpha -> 1.2.0
[InlineData("v1.0.2-alpha+1c764dae", "1.0.0", true, false)] // v-prefix + prerelease + build metadata combined
[InlineData("1.0.2-alpha+abc", "1.0.2-alpha", false, false)] // same core -> not newer
[InlineData("1.2.0-rc1", "1.2.0", false, false)] // prerelease of an already-installed release
public void Compare_ParseableVersions(string latest, string current, bool expectedNewer, bool expectedUnparseable)
{
var result = VersionComparer.Compare(latest, current);
@@ -17,9 +25,8 @@ public class VersionComparerTests
}
[Theory]
[InlineData("0.2.0-beta", "0.1.0")]
[InlineData("0.2.0", "0.1.0-alpha")]
[InlineData("garbage", "0.1.0")]
[InlineData("0.2.0", "garbage")]
[InlineData("", "0.1.0")]
public void Compare_UnparseableReturnsNotNewer(string latest, string current)
{