Skip to content

Commit 7fd213f

Browse files
CopilotAArnott
andcommitted
Add unit tests for versionHeightOffsetAppliesTo functionality
Co-authored-by: AArnott <[email protected]>
1 parent 02c8e85 commit 7fd213f

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

test/Nerdbank.GitVersioning.Tests/ReleaseManagerTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,47 @@ public void PrepareRelease_WithCustomCommitMessagePattern(string initialVersion,
703703
Assert.Equal(expectedCommitMessage, releaseBranchCommit.MessageShort);
704704
}
705705

706+
[Fact]
707+
public void PrepareRelease_ResetsVersionHeightOffsetAppliesTo()
708+
{
709+
// create and configure repository
710+
this.InitializeSourceControl();
711+
712+
var initialVersionOptions = new VersionOptions()
713+
{
714+
Version = SemanticVersion.Parse("1.0-beta"),
715+
VersionHeightOffset = 5,
716+
VersionHeightOffsetAppliesTo = SemanticVersion.Parse("1.0-beta"),
717+
};
718+
719+
var expectedReleaseVersionOptions = new VersionOptions()
720+
{
721+
Version = SemanticVersion.Parse("1.0"),
722+
VersionHeightOffset = 5,
723+
VersionHeightOffsetAppliesTo = SemanticVersion.Parse("1.0-beta"),
724+
};
725+
726+
var expectedMainVersionOptions = new VersionOptions()
727+
{
728+
Version = SemanticVersion.Parse("1.1-alpha"),
729+
};
730+
731+
// create version.json
732+
this.WriteVersionFile(initialVersionOptions);
733+
734+
Commit tipBeforePrepareRelease = this.LibGit2Repository.Head.Tip;
735+
736+
var releaseManager = new ReleaseManager();
737+
releaseManager.PrepareRelease(this.RepoPath);
738+
739+
this.SetContextToHead();
740+
VersionOptions newVersion = this.Context.VersionFile.GetVersion();
741+
Assert.Equal(expectedMainVersionOptions, newVersion);
742+
743+
VersionOptions releaseVersion = this.GetVersionOptions(committish: this.LibGit2Repository.Branches["v1.0"].Tip.Sha);
744+
Assert.Equal(expectedReleaseVersionOptions, releaseVersion);
745+
}
746+
706747
/// <inheritdoc/>
707748
protected override void InitializeSourceControl(bool withInitialCommit = true)
708749
{

test/Nerdbank.GitVersioning.Tests/VersionOracleTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,63 @@ public void HeightInBuildMetadata()
219219
Assert.Equal(2, oracle.VersionHeightOffset);
220220
}
221221

222+
[Fact]
223+
public void VersionHeightOffsetAppliesTo_Matching()
224+
{
225+
// When VersionHeightOffsetAppliesTo matches the current version, the offset should be applied
226+
VersionOptions workingCopyVersion = new VersionOptions
227+
{
228+
Version = SemanticVersion.Parse("7.8.9-beta"),
229+
VersionHeightOffset = 5,
230+
VersionHeightOffsetAppliesTo = SemanticVersion.Parse("7.8.9-beta"),
231+
};
232+
this.WriteVersionFile(workingCopyVersion);
233+
this.InitializeSourceControl();
234+
var oracle = new VersionOracle(this.Context);
235+
236+
// The offset should be applied because the version matches
237+
Assert.Equal(5, oracle.VersionHeightOffset);
238+
Assert.Equal(1, oracle.VersionHeight);
239+
}
240+
241+
[Fact]
242+
public void VersionHeightOffsetAppliesTo_NotMatching()
243+
{
244+
// When VersionHeightOffsetAppliesTo doesn't match the current version, the offset should NOT be applied
245+
VersionOptions workingCopyVersion = new VersionOptions
246+
{
247+
Version = SemanticVersion.Parse("7.9-beta"),
248+
VersionHeightOffset = 5,
249+
VersionHeightOffsetAppliesTo = SemanticVersion.Parse("7.8-beta"),
250+
};
251+
this.WriteVersionFile(workingCopyVersion);
252+
this.InitializeSourceControl();
253+
var oracle = new VersionOracle(this.Context);
254+
255+
// The offset should NOT be applied because the version changed (7.8 -> 7.9)
256+
Assert.Equal(0, oracle.VersionHeightOffset);
257+
Assert.Equal(1, oracle.VersionHeight);
258+
}
259+
260+
[Fact]
261+
public void VersionHeightOffsetAppliesTo_BuildNumberChange()
262+
{
263+
// When VersionHeightOffsetAppliesTo has a different build number, the offset should NOT be applied
264+
VersionOptions workingCopyVersion = new VersionOptions
265+
{
266+
Version = SemanticVersion.Parse("7.9-beta"),
267+
VersionHeightOffset = 5,
268+
VersionHeightOffsetAppliesTo = SemanticVersion.Parse("7.8-beta"),
269+
};
270+
this.WriteVersionFile(workingCopyVersion);
271+
this.InitializeSourceControl();
272+
var oracle = new VersionOracle(this.Context);
273+
274+
// The offset should NOT be applied because the minor version changed (7.8 -> 7.9)
275+
Assert.Equal(0, oracle.VersionHeightOffset);
276+
Assert.Equal(1, oracle.VersionHeight);
277+
}
278+
222279
[Theory]
223280
[InlineData("7.8.9-foo.25", "7.8.9-foo-0025")]
224281
[InlineData("7.8.9-foo.25s", "7.8.9-foo-25s")]

0 commit comments

Comments
 (0)