Skip to content

Commit 3293a66

Browse files
committed
decompose gitversioncontext
1 parent c5e8936 commit 3293a66

12 files changed

+87
-233
lines changed

AcceptanceTests/RepositoryFixtureBase.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ public ExecutionResults ExecuteGitVersion(bool inProcess = true)
2626
}
2727

2828
var vf = new GitVersionFinder();
29-
var sv = vf.FindVersion(new GitVersionContext
30-
{
31-
Repository = Repository,
32-
CurrentBranch = Repository.Head
33-
});
29+
var sv = vf.FindVersion(new GitVersionContext(Repository));
3430

3531
var vars = VariableProvider.GetVariablesFor(sv);
3632

@@ -51,4 +47,4 @@ public void Dispose()
5147
}
5248
}
5349
}
54-
}
50+
}

GitVersionCore/GitVersionContext.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@
77
/// </summary>
88
public class GitVersionContext
99
{
10-
public IRepository Repository;
11-
public Branch CurrentBranch;
12-
}
10+
public GitVersionContext(IRepository repository)
11+
: this(repository, repository.Head)
12+
{
13+
}
14+
15+
public GitVersionContext(IRepository repository, Branch currentBranch)
16+
{
17+
Repository = repository;
18+
CurrentBranch = currentBranch;
19+
}
1320

21+
public IRepository Repository { get; private set; }
22+
public Branch CurrentBranch { get; private set; }
23+
}
1424
}

GitVersionCore/VersionCache.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ public static SemanticVersion GetVersion(string gitDirectory)
5353
static SemanticVersion GetSemanticVersion(Repository repository)
5454
{
5555
var versionForRepositoryFinder = new GitVersionFinder();
56-
var gitVersionContext = new GitVersionContext
57-
{
58-
CurrentBranch = repository.Head,
59-
Repository = repository
60-
};
56+
var gitVersionContext = new GitVersionContext(repository);
6157
Logger.WriteInfo("Running against branch: " + gitVersionContext.CurrentBranch.Name);
6258
return versionForRepositoryFinder.FindVersion(gitVersionContext);
6359
}

Tests/BranchFinders/DevelopTests.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ public void Commit_on_develop_and_previous_commit_on_master_is_a_hotfix()
2020
{
2121
commitOnDevelop
2222
};
23-
var version = finder.FindVersion(new GitVersionContext
23+
var repository = new MockRepository
2424
{
25-
Repository = new MockRepository
26-
{
27-
Branches = new MockBranchCollection
25+
Branches = new MockBranchCollection
2826
{
2927
new MockBranch("master")
3028
{
@@ -36,12 +34,10 @@ public void Commit_on_develop_and_previous_commit_on_master_is_a_hotfix()
3634
},
3735
mockBranch
3836
},
39-
},
40-
CurrentBranch = mockBranch
41-
});
37+
};
38+
var version = finder.FindVersion(new GitVersionContext(repository, mockBranch));
4239
Assert.AreEqual(2, version.Minor, "Minor should be master.Minor+1");
4340
ObjectApprover.VerifyWithJson(version, Scrubbers.GuidAndDateScrubber);
44-
4541
}
4642

4743
[Test]
@@ -61,33 +57,29 @@ public void Commit_on_develop_and_previous_commit_on_master_has_a_tag()
6157
{
6258
commitOnDevelop
6359
};
64-
var context = new GitVersionContext
60+
var repository = new MockRepository
6561
{
66-
Repository = new MockRepository
67-
{
68-
Branches = new MockBranchCollection
62+
Branches = new MockBranchCollection
6963
{
7064
new MockBranch("master")
7165
{
7266
commitOnMaster
7367
},
7468
develop
7569
},
76-
Tags = new MockTagCollection
70+
Tags = new MockTagCollection
7771
{
7872
new MockTag
7973
{
8074
TargetEx = commitOnMaster,
8175
NameEx = "0.1.0"
8276
}
8377
}
84-
},
85-
CurrentBranch = develop
8678
};
79+
var context = new GitVersionContext(repository, develop);
8780

8881
var version = finder.FindVersion(context);
8982
Assert.AreEqual(2, version.Minor, "Minor should be master.Minor+1");
9083
ObjectApprover.VerifyWithJson(version, Scrubbers.GuidAndDateScrubber);
9184
}
92-
9385
}

Tests/BranchFinders/FeatureBranchTests.cs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ public void Feature_branch_with_no_commit()
2020

2121
var finder = new FeatureVersionFinder();
2222

23-
var version = finder.FindVersion(new GitVersionContext
24-
{
25-
Repository = repo,
26-
CurrentBranch = featureBranch
27-
});
23+
var version = finder.FindVersion(new GitVersionContext(repo, featureBranch));
2824

2925
var masterVersion = FindersHelper.RetrieveMasterVersion(repo);
3026

@@ -49,11 +45,7 @@ public void Feature_branch_with_1_commit()
4945

5046
var finder = new FeatureVersionFinder();
5147

52-
var version = finder.FindVersion(new GitVersionContext
53-
{
54-
Repository = repo,
55-
CurrentBranch = featureBranch
56-
});
48+
var version = finder.FindVersion(new GitVersionContext(repo, featureBranch));
5749

5850
var masterVersion = FindersHelper.RetrieveMasterVersion(repo);
5951

@@ -81,11 +73,7 @@ public void Feature_branch_with_2_commits()
8173

8274
var finder = new FeatureVersionFinder();
8375

84-
var version = finder.FindVersion(new GitVersionContext
85-
{
86-
Repository = repo,
87-
CurrentBranch = featureBranch
88-
});
76+
var version = finder.FindVersion(new GitVersionContext(repo, featureBranch));
8977

9078
var masterVersion = FindersHelper.RetrieveMasterVersion(repo);
9179

@@ -112,11 +100,7 @@ public void Feature_branch_with_2_commits_but_building_an_commit()
112100

113101
var finder = new FeatureVersionFinder();
114102

115-
var version = finder.FindVersion(new GitVersionContext
116-
{
117-
Repository = repo,
118-
CurrentBranch = featureBranch,
119-
});
103+
var version = finder.FindVersion(new GitVersionContext(repo, featureBranch));
120104

121105
var masterVersion = FindersHelper.RetrieveMasterVersion(repo);
122106

@@ -125,4 +109,4 @@ public void Feature_branch_with_2_commits_but_building_an_commit()
125109
ObjectApprover.VerifyWithJson(version, Scrubbers.GuidAndDateScrubber);
126110
}
127111
}
128-
}
112+
}

Tests/BranchFinders/PullBranchTests.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
[TestFixture]
77
public class PullBranchTests : Lg2sHelperBase
88
{
9-
109
[Test, Ignore("Not valid since Github wont allow empty pulls")]
1110
public void Pull_request_with_no_commit()
1211
{
13-
1412
}
1513

16-
1714
[Test]
1815
public void Invalid_pull_branch_name()
1916
{
@@ -38,11 +35,7 @@ void AssertInvalidPullBranchName(string invalidFakePullBranchName)
3835

3936
var finder = new PullVersionFinder();
4037

41-
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext
42-
{
43-
Repository = repo,
44-
CurrentBranch = pullBranch,
45-
}));
38+
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext(repo, pullBranch)));
4639
}
4740
}
4841

@@ -61,11 +54,7 @@ public void Pull_branch_with_1_commit()
6154

6255
var finder = new PullVersionFinder();
6356

64-
var version = finder.FindVersion(new GitVersionContext
65-
{
66-
Repository = repo,
67-
CurrentBranch = pullBranch,
68-
});
57+
var version = finder.FindVersion(new GitVersionContext(repo, pullBranch));
6958

7059
var masterVersion = FindersHelper.RetrieveMasterVersion(repo);
7160

@@ -91,11 +80,7 @@ public void Pull_branch_with_2_commits()
9180

9281
var finder = new PullVersionFinder();
9382

94-
var version = finder.FindVersion(new GitVersionContext
95-
{
96-
Repository = repo,
97-
CurrentBranch = pullBranch,
98-
});
83+
var version = finder.FindVersion(new GitVersionContext(repo, pullBranch));
9984

10085
var masterVersion = FindersHelper.RetrieveMasterVersion(repo);
10186

@@ -104,4 +89,4 @@ public void Pull_branch_with_2_commits()
10489
ObjectApprover.VerifyWithJson(version, Scrubbers.GuidAndDateScrubber);
10590
}
10691
}
107-
}
92+
}

Tests/BranchFinders/ReleaseTests.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ public void EnsureAReleaseBranchNameDoesNotExposeAPatchSegment()
1818

1919
var finder = new ReleaseVersionFinder();
2020

21-
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext
22-
{
23-
Repository = repo,
24-
CurrentBranch = releaseBranch,
25-
}));
21+
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext(repo, releaseBranch)));
2622
}
2723
}
2824

@@ -39,13 +35,10 @@ public void EnsureAReleaseBranchNameDoesNotExposeAStability()
3935

4036
var finder = new ReleaseVersionFinder();
4137

42-
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext
43-
{
44-
Repository = repo,
45-
CurrentBranch = releaseBranch,
46-
}));
38+
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext(repo, releaseBranch)));
4739
}
4840
}
41+
4942
//TODO:
5043
//[Test]
5144
//[ExpectedException]
@@ -60,5 +53,4 @@ public void After_merge_to_master()
6053
//TODO
6154
//Assert.Throws<Exception>(() => FinderWrapper.FindVersionForCommit("TODO", "release-0.5.0"));
6255
}
63-
6456
}

Tests/GitFlow/GitFlowVersionFinderTests.cs

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using GitVersion;
1+
using System.IO;
2+
using GitVersion;
23
using LibGit2Sharp;
34
using NUnit.Framework;
4-
using System.IO;
55
using ObjectApproval;
66

77
[TestFixture]
@@ -19,11 +19,7 @@ public void RequiresALocalMasterBranch()
1919

2020
var finder = new GitVersionFinder();
2121

22-
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext
23-
{
24-
Repository = repo,
25-
CurrentBranch = repo.Head,
26-
}));
22+
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext(repo)));
2723
}
2824
}
2925

@@ -39,11 +35,7 @@ public void RequiresALocalDevelopBranch()
3935

4036
var finder = new GitVersionFinder();
4137

42-
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext
43-
{
44-
Repository = repo,
45-
CurrentBranch = repo.Head,
46-
}));
38+
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext(repo)));
4739
}
4840
}
4941

@@ -64,11 +56,7 @@ public void AFeatureBranchIsRequiredToBranchOffOfDevelopBranch()
6456

6557
var finder = new GitVersionFinder();
6658

67-
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext
68-
{
69-
Repository = repo,
70-
CurrentBranch = feature,
71-
}));
59+
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext(repo, feature)));
7260
}
7361
}
7462

@@ -89,11 +77,7 @@ public void AHotfixBranchIsRequiredToBranchOffOfMasterBranch()
8977

9078
var finder = new GitVersionFinder();
9179

92-
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext
93-
{
94-
Repository = repo,
95-
CurrentBranch = feature,
96-
}));
80+
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext(repo, feature)));
9781
}
9882
}
9983

@@ -114,11 +98,7 @@ public void APullRequestBranchIsRequiredToBranchOffOfDevelopBranch()
11498

11599
var finder = new GitVersionFinder();
116100

117-
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext
118-
{
119-
Repository = repo,
120-
CurrentBranch = pull,
121-
}));
101+
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext(repo, pull)));
122102
}
123103
}
124104

@@ -137,11 +117,7 @@ public void AFeatureBranchDoesNotRequireASpecificPrefix()
137117

138118
var finder = new GitVersionFinder();
139119

140-
var versionAndBranch = finder.FindVersion(new GitVersionContext
141-
{
142-
Repository = repo,
143-
CurrentBranch = repo.Head,
144-
});
120+
var versionAndBranch = finder.FindVersion(new GitVersionContext(repo));
145121

146122
ObjectApprover.VerifyWithJson(versionAndBranch, Scrubbers.GuidAndDateScrubber);
147123
}
@@ -162,11 +138,7 @@ public void AFeatureBranchPrefixIsNotIncludedInTag()
162138

163139
var finder = new GitVersionFinder();
164140

165-
var versionAndBranch = finder.FindVersion(new GitVersionContext
166-
{
167-
Repository = repo,
168-
CurrentBranch = repo.Head,
169-
});
141+
var versionAndBranch = finder.FindVersion(new GitVersionContext(repo));
170142

171143
ObjectApprover.VerifyWithJson(versionAndBranch, Scrubbers.GuidAndDateScrubber);
172144
}
@@ -194,11 +166,7 @@ public void AReleaseBranchIsRequiredToBranchOffOfDevelopBranch()
194166

195167
var finder = new GitVersionFinder();
196168

197-
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext
198-
{
199-
Repository = repo,
200-
CurrentBranch = feature,
201-
}));
169+
Assert.Throws<ErrorException>(() => finder.FindVersion(new GitVersionContext(repo, feature)));
202170
}
203171
}
204-
}
172+
}

0 commit comments

Comments
 (0)