Skip to content

Commit 506db33

Browse files
committed
replaced MockBranchCollection with Substitute.For
1 parent 01c8940 commit 506db33

File tree

3 files changed

+65
-91
lines changed

3 files changed

+65
-91
lines changed

src/GitVersionCore.Tests/Helpers/GitVersionContextBuilder.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using GitTools.Testing;
35
using GitVersion;
46
using GitVersion.Configuration;
@@ -8,6 +10,7 @@
810
using GitVersionCore.Tests.Mocks;
911
using Microsoft.Extensions.DependencyInjection;
1012
using Microsoft.Extensions.Options;
13+
using NSubstitute;
1114

1215
namespace GitVersionCore.Tests
1316
{
@@ -53,7 +56,11 @@ private GitVersionContextBuilder AddBranch(string branchName)
5356
{
5457
new MockCommit()
5558
};
56-
((MockBranchCollection)repository.Branches).Add(mockBranch);
59+
60+
var branches = repository.Branches.ToList();
61+
branches.Add(mockBranch);
62+
repository.Branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)branches).GetEnumerator());
63+
5764
((MockRepository)repository).Head = mockBranch;
5865
return this;
5966
}
@@ -83,12 +90,11 @@ public void Build()
8390
private static IGitRepository CreateRepository()
8491
{
8592
var mockBranch = new MockBranch("master") { new MockCommit { CommitterEx = Generate.SignatureNow() } };
93+
var branches = Substitute.For<IBranchCollection>();
94+
branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)new[] { mockBranch }).GetEnumerator());
8695
var mockRepository = new MockRepository
8796
{
88-
Branches = new MockBranchCollection
89-
{
90-
mockBranch
91-
},
97+
Branches = branches,
9298
Head = mockBranch
9399
};
94100

src/GitVersionCore.Tests/Mocks/MockBranchCollection.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/GitVersionCore.Tests/Model/GitVersionContextTests.cs

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using LibGit2Sharp;
1111
using Microsoft.Extensions.DependencyInjection;
1212
using Microsoft.Extensions.Options;
13+
using NSubstitute;
1314
using NUnit.Framework;
1415
using Shouldly;
1516

@@ -24,18 +25,19 @@ public void CanInheritVersioningMode(VersioningMode mode)
2425
using var fixture = new EmptyRepositoryFixture();
2526

2627
var config = new ConfigurationBuilder()
27-
.Add(new Config { VersioningMode = mode })
28-
.Build();
28+
.Add(new Config { VersioningMode = mode })
29+
.Build();
2930

3031
var branchName = "master";
32+
3133
var mockBranch = new MockBranch(branchName) { new MockCommit { CommitterEx = Generate.SignatureNow() } };
34+
var branches = Substitute.For<IBranchCollection>();
35+
branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)new[] { mockBranch }).GetEnumerator());
36+
3237
var mockRepository = new MockRepository
3338
{
3439
Head = mockBranch,
35-
Branches = new MockBranchCollection
36-
{
37-
mockBranch
38-
}
40+
Branches = branches
3941
};
4042

4143
var context = GetGitVersionContext(fixture.RepositoryPath, mockRepository, branchName, config);
@@ -54,8 +56,8 @@ public void CanInheritIncrement(IncrementStrategy increment, IncrementStrategy?
5456
const string dummyBranchName = "dummy";
5557

5658
var config = new ConfigurationBuilder()
57-
.Add(new Config { Increment = increment })
58-
.Build();
59+
.Add(new Config { Increment = increment })
60+
.Build();
5961

6062
using var fixture = new EmptyRepositoryFixture();
6163
fixture.MakeACommit();
@@ -74,31 +76,31 @@ public void UsesBranchSpecificConfigOverTopLevelDefaults()
7476

7577
var branchName = "develop";
7678
var config = new ConfigurationBuilder()
77-
.Add(new Config
78-
{
79-
VersioningMode = VersioningMode.ContinuousDelivery,
80-
Branches =
81-
{
82-
{
83-
branchName, new BranchConfig
84-
{
85-
VersioningMode = VersioningMode.ContinuousDeployment,
86-
Tag = "alpha"
87-
}
88-
}
89-
}
90-
})
91-
.Build();
92-
79+
.Add(new Config
80+
{
81+
VersioningMode = VersioningMode.ContinuousDelivery,
82+
Branches =
83+
{
84+
{
85+
branchName, new BranchConfig
86+
{
87+
VersioningMode = VersioningMode.ContinuousDeployment,
88+
Tag = "alpha"
89+
}
90+
}
91+
}
92+
})
93+
.Build();
94+
95+
var master = new MockBranch("master") { new MockCommit { CommitterEx = Generate.SignatureNow() } };
9396
var develop = new MockBranch(branchName) { new MockCommit { CommitterEx = Generate.SignatureNow() } };
97+
var branches = Substitute.For<IBranchCollection>();
98+
branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)new[] { master, develop }).GetEnumerator());
99+
94100
var mockRepository = new MockRepository
95101
{
96102
Head = develop,
97-
Branches = new MockBranchCollection
98-
{
99-
new MockBranch("master") { new MockCommit { CommitterEx = Generate.SignatureNow() } },
100-
develop
101-
}
103+
Branches = branches
102104
};
103105

104106
var context = GetGitVersionContext(fixture.RepositoryPath, mockRepository, branchName, config);
@@ -122,27 +124,26 @@ public void UsesFirstBranchConfigWhenMultipleMatch()
122124
SourceBranches = new HashSet<string>()
123125
};
124126
var config = new ConfigurationBuilder()
125-
.Add(new Config
126-
{
127-
VersioningMode = VersioningMode.ContinuousDelivery,
128-
Branches =
129-
{
130-
{ "release/latest", new BranchConfig(branchConfig) { Increment = IncrementStrategy.None, Regex = "release/latest" } },
131-
{ "release", new BranchConfig(branchConfig) { Increment = IncrementStrategy.Patch, Regex = "releases?[/-]" } }
132-
}
133-
})
134-
.Build();
127+
.Add(new Config
128+
{
129+
VersioningMode = VersioningMode.ContinuousDelivery,
130+
Branches =
131+
{
132+
{ "release/latest", new BranchConfig(branchConfig) { Increment = IncrementStrategy.None, Regex = "release/latest" } },
133+
{ "release", new BranchConfig(branchConfig) { Increment = IncrementStrategy.Patch, Regex = "releases?[/-]" } }
134+
}
135+
})
136+
.Build();
135137

136138
var releaseLatestBranch = new MockBranch("release/latest") { new MockCommit { CommitterEx = Generate.SignatureNow() } };
137139
var releaseVersionBranch = new MockBranch("release/1.0.0") { new MockCommit { CommitterEx = Generate.SignatureNow() } };
138140

141+
var branches = Substitute.For<IBranchCollection>();
142+
branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)new[] { releaseLatestBranch, releaseVersionBranch }).GetEnumerator());
143+
139144
var mockRepository = new MockRepository
140145
{
141-
Branches = new MockBranchCollection
142-
{
143-
releaseLatestBranch,
144-
releaseVersionBranch
145-
},
146+
Branches = branches,
146147
Head = releaseLatestBranch
147148
};
148149

@@ -158,15 +159,15 @@ public void UsesFirstBranchConfigWhenMultipleMatch()
158159
public void CanFindParentBranchForInheritingIncrementStrategy()
159160
{
160161
var config = new ConfigurationBuilder()
161-
.Add(new Config
162-
{
163-
Branches =
164-
{
165-
{ "develop", new BranchConfig { Increment = IncrementStrategy.Major } },
166-
{ "feature", new BranchConfig { Increment = IncrementStrategy.Inherit } }
167-
}
168-
})
169-
.Build();
162+
.Add(new Config
163+
{
164+
Branches =
165+
{
166+
{ "develop", new BranchConfig { Increment = IncrementStrategy.Major } },
167+
{ "feature", new BranchConfig { Increment = IncrementStrategy.Inherit } }
168+
}
169+
})
170+
.Build();
170171

171172
using var fixture = new EmptyRepositoryFixture();
172173
fixture.Repository.MakeACommit();

0 commit comments

Comments
 (0)