Skip to content

Commit 6f81b70

Browse files
committed
Cover commit counting mechanism with tests
Fix #79
1 parent d75fb72 commit 6f81b70

File tree

3 files changed

+215
-2
lines changed

3 files changed

+215
-2
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
using System;
2+
using GitVersion;
3+
using LibGit2Sharp;
4+
using NUnit.Framework;
5+
6+
[TestFixture]
7+
public class VersionByCommitFixture : Lg2sHelperBase
8+
{
9+
/*
10+
* hotfix-1.2.1 -----------C--
11+
* / \
12+
* master A----------------F-----H-------N
13+
* \ / \ /
14+
* hotfix-1.3.1 \ / ----L
15+
* \ / \
16+
* release-1.3.0 \ -D----G--- \
17+
* \ / \ \
18+
* develop -----B----E-------I-----M--O--P
19+
* \ /
20+
* feature -------J-K-
21+
*
22+
*
23+
* - A is tagged `1.2.0`
24+
* - F is tagged `1.2.1`
25+
* - H is tagged `1.3.0`
26+
* - N is tagged `1.3.1`
27+
*/
28+
29+
[Test]
30+
public void CanCorrectlyDetectCommitCountsWhenThatApplies()
31+
{
32+
var repoPath = Clone(CCTestRepoWorkingDirPath);
33+
34+
using (var repo = new Repository(repoPath))
35+
{
36+
ResetToP(repo);
37+
Assert.AreEqual(7, CommitCountFor(repo, "develop"));
38+
39+
ResetToO(repo);
40+
Assert.AreEqual(6, CommitCountFor(repo, "develop"));
41+
42+
ResetToN(repo);
43+
Assert.IsNull(CommitCountFor(repo, "master"));
44+
45+
ResetToM(repo);
46+
Assert.AreEqual(5, CommitCountFor(repo, "develop"));
47+
48+
ResetToL(repo);
49+
Assert.AreEqual(1, CommitCountFor(repo, "hotfix-1.3.1"));
50+
51+
ResetToK(repo);
52+
Assert.AreEqual(2, CommitCountFor(repo, "feature"));
53+
54+
ResetToJ(repo);
55+
Assert.AreEqual(1, CommitCountFor(repo, "feature"));
56+
57+
ResetToI(repo);
58+
Assert.AreEqual(2, CommitCountFor(repo, "develop"));
59+
60+
ResetToH(repo);
61+
Assert.IsNull(CommitCountFor(repo, "master"));
62+
63+
ResetToG(repo);
64+
Assert.AreEqual(2, CommitCountFor(repo, "release-1.3.0"));
65+
66+
ResetToF(repo);
67+
Assert.IsNull(CommitCountFor(repo, "master"));
68+
69+
ResetToE(repo);
70+
Assert.AreEqual(2, CommitCountFor(repo, "develop"));
71+
72+
ResetToD(repo);
73+
Assert.AreEqual(1, CommitCountFor(repo, "release-1.3.0"));
74+
75+
ResetToC(repo);
76+
Assert.AreEqual(1, CommitCountFor(repo, "hotfix-1.2.1"));
77+
78+
ResetToB(repo);
79+
Assert.AreEqual(1, CommitCountFor(repo, "develop"));
80+
}
81+
}
82+
83+
static int? CommitCountFor(Repository repo, string branchName)
84+
{
85+
var gvf = new GitVersionFinder();
86+
var context = new GitVersionContext
87+
{
88+
CurrentBranch = repo.Branches[branchName],
89+
Repository = repo
90+
};
91+
92+
var sv = gvf.FindVersion(context);
93+
94+
var number = sv.BuildMetaData.CommitsSinceTag;
95+
96+
return number;
97+
}
98+
99+
void DropTags(Repository repo, params string[] names)
100+
{
101+
foreach (var name in names)
102+
{
103+
if (repo.Tags[name] == null)
104+
{
105+
continue;
106+
}
107+
108+
repo.Tags.Remove(name);
109+
}
110+
}
111+
112+
void DropBranches(Repository repo, params string[] names)
113+
{
114+
foreach (var name in names)
115+
{
116+
if (repo.Branches[name] == null)
117+
{
118+
continue;
119+
}
120+
121+
repo.Branches.Remove(name);
122+
}
123+
}
124+
125+
void ResetBranch(Repository repo, string name, string committish)
126+
{
127+
var b = repo.Branches[name];
128+
Assert.IsNotNull(b);
129+
repo.Refs.UpdateTarget(b.CanonicalName, committish);
130+
}
131+
132+
void ResetToP(Repository repo)
133+
{
134+
ResetBranch(repo, "develop", "4d65c519f88773854f9345eaf5dbb30cb49f6a74");
135+
}
136+
137+
void ResetToO(Repository repo)
138+
{
139+
ResetBranch(repo, "develop", "7655537837096d925a4f974232f78ec589d86ebd");
140+
}
141+
142+
void ResetToN(Repository repo)
143+
{
144+
ResetBranch(repo, "develop", "0b7a2482ab7d167cefa4ecfc106db001dc5c17ff");
145+
}
146+
147+
void ResetToM(Repository repo)
148+
{
149+
ResetBranch(repo, "develop", "0b7a2482ab7d167cefa4ecfc106db001dc5c17ff");
150+
ResetBranch(repo, "master", "5b84136c848fd48f1f8b3fa4e1b767a1f6101279");
151+
DropTags(repo, "1.3.1");
152+
}
153+
154+
void ResetToL(Repository repo)
155+
{
156+
ResetBranch(repo, "develop", "243f56dcdb543688fd0a99bd3e0e72dd9a786603");
157+
}
158+
159+
void ResetToK(Repository repo)
160+
{
161+
DropBranches(repo, "hotfix-1.3.1");
162+
}
163+
164+
void ResetToJ(Repository repo)
165+
{
166+
ResetBranch(repo, "feature", "0491c5dac30d706f4e54c5cb26d082baad8228d1");
167+
}
168+
169+
void ResetToI(Repository repo)
170+
{
171+
DropBranches(repo, "feature");
172+
}
173+
174+
void ResetToH(Repository repo)
175+
{
176+
ResetBranch(repo, "develop", "320f4b6820cf4b0853dc08ac153f04fbd4958200");
177+
}
178+
179+
void ResetToG(Repository repo)
180+
{
181+
ResetBranch(repo, "master", "576a28e321cd6dc764b52c5fface672fa076f37f");
182+
DropTags(repo, "1.3.0");
183+
}
184+
185+
void ResetToF(Repository repo)
186+
{
187+
ResetBranch(repo, "release-1.3.0", "b53054c614d36edc9d1bee8c35cd2ed575a43607");
188+
}
189+
190+
void ResetToE(Repository repo)
191+
{
192+
ResetBranch(repo, "master", "8c890487ed143d5a72d151e64be1c5ddb314c908");
193+
DropTags(repo, "1.2.1");
194+
}
195+
196+
void ResetToD(Repository repo)
197+
{
198+
ResetBranch(repo, "develop", "fab69e28ee35dd912c0c95d5993dd84e4f2bcd92");
199+
}
200+
201+
void ResetToC(Repository repo)
202+
{
203+
DropBranches(repo, "release-1.3.0");
204+
}
205+
206+
void ResetToB(Repository repo)
207+
{
208+
DropBranches(repo, "hotfix-1.2.1");
209+
}
210+
}

Tests/Helpers/Lg2sHelperBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ static Lg2sHelperBase()
3636
}
3737
}
3838

39-
public static string ASBMTestRepoWorkingDirPath;
40-
public static DirectoryInfo ResourcesDirectory;
39+
protected static string ASBMTestRepoWorkingDirPath { private set; get; }
40+
protected static string CCTestRepoWorkingDirPath { private set; get; }
41+
static DirectoryInfo ResourcesDirectory;
4142

4243
static void SetUpTestEnvironment()
4344
{
@@ -54,6 +55,7 @@ static void SetUpTestEnvironment()
5455

5556
// Setup standard paths to our test repositories
5657
ASBMTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "asbm_wd");
58+
CCTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "commit_counting_wd");
5759
}
5860

5961
protected SelfCleaningDirectory BuildSelfCleaningDirectory()

Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<Compile Include="BranchFinders\DevelopTests.cs" />
104104
<Compile Include="BuildServers\TeamCityTests.cs" />
105105
<Compile Include="BuildServers\ContinuaCiTests.cs" />
106+
<Compile Include="GitFlow\VersionByCommitFixture.cs" />
106107
<Compile Include="GitFlow\GitFlowVersionFinderTests.cs" />
107108
<Compile Include="GitHelperTests.cs" />
108109
<Compile Include="GitVersionTask.cs" />

0 commit comments

Comments
 (0)