Skip to content

Commit 81ad112

Browse files
committed
Fix broken unit tests
1 parent 6baa590 commit 81ad112

File tree

8 files changed

+92
-66
lines changed

8 files changed

+92
-66
lines changed

src/GitVersion.Core.Tests/IntegrationTests/SupportBranchScenarios.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitVersion.Configuration;
12
using GitVersion.Core.Tests.Helpers;
23
using LibGit2Sharp;
34

@@ -73,27 +74,33 @@ public void WhenSupportIsBranchedAndTaggedFromAnotherSupportEnsureNewMinorIsUsed
7374
[Test]
7475
public void WhenSupportIsBranchedFromMainWithSpecificTag()
7576
{
77+
var configuration = GitFlowConfigurationBuilder.New.Build();
78+
7679
using var fixture = new EmptyRepositoryFixture();
77-
fixture.Repository.MakeACommit();
80+
81+
fixture.MakeACommit();
7882
fixture.AssertFullSemver("0.0.1+1");
7983

80-
fixture.Repository.ApplyTag("1.4.0-rc");
81-
fixture.Repository.MakeACommit();
82-
fixture.Repository.CreateBranch("support/1");
83-
Commands.Checkout(fixture.Repository, "support/1");
84-
fixture.AssertFullSemver("1.4.0+1");
84+
fixture.ApplyTag("1.4.0-rc");
85+
fixture.MakeACommit();
86+
fixture.BranchTo("support/1");
87+
88+
fixture.AssertFullSemver("1.4.0+1", configuration);
8589
}
8690

8791
[Test]
8892
public void WhenSupportIsBranchedFromMainWithSpecificTagOnCommit()
8993
{
94+
var configuration = GitFlowConfigurationBuilder.New.Build();
95+
9096
using var fixture = new EmptyRepositoryFixture();
91-
fixture.Repository.MakeACommit();
92-
fixture.AssertFullSemver("0.0.1+1");
9397

94-
fixture.Repository.ApplyTag("1.4.0-rc");
95-
fixture.Repository.CreateBranch("support/1");
96-
Commands.Checkout(fixture.Repository, "support/1");
97-
fixture.AssertFullSemver("1.4.0");
98+
fixture.MakeACommit();
99+
fixture.AssertFullSemver("0.0.1+1", configuration);
100+
101+
fixture.ApplyTag("1.4.0-rc");
102+
fixture.BranchTo("support/1");
103+
104+
fixture.AssertFullSemver("1.4.0+0", configuration);
98105
}
99106
}

src/GitVersion.Core.Tests/VersionCalculation/SemanticVersionTests.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace GitVersion.Core.Tests;
66
[TestFixture]
77
public class SemanticVersionTests : TestBase
88
{
9-
[TestCase("1.2.3", 1, 2, 3, null, null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
9+
[TestCase("1.2.3", 1, 2, 3, "", null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
1010
[TestCase("1.2.3-beta", 1, 2, 3, "beta", null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
1111
[TestCase("1.2.3-beta3", 1, 2, 3, "beta", 3, null, null, null, null, "1.2.3-beta.3", null, SemanticVersionFormat.Strict)]
1212
[TestCase("1.2.3-beta.3", 1, 2, 3, "beta", 3, null, null, null, null, "1.2.3-beta.3", null, SemanticVersionFormat.Strict)]
@@ -19,25 +19,24 @@ public class SemanticVersionTests : TestBase
1919
[TestCase("1.2.3-rc3.1", 1, 2, 3, "rc3", 1, null, null, null, null, "1.2.3-rc3.1", null, SemanticVersionFormat.Strict)]
2020
[TestCase("1.2.3-beta3f", 1, 2, 3, "beta3f", null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
2121
[TestCase("1.2.3-notAStability1", 1, 2, 3, "notAStability", 1, null, null, null, null, "1.2.3-notAStability.1", null, SemanticVersionFormat.Strict)]
22-
[TestCase("1.2.3+4", 1, 2, 3, null, null, 4, null, null, null, null, null, SemanticVersionFormat.Strict)]
23-
[TestCase("1.2.3+4.Branch.Foo", 1, 2, 3, null, null, 4, "Foo", null, null, null, null, SemanticVersionFormat.Strict)]
24-
[TestCase("1.2.3+randomMetaData", 1, 2, 3, null, null, null, null, null, "randomMetaData", null, null, SemanticVersionFormat.Strict)]
22+
[TestCase("1.2.3+4", 1, 2, 3, "", null, 4, null, null, null, null, null, SemanticVersionFormat.Strict)]
23+
[TestCase("1.2.3+4.Branch.Foo", 1, 2, 3, "", null, 4, "Foo", null, null, null, null, SemanticVersionFormat.Strict)]
24+
[TestCase("1.2.3+randomMetaData", 1, 2, 3, "", null, null, null, null, "randomMetaData", null, null, SemanticVersionFormat.Strict)]
2525
[TestCase("1.2.3-beta.1+4.Sha.12234.Othershiz", 1, 2, 3, "beta", 1, 4, null, "12234", "Othershiz", null, null, SemanticVersionFormat.Strict)]
26-
[TestCase("1.2.3", 1, 2, 3, null, null, null, null, null, null, null, ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
27-
[TestCase("v1.2.3", 1, 2, 3, null, null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
28-
[TestCase("V1.2.3", 1, 2, 3, null, null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
29-
[TestCase("version-1.2.3", 1, 2, 3, null, null, null, null, null, null, "1.2.3", "version-", SemanticVersionFormat.Strict)]
26+
[TestCase("1.2.3", 1, 2, 3, "", null, null, null, null, null, null, ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
27+
[TestCase("v1.2.3", 1, 2, 3, "", null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
28+
[TestCase("V1.2.3", 1, 2, 3, "", null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
29+
[TestCase("version-1.2.3", 1, 2, 3, "", null, null, null, null, null, "1.2.3", "version-", SemanticVersionFormat.Strict)]
3030
[TestCase("1.0.0-develop-20201007113711", 1, 0, 0, "develop-20201007113711", null, null, null, null, null, "1.0.0-develop-20201007113711", null, SemanticVersionFormat.Strict)]
3131
[TestCase("20201007113711.658165168461351.64136516984163213-develop-20201007113711.98848747823+65416321321", 20201007113711, 658165168461351, 64136516984163213, "develop-20201007113711", 98848747823, 65416321321, null, null, null, "20201007113711.658165168461351.64136516984163213-develop-20201007113711.98848747823+65416321321", null, SemanticVersionFormat.Strict)]
32-
33-
[TestCase("1.2", 1, 2, 0, null, null, null, null, null, null, "1.2.0", null, SemanticVersionFormat.Loose)]
32+
[TestCase("1.2", 1, 2, 0, "", null, null, null, null, null, "1.2.0", null, SemanticVersionFormat.Loose)]
3433
[TestCase("1.2-alpha4", 1, 2, 0, "alpha", 4, null, null, null, null, "1.2.0-alpha.4", null, SemanticVersionFormat.Loose)]
3534
[TestCase("01.02.03-rc03", 1, 2, 3, "rc", 3, null, null, null, null, "1.2.3-rc.3", null, SemanticVersionFormat.Loose)]
36-
[TestCase("1.2.3.4", 1, 2, 3, null, null, 4, null, null, null, "1.2.3+4", null, SemanticVersionFormat.Loose)]
37-
[TestCase("1", 1, 0, 0, null, null, null, null, null, null, "1.0.0", null, SemanticVersionFormat.Loose)]
38-
[TestCase("1.1", 1, 1, 0, null, null, null, null, null, null, "1.1.0", null, SemanticVersionFormat.Loose)]
35+
[TestCase("1.2.3.4", 1, 2, 3, "", null, 4, null, null, null, "1.2.3+4", null, SemanticVersionFormat.Loose)]
36+
[TestCase("1", 1, 0, 0, "", null, null, null, null, null, "1.0.0", null, SemanticVersionFormat.Loose)]
37+
[TestCase("1.1", 1, 1, 0, "", null, null, null, null, null, "1.1.0", null, SemanticVersionFormat.Loose)]
3938
public void ValidateVersionParsing(
40-
string? versionString, long major, long minor, long patch, string? tag, long? tagNumber, long? numberOfBuilds,
39+
string? versionString, long major, long minor, long patch, string label, long? tagNumber, long? numberOfBuilds,
4140
string? branchName, string? sha, string? otherMetaData, string? fullFormattedVersionString, string? tagPrefixRegex, SemanticVersionFormat format = SemanticVersionFormat.Strict)
4241
{
4342
fullFormattedVersionString ??= versionString;
@@ -51,7 +50,7 @@ public void ValidateVersionParsing(
5150
Assert.That(version.Major, Is.EqualTo(major));
5251
Assert.That(version.Minor, Is.EqualTo(minor));
5352
Assert.That(version.Patch, Is.EqualTo(patch));
54-
Assert.That(version.PreReleaseTag.Name, Is.EqualTo(tag));
53+
Assert.That(version.PreReleaseTag.Name, Is.EqualTo(label));
5554
Assert.That(version.PreReleaseTag.Number, Is.EqualTo(tagNumber));
5655

5756
Assert.That(version.BuildMetaData.CommitsSinceTag, Is.EqualTo(numberOfBuilds));

src/GitVersion.Core/Configuration/ConfigurationExtensions.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,41 @@ public static bool IsReleaseBranch(this IGitVersionConfiguration configuration,
5959
public static bool IsReleaseBranch(this IGitVersionConfiguration configuration, ReferenceName branchName)
6060
=> configuration.GetBranchConfiguration(branchName).IsReleaseBranch ?? false;
6161

62-
public static string GetBranchSpecificTag(this EffectiveConfiguration configuration, ILog log, string? branchFriendlyName,
63-
string? branchNameOverride)
62+
public static string? GetBranchSpecificLabel(
63+
this EffectiveConfiguration configuration, ILog log, ReferenceName branchName, string? branchNameOverride)
64+
=> GetBranchSpecificLabel(configuration, log, branchName.WithoutOrigin, branchNameOverride);
65+
66+
public static string? GetBranchSpecificLabel(
67+
this EffectiveConfiguration configuration, ILog log, string? branchName, string? branchNameOverride)
6468
{
65-
var tagToUse = configuration.Label;
66-
if (tagToUse == "useBranchName")
69+
configuration.NotNull();
70+
71+
var label = configuration.Label;
72+
if (label == "useBranchName")
6773
{
68-
tagToUse = ConfigurationConstants.BranchNamePlaceholder;
74+
label = ConfigurationConstants.BranchNamePlaceholder;
6975
}
7076

71-
if (tagToUse.Contains(ConfigurationConstants.BranchNamePlaceholder))
77+
if (label?.Contains(ConfigurationConstants.BranchNamePlaceholder) == true)
7278
{
7379
log.Info("Using branch name to calculate version tag");
7480

75-
var branchName = branchNameOverride ?? branchFriendlyName;
81+
var value = branchNameOverride ?? branchName;
82+
7683
if (!configuration.BranchPrefixToTrim.IsNullOrWhiteSpace())
7784
{
78-
var branchNameTrimmed = branchName?.RegexReplace(configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
79-
branchName = branchNameTrimmed.IsNullOrEmpty() ? branchName : branchNameTrimmed;
85+
var branchNameTrimmed = value?.RegexReplace(
86+
configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase
87+
);
88+
value = branchNameTrimmed.IsNullOrEmpty() ? value : branchNameTrimmed;
8089
}
8190

82-
branchName = branchName?.RegexReplace("[^a-zA-Z0-9-]", "-");
91+
value = value?.RegexReplace("[^a-zA-Z0-9-]", "-");
8392

84-
tagToUse = tagToUse.Replace(ConfigurationConstants.BranchNamePlaceholder, branchName);
93+
label = label.Replace(ConfigurationConstants.BranchNamePlaceholder, value);
8594
}
8695

87-
return tagToUse;
96+
return label;
8897
}
8998

9099
public static (string GitDirectory, string WorkingTreeDirectory)? FindGitDir(this string path)

src/GitVersion.Core/Configuration/EffectiveConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public EffectiveConfiguration(IGitVersionConfiguration configuration, IBranchCon
3939
AssemblyFileVersioningFormat = configuration.AssemblyFileVersioningFormat;
4040
VersioningMode = branchConfiguration.VersioningMode.Value;
4141
LabelPrefix = configuration.LabelPrefix;
42-
Label = branchConfiguration.Label ?? string.Empty;
42+
Label = branchConfiguration.Label;
4343
NextVersion = configuration.NextVersion;
4444
Increment = branchConfiguration.Increment;
4545
BranchPrefixToTrim = branchConfiguration.RegularExpression;
@@ -140,7 +140,7 @@ protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningSche
140140
/// <summary>
141141
/// Label to use when calculating SemVer
142142
/// </summary>
143-
public string Label { get; }
143+
public string? Label { get; }
144144

145145
public string? NextVersion { get; }
146146

src/GitVersion.Core/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ GitVersion.Configuration.EffectiveConfiguration.EffectiveConfiguration(GitVersio
151151
GitVersion.Configuration.EffectiveConfiguration.Increment.get -> GitVersion.IncrementStrategy
152152
GitVersion.Configuration.EffectiveConfiguration.IsMainline.get -> bool
153153
GitVersion.Configuration.EffectiveConfiguration.IsReleaseBranch.get -> bool
154-
GitVersion.Configuration.EffectiveConfiguration.Label.get -> string!
154+
GitVersion.Configuration.EffectiveConfiguration.Label.get -> string?
155155
GitVersion.Configuration.EffectiveConfiguration.LabelNumberPattern.get -> string?
156156
GitVersion.Configuration.EffectiveConfiguration.LabelPreReleaseWeight.get -> int
157157
GitVersion.Configuration.EffectiveConfiguration.LabelPrefix.get -> string?
@@ -1076,7 +1076,8 @@ static GitVersion.Configuration.BranchConfigurationBuilder.New.get -> GitVersion
10761076
static GitVersion.Configuration.ConfigurationExtensions.FindGitDir(this string! path) -> (string! GitDirectory, string! WorkingTreeDirectory)?
10771077
static GitVersion.Configuration.ConfigurationExtensions.GetBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.IBranch! branch) -> GitVersion.Configuration.IBranchConfiguration!
10781078
static GitVersion.Configuration.ConfigurationExtensions.GetBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.ReferenceName! branchName) -> GitVersion.Configuration.IBranchConfiguration!
1079-
static GitVersion.Configuration.ConfigurationExtensions.GetBranchSpecificTag(this GitVersion.Configuration.EffectiveConfiguration! configuration, GitVersion.Logging.ILog! log, string? branchFriendlyName, string? branchNameOverride) -> string!
1079+
static GitVersion.Configuration.ConfigurationExtensions.GetBranchSpecificLabel(this GitVersion.Configuration.EffectiveConfiguration! configuration, GitVersion.Logging.ILog! log, GitVersion.ReferenceName! branchName, string? branchNameOverride) -> string?
1080+
static GitVersion.Configuration.ConfigurationExtensions.GetBranchSpecificLabel(this GitVersion.Configuration.EffectiveConfiguration! configuration, GitVersion.Logging.ILog! log, string? branchName, string? branchNameOverride) -> string?
10801081
static GitVersion.Configuration.ConfigurationExtensions.GetEffectiveConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.IBranch! branch) -> GitVersion.Configuration.EffectiveConfiguration!
10811082
static GitVersion.Configuration.ConfigurationExtensions.GetFallbackBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration) -> GitVersion.Configuration.IBranchConfiguration!
10821083
static GitVersion.Configuration.ConfigurationExtensions.GetReleaseBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration) -> System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string!, GitVersion.Configuration.IBranchConfiguration!>>!

src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public virtual NextVersion FindVersion()
4545

4646
var nextVersion = Calculate(Context.CurrentBranch, Context.Configuration);
4747
var baseVersion = nextVersion.BaseVersion;
48-
var preReleaseTagName = nextVersion.Configuration.GetBranchSpecificTag(
49-
this.log, Context.CurrentBranch.Name.WithoutOrigin, baseVersion.BranchNameOverride
48+
var preReleaseTagName = nextVersion.Configuration.GetBranchSpecificLabel(
49+
this.log, Context.CurrentBranch.Name, baseVersion.BranchNameOverride
5050
);
5151

5252
SemanticVersion semver;
@@ -80,24 +80,36 @@ public virtual NextVersion FindVersion()
8080
}
8181
}
8282

83-
if (semver.CompareTo(Context.CurrentCommitTaggedVersion) == 0)
84-
{
85-
// Will always be 0, don't bother with the +0 on tags
86-
semver.BuildMetaData.CommitsSinceTag = null;
87-
}
88-
else if (!string.IsNullOrEmpty(preReleaseTagName))
83+
if (Context.CurrentCommitTaggedVersion is null || Context.CurrentCommitTaggedVersion?.HasPreReleaseTagWithLabel == true)
8984
{
90-
long? number;
91-
92-
if (semver.PreReleaseTag.Name == preReleaseTagName)
85+
if (!string.IsNullOrEmpty(preReleaseTagName)
86+
&& Context.CurrentCommitTaggedVersion?.PreReleaseTag.Name == preReleaseTagName)
9387
{
94-
number = semver.PreReleaseTag.Number + 1;
88+
semver.PreReleaseTag = Context.CurrentCommitTaggedVersion.PreReleaseTag;
9589
}
96-
else
90+
else if (preReleaseTagName == string.Empty)
9791
{
98-
number = 1;
92+
semver.PreReleaseTag = new SemanticVersionPreReleaseTag();
93+
}
94+
else if (preReleaseTagName != null)
95+
{
96+
long? number;
97+
if (semver.PreReleaseTag.Name == preReleaseTagName)
98+
{
99+
number = semver.PreReleaseTag.Number + 1;
100+
}
101+
else
102+
{
103+
number = 1;
104+
}
105+
semver.PreReleaseTag = new SemanticVersionPreReleaseTag(preReleaseTagName, number);
99106
}
100-
semver.PreReleaseTag = new SemanticVersionPreReleaseTag(preReleaseTagName, number);
107+
}
108+
109+
if (semver.CompareTo(Context.CurrentCommitTaggedVersion) == 0)
110+
{
111+
// Will always be 0, don't bother with the +0 on tags
112+
semver.BuildMetaData.CommitsSinceTag = null;
101113
}
102114

103115
return new(semver, baseVersion, new(nextVersion.Branch, nextVersion.Configuration));
@@ -177,9 +189,7 @@ static NextVersion CompareVersions(
177189

178190
log.Info($"Base version used: {calculatedBase}");
179191

180-
var nextVersion = new NextVersion(maxVersion.IncrementedVersion, calculatedBase, maxVersion.Branch, maxVersion.Configuration);
181-
182-
return nextVersion;
192+
return new NextVersion(maxVersion.IncrementedVersion, calculatedBase, maxVersion.Branch, maxVersion.Configuration);
183193
}
184194
}
185195

@@ -236,10 +246,9 @@ private SemanticVersion GetIncrementedVersion(EffectiveBranchConfiguration effec
236246
baseVersion: baseVersion,
237247
configuration: effectiveBranchConfiguration.Value
238248
);
239-
var incrementedVersion = incrementStrategy == VersionField.None
249+
return incrementStrategy == VersionField.None
240250
? baseVersion.SemanticVersion
241251
: baseVersion.SemanticVersion.IncrementVersion(incrementStrategy);
242-
return incrementedVersion;
243252
}
244253

245254
private bool IncludeVersion(BaseVersion baseVersion, IIgnoreConfiguration ignoreConfiguration)

src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionPreReleaseTag.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public sealed class SemanticVersionPreReleaseTag :
1919

2020
public SemanticVersionPreReleaseTag(string name, long? number)
2121
{
22-
Name = name.NotNullOrEmpty();
22+
Name = name.NotNull();
2323
Number = number;
2424
}
2525

src/GitVersion.Core/VersionCalculation/VariableProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ public VersionVariables GetVariablesFor(SemanticVersion semanticVersion, Effecti
2727
// Continuous Deployment always requires a pre-release tag unless the commit is tagged
2828
if (!semanticVersion.PreReleaseTag.HasTag())
2929
{
30-
semanticVersion.PreReleaseTag.Name = configuration.GetBranchSpecificTag(this.log, semanticVersion.BuildMetaData.Branch, null);
30+
var label = configuration.GetBranchSpecificLabel(this.log, semanticVersion.BuildMetaData.Branch, null);
31+
semanticVersion.PreReleaseTag.Name = label ?? string.Empty;
3132
if (semanticVersion.PreReleaseTag.Name.IsNullOrEmpty())
3233
{
3334
// TODO: Why do we manipulating the semantic version here in the VariableProvider? The method name is GET not MANIPULATE.
3435
// What is about the separation of concern and single-responsibility principle?
35-
semanticVersion.PreReleaseTag.Name = configuration.Label;
36+
semanticVersion.PreReleaseTag.Name = configuration.Label ?? string.Empty;
3637
}
3738
}
3839
}

0 commit comments

Comments
 (0)