Skip to content

Commit 3192f1e

Browse files
committed
Document unexpected behaviour by extending GitflowComplexExample.
1 parent 6f7e04c commit 3192f1e

File tree

6 files changed

+96
-29
lines changed

6 files changed

+96
-29
lines changed

src/GitVersion.Core.Tests/Extensions/GitRepositoryTestingExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ public static void WriteVersionVariables(this RepositoryFixtureBase fixture, str
137137
}
138138

139139
public static void AssertFullSemver(this RepositoryFixtureBase fixture, string fullSemver,
140-
IGitVersionConfiguration? configuration = null, IRepository? repository = null, string? commitId = null, bool onlyTrackedBranches = true, string? targetBranch = null)
140+
IGitVersionConfiguration? configuration = null, IRepository? repository = null, string? commitId = null, bool onlyTrackedBranches = true, string? targetBranch = null, string? customMessage = null)
141141
{
142142
repository ??= fixture.Repository;
143143

144144
var variables = GetVersion(fixture, configuration, repository, commitId, onlyTrackedBranches, targetBranch);
145-
variables.FullSemVer.ShouldBe(fullSemver);
145+
variables.FullSemVer.ShouldBe(fullSemver, customMessage);
146146
if (commitId == null)
147147
{
148148
fixture.SequenceDiagram.NoteOver(fullSemver, repository.Head.FriendlyName, color: "#D3D3D3");
Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using GitVersion.Configuration;
22
using GitVersion.Core.Tests.Helpers;
3+
using GitVersion.Helpers;
4+
using LibGit2Sharp;
35

46
namespace GitVersion.Core.Tests.IntegrationTests;
57

@@ -9,24 +11,29 @@ public class GitflowScenarios : TestBase
911
[Test]
1012
public void GitflowComplexExample()
1113
{
14+
var keepBranches = true;
1215
const string developBranch = "develop";
1316
const string feature1Branch = "feature/f1";
1417
const string feature2Branch = "feature/f2";
1518
const string release1Branch = "release/1.1.0";
1619
const string release2Branch = "release/1.2.0";
1720
const string hotfixBranch = "hotfix/hf";
21+
1822
var configuration = GitFlowConfigurationBuilder.New.Build();
1923

20-
using var fixture = new BaseGitFlowRepositoryFixture("1.0.0");
21-
fixture.AssertFullSemver("1.1.0-alpha.1", configuration);
24+
using var fixture = new BaseGitFlowRepositoryFixture(initialMainAction, deleteOnDispose: false);
25+
var fullSemver = "1.1.0-alpha.1";
26+
fixture.AssertFullSemver(fullSemver, configuration);
2227

2328
// Feature 1
2429
fixture.BranchTo(feature1Branch);
25-
fixture.MakeACommit("added feature 1");
26-
fixture.AssertFullSemver("1.1.0-f1.1+2", configuration);
30+
31+
fixture.MakeACommit($"added feature 1 >> {fullSemver}");
32+
fullSemver = "1.1.0-f1.1+2";
33+
fixture.AssertFullSemver(fullSemver, configuration);
2734
fixture.Checkout(developBranch);
2835
fixture.MergeNoFF(feature1Branch);
29-
fixture.Repository.Branches.Remove(fixture.Repository.Branches[feature1Branch]);
36+
if (!keepBranches) fixture.Repository.Branches.Remove(fixture.Repository.Branches[feature1Branch]);
3037
fixture.AssertFullSemver("1.1.0-alpha.3", configuration);
3138

3239
// Release 1.1.0
@@ -45,40 +52,99 @@ public void GitflowComplexExample()
4552

4653
// Feature 2
4754
fixture.BranchTo(feature2Branch);
48-
fixture.MakeACommit("added feature 2");
49-
fixture.AssertFullSemver("1.2.0-f2.1+2", configuration);
55+
fullSemver = "1.2.0-f2.1+2";
56+
fixture.MakeACommit($"added feature 2 >> {fullSemver}");
57+
fixture.AssertFullSemver(fullSemver, configuration);
5058
fixture.Checkout(developBranch);
5159
fixture.MergeNoFF(feature2Branch);
52-
fixture.Repository.Branches.Remove(fixture.Repository.Branches[feature2Branch]);
60+
if (!keepBranches) fixture.Repository.Branches.Remove(fixture.Repository.Branches[feature2Branch]);
5361
fixture.AssertFullSemver("1.2.0-alpha.3", configuration);
5462

5563
// Release 1.2.0
5664
fixture.BranchTo(release2Branch);
57-
fixture.MakeACommit("release stabilization");
58-
fixture.AssertFullSemver("1.2.0-beta.1+8", configuration);
65+
fullSemver = "1.2.0-beta.1+8";
66+
fixture.MakeACommit($"release stabilization >> {fullSemver}");
67+
fixture.AssertFullSemver(fullSemver, configuration);
5968
fixture.Checkout(MainBranch);
6069
fixture.MergeNoFF(release2Branch);
6170
fixture.AssertFullSemver("1.2.0-5", configuration);
6271
fixture.ApplyTag("1.2.0");
6372
fixture.AssertFullSemver("1.2.0", configuration);
6473
fixture.Checkout(developBranch);
6574
fixture.MergeNoFF(release2Branch);
66-
fixture.Repository.Branches.Remove(fixture.Repository.Branches[release2Branch]);
75+
if (!keepBranches) fixture.Repository.Branches.Remove(fixture.Repository.Branches[release2Branch]);
6776
fixture.AssertFullSemver("1.3.0-alpha.1", configuration);
6877

6978
// Hotfix
7079
fixture.Checkout(MainBranch);
7180
fixture.BranchTo(hotfixBranch);
72-
fixture.MakeACommit("added hotfix");
73-
fixture.AssertFullSemver("1.2.1-beta.1+1", configuration);
81+
fullSemver = "1.2.1-beta.1+1";
82+
fixture.MakeACommit($"added hotfix >> {fullSemver}");
83+
fixture.AssertFullSemver(fullSemver, configuration);
7484
fixture.Checkout(MainBranch);
7585
fixture.MergeNoFF(hotfixBranch);
7686
fixture.AssertFullSemver("1.2.1-2", configuration);
7787
fixture.ApplyTag("1.2.1");
7888
fixture.AssertFullSemver("1.2.1", configuration);
7989
fixture.Checkout(developBranch);
8090
fixture.MergeNoFF(hotfixBranch);
81-
fixture.Repository.Branches.Remove(fixture.Repository.Branches[hotfixBranch]);
91+
if (!keepBranches) fixture.Repository.Branches.Remove(fixture.Repository.Branches[hotfixBranch]);
8292
fixture.AssertFullSemver("1.3.0-alpha.2", configuration);
93+
94+
fixture.Checkout(feature2Branch);
95+
fixture.AssertFullSemver(
96+
"1.3.0-f2.1+0",
97+
configuration,
98+
customMessage:
99+
"Feature branches use inherited versioning (increment: inherit), " + System.Environment.NewLine +
100+
"and your config inherits from develop." + System.Environment.NewLine + System.Environment.NewLine +
101+
"GitVersion uses the merge base between the feature and develop to determine the version." + System.Environment.NewLine + System.Environment.NewLine +
102+
"As develop progresses (e.g., by releasing 1.2.0), rebuilding old feature branches can" + System.Environment.NewLine +
103+
"produce different versions.");
104+
105+
fullSemver = "1.3.0-f2.1+1";
106+
fixture.MakeACommit(
107+
"feature 2 additional commit after original feature has been merged to develop " + System.Environment.NewLine +
108+
$"and release/1.2.0 has already happened >> {fullSemver}" +
109+
"Problem #1: 1.3.0-f2.1+0 is what I observe when I run dotnet-gitversion 6.3.0 but in the repo the assertion is 1.3.0-f2.1+1" +
110+
"After rebase 1.3.0-f2.1+3 is both what the test asserts and what I observe when I run dotnet-gitversion 6.3.0." +
111+
"Problem #2: I expected to get the same before and after the rebase." +
112+
"" +
113+
"Whether my expectations are correct or not could we at least build upon the documentation I have started to add " +
114+
"as an explanation of observed behaviour. I'm happy to translate an explanation in to test " +
115+
"documentation if you confirm it would be accepted on PR."
116+
);
117+
118+
var identity = new Identity(
119+
fixture.Repository.Head.Tip.Committer.Name,
120+
fixture.Repository.Head.Tip.Committer.Email);
121+
fixture.AssertFullSemver(fullSemver, configuration);
122+
var rebaseResult = fixture.Repository.Rebase.Start(
123+
fixture.Repository.Branches[feature2Branch],
124+
fixture.Repository.Branches[developBranch],
125+
fixture.Repository.Branches[developBranch],
126+
identity,
127+
new RebaseOptions());
128+
while (rebaseResult != null && rebaseResult.Status != RebaseStatus.Complete)
129+
{
130+
rebaseResult = fixture.Repository.Rebase.Continue(identity, new RebaseOptions());
131+
}
132+
133+
fixture.AssertFullSemver(fullSemver, configuration, customMessage: "I expected to get the same before and after the rebase.");
134+
135+
void initialMainAction(IRepository r)
136+
{
137+
if (configuration is GitVersionConfiguration concreteConfig)
138+
{
139+
var yaml = new ConfigurationSerializer().Serialize(concreteConfig);
140+
const string fileName = "GitVersion.yml";
141+
var filePath = FileSystemHelper.Path.Combine(r.Info.Path, "..", fileName);
142+
File.WriteAllText(filePath, yaml);
143+
r.Index.Add(fileName);
144+
r.Index.Write();
145+
}
146+
147+
r.MakeATaggedCommit("1.0.0", $"Initial commit on {MainBranch}");
148+
}
83149
}
84150
}

src/GitVersion.Testing/Fixtures/BaseGitFlowRepositoryFixture.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ public class BaseGitFlowRepositoryFixture : EmptyRepositoryFixture
1212
/// <para>Creates a repo with a develop branch off main which is a single commit ahead of main branch</para>
1313
/// <para>Main will be tagged with the initial version before branching develop</para>
1414
/// </summary>
15-
public BaseGitFlowRepositoryFixture(string initialVersion, string branchName = MainBranch) :
16-
this(r => r.MakeATaggedCommit(initialVersion), branchName)
15+
public BaseGitFlowRepositoryFixture(string initialVersion, string branchName = MainBranch, bool deleteOnDispose = true) :
16+
this(r => r.MakeATaggedCommit(initialVersion), branchName, deleteOnDispose)
1717
{
1818
}
1919

2020
/// <summary>
2121
/// <para>Creates a repo with a develop branch off main which is a single commit ahead of main</para>
2222
/// <para>The initial setup actions will be performed before branching develop</para>
2323
/// </summary>
24-
public BaseGitFlowRepositoryFixture(Action<IRepository> initialMainAction, string branchName = MainBranch) :
25-
base(branchName) => SetupRepo(initialMainAction);
24+
public BaseGitFlowRepositoryFixture(Action<IRepository> initialMainAction, string branchName = MainBranch, bool deleteOnDispose = true) :
25+
base(branchName, deleteOnDispose) => SetupRepo(initialMainAction);
2626

2727
private void SetupRepo(Action<IRepository> initialMainAction)
2828
{
@@ -33,6 +33,6 @@ private void SetupRepo(Action<IRepository> initialMainAction)
3333
initialMainAction(Repository);
3434

3535
Commands.Checkout(Repository, Repository.CreateBranch("develop"));
36-
Repository.MakeACommit();
36+
Repository.MakeACommit("First commit on new branch 'develop'");
3737
}
3838
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
namespace GitVersion.Testing;
22

3-
public class EmptyRepositoryFixture(string branchName = RepositoryFixtureBase.MainBranch) : RepositoryFixtureBase(path => CreateNewRepository(path, branchName));
3+
public class EmptyRepositoryFixture(string branchName = RepositoryFixtureBase.MainBranch, bool deleteOnDispose = true) : RepositoryFixtureBase(path => CreateNewRepository(path, branchName), deleteOnDispose);

src/GitVersion.Testing/Fixtures/RepositoryFixtureBase.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ namespace GitVersion.Testing;
1010
public abstract class RepositoryFixtureBase : IDisposable
1111
{
1212
public const string MainBranch = "master";
13-
public const bool DeleteOnDispose = true;
13+
private readonly bool deleteOnDispose;
1414

15-
protected RepositoryFixtureBase(Func<string, Repository> repositoryBuilder)
16-
: this(repositoryBuilder(FileSystemHelper.Path.GetRepositoryTempPath()))
15+
protected RepositoryFixtureBase(Func<string, Repository> repositoryBuilder, bool deleteOnDispose = true)
16+
: this(repositoryBuilder(FileSystemHelper.Path.GetRepositoryTempPath()), deleteOnDispose)
1717
{
1818
}
1919

20-
protected RepositoryFixtureBase(Repository repository)
20+
protected RepositoryFixtureBase(Repository repository, bool deleteOnDispose = true)
2121
{
2222
SequenceDiagram = new();
2323
Repository = repository.ShouldNotBeNull();
2424
Repository.Config.Set("user.name", "Test");
2525
Repository.Config.Set("user.email", "[email protected]");
26+
this.deleteOnDispose = deleteOnDispose;
2627
}
2728

2829
public Repository Repository { get; }
@@ -50,7 +51,7 @@ protected virtual void Dispose(bool disposing)
5051
Repository.Dispose();
5152
var directoryPath = FileSystemHelper.Path.GetFileName(RepositoryPath);
5253

53-
if (DeleteOnDispose)
54+
if (deleteOnDispose)
5455
{
5556
try
5657
{

src/GitVersion.Testing/GitTestExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ private static Commit CreateFileAndCommit(this IRepository repository, string re
3838
Generate.SignatureNow(), Generate.SignatureNow());
3939
}
4040

41-
public static Tag MakeATaggedCommit(this IRepository repository, string tag)
41+
public static Tag MakeATaggedCommit(this IRepository repository, string tag, string? commitMessage = null)
4242
{
43-
var commit = repository.MakeACommit();
43+
var commit = repository.MakeACommit(commitMessage);
4444
var existingTag = repository.Tags.SingleOrDefault(t => t.FriendlyName == tag);
4545
return existingTag ?? repository.Tags.Add(tag, commit);
4646
}

0 commit comments

Comments
 (0)