Skip to content

Commit f05b039

Browse files
Merge pull request #176 from AshleighAdams/fix-min-ver-with-post-release
Fixed min versions not working correctly with post releases
2 parents 3d5915b + 947dd40 commit f05b039

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/Verlite.Core/VersionCalculator.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,24 @@ public static SemVer FromTagInfomation(SemVer? lastTag, VersionCalculationOption
7777
bool directTag = height == 0;
7878
if (directTag)
7979
{
80-
if (options.MinimumVersion > lastTag.Value.CoreVersion)
80+
// check the tag is valid:
81+
// min: 1.0.0, tag: 1.0.0-rc.1: allowed
82+
// min: 1.0.0-rc.2, tag: 1.0.0-rc.1: not allowed
83+
// min: 1.0.0+rev.2, tag: 1.0.0+rev.3: allowed
84+
// min: 1.0.0+rev.2, tag: 1.0.0+rev.1: not allowed
85+
// min: 1.0.0-rc.2+rev.2, tag: 1.0.0-rc.1+rev.3: not allowed
86+
// min: 1.0.0-rc.2+rev.2, tag: 1.0.0-rc.2+rev.1: not allowed
87+
SemVer targetVersion, minVersion;
88+
if (options.MinimumVersion.Prerelease is not null)
89+
(targetVersion, minVersion) = (lastTag.Value, options.MinimumVersion);
90+
else
91+
{
92+
(targetVersion, minVersion) = (lastTag.Value, options.MinimumVersion);
93+
if (targetVersion.CoreVersion > targetVersion) // allow prereleases to be considered a core release
94+
targetVersion = targetVersion.CoreVersion;
95+
}
96+
97+
if (targetVersion < options.MinimumVersion)
8198
throw new VersionCalculationException($"Direct tag ({lastTag.Value}) destined version ({lastTag.Value.CoreVersion}) is below the minimum version ({options.MinimumVersion}).");
8299

83100
var directVersion = lastTag.Value;

tests/UnitTests/VersionTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,42 @@ public void CoreVersionFunctions()
9595
new SemVer(2, 0, 0).CoreVersion.Should().Be(new SemVer(2, 0, 0));
9696
}
9797

98+
[Fact]
99+
public void CoreVersionFunctionsWithPostTag()
100+
{
101+
var opts = new VersionCalculationOptions()
102+
{
103+
MinimumVersion = SemVer.Parse("1.0.0+rev.12"),
104+
};
105+
var directTag = SemVer.Parse("1.0.0+rev.500");
106+
var version = VersionCalculator.FromTagInfomation(directTag, opts, 0);
107+
version.Should().Be(directTag);
108+
}
109+
110+
[Fact]
111+
public void PrereleaseVersionFunctionsWithPostTag()
112+
{
113+
var opts = new VersionCalculationOptions()
114+
{
115+
MinimumVersion = SemVer.Parse("1.0.0-rc.2+rev.12"),
116+
};
117+
var directTag = SemVer.Parse("1.0.0-rc.2+rev.500");
118+
var version = VersionCalculator.FromTagInfomation(directTag, opts, 0);
119+
version.Should().Be(directTag);
120+
}
121+
122+
[Fact]
123+
public void InvalidPrereleaseVersionThrowsWithPostTag()
124+
{
125+
var opts = new VersionCalculationOptions()
126+
{
127+
MinimumVersion = SemVer.Parse("1.0.0-rc.2+rev.12"),
128+
};
129+
130+
Assert.Throws<VersionCalculationException>(() => VersionCalculator.FromTagInfomation(SemVer.Parse("1.0.0-rc.1+rev.501"), opts, 0));
131+
Assert.Throws<VersionCalculationException>(() => VersionCalculator.FromTagInfomation(SemVer.Parse("1.0.0-rc.2+rev.1"), opts, 0));
132+
}
133+
98134
[Fact]
99135
[Obsolete("Function tested is obsolete.")]
100136
public void DestinedVersionFunctions()

0 commit comments

Comments
 (0)