Skip to content

Commit b2c984a

Browse files
committed
Always generate 4-component AssemblyVersion
Fix #26
1 parent abf25bd commit b2c984a

File tree

6 files changed

+152
-4
lines changed

6 files changed

+152
-4
lines changed

src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,14 @@ private void AssertStandardProperties(VersionOptions versionOptions, BuildResult
209209
Version idAsVersion = this.Repo.Head.Commits.First().GetIdAsVersion(relativeProjectDirectory);
210210
string commitIdShort = this.Repo.Head.Commits.First().Id.Sha.Substring(0, 10);
211211
Version version = this.Repo.Head.Commits.First().GetIdAsVersion(relativeProjectDirectory);
212-
Version assemblyVersion = versionOptions.AssemblyVersion ?? versionOptions.Version.Version;
212+
Version assemblyVersion = (versionOptions.AssemblyVersion ?? versionOptions.Version.Version).EnsureNonNegativeComponents();
213213
Assert.Equal($"{version}", buildResult.AssemblyFileVersion);
214214
Assert.Equal($"{idAsVersion.Major}.{idAsVersion.Minor}.{idAsVersion.Build}{versionOptions.Version.Prerelease}+g{commitIdShort}", buildResult.AssemblyInformationalVersion);
215-
Assert.Equal($"{assemblyVersion.Major}.{assemblyVersion.Minor}", buildResult.AssemblyVersion);
215+
216+
// The assembly version property should always have four integer components to it,
217+
// per bug https://github.com/AArnott/Nerdbank.GitVersioning/issues/26
218+
Assert.Equal($"{assemblyVersion.Major}.{assemblyVersion.Minor}.{assemblyVersion.Build}.{assemblyVersion.Revision}", buildResult.AssemblyVersion);
219+
216220
Assert.Equal(idAsVersion.Build.ToString(), buildResult.BuildNumber);
217221
Assert.Equal(idAsVersion.Build.ToString(), buildResult.BuildNumberFirstAndSecondComponentsIfApplicable);
218222
Assert.Equal(idAsVersion.Build.ToString(), buildResult.BuildNumberFirstComponent);

src/NerdBank.GitVersioning.Tests/NerdBank.GitVersioning.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<Compile Include="RepoTestBase.cs" />
8080
<Compile Include="SemanticVersionTests.cs" />
8181
<Compile Include="TestUtilities.cs" />
82+
<Compile Include="VersionExtensionsTests.cs" />
8283
<Compile Include="VersionFileTests.cs" />
8384
<Compile Include="VersionOptionsTests.cs" />
8485
</ItemGroup>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Nerdbank.GitVersioning;
7+
using Xunit;
8+
9+
public class VersionExtensionsTests
10+
{
11+
[Fact]
12+
public void EnsureNonNegativeComponents_NoValues()
13+
{
14+
var version = new Version().EnsureNonNegativeComponents();
15+
Assert.Equal(0, version.Major);
16+
Assert.Equal(0, version.Minor);
17+
Assert.Equal(0, version.Build);
18+
Assert.Equal(0, version.Revision);
19+
}
20+
21+
[Fact]
22+
public void EnsureNonNegativeComponents_2Values()
23+
{
24+
var version = new Version(1, 2).EnsureNonNegativeComponents();
25+
Assert.Equal(1, version.Major);
26+
Assert.Equal(2, version.Minor);
27+
Assert.Equal(0, version.Build);
28+
Assert.Equal(0, version.Revision);
29+
}
30+
31+
[Fact]
32+
public void EnsureNonNegativeComponents_3Values()
33+
{
34+
var version = new Version(1, 2, 3).EnsureNonNegativeComponents();
35+
Assert.Equal(1, version.Major);
36+
Assert.Equal(2, version.Minor);
37+
Assert.Equal(3, version.Build);
38+
Assert.Equal(0, version.Revision);
39+
}
40+
41+
[Fact]
42+
public void EnsureNonNegativeComponents_4Values()
43+
{
44+
var original = new Version(1, 2, 3, 4);
45+
var version = original.EnsureNonNegativeComponents();
46+
Assert.Same(original, version);
47+
}
48+
49+
[Fact]
50+
public void ToStringSafe()
51+
{
52+
Assert.Equal("1.2.3.4", new Version(1, 2, 3, 4).ToStringSafe(4));
53+
Assert.Equal("1.2.3.0", new Version(1, 2, 3).ToStringSafe(4));
54+
Assert.Equal("1.2.0.0", new Version(1, 2).ToStringSafe(4));
55+
56+
Assert.Equal("1.2.3", new Version(1, 2, 3, 4).ToStringSafe(3));
57+
Assert.Equal("1.2.3", new Version(1, 2, 3).ToStringSafe(3));
58+
Assert.Equal("1.2.0", new Version(1, 2).ToStringSafe(3));
59+
60+
Assert.Equal("1.2", new Version(1, 2, 3, 4).ToStringSafe(2));
61+
Assert.Equal("1.2", new Version(1, 2, 3).ToStringSafe(2));
62+
Assert.Equal("1.2", new Version(1, 2).ToStringSafe(2));
63+
64+
Assert.Equal("1", new Version(1, 2).ToStringSafe(1));
65+
66+
Assert.Equal(string.Empty, new Version(1, 2).ToStringSafe(0));
67+
}
68+
}

src/NerdBank.GitVersioning/NerdBank.GitVersioning.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<Compile Include="Properties\AssemblyInfo.cs" />
6363
<Compile Include="SemanticVersion.cs" />
6464
<Compile Include="SemanticVersionJsonConverter.cs" />
65+
<Compile Include="VersionExtensions.cs" />
6566
<Compile Include="VersionFile.cs" />
6667
<Compile Include="VersionOptions.cs" />
6768
</ItemGroup>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
namespace Nerdbank.GitVersioning
2+
{
3+
using System;
4+
using Validation;
5+
6+
/// <summary>
7+
/// Extension methods for the <see cref="Version"/> class.
8+
/// </summary>
9+
public static class VersionExtensions
10+
{
11+
/// <summary>
12+
/// Returns a <see cref="Version"/> instance where the specified number of components
13+
/// are guaranteed to be non-negative. Any applicable negative components are converted to zeros.
14+
/// </summary>
15+
/// <param name="version">The version to use as a template for the returned value.</param>
16+
/// <param name="fieldCount">The number of version components to ensure are non-negative.</param>
17+
/// <returns>
18+
/// The same as <paramref name="version"/> except with any applicable negative values
19+
/// translated to zeros.
20+
/// </returns>
21+
public static Version EnsureNonNegativeComponents(this Version version, int fieldCount = 4)
22+
{
23+
Requires.NotNull(version, nameof(version));
24+
Requires.Range(fieldCount >= 0 && fieldCount <= 4, nameof(fieldCount));
25+
26+
int maj = fieldCount >= 1 ? Math.Max(0, version.Major) : version.Major;
27+
int min = fieldCount >= 2 ? Math.Max(0, version.Minor) : version.Minor;
28+
int bld = fieldCount >= 3 ? Math.Max(0, version.Build) : version.Build;
29+
int rev = fieldCount >= 4 ? Math.Max(0, version.Revision) : version.Revision;
30+
31+
if (version.Major == maj &&
32+
version.Minor == min &&
33+
version.Build == bld &&
34+
version.Revision == rev)
35+
{
36+
return version;
37+
}
38+
39+
if (rev >= 0)
40+
{
41+
return new Version(maj, min, bld, rev);
42+
}
43+
else if (bld >= 0)
44+
{
45+
return new Version(maj, min, bld);
46+
}
47+
else
48+
{
49+
throw Assumes.NotReachable();
50+
}
51+
}
52+
53+
/// <summary>
54+
/// Converts the value of the current System.Version object to its equivalent System.String
55+
/// representation. A specified count indicates the number of components to return.
56+
/// </summary>
57+
/// <param name="version">The instance to serialize as a string.</param>
58+
/// <param name="fieldCount">The number of components to return. The fieldCount ranges from 0 to 4.</param>
59+
/// <returns>
60+
/// The System.String representation of the values of the major, minor, build, and
61+
/// revision components of the current System.Version object, each separated by a
62+
/// period character ('.'). The fieldCount parameter determines how many components
63+
/// are returned.fieldCount Return Value 0 An empty string (""). 1 major 2 major.minor
64+
/// 3 major.minor.build 4 major.minor.build.revision For example, if you create System.Version
65+
/// object using the constructor Version(1,3,5), ToString(2) returns "1.3" and ToString(4)
66+
/// returns "1.3.5.0".
67+
/// </returns>
68+
public static string ToStringSafe(this Version version, int fieldCount)
69+
{
70+
return version.EnsureNonNegativeComponents(fieldCount).ToString(fieldCount);
71+
}
72+
}
73+
}

src/Nerdbank.GitVersioning.Tasks/GetBuildVersion.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ public override bool Execute()
116116
? new Version(typedVersion.Major, typedVersion.Minor, typedVersion.Build)
117117
: new Version(typedVersion.Major, typedVersion.Minor);
118118
this.SimpleVersion = typedVersionWithoutRevision.ToString();
119-
this.MajorMinorVersion = new Version(typedVersion.Major, typedVersion.Minor).ToString();
120-
this.AssemblyVersion = versionOptions?.AssemblyVersion?.ToString() ?? this.MajorMinorVersion;
119+
var majorMinorVersion = new Version(typedVersion.Major, typedVersion.Minor);
120+
this.MajorMinorVersion = majorMinorVersion.ToString();
121+
this.AssemblyVersion = (versionOptions?.AssemblyVersion ?? majorMinorVersion).ToStringSafe(4);
121122
this.BuildNumber = Math.Max(0, typedVersion.Build);
122123
this.Version = typedVersion.ToString();
123124
}

0 commit comments

Comments
 (0)