Skip to content

Commit

Permalink
Fix broken unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HHobeck committed Mar 12, 2023
1 parent 6baa590 commit 794ba6a
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GitVersion.Configuration;
using GitVersion.Core.Tests.Helpers;
using LibGit2Sharp;

Expand Down Expand Up @@ -73,27 +74,33 @@ public void WhenSupportIsBranchedAndTaggedFromAnotherSupportEnsureNewMinorIsUsed
[Test]
public void WhenSupportIsBranchedFromMainWithSpecificTag()
{
var configuration = GitFlowConfigurationBuilder.New.Build();

using var fixture = new EmptyRepositoryFixture();
fixture.Repository.MakeACommit();

fixture.MakeACommit();
fixture.AssertFullSemver("0.0.1+1");

fixture.Repository.ApplyTag("1.4.0-rc");
fixture.Repository.MakeACommit();
fixture.Repository.CreateBranch("support/1");
Commands.Checkout(fixture.Repository, "support/1");
fixture.AssertFullSemver("1.4.0+1");
fixture.ApplyTag("1.4.0-rc");
fixture.MakeACommit();
fixture.BranchTo("support/1");

fixture.AssertFullSemver("1.4.0+1", configuration);
}

[Test]
public void WhenSupportIsBranchedFromMainWithSpecificTagOnCommit()
{
var configuration = GitFlowConfigurationBuilder.New.Build();

using var fixture = new EmptyRepositoryFixture();
fixture.Repository.MakeACommit();
fixture.AssertFullSemver("0.0.1+1");

fixture.Repository.ApplyTag("1.4.0-rc");
fixture.Repository.CreateBranch("support/1");
Commands.Checkout(fixture.Repository, "support/1");
fixture.AssertFullSemver("1.4.0");
fixture.MakeACommit();
fixture.AssertFullSemver("0.0.1+1", configuration);

fixture.ApplyTag("1.4.0-rc");
fixture.BranchTo("support/1");

fixture.AssertFullSemver("1.4.0+0", configuration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace GitVersion.Core.Tests;
[TestFixture]
public class SemanticVersionTests : TestBase
{
[TestCase("1.2.3", 1, 2, 3, null, null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3", 1, 2, 3, "", null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3-beta", 1, 2, 3, "beta", null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3-beta3", 1, 2, 3, "beta", 3, null, null, null, null, "1.2.3-beta.3", null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3-beta.3", 1, 2, 3, "beta", 3, null, null, null, null, "1.2.3-beta.3", null, SemanticVersionFormat.Strict)]
Expand All @@ -19,25 +19,24 @@ public class SemanticVersionTests : TestBase
[TestCase("1.2.3-rc3.1", 1, 2, 3, "rc3", 1, null, null, null, null, "1.2.3-rc3.1", null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3-beta3f", 1, 2, 3, "beta3f", null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3-notAStability1", 1, 2, 3, "notAStability", 1, null, null, null, null, "1.2.3-notAStability.1", null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3+4", 1, 2, 3, null, null, 4, null, null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3+4.Branch.Foo", 1, 2, 3, null, null, 4, "Foo", null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3+randomMetaData", 1, 2, 3, null, null, null, null, null, "randomMetaData", null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3+4", 1, 2, 3, "", null, 4, null, null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3+4.Branch.Foo", 1, 2, 3, "", null, 4, "Foo", null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3+randomMetaData", 1, 2, 3, "", null, null, null, null, "randomMetaData", null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3-beta.1+4.Sha.12234.Othershiz", 1, 2, 3, "beta", 1, 4, null, "12234", "Othershiz", null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3", 1, 2, 3, null, null, null, null, null, null, null, ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
[TestCase("v1.2.3", 1, 2, 3, null, null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
[TestCase("V1.2.3", 1, 2, 3, null, null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
[TestCase("version-1.2.3", 1, 2, 3, null, null, null, null, null, null, "1.2.3", "version-", SemanticVersionFormat.Strict)]
[TestCase("1.2.3", 1, 2, 3, "", null, null, null, null, null, null, ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
[TestCase("v1.2.3", 1, 2, 3, "", null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
[TestCase("V1.2.3", 1, 2, 3, "", null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
[TestCase("version-1.2.3", 1, 2, 3, "", null, null, null, null, null, "1.2.3", "version-", SemanticVersionFormat.Strict)]
[TestCase("1.0.0-develop-20201007113711", 1, 0, 0, "develop-20201007113711", null, null, null, null, null, "1.0.0-develop-20201007113711", null, SemanticVersionFormat.Strict)]
[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)]

[TestCase("1.2", 1, 2, 0, null, null, null, null, null, null, "1.2.0", null, SemanticVersionFormat.Loose)]
[TestCase("1.2", 1, 2, 0, "", null, null, null, null, null, "1.2.0", null, SemanticVersionFormat.Loose)]
[TestCase("1.2-alpha4", 1, 2, 0, "alpha", 4, null, null, null, null, "1.2.0-alpha.4", null, SemanticVersionFormat.Loose)]
[TestCase("01.02.03-rc03", 1, 2, 3, "rc", 3, null, null, null, null, "1.2.3-rc.3", null, SemanticVersionFormat.Loose)]
[TestCase("1.2.3.4", 1, 2, 3, null, null, 4, null, null, null, "1.2.3+4", null, SemanticVersionFormat.Loose)]
[TestCase("1", 1, 0, 0, null, null, null, null, null, null, "1.0.0", null, SemanticVersionFormat.Loose)]
[TestCase("1.1", 1, 1, 0, null, null, null, null, null, null, "1.1.0", null, SemanticVersionFormat.Loose)]
[TestCase("1.2.3.4", 1, 2, 3, "", null, 4, null, null, null, "1.2.3+4", null, SemanticVersionFormat.Loose)]
[TestCase("1", 1, 0, 0, "", null, null, null, null, null, "1.0.0", null, SemanticVersionFormat.Loose)]
[TestCase("1.1", 1, 1, 0, "", null, null, null, null, null, "1.1.0", null, SemanticVersionFormat.Loose)]
public void ValidateVersionParsing(
string? versionString, long major, long minor, long patch, string? tag, long? tagNumber, long? numberOfBuilds,
string? versionString, long major, long minor, long patch, string label, long? tagNumber, long? numberOfBuilds,
string? branchName, string? sha, string? otherMetaData, string? fullFormattedVersionString, string? tagPrefixRegex, SemanticVersionFormat format = SemanticVersionFormat.Strict)
{
fullFormattedVersionString ??= versionString;
Expand All @@ -51,7 +50,7 @@ public void ValidateVersionParsing(
Assert.That(version.Major, Is.EqualTo(major));
Assert.That(version.Minor, Is.EqualTo(minor));
Assert.That(version.Patch, Is.EqualTo(patch));
Assert.That(version.PreReleaseTag.Name, Is.EqualTo(tag));
Assert.That(version.PreReleaseTag.Name, Is.EqualTo(label));
Assert.That(version.PreReleaseTag.Number, Is.EqualTo(tagNumber));
Assert.That(version.BuildMetaData.CommitsSinceTag, Is.EqualTo(numberOfBuilds));
Expand Down
35 changes: 23 additions & 12 deletions src/GitVersion.Core/Configuration/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,43 @@ public static bool IsReleaseBranch(this IGitVersionConfiguration configuration,
public static bool IsReleaseBranch(this IGitVersionConfiguration configuration, ReferenceName branchName)
=> configuration.GetBranchConfiguration(branchName).IsReleaseBranch ?? false;

public static string GetBranchSpecificTag(this EffectiveConfiguration configuration, ILog log, string? branchFriendlyName,
string? branchNameOverride)
public static string? GetBranchSpecificLabel(
this EffectiveConfiguration configuration, ILog log, ReferenceName branchName, string? branchNameOverride)
=> GetBranchSpecificLabel(configuration, log, branchName.WithoutOrigin, branchNameOverride);

public static string? GetBranchSpecificLabel(
this EffectiveConfiguration configuration, ILog log, string? branchName, string? branchNameOverride)
{
var tagToUse = configuration.Label;
if (tagToUse == "useBranchName")
configuration.NotNull();

//if (branchName == "(no branch)") return string.Empty;

var label = configuration.Label;
if (label == "useBranchName")
{
tagToUse = ConfigurationConstants.BranchNamePlaceholder;
label = ConfigurationConstants.BranchNamePlaceholder;
}

if (tagToUse.Contains(ConfigurationConstants.BranchNamePlaceholder))
if (label?.Contains(ConfigurationConstants.BranchNamePlaceholder) == true)
{
log.Info("Using branch name to calculate version tag");

var branchName = branchNameOverride ?? branchFriendlyName;
var value = branchNameOverride ?? branchName;

if (!configuration.BranchPrefixToTrim.IsNullOrWhiteSpace())
{
var branchNameTrimmed = branchName?.RegexReplace(configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
branchName = branchNameTrimmed.IsNullOrEmpty() ? branchName : branchNameTrimmed;
var branchNameTrimmed = value?.RegexReplace(
configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase
);
value = branchNameTrimmed.IsNullOrEmpty() ? value : branchNameTrimmed;
}

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

tagToUse = tagToUse.Replace(ConfigurationConstants.BranchNamePlaceholder, branchName);
label = label.Replace(ConfigurationConstants.BranchNamePlaceholder, value);
}

return tagToUse;
return label;
}

public static (string GitDirectory, string WorkingTreeDirectory)? FindGitDir(this string path)
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersion.Core/Configuration/EffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public EffectiveConfiguration(IGitVersionConfiguration configuration, IBranchCon
AssemblyFileVersioningFormat = configuration.AssemblyFileVersioningFormat;
VersioningMode = branchConfiguration.VersioningMode.Value;
LabelPrefix = configuration.LabelPrefix;
Label = branchConfiguration.Label ?? string.Empty;
Label = branchConfiguration.Label;
NextVersion = configuration.NextVersion;
Increment = branchConfiguration.Increment;
BranchPrefixToTrim = branchConfiguration.RegularExpression;
Expand Down Expand Up @@ -140,7 +140,7 @@ protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningSche
/// <summary>
/// Label to use when calculating SemVer
/// </summary>
public string Label { get; }
public string? Label { get; }

public string? NextVersion { get; }

Expand Down
5 changes: 3 additions & 2 deletions src/GitVersion.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ GitVersion.Configuration.EffectiveConfiguration.EffectiveConfiguration(GitVersio
GitVersion.Configuration.EffectiveConfiguration.Increment.get -> GitVersion.IncrementStrategy
GitVersion.Configuration.EffectiveConfiguration.IsMainline.get -> bool
GitVersion.Configuration.EffectiveConfiguration.IsReleaseBranch.get -> bool
GitVersion.Configuration.EffectiveConfiguration.Label.get -> string!
GitVersion.Configuration.EffectiveConfiguration.Label.get -> string?
GitVersion.Configuration.EffectiveConfiguration.LabelNumberPattern.get -> string?
GitVersion.Configuration.EffectiveConfiguration.LabelPreReleaseWeight.get -> int
GitVersion.Configuration.EffectiveConfiguration.LabelPrefix.get -> string?
Expand Down Expand Up @@ -1076,7 +1076,8 @@ static GitVersion.Configuration.BranchConfigurationBuilder.New.get -> GitVersion
static GitVersion.Configuration.ConfigurationExtensions.FindGitDir(this string! path) -> (string! GitDirectory, string! WorkingTreeDirectory)?
static GitVersion.Configuration.ConfigurationExtensions.GetBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.IBranch! branch) -> GitVersion.Configuration.IBranchConfiguration!
static GitVersion.Configuration.ConfigurationExtensions.GetBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.ReferenceName! branchName) -> GitVersion.Configuration.IBranchConfiguration!
static GitVersion.Configuration.ConfigurationExtensions.GetBranchSpecificTag(this GitVersion.Configuration.EffectiveConfiguration! configuration, GitVersion.Logging.ILog! log, string? branchFriendlyName, string? branchNameOverride) -> string!
static GitVersion.Configuration.ConfigurationExtensions.GetBranchSpecificLabel(this GitVersion.Configuration.EffectiveConfiguration! configuration, GitVersion.Logging.ILog! log, GitVersion.ReferenceName! branchName, string? branchNameOverride) -> string?
static GitVersion.Configuration.ConfigurationExtensions.GetBranchSpecificLabel(this GitVersion.Configuration.EffectiveConfiguration! configuration, GitVersion.Logging.ILog! log, string? branchName, string? branchNameOverride) -> string?
static GitVersion.Configuration.ConfigurationExtensions.GetEffectiveConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.IBranch! branch) -> GitVersion.Configuration.EffectiveConfiguration!
static GitVersion.Configuration.ConfigurationExtensions.GetFallbackBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration) -> GitVersion.Configuration.IBranchConfiguration!
static GitVersion.Configuration.ConfigurationExtensions.GetReleaseBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration) -> System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string!, GitVersion.Configuration.IBranchConfiguration!>>!
Expand Down
49 changes: 29 additions & 20 deletions src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public virtual NextVersion FindVersion()

var nextVersion = Calculate(Context.CurrentBranch, Context.Configuration);
var baseVersion = nextVersion.BaseVersion;
var preReleaseTagName = nextVersion.Configuration.GetBranchSpecificTag(
this.log, Context.CurrentBranch.Name.WithoutOrigin, baseVersion.BranchNameOverride
var preReleaseTagName = nextVersion.Configuration.GetBranchSpecificLabel(
this.log, Context.CurrentBranch.Name, baseVersion.BranchNameOverride
);

SemanticVersion semver;
Expand Down Expand Up @@ -80,24 +80,36 @@ public virtual NextVersion FindVersion()
}
}

if (semver.CompareTo(Context.CurrentCommitTaggedVersion) == 0)
{
// Will always be 0, don't bother with the +0 on tags
semver.BuildMetaData.CommitsSinceTag = null;
}
else if (!string.IsNullOrEmpty(preReleaseTagName))
if (Context.CurrentCommitTaggedVersion is null || Context.CurrentCommitTaggedVersion?.HasPreReleaseTagWithLabel == true)
{
long? number;

if (semver.PreReleaseTag.Name == preReleaseTagName)
if (!string.IsNullOrEmpty(preReleaseTagName)
&& Context.CurrentCommitTaggedVersion?.PreReleaseTag.Name == preReleaseTagName)
{
number = semver.PreReleaseTag.Number + 1;
semver.PreReleaseTag = Context.CurrentCommitTaggedVersion.PreReleaseTag;
}
else
else if (preReleaseTagName == string.Empty)
{
number = 1;
semver.PreReleaseTag = new SemanticVersionPreReleaseTag();
}
else if (preReleaseTagName != null)
{
long? number;
if (semver.PreReleaseTag.Name == preReleaseTagName)
{
number = semver.PreReleaseTag.Number + 1;
}
else
{
number = 1;
}
semver.PreReleaseTag = new SemanticVersionPreReleaseTag(preReleaseTagName, number);
}
semver.PreReleaseTag = new SemanticVersionPreReleaseTag(preReleaseTagName, number);
}

if (semver.CompareTo(Context.CurrentCommitTaggedVersion) == 0)
{
// Will always be 0, don't bother with the +0 on tags
semver.BuildMetaData.CommitsSinceTag = null;
}

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

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

var nextVersion = new NextVersion(maxVersion.IncrementedVersion, calculatedBase, maxVersion.Branch, maxVersion.Configuration);

return nextVersion;
return new NextVersion(maxVersion.IncrementedVersion, calculatedBase, maxVersion.Branch, maxVersion.Configuration);
}
}

Expand Down Expand Up @@ -236,10 +246,9 @@ private SemanticVersion GetIncrementedVersion(EffectiveBranchConfiguration effec
baseVersion: baseVersion,
configuration: effectiveBranchConfiguration.Value
);
var incrementedVersion = incrementStrategy == VersionField.None
return incrementStrategy == VersionField.None
? baseVersion.SemanticVersion
: baseVersion.SemanticVersion.IncrementVersion(incrementStrategy);
return incrementedVersion;
}

private bool IncludeVersion(BaseVersion baseVersion, IIgnoreConfiguration ignoreConfiguration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class SemanticVersionPreReleaseTag :

public SemanticVersionPreReleaseTag(string name, long? number)
{
Name = name.NotNullOrEmpty();
Name = name.NotNull();
Number = number;
}

Expand Down
4 changes: 2 additions & 2 deletions src/GitVersion.Core/VersionCalculation/VariableProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public VersionVariables GetVariablesFor(SemanticVersion semanticVersion, Effecti
// Continuous Deployment always requires a pre-release tag unless the commit is tagged
if (!semanticVersion.PreReleaseTag.HasTag())
{
semanticVersion.PreReleaseTag.Name = configuration.GetBranchSpecificTag(this.log, semanticVersion.BuildMetaData.Branch, null);
semanticVersion.PreReleaseTag.Name = configuration.GetBranchSpecificLabel(this.log, semanticVersion.BuildMetaData.Branch, null);
if (semanticVersion.PreReleaseTag.Name.IsNullOrEmpty())
{
// TODO: Why do we manipulating the semantic version here in the VariableProvider? The method name is GET not MANIPULATE.
// What is about the separation of concern and single-responsibility principle?
semanticVersion.PreReleaseTag.Name = configuration.Label;
semanticVersion.PreReleaseTag.Name = configuration.Label ?? string.Empty;
}
}
}
Expand Down

0 comments on commit 794ba6a

Please sign in to comment.