Skip to content

Commit 6d7ba32

Browse files
committed
cleanup
1 parent 089032a commit 6d7ba32

File tree

9 files changed

+36
-111
lines changed

9 files changed

+36
-111
lines changed

src/GitVersion.LibGit2Sharp/Git/Commit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public virtual IObjectId Id
5151
}
5252
}
5353

54-
public virtual DateTimeOffset? CommitterWhen => innerObjectId.Committer.When;
54+
public virtual DateTimeOffset CommitterWhen => innerObjectId.Committer.When;
5555
public virtual string Message => innerObjectId.Message;
5656
}
5757
}

src/GitVersion.LibGit2Sharp/Git/ReferenceCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ protected ReferenceCollection()
1313
{
1414
}
1515

16-
public IEnumerator<IReference> GetEnumerator()
16+
public virtual IEnumerator<IReference> GetEnumerator()
1717
{
1818
return innerCollection.Select(reference => new Reference(reference)).GetEnumerator();
1919
}

src/GitVersionCore.Tests/Core/RepositoryExtensionsTests.cs

Lines changed: 23 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -68,122 +68,43 @@ private static void EnsureLocalBranchExistsForCurrentBranch(IGitRepository repo,
6868
public void EnsureLocalBranchExistsForCurrentBranch_CaseInsensitivelyMatchesBranches()
6969
{
7070
var log = Substitute.For<ILog>();
71-
var repository = MockRepository();
71+
var repository = Substitute.For<IGitRepository>();
7272
var remote = MockRemote(repository);
7373

7474
EnsureLocalBranchExistsForCurrentBranch(repository, log, remote, "refs/heads/featurE/feat-test");
7575
}
7676

77-
private static IGitRepository MockRepository()
78-
{
79-
var repository = Substitute.For<IGitRepository>();
80-
return repository;
81-
}
82-
8377
private static IRemote MockRemote(IGitRepository repository)
8478
{
85-
var branches = new TestableBranchCollection();
86-
var tipId = new ObjectId("c6d8764d20ff16c0df14c73680e52b255b608926");
87-
var tip = new TestableCommit(tipId);
88-
var head = branches.Add("refs/heads/feature/feat-test", tip);
89-
var remote = new TesatbleRemote("origin");
90-
var references = new TestableReferenceCollection
91-
{
92-
{
93-
"develop", "refs/heads/develop"
94-
}
95-
};
96-
97-
repository.Refs.Returns(references);
98-
repository.Head.Returns(head);
99-
repository.Branches.Returns(branches);
100-
return remote;
101-
}
102-
103-
private class TestableBranchCollection : BranchCollection
104-
{
105-
private IDictionary<string, IBranch> branches = new Dictionary<string, IBranch>();
79+
var tipId = Substitute.For<IObjectId>();
80+
tipId.Sha.Returns("c6d8764d20ff16c0df14c73680e52b255b608926");
10681

107-
public override IBranch this[string name] =>
108-
branches.ContainsKey(name)
109-
? branches[name]
110-
: null;
82+
var tip = Substitute.For<ICommit>();
83+
tip.Id.Returns(tipId);
84+
tip.Sha.Returns(tipId.Sha);
11185

112-
public IBranch Add(string name, ICommit commit)
113-
{
114-
var branch = new TestableBranch(name, commit);
115-
branches.Add(name, branch);
116-
return branch;
117-
}
86+
var remote = Substitute.For<IRemote>();
87+
remote.Name.Returns("origin");
11888

119-
public override IEnumerator<IBranch> GetEnumerator()
120-
{
121-
return branches.Values.GetEnumerator();
122-
}
123-
}
89+
var branch = Substitute.For<IBranch>();
90+
branch.Tip.Returns(tip);
91+
branch.CanonicalName.Returns("refs/heads/feature/feat-test");
12492

125-
private class TestableBranch : Branch
126-
{
127-
private readonly string canonicalName;
128-
private readonly ICommit tip;
93+
var branches = Substitute.For<IBranchCollection>();
94+
branches[branch.CanonicalName].Returns(branch);
95+
branches.GetEnumerator().Returns(x => ((IEnumerable<IBranch>)new[] { branch }).GetEnumerator());
12996

130-
public TestableBranch(string canonicalName, ICommit tip)
131-
{
132-
this.tip = tip;
133-
this.canonicalName = canonicalName;
134-
}
135-
136-
public override string CanonicalName => canonicalName;
137-
public override ICommit Tip => tip;
138-
}
97+
var reference = Substitute.For<IReference>();
98+
reference.CanonicalName.Returns("refs/heads/develop");
13999

140-
private class TestableCommit : Commit
141-
{
142-
private IObjectId id;
100+
var references = Substitute.For<IReferenceCollection>();
101+
references["develop"].Returns(reference);
102+
references.GetEnumerator().Returns(x => ((IEnumerable<IReference>)new[] { reference }).GetEnumerator());
143103

144-
public TestableCommit(IObjectId id)
145-
{
146-
this.id = id;
147-
}
148-
149-
public override IObjectId Id => id;
150-
public override string Sha => id.Sha;
151-
}
152-
153-
private class TesatbleRemote : Remote
154-
{
155-
private string name;
156-
157-
public TesatbleRemote(string name)
158-
{
159-
this.name = name;
160-
}
161-
162-
public override string Name => name;
163-
}
164-
165-
private class TestableReferenceCollection : ReferenceCollection
166-
{
167-
private IReference reference;
168-
public override void Add(string name, string canonicalRefNameOrObjectish, bool allowOverwrite = false)
169-
{
170-
reference = new TestableReference(canonicalRefNameOrObjectish);
171-
}
172-
public override void UpdateTarget(IReference directRef, IObjectId targetId)
173-
{
174-
}
175-
public override IReference this[string name] => reference;
176-
}
177-
178-
private class TestableReference : Reference
179-
{
180-
181-
public TestableReference(string canonicalName)
182-
{
183-
this.CanonicalName = canonicalName;
184-
}
185-
186-
public override string CanonicalName { get; }
104+
repository.Refs.Returns(references);
105+
repository.Head.Returns(branch);
106+
repository.Branches.Returns(branches);
107+
return remote;
187108
}
188109
}
189110
}

src/GitVersionCore.Tests/IntegrationTests/IgnoreBeforeScenarios.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
using System;
12
using GitTools.Testing;
23
using GitVersion;
34
using GitVersion.Configuration;
45
using GitVersion.Model.Configuration;
56
using GitVersionCore.Tests.Helpers;
7+
using NSubstitute;
68
using NUnit.Framework;
79

810
namespace GitVersionCore.Tests.IntegrationTests
@@ -14,14 +16,17 @@ public class IgnoreBeforeScenarios : TestBase
1416
public void ShouldFallbackToBaseVersionWhenAllCommitsAreIgnored()
1517
{
1618
using var fixture = new EmptyRepositoryFixture();
17-
var commit = new Commit(fixture.Repository.MakeACommit());
19+
var objectId = fixture.Repository.MakeACommit();
20+
var commit = Substitute.For<ICommit>();
21+
commit.Sha.Returns(objectId.Sha);
22+
commit.CommitterWhen.Returns(DateTimeOffset.Now);
1823

1924
var config = new ConfigurationBuilder()
2025
.Add(new Config
2126
{
2227
Ignore = new IgnoreConfig
2328
{
24-
Before = commit.CommitterWhen.Value.AddMinutes(1)
29+
Before = commit.CommitterWhen.AddMinutes(1)
2530
}
2631
}).Build();
2732

src/GitVersionCore.Tests/Mocks/MockCommit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public MockCommit()
2828
public string Message => MessageEx;
2929

3030
public Signature CommitterEx;
31-
public DateTimeOffset? CommitterWhen => CommitterEx.When;
31+
public DateTimeOffset CommitterWhen => CommitterEx.When;
3232

3333
private readonly IObjectId idEx;
3434
public IObjectId Id => idEx;

src/GitVersionCore.Tests/VersionCalculation/NextVersionCalculatorTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Microsoft.Extensions.DependencyInjection;
1212
using NUnit.Framework;
1313
using Shouldly;
14-
using Branch = GitVersion.Branch;
1514

1615
namespace GitVersionCore.Tests.VersionCalculation
1716
{

src/GitVersionCore/Core/Abstractions/Git/ICommit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public interface ICommit : IEquatable<ICommit>, IComparable<ICommit>
88
IEnumerable<ICommit> Parents { get; }
99
string Sha { get; }
1010
IObjectId Id { get; }
11-
DateTimeOffset? CommitterWhen { get; }
11+
DateTimeOffset CommitterWhen { get; }
1212
string Message { get; }
1313
}
1414
}

src/GitVersionCore/VersionCalculation/BaseVersionCalculators/MergeMessageVersionStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public MergeMessageVersionStrategy(ILog log, Lazy<GitVersionContext> versionCont
2424

2525
public override IEnumerable<BaseVersion> GetVersions()
2626
{
27-
var commitsPriorToThan = Context.CurrentBranch.Commits.GetCommitsPriorTo(Context.CurrentCommit.CommitterWhen.Value);
27+
var commitsPriorToThan = Context.CurrentBranch.Commits.GetCommitsPriorTo(Context.CurrentCommit.CommitterWhen);
2828
var baseVersions = commitsPriorToThan
2929
.SelectMany(c =>
3030
{

src/GitVersionCore/VersionCalculation/MainlineVersionCalculator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public SemanticVersionBuildMetaData CreateVersionBuildMetaData(ICommit baseVersi
9696
context.CurrentBranch.FriendlyName,
9797
context.CurrentCommit.Sha,
9898
shortSha,
99-
context.CurrentCommit.CommitterWhen.Value,
99+
context.CurrentCommit.CommitterWhen,
100100
context.NumberOfUncommittedChanges);
101101
}
102102

0 commit comments

Comments
 (0)