Skip to content

Commit

Permalink
Merge pull request #256 from AArnott/fix198
Browse files Browse the repository at this point in the history
Fix version height bugs
  • Loading branch information
AArnott authored Nov 25, 2018
2 parents f278238 + 74632f1 commit 701af4b
Show file tree
Hide file tree
Showing 13 changed files with 397 additions and 79 deletions.
8 changes: 4 additions & 4 deletions src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ public async Task GetBuildVersion_In_Git_No_VersionFile_At_All()
var repo = new Repository(this.RepoPath); // do not assign Repo property to avoid commits being generated later
repo.Commit("empty", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true });
var buildResult = await this.BuildAsync();
Assert.Equal("0.0.1." + repo.Head.Commits.First().GetIdAsVersion().Revision, buildResult.BuildVersion);
Assert.Equal("0.0.1+g" + repo.Head.Commits.First().Id.Sha.Substring(0, 10), buildResult.AssemblyInformationalVersion);
Assert.Equal("0.0.0." + repo.Head.Commits.First().GetIdAsVersion().Revision, buildResult.BuildVersion);
Assert.Equal("0.0.0+g" + repo.Head.Commits.First().Id.Sha.Substring(0, 10), buildResult.AssemblyInformationalVersion);
}

[Fact]
Expand Down Expand Up @@ -375,7 +375,7 @@ public async Task GetBuildVersion_CustomBuildNumberOffset()
var versionOptions = new VersionOptions
{
Version = new SemanticVersion(new Version(14, 1)),
BuildNumberOffset = 5,
VersionHeightOffset = 5,
};
this.WriteVersionFile(versionOptions);
var buildResult = await this.BuildAsync();
Expand Down Expand Up @@ -405,7 +405,7 @@ public async Task GetBuildVersion_Minus1BuildOffset_NotYetCommitted()
var versionOptions = new VersionOptions
{
Version = new SemanticVersion(new Version(14, 1)),
BuildNumberOffset = -1,
VersionHeightOffset = -1,
};
VersionFile.SetVersion(this.RepoPath, versionOptions);
var buildResult = await this.BuildAsync();
Expand Down
14 changes: 7 additions & 7 deletions src/NerdBank.GitVersioning.Tests/GitExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ public void GetVersionHeight_VersionJsonHasUnrelatedHistory()
Assert.Equal(0, this.Repo.GetVersionHeight()); // exercise code that handles the file not yet checked in.
Commands.Stage(this.Repo, versionJsonPath);
this.Repo.Commit("Add unrelated version.json file.", this.Signer, this.Signer);
Assert.Equal(1, this.Repo.GetVersionHeight()); // exercise code that handles a checked in file.
Assert.Equal(0, this.Repo.GetVersionHeight()); // exercise code that handles a checked in file.

// And now the repo has decided to use this package.
this.WriteVersionFile();

Assert.Equal(1, this.Repo.Head.GetVersionHeight());
Assert.Equal(1, this.Repo.GetVersionHeight());

// Also emulate case of where the related project.json was just changed to conform,
// Also emulate case of where the related version.json was just changed to conform,
// but not yet checked in.
this.Repo.Reset(ResetMode.Mixed, this.Repo.Head.Tip.Parents.Single());
Assert.Equal(0, this.Repo.GetVersionHeight());
Expand All @@ -111,7 +111,7 @@ public void GetVersionHeight_VersionJsonHasParsingErrorsInHistory()
Assert.Equal(0, this.Repo.GetVersionHeight());
Commands.Stage(this.Repo, versionJsonPath);
this.Repo.Commit("Add broken version.json file.", this.Signer, this.Signer);
Assert.Equal(1, this.Repo.GetVersionHeight());
Assert.Equal(0, this.Repo.GetVersionHeight());

// Now fix it.
this.WriteVersionFile();
Expand Down Expand Up @@ -254,13 +254,13 @@ public void GetIdAsVersion_ResetsBuildNumberForEachMajorMinorVersion()
[InlineData("2.5", "2.0", 0)]
[InlineData("2.5", "2.0", 5)]
[InlineData("2.5", "2.0", -1)]
public void GetIdAsVersion_Roundtrip(string version, string assemblyVersion, int buildNumberOffset)
public void GetIdAsVersion_Roundtrip(string version, string assemblyVersion, int versionHeightOffset)
{
this.WriteVersionFile(new VersionOptions
{
Version = SemanticVersion.Parse(version),
AssemblyVersion = new VersionOptions.AssemblyVersionOptions(new Version(assemblyVersion)),
BuildNumberOffset = buildNumberOffset,
VersionHeightOffset = versionHeightOffset,
});

Commit[] commits = new Commit[16]; // create enough that statistically we'll likely hit interesting bits as MSB and LSB
Expand Down Expand Up @@ -293,15 +293,15 @@ public void GetIdAsVersion_Roundtrip_UnstableOffset(int startingOffset, int offs
{
Version = SemanticVersion.Parse("1.2"),
AssemblyVersion = null,
BuildNumberOffset = startingOffset,
VersionHeightOffset = startingOffset,
};
this.WriteVersionFile(versionOptions);

Commit[] commits = new Commit[16]; // create enough that statistically we'll likely hit interesting bits as MSB and LSB
Version[] versions = new Version[commits.Length];
for (int i = 0; i < commits.Length; i += 2)
{
versionOptions.BuildNumberOffset += offsetStepChange;
versionOptions.VersionHeightOffset += offsetStepChange;
commits[i] = this.WriteVersionFile(versionOptions);
versions[i] = commits[i].GetIdAsVersion();

Expand Down
17 changes: 9 additions & 8 deletions src/NerdBank.GitVersioning.Tests/VersionFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ public void IsVersionDefined_String_ConsiderAncestorFolders()
[Theory]
[InlineData("2.3", null, null, 0, null, @"{""version"":""2.3""}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Minor, 0, null, @"{""version"":""2.3"",""assemblyVersion"":""2.2""}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Minor, -1, new[] { "refs/heads/master" }, @"{""version"":""2.3"",""assemblyVersion"":""2.2"",""versionHeightOffset"":-1,""publicReleaseRefSpec"":[""refs/heads/master""]}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Minor, -1, new[] { "refs/heads/master" }, @"{""version"":""2.3"",""assemblyVersion"":""2.2"",""buildNumberOffset"":-1,""publicReleaseRefSpec"":[""refs/heads/master""]}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Minor, 0, null, @"{""version"":""2.3"",""assemblyVersion"":{""version"":""2.2""}}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Revision, 0, null, @"{""version"":""2.3"",""assemblyVersion"":{""version"":""2.2"", ""precision"":""revision""}}")]
public void GetVersion_JsonCompatibility(string version, string assemblyVersion, object precision, int buildNumberOffset, string[] publicReleaseRefSpec, string json)
public void GetVersion_JsonCompatibility(string version, string assemblyVersion, object precision, int versionHeightOffset, string[] publicReleaseRefSpec, string json)
{
File.WriteAllText(Path.Combine(this.RepoPath, VersionFile.JsonFileName), json);

Expand All @@ -89,7 +90,7 @@ public void GetVersion_JsonCompatibility(string version, string assemblyVersion,
Assert.Equal(version, options.Version?.ToString());
Assert.Equal(assemblyVersion, options.AssemblyVersion?.Version?.ToString());
Assert.Equal(precision, options.AssemblyVersion?.PrecisionOrDefault);
Assert.Equal(buildNumberOffset, options.BuildNumberOffsetOrDefault);
Assert.Equal(versionHeightOffset, options.VersionHeightOffsetOrDefault);
Assert.Equal(publicReleaseRefSpec, options.PublicReleaseRefSpec);
}

Expand Down Expand Up @@ -117,15 +118,15 @@ public void SetVersion_GetVersionFromFile(string expectedVersion, string expecte
[InlineData("2.3", null, VersionOptions.VersionPrecision.Minor, 0, false, @"{""version"":""2.3""}")]
[InlineData("2.3", null, VersionOptions.VersionPrecision.Minor, null, true, @"{""version"":""2.3"",""assemblyVersion"":{""precision"":""minor""},""inherit"":true}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Minor, 0, false, @"{""version"":""2.3"",""assemblyVersion"":""2.2""}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Minor, -1, false, @"{""version"":""2.3"",""assemblyVersion"":""2.2"",""buildNumberOffset"":-1}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Revision, -1, false, @"{""version"":""2.3"",""assemblyVersion"":{""version"":""2.2"",""precision"":""revision""},""buildNumberOffset"":-1}")]
public void SetVersion_WritesSimplestFile(string version, string assemblyVersion, VersionOptions.VersionPrecision? precision, int? buildNumberOffset, bool inherit, string expectedJson)
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Minor, -1, false, @"{""version"":""2.3"",""assemblyVersion"":""2.2"",""versionHeightOffset"":-1}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Revision, -1, false, @"{""version"":""2.3"",""assemblyVersion"":{""version"":""2.2"",""precision"":""revision""},""versionHeightOffset"":-1}")]
public void SetVersion_WritesSimplestFile(string version, string assemblyVersion, VersionOptions.VersionPrecision? precision, int? versionHeightOffset, bool inherit, string expectedJson)
{
var versionOptions = new VersionOptions
{
Version = SemanticVersion.Parse(version),
AssemblyVersion = assemblyVersion != null || precision != null ? new VersionOptions.AssemblyVersionOptions(assemblyVersion != null ? new Version(assemblyVersion) : null, precision) : null,
BuildNumberOffset = buildNumberOffset,
VersionHeightOffset = versionHeightOffset,
Inherit = inherit,
};
string pathWritten = VersionFile.SetVersion(this.RepoPath, versionOptions);
Expand Down Expand Up @@ -327,7 +328,7 @@ public void VersionJson_Inheritance(bool commitInSourceControl, bool bareRepo)
level3 = new VersionOptions
{
Inherit = true,
BuildNumberOffset = 1,
VersionHeightOffset = 1,
},
@"foo\bar");
this.WriteVersionFile(
Expand Down Expand Up @@ -369,7 +370,7 @@ public void VersionJson_Inheritance(bool commitInSourceControl, bool bareRepo)
Assert.Equal(level1.Version.Version.Minor, level3Options.Version.Version.Minor);
Assert.Equal(level2.AssemblyVersion.Precision, level3Options.AssemblyVersion.Precision);
Assert.Equal(level2.AssemblyVersion.Precision, level3Options.AssemblyVersion.Precision);
Assert.Equal(level3.BuildNumberOffset, level3Options.BuildNumberOffset);
Assert.Equal(level3.VersionHeightOffset, level3Options.VersionHeightOffset);
Assert.True(level3Options.Inherit);

var level2NoInheritOptions = GetOption("noInherit");
Expand Down
8 changes: 4 additions & 4 deletions src/NerdBank.GitVersioning.Tests/VersionOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void FromVersion()
Assert.Equal(new Version(1, 2), vo.Version.Version);
Assert.Equal("-pre", vo.Version.Prerelease);
Assert.Null(vo.AssemblyVersion);
Assert.Equal(0, vo.BuildNumberOffsetOrDefault);
Assert.Equal(0, vo.VersionHeightOffsetOrDefault);
}

[Fact]
Expand All @@ -25,13 +25,13 @@ public void Equality()
{
Version = new SemanticVersion("1.2"),
AssemblyVersion = new VersionOptions.AssemblyVersionOptions(new Version("1.3")),
BuildNumberOffset = 2,
VersionHeightOffset = 2,
};
var vo1b = new VersionOptions
{
Version = new SemanticVersion("1.2"),
AssemblyVersion = new VersionOptions.AssemblyVersionOptions(new Version("1.3")),
BuildNumberOffset = 2,
VersionHeightOffset = 2,
};

var vo2VaryAV = new VersionOptions
Expand All @@ -48,7 +48,7 @@ public void Equality()
{
Version = new SemanticVersion("1.2"),
AssemblyVersion = new VersionOptions.AssemblyVersionOptions(new Version("1.3")),
BuildNumberOffset = 3,
VersionHeightOffset = 3,
};

Assert.Equal(vo1a, vo1b);
Expand Down
45 changes: 40 additions & 5 deletions src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,55 @@ public void MajorMinorBuildPrereleaseBuildMetadata()
Assert.Equal(0, oracle.VersionHeightOffset);
}

[Theory]
[InlineData("0.1", "0.2")]
[InlineData("0.1.0+{height}", "0.1.5+{height}")]
[InlineData("0.1.5-alpha0.{height}", "0.1.5-alpha1.{height}")]
[InlineData("0.1.5-beta.{height}", "0.1.5-beta1.{height}")]
[InlineData("0.1.5-alpha.{height}", "0.1.5-beta.{height}")]
[InlineData("0.1.5-alpha.1.{height}", "0.1.5-beta.1.{height}")]
public void VersionHeightResetsWithVersionSpecChanges(string initial, string next)
{
var options = new VersionOptions
{
Version = SemanticVersion.Parse(initial),
};
this.WriteVersionFile(options);
this.InitializeSourceControl();
this.AddCommits(10);

var oracle = VersionOracle.Create(this.RepoPath);
Assert.Equal(11, oracle.VersionHeight);
Assert.Equal(11, this.Repo.Head.GetVersionHeight());

options.Version = SemanticVersion.Parse(next);

this.WriteVersionFile(options);
oracle = VersionOracle.Create(this.RepoPath);
Assert.Equal(1, oracle.VersionHeight);
Assert.Equal(1, this.Repo.Head.GetVersionHeight());

foreach (var commit in this.Repo.Head.Commits)
{
var versionFromId = commit.GetIdAsVersion();
Assert.Contains(commit, this.Repo.GetCommitsFromVersion(versionFromId));
}
}

[Fact]
public void HeightInPrerelease()
{
VersionOptions workingCopyVersion = new VersionOptions
{
Version = SemanticVersion.Parse("7.8.9-beta.{height}.foo"),
BuildNumberOffset = 2,
VersionHeightOffset = 2,
};
this.WriteVersionFile(workingCopyVersion);
this.InitializeSourceControl();
var oracle = VersionOracle.Create(this.RepoPath);
Assert.Equal("7.8", oracle.MajorMinorVersion.ToString());
Assert.Equal(9, oracle.BuildNumber);
Assert.Equal(oracle.VersionHeight + oracle.VersionHeightOffset, oracle.Version.Revision);
Assert.Equal(-1, oracle.Version.Revision);

Assert.Equal("-beta." + (oracle.VersionHeight + oracle.VersionHeightOffset) + ".foo", oracle.PrereleaseVersion);

Expand All @@ -109,7 +144,7 @@ public void HeightInBuildMetadata()
VersionOptions workingCopyVersion = new VersionOptions
{
Version = SemanticVersion.Parse("7.8.9-beta+another.{height}.foo"),
BuildNumberOffset = 2,
VersionHeightOffset = 2,
};
this.WriteVersionFile(workingCopyVersion);
this.InitializeSourceControl();
Expand Down Expand Up @@ -137,7 +172,7 @@ public void SemVer1PrereleaseConversion(string semVer2, string semVer1)
VersionOptions workingCopyVersion = new VersionOptions
{
Version = SemanticVersion.Parse(semVer2),
BuildNumberOffset = 2,
VersionHeightOffset = 2,
};
this.WriteVersionFile(workingCopyVersion);
this.InitializeSourceControl();
Expand All @@ -152,7 +187,7 @@ public void SemVer1PrereleaseConversionPadding()
VersionOptions workingCopyVersion = new VersionOptions
{
Version = SemanticVersion.Parse("7.8.9-foo.25"),
BuildNumberOffset = 2,
VersionHeightOffset = 2,
SemVer1NumericIdentifierPadding = 3,
};
this.WriteVersionFile(workingCopyVersion);
Expand Down
Loading

0 comments on commit 701af4b

Please sign in to comment.