Skip to content

Commit fbefac6

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

File tree

3 files changed

+214
-2
lines changed

3 files changed

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

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)