Skip to content

Commit cb0b9e0

Browse files
committed
replaced MockBranch with Substitute.For
1 parent 506db33 commit cb0b9e0

File tree

8 files changed

+78
-230
lines changed

8 files changed

+78
-230
lines changed

src/GitVersionCore.Tests/Extensions/GitToolsTestingExtensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45
using GitTools.Testing;
@@ -13,12 +14,32 @@
1314
using LibGit2Sharp;
1415
using Microsoft.Extensions.DependencyInjection;
1516
using Microsoft.Extensions.Options;
17+
using NSubstitute;
1618
using Shouldly;
1719

1820
namespace GitVersionCore.Tests
1921
{
2022
public static class GitToolsTestingExtensions
2123
{
24+
public static IBranch CreateMockBranch(string name, params ICommit[] commits)
25+
{
26+
var branch = Substitute.For<IBranch>();
27+
branch.FriendlyName.Returns(name);
28+
branch.CanonicalName.Returns(name);
29+
branch.NameWithoutOrigin.Returns(name);
30+
branch.NameWithoutRemote.Returns(name);
31+
branch.IsTracking.Returns(true);
32+
branch.IsRemote.Returns(false);
33+
branch.IsDetachedHead.Returns(false);
34+
branch.Tip.Returns(commits.FirstOrDefault());
35+
36+
var commitsCollection = Substitute.For<ICommitCollection>();
37+
commitsCollection.GetEnumerator().Returns(_ => ((IEnumerable<ICommit>)commits).GetEnumerator());
38+
commitsCollection.GetCommitsPriorTo(Arg.Any<DateTimeOffset>()).Returns(commits);
39+
branch.Commits.Returns(commitsCollection);
40+
return branch;
41+
}
42+
2243
public static IBranch FindBranch(this IGitRepository repository, string branchName)
2344
{
2445
return repository.Branches.FirstOrDefault(x => x.NameWithoutRemote == branchName);

src/GitVersionCore.Tests/Helpers/GitVersionContextBuilder.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ private GitVersionContextBuilder WithBranch(string branchName)
5252

5353
private GitVersionContextBuilder AddBranch(string branchName)
5454
{
55-
var mockBranch = new MockBranch(branchName)
56-
{
57-
new MockCommit()
58-
};
55+
var mockBranch = GitToolsTestingExtensions.CreateMockBranch(branchName, new MockCommit());
5956

6057
var branches = repository.Branches.ToList();
6158
branches.Add(mockBranch);
@@ -89,7 +86,8 @@ public void Build()
8986

9087
private static IGitRepository CreateRepository()
9188
{
92-
var mockBranch = new MockBranch("master") { new MockCommit { CommitterEx = Generate.SignatureNow() } };
89+
var mockCommit = new MockCommit { CommitterEx = Generate.SignatureNow() };
90+
var mockBranch = GitToolsTestingExtensions.CreateMockBranch("master", mockCommit);
9391
var branches = Substitute.For<IBranchCollection>();
9492
branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)new[] { mockBranch }).GetEnumerator());
9593
var mockRepository = new MockRepository

src/GitVersionCore.Tests/Mocks/MockBranch.cs

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

src/GitVersionCore.Tests/Mocks/MockCommitCollection.cs

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

src/GitVersionCore.Tests/Mocks/MockRepository.cs

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,14 @@ public class MockRepository : IGitRepository
1212
public IBranch Head { get; set; }
1313
public ITagCollection Tags => Substitute.For<ITagCollection>();
1414
public IReferenceCollection Refs => Substitute.For<IReferenceCollection>();
15-
1615
public IBranchCollection Branches { get; set; }
1716
public ICommitCollection Commits
1817
{
1918
get => commits ?? Head.Commits;
2019
set => commits = value;
2120
}
2221

23-
public string Path { get; }
24-
public string WorkingDirectory { get; }
25-
public bool IsHeadDetached { get; }
26-
public IGitRepository CreateNew(string gitRootPath)
27-
{
28-
throw new NotImplementedException();
29-
}
3022
public int GetNumberOfUncommittedChanges() => 0;
31-
public ICommit FindMergeBase(ICommit commit, ICommit otherCommit) => throw new NotImplementedException();
32-
public string ShortenObjectId(ICommit commit) => throw new NotImplementedException();
33-
public bool GitRepoHasMatchingRemote(string targetUrl) => throw new NotImplementedException();
34-
public void CleanupDuplicateOrigin(string gitRootPath, string remoteName) => throw new NotImplementedException();
35-
public bool GetMatchingCommitBranch(ICommit baseVersionSource, IBranch branch, ICommit firstMatchingCommit)
36-
{
37-
throw new NotImplementedException();
38-
}
39-
public IEnumerable<ICommit> GetCommitsReacheableFrom(ICommit commit, IBranch branch)
40-
{
41-
throw new NotImplementedException();
42-
}
4323
public IEnumerable<ICommit> GetCommitsReacheableFromHead(ICommit headCommit)
4424
{
4525
var filter = new CommitFilter
@@ -52,46 +32,26 @@ public IEnumerable<ICommit> GetCommitsReacheableFromHead(ICommit headCommit)
5232

5333
return commitCollection.ToList();
5434
}
55-
public ICommit GetForwardMerge(ICommit commitToFindCommonBase, ICommit findMergeBase)
56-
{
57-
throw new NotImplementedException();
58-
}
59-
public IEnumerable<ICommit> GetMergeBaseCommits(ICommit mergeCommit, ICommit mergedHead, ICommit findMergeBase)
60-
{
61-
throw new NotImplementedException();
62-
}
63-
public ICommit GetBaseVersionSource(ICommit currentBranchTip)
64-
{
65-
throw new NotImplementedException();
66-
}
67-
public IEnumerable<ICommit> GetMainlineCommitLog(ICommit baseVersionSource, ICommit mainlineTip)
68-
{
69-
throw new NotImplementedException();
70-
}
71-
public IEnumerable<ICommit> GetCommitLog(ICommit baseVersionSource, ICommit currentCommit)
72-
{
73-
throw new NotImplementedException();
74-
}
75-
public void Checkout(string commitOrBranchSpec)
76-
{
77-
throw new NotImplementedException();
78-
}
79-
public void Checkout(IBranch branch)
80-
{
81-
throw new NotImplementedException();
82-
}
83-
public void Fetch(string remote, IEnumerable<string> refSpecs, AuthenticationInfo auth, string logMessage)
84-
{
85-
throw new NotImplementedException();
86-
}
87-
public void CreateBranchForPullRequestBranch(AuthenticationInfo auth)
88-
{
89-
throw new NotImplementedException();
90-
}
91-
public string Clone(string sourceUrl, string workdirPath, AuthenticationInfo auth)
92-
{
93-
throw new NotImplementedException();
94-
}
35+
public string Path => throw new NotImplementedException();
36+
public string WorkingDirectory => throw new NotImplementedException();
37+
public bool IsHeadDetached => throw new NotImplementedException();
38+
public IGitRepository CreateNew(string gitRootPath) => throw new NotImplementedException();
39+
public ICommit FindMergeBase(ICommit commit, ICommit otherCommit) => throw new NotImplementedException();
40+
public string ShortenObjectId(ICommit commit) => throw new NotImplementedException();
41+
public bool GitRepoHasMatchingRemote(string targetUrl) => throw new NotImplementedException();
42+
public void CleanupDuplicateOrigin(string gitRootPath, string remoteName) => throw new NotImplementedException();
43+
public bool GetMatchingCommitBranch(ICommit baseVersionSource, IBranch branch, ICommit firstMatchingCommit) => throw new NotImplementedException();
44+
public IEnumerable<ICommit> GetCommitsReacheableFrom(ICommit commit, IBranch branch) => throw new NotImplementedException();
45+
public ICommit GetForwardMerge(ICommit commitToFindCommonBase, ICommit findMergeBase) => throw new NotImplementedException();
46+
public IEnumerable<ICommit> GetMergeBaseCommits(ICommit mergeCommit, ICommit mergedHead, ICommit findMergeBase) => throw new NotImplementedException();
47+
public ICommit GetBaseVersionSource(ICommit currentBranchTip) => throw new NotImplementedException();
48+
public IEnumerable<ICommit> GetMainlineCommitLog(ICommit baseVersionSource, ICommit mainlineTip) => throw new NotImplementedException();
49+
public IEnumerable<ICommit> GetCommitLog(ICommit baseVersionSource, ICommit currentCommit) => throw new NotImplementedException();
50+
public void Checkout(string commitOrBranchSpec) => throw new NotImplementedException();
51+
public void Checkout(IBranch branch) => throw new NotImplementedException();
52+
public void Fetch(string remote, IEnumerable<string> refSpecs, AuthenticationInfo auth, string logMessage) => throw new NotImplementedException();
53+
public void CreateBranchForPullRequestBranch(AuthenticationInfo auth) => throw new NotImplementedException();
54+
public string Clone(string sourceUrl, string workdirPath, AuthenticationInfo auth) => throw new NotImplementedException();
9555
public IRemote EnsureOnlyOneRemoteIsDefined() => throw new NotImplementedException();
9656
public void Dispose() => throw new NotImplementedException();
9757
}

src/GitVersionCore.Tests/Model/GitVersionContextTests.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ public void CanInheritVersioningMode(VersioningMode mode)
2828
.Add(new Config { VersioningMode = mode })
2929
.Build();
3030

31-
var branchName = "master";
31+
const string branchName = "master";
32+
33+
var mockCommit = new MockCommit { CommitterEx = Generate.SignatureNow() };
34+
var mockBranch = GitToolsTestingExtensions.CreateMockBranch(branchName, mockCommit);
3235

33-
var mockBranch = new MockBranch(branchName) { new MockCommit { CommitterEx = Generate.SignatureNow() } };
3436
var branches = Substitute.For<IBranchCollection>();
3537
branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)new[] { mockBranch }).GetEnumerator());
3638

@@ -74,7 +76,7 @@ public void UsesBranchSpecificConfigOverTopLevelDefaults()
7476
{
7577
using var fixture = new EmptyRepositoryFixture();
7678

77-
var branchName = "develop";
79+
const string branchName = "develop";
7880
var config = new ConfigurationBuilder()
7981
.Add(new Config
8082
{
@@ -92,8 +94,9 @@ public void UsesBranchSpecificConfigOverTopLevelDefaults()
9294
})
9395
.Build();
9496

95-
var master = new MockBranch("master") { new MockCommit { CommitterEx = Generate.SignatureNow() } };
96-
var develop = new MockBranch(branchName) { new MockCommit { CommitterEx = Generate.SignatureNow() } };
97+
var master = GitToolsTestingExtensions.CreateMockBranch("master", new MockCommit { CommitterEx = Generate.SignatureNow() });
98+
var develop = GitToolsTestingExtensions.CreateMockBranch(branchName, new MockCommit { CommitterEx = Generate.SignatureNow() });
99+
97100
var branches = Substitute.For<IBranchCollection>();
98101
branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)new[] { master, develop }).GetEnumerator());
99102

@@ -135,8 +138,8 @@ public void UsesFirstBranchConfigWhenMultipleMatch()
135138
})
136139
.Build();
137140

138-
var releaseLatestBranch = new MockBranch("release/latest") { new MockCommit { CommitterEx = Generate.SignatureNow() } };
139-
var releaseVersionBranch = new MockBranch("release/1.0.0") { new MockCommit { CommitterEx = Generate.SignatureNow() } };
141+
var releaseLatestBranch = GitToolsTestingExtensions.CreateMockBranch("release/latest", new MockCommit { CommitterEx = Generate.SignatureNow() });
142+
var releaseVersionBranch = GitToolsTestingExtensions.CreateMockBranch("release/1.0.0", new MockCommit { CommitterEx = Generate.SignatureNow() });
140143

141144
var branches = Substitute.For<IBranchCollection>();
142145
branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)new[] { releaseLatestBranch, releaseVersionBranch }).GetEnumerator());

0 commit comments

Comments
 (0)