From ba14aa70ba54218132e3c202e37967ec6b2f2d77 Mon Sep 17 00:00:00 2001 From: Hardy Hobeck Date: Tue, 21 Mar 2023 19:02:13 +0100 Subject: [PATCH] Fix wrong version generation for ContinuesDeployment actual: 5.0.0-beta.1+3 expected 5.0.0-beta.3 --- .../IntegrationTests/DocumentationSamples.cs | 1 + .../IntegrationTests/OtherScenarios.cs | 40 +++++++++++++++++++ ...ableWhenCurrentCommitIsTagged.approved.txt | 8 ++-- .../VariableProviderTests.cs | 4 +- .../NextVersionCalculator.cs | 4 +- .../VersionCalculation/VariableProvider.cs | 28 ++++++------- 6 files changed, 61 insertions(+), 24 deletions(-) diff --git a/src/GitVersion.Core.Tests/IntegrationTests/DocumentationSamples.cs b/src/GitVersion.Core.Tests/IntegrationTests/DocumentationSamples.cs index 1def653a52..fb492f5ed6 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/DocumentationSamples.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/DocumentationSamples.cs @@ -250,6 +250,7 @@ public void GitFlowSupportHotfixRelease() fixture.SequenceDiagram.NoteOver("Hotfix branches are deleted once merged", "hotfix/1.3.1"); fixture.AssertFullSemver("1.3.1+4"); fixture.ApplyTag("1.3.1"); + fixture.AssertFullSemver("1.3.1"); Console.WriteLine(fixture.SequenceDiagram.GetDiagram()); } diff --git a/src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs index 106a43b5e4..1e57c36502 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs @@ -988,4 +988,44 @@ public void IncreaseVersionWithBumpMessageWhenCommitMessageIncrementIsEnabledAnd // ✅ succeeds as expected fixture.AssertFullSemver("2.0.0-pre.1", configuration); } + + [Test] + public void ShouldProvideTheCorrectVersionEvenIfPreReleaseLabelExistsInTheGitTagMain() + { + var configuration = GitFlowConfigurationBuilder.New + .WithNextVersion("5.0") + .WithSemanticVersionFormat(SemanticVersionFormat.Loose) + .WithBranch("main", _ => _ + .WithLabel("beta") + .WithIncrement(IncrementStrategy.Patch) + .WithVersioningMode(VersioningMode.ContinuousDeployment) + ).Build(); + + using EmptyRepositoryFixture fixture = new("main"); + fixture.MakeACommit(); + fixture.MakeACommit(); + fixture.MakeACommit(); + + // ✅ succeeds as expected + fixture.AssertFullSemver("5.0.0-beta.1", configuration); + + fixture.MakeACommit(); + + // ✅ succeeds as expected + fixture.AssertFullSemver("5.0.0-beta.2", configuration); + + fixture.ApplyTag("5.0.0-beta.3"); + + // ✅ succeeds as expected + fixture.AssertFullSemver("5.0.0-beta.3", configuration); + + fixture.MakeATaggedCommit("5.0.0-rc.1"); + + // ✅ succeeds as expected + fixture.AssertFullSemver("5.0.0-beta.4", configuration); + + fixture.MakeACommit(); + + fixture.AssertFullSemver("5.0.0-beta.5", configuration); + } } diff --git a/src/GitVersion.Core.Tests/VersionCalculation/Approved/VariableProviderTests.ProvidesVariablesInContinuousDeploymentModeForStableWhenCurrentCommitIsTagged.approved.txt b/src/GitVersion.Core.Tests/VersionCalculation/Approved/VariableProviderTests.ProvidesVariablesInContinuousDeploymentModeForStableWhenCurrentCommitIsTagged.approved.txt index e1dccf214f..9da6d708b3 100644 --- a/src/GitVersion.Core.Tests/VersionCalculation/Approved/VariableProviderTests.ProvidesVariablesInContinuousDeploymentModeForStableWhenCurrentCommitIsTagged.approved.txt +++ b/src/GitVersion.Core.Tests/VersionCalculation/Approved/VariableProviderTests.ProvidesVariablesInContinuousDeploymentModeForStableWhenCurrentCommitIsTagged.approved.txt @@ -8,14 +8,14 @@ "PreReleaseLabelWithDash": "", "PreReleaseNumber": null, "WeightedPreReleaseNumber": 0, - "BuildMetaData": 5, - "FullBuildMetaData": "5.Sha.commitSha", + "BuildMetaData": null, + "FullBuildMetaData": "Sha.commitSha", "MajorMinorPatch": "1.2.3", "SemVer": "1.2.3", "AssemblySemVer": "1.2.3.0", "AssemblySemFileVer": "1.2.3.0", - "FullSemVer": "1.2.3+5", - "InformationalVersion": "1.2.3+5.Sha.commitSha", + "FullSemVer": "1.2.3", + "InformationalVersion": "1.2.3+Sha.commitSha", "BranchName": "", "EscapedBranchName": "", "Sha": "commitSha", diff --git a/src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs b/src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs index 0168c2be9e..4b1a156202 100644 --- a/src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs +++ b/src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs @@ -146,9 +146,9 @@ public void ProvidesVariablesInContinuousDeploymentModeForStableWhenCurrentCommi var configuration = new TestEffectiveConfiguration(versioningMode: VersioningMode.ContinuousDeployment); - var vars = this.variableProvider.GetVariablesFor(semVer, configuration, SemanticVersion.Empty); + var variables = this.variableProvider.GetVariablesFor(semVer, configuration, SemanticVersion.Empty); - vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved")); + variables.ToString().ShouldMatchApproved(c => c.SubFolder("Approved")); } [Test] diff --git a/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs b/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs index 218613c1c4..760527802b 100644 --- a/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs +++ b/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs @@ -118,9 +118,7 @@ private NextVersion Calculate(IBranch branch, IGitVersionConfiguration configura using (log.IndentLog("Calculating the base versions")) { var nextVersions = GetNextVersions(branch, configuration).ToArray(); - var maxVersion = nextVersions.Max(); - - maxVersion.NotNull(); + var maxVersion = nextVersions.Max()!; var matchingVersionsOnceIncremented = nextVersions .Where(v => v.BaseVersion.BaseVersionSource != null && v.IncrementedVersion == maxVersion.IncrementedVersion) diff --git a/src/GitVersion.Core/VersionCalculation/VariableProvider.cs b/src/GitVersion.Core/VersionCalculation/VariableProvider.cs index 222448520f..b65280a54b 100644 --- a/src/GitVersion.Core/VersionCalculation/VariableProvider.cs +++ b/src/GitVersion.Core/VersionCalculation/VariableProvider.cs @@ -15,22 +15,19 @@ public class VariableProvider : IVariableProvider public VersionVariables GetVariablesFor( SemanticVersion semanticVersion, EffectiveConfiguration configuration, SemanticVersion? currentCommitTaggedVersion) { + semanticVersion.NotNull(); + configuration.NotNull(); + var preReleaseTagName = semanticVersion.PreReleaseTag.Name; + var isContinuousDeploymentMode = configuration.VersioningMode == VersioningMode.ContinuousDeployment; + + var label = configuration.GetBranchSpecificLabel(semanticVersion.BuildMetaData.Branch, null); + var isCommitTagged = currentCommitTaggedVersion is not null && currentCommitTaggedVersion.IsMatchForBranchSpecificLabel(label); - var isContinuousDeploymentMode = configuration.VersioningMode == VersioningMode.ContinuousDeployment - && currentCommitTaggedVersion is null; - if (isContinuousDeploymentMode) + // Continuous Deployment always requires a pre-release tag unless the commit is tagged + if (isContinuousDeploymentMode && !isCommitTagged && !semanticVersion.PreReleaseTag.HasTag() && preReleaseTagName.IsNullOrEmpty()) { - semanticVersion = new SemanticVersion(semanticVersion); - // Continuous Deployment always requires a pre-release tag unless the commit is tagged - if (!semanticVersion.PreReleaseTag.HasTag()) - { - preReleaseTagName = configuration.GetBranchSpecificLabel(semanticVersion.BuildMetaData.Branch, null); - if (preReleaseTagName.IsNullOrEmpty()) - { - preReleaseTagName = configuration.Label ?? string.Empty; - } - } + preReleaseTagName = label ?? string.Empty; } // Evaluate tag number pattern and append to prerelease tag, preserving build metadata @@ -45,7 +42,7 @@ public VersionVariables GetVariablesFor( } } - if (isContinuousDeploymentMode || appendTagNumberPattern || configuration.VersioningMode == VersioningMode.Mainline) + if ((!isCommitTagged && isContinuousDeploymentMode) || appendTagNumberPattern || configuration.VersioningMode == VersioningMode.Mainline) { semanticVersion = PromoteNumberOfCommitsToTagNumber(semanticVersion, preReleaseTagName); } @@ -60,7 +57,8 @@ public VersionVariables GetVariablesFor( }; } - if (semanticVersion.CompareTo(currentCommitTaggedVersion) == 0) + if (semanticVersion.BuildMetaData.CommitsSinceTag.HasValue + && (isContinuousDeploymentMode || semanticVersion.CompareTo(currentCommitTaggedVersion) == 0)) { // Will always be 0, don't bother with the +0 on tags semanticVersion = new(semanticVersion)