Skip to content

Commit

Permalink
replaced MockCommit with Substitute.For
Browse files Browse the repository at this point in the history
  • Loading branch information
arturcic committed Jan 19, 2021
1 parent cb0b9e0 commit a596c38
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 92 deletions.
16 changes: 16 additions & 0 deletions src/GitVersionCore.Tests/Extensions/GitToolsTestingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ namespace GitVersionCore.Tests
{
public static class GitToolsTestingExtensions
{
private static int commitCount = 1;
private static DateTimeOffset when = DateTimeOffset.Now;

public static ICommit CreateMockCommit()
{
var objectId = Substitute.For<IObjectId>();
objectId.Sha.Returns(Guid.NewGuid().ToString().Replace("-", "") + "00000000");

var commit = Substitute.For<ICommit>();
commit.Id.Returns(objectId);
commit.Sha.Returns(objectId.Sha);
commit.Message.Returns("Commit " + commitCount++);
commit.Parents.Returns(Enumerable.Empty<ICommit>());
commit.CommitterWhen.Returns(when.AddSeconds(1));
return commit;
}
public static IBranch CreateMockBranch(string name, params ICommit[] commits)
{
var branch = Substitute.For<IBranch>();
Expand Down
5 changes: 3 additions & 2 deletions src/GitVersionCore.Tests/Helpers/GitVersionContextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ private GitVersionContextBuilder WithBranch(string branchName)

private GitVersionContextBuilder AddBranch(string branchName)
{
var mockBranch = GitToolsTestingExtensions.CreateMockBranch(branchName, new MockCommit());
var mockCommit = GitToolsTestingExtensions.CreateMockCommit();
var mockBranch = GitToolsTestingExtensions.CreateMockBranch(branchName, mockCommit);

var branches = repository.Branches.ToList();
branches.Add(mockBranch);
Expand Down Expand Up @@ -86,7 +87,7 @@ public void Build()

private static IGitRepository CreateRepository()
{
var mockCommit = new MockCommit { CommitterEx = Generate.SignatureNow() };
var mockCommit = GitToolsTestingExtensions.CreateMockCommit();
var mockBranch = GitToolsTestingExtensions.CreateMockBranch("master", mockCommit);
var branches = Substitute.For<IBranchCollection>();
branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)new[] { mockBranch }).GetEnumerator());
Expand Down
52 changes: 0 additions & 52 deletions src/GitVersionCore.Tests/Mocks/MockCommit.cs

This file was deleted.

10 changes: 5 additions & 5 deletions src/GitVersionCore.Tests/Model/GitVersionContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void CanInheritVersioningMode(VersioningMode mode)

const string branchName = "master";

var mockCommit = new MockCommit { CommitterEx = Generate.SignatureNow() };
var mockCommit = GitToolsTestingExtensions.CreateMockCommit();
var mockBranch = GitToolsTestingExtensions.CreateMockBranch(branchName, mockCommit);

var branches = Substitute.For<IBranchCollection>();
Expand Down Expand Up @@ -94,8 +94,8 @@ public void UsesBranchSpecificConfigOverTopLevelDefaults()
})
.Build();

var master = GitToolsTestingExtensions.CreateMockBranch("master", new MockCommit { CommitterEx = Generate.SignatureNow() });
var develop = GitToolsTestingExtensions.CreateMockBranch(branchName, new MockCommit { CommitterEx = Generate.SignatureNow() });
var master = GitToolsTestingExtensions.CreateMockBranch("master", GitToolsTestingExtensions.CreateMockCommit());
var develop = GitToolsTestingExtensions.CreateMockBranch(branchName, GitToolsTestingExtensions.CreateMockCommit());

var branches = Substitute.For<IBranchCollection>();
branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)new[] { master, develop }).GetEnumerator());
Expand Down Expand Up @@ -138,8 +138,8 @@ public void UsesFirstBranchConfigWhenMultipleMatch()
})
.Build();

var releaseLatestBranch = GitToolsTestingExtensions.CreateMockBranch("release/latest", new MockCommit { CommitterEx = Generate.SignatureNow() });
var releaseVersionBranch = GitToolsTestingExtensions.CreateMockBranch("release/1.0.0", new MockCommit { CommitterEx = Generate.SignatureNow() });
var releaseLatestBranch = GitToolsTestingExtensions.CreateMockBranch("release/latest", GitToolsTestingExtensions.CreateMockCommit());
var releaseVersionBranch = GitToolsTestingExtensions.CreateMockBranch("release/1.0.0", GitToolsTestingExtensions.CreateMockCommit());

var branches = Substitute.For<IBranchCollection>();
branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)new[] { releaseLatestBranch, releaseVersionBranch }).GetEnumerator());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Collections.Generic;
using GitTools.Testing;
using GitVersion;
using GitVersion.Model.Configuration;
using GitVersion.VersionCalculation;
using GitVersionCore.Tests.Helpers;
using GitVersionCore.Tests.Mocks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NSubstitute;
using NUnit.Framework;
using Shouldly;

Expand Down Expand Up @@ -85,7 +84,7 @@ public void UsesWhenFromNextBestMatchIfHighestDoesntHaveWhenReversedOrder()
public void ShouldNotFilterVersion()
{
var fakeIgnoreConfig = new TestIgnoreConfig(new ExcludeSourcesContainingExclude());
var version = new BaseVersion("dummy", false, new SemanticVersion(2), new MockCommit(), null);
var version = new BaseVersion("dummy", false, new SemanticVersion(2), GitToolsTestingExtensions.CreateMockCommit(), null);

var versionCalculator = GetBaseVersionCalculator(contextBuilder =>
{
Expand All @@ -110,8 +109,8 @@ public void ShouldFilterVersion()
{
var fakeIgnoreConfig = new TestIgnoreConfig(new ExcludeSourcesContainingExclude());

var higherVersion = new BaseVersion("exclude", false, new SemanticVersion(2), new MockCommit(), null);
var lowerVersion = new BaseVersion("dummy", false, new SemanticVersion(1), new MockCommit(), null);
var higherVersion = new BaseVersion("exclude", false, new SemanticVersion(2), GitToolsTestingExtensions.CreateMockCommit(), null);
var lowerVersion = new BaseVersion("dummy", false, new SemanticVersion(1), GitToolsTestingExtensions.CreateMockCommit(), null);

var versionCalculator = GetBaseVersionCalculator(contextBuilder =>
{
Expand All @@ -136,7 +135,7 @@ public void ShouldIgnorePreReleaseVersionInMainlineMode()
{
var fakeIgnoreConfig = new TestIgnoreConfig(new ExcludeSourcesContainingExclude());

var lowerVersion = new BaseVersion("dummy", false, new SemanticVersion(1), new MockCommit(), null);
var lowerVersion = new BaseVersion("dummy", false, new SemanticVersion(1), GitToolsTestingExtensions.CreateMockCommit(), null);
var preReleaseVersion = new BaseVersion(
"prerelease",
false,
Expand All @@ -148,7 +147,7 @@ public void ShouldIgnorePreReleaseVersionInMainlineMode()
Number = 1
}
},
new MockCommit(),
GitToolsTestingExtensions.CreateMockCommit(),
null
);

Expand Down Expand Up @@ -218,7 +217,15 @@ private sealed class V1Strategy : IVersionStrategy

public V1Strategy(DateTimeOffset? when)
{
this.when = when == null ? null : new MockCommit { CommitterEx = Generate.Signature(when.Value) };
if (when != null)
{
this.when = GitToolsTestingExtensions.CreateMockCommit();
this.when.CommitterWhen.Returns(when.Value);
}
else
{
this.when = null;
}
}

public IEnumerable<BaseVersion> GetVersions()
Expand All @@ -233,7 +240,15 @@ private sealed class V2Strategy : IVersionStrategy

public V2Strategy(DateTimeOffset? when)
{
this.when = when == null ? null : new MockCommit { CommitterEx = Generate.Signature(when.Value) };
if (when != null)
{
this.when = GitToolsTestingExtensions.CreateMockCommit();
this.when.CommitterWhen.Returns(when.Value);
}
else
{
this.when = null;
}
}

public IEnumerable<BaseVersion> GetVersions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using GitVersion;
using GitVersion.VersionCalculation;
using GitVersionCore.Tests.Helpers;
using GitVersionCore.Tests.Mocks;
using NUnit.Framework;
using Shouldly;

Expand All @@ -23,7 +22,7 @@ public void VerifyNullGuard()
[Test]
public void WhenCommitShouldExcludeWithReason()
{
var commit = new MockCommit(); //when = UtcNow
var commit = GitToolsTestingExtensions.CreateMockCommit();
var version = new BaseVersion("dummy", false, new SemanticVersion(1), commit, string.Empty);
var futureDate = DateTimeOffset.UtcNow.AddYears(1);
var sut = new MinDateVersionFilter(futureDate);
Expand All @@ -35,7 +34,7 @@ public void WhenCommitShouldExcludeWithReason()
[Test]
public void WhenShaMismatchShouldNotExclude()
{
var commit = new MockCommit(); //when = UtcNow
var commit = GitToolsTestingExtensions.CreateMockCommit();
var version = new BaseVersion("dummy", false, new SemanticVersion(1), commit, string.Empty);
var pastDate = DateTimeOffset.UtcNow.AddYears(-1);
var sut = new MinDateVersionFilter(pastDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using GitVersion.VersionCalculation;
using GitVersionCore.Tests.Helpers;
using GitVersionCore.Tests.IntegrationTests;
using GitVersionCore.Tests.Mocks;
using LibGit2Sharp;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
Expand All @@ -26,7 +25,8 @@ public void ShouldIncrementVersionBasedOnConfig()
contextBuilder
.OverrideServices(services =>
{
services.AddSingleton<IBaseVersionCalculator>(new TestBaseVersionCalculator(true, new SemanticVersion(1), new MockCommit()));
var testBaseVersionCalculator = new TestBaseVersionCalculator(true, new SemanticVersion(1), GitToolsTestingExtensions.CreateMockCommit());
services.AddSingleton<IBaseVersionCalculator>(testBaseVersionCalculator);
services.AddSingleton<IMainlineVersionCalculator>(new TestMainlineVersionCalculator(semanticVersionBuildMetaData));
})
.WithConfig(new Config())
Expand All @@ -50,7 +50,8 @@ public void DoesNotIncrementWhenBaseVersionSaysNotTo()
contextBuilder
.OverrideServices(services =>
{
services.AddSingleton<IBaseVersionCalculator>(new TestBaseVersionCalculator(false, new SemanticVersion(1), new MockCommit()));
var testBaseVersionCalculator = new TestBaseVersionCalculator(false, new SemanticVersion(1), GitToolsTestingExtensions.CreateMockCommit());
services.AddSingleton<IBaseVersionCalculator>(testBaseVersionCalculator);
services.AddSingleton<IMainlineVersionCalculator>(new TestMainlineVersionCalculator(semanticVersionBuildMetaData));
})
.WithConfig(new Config())
Expand All @@ -74,7 +75,8 @@ public void AppliesBranchPreReleaseTag()
contextBuilder
.OverrideServices(services =>
{
services.AddSingleton<IBaseVersionCalculator>(new TestBaseVersionCalculator(false, new SemanticVersion(1), new MockCommit()));
var testBaseVersionCalculator = new TestBaseVersionCalculator(false, new SemanticVersion(1), GitToolsTestingExtensions.CreateMockCommit());
services.AddSingleton<IBaseVersionCalculator>(testBaseVersionCalculator);
services.AddSingleton<IMainlineVersionCalculator>(new TestMainlineVersionCalculator(semanticVersionBuildMetaData));
})
.WithDevelopBranch()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using GitVersion;
using GitVersion.VersionCalculation;
using GitVersionCore.Tests.Helpers;
using GitVersionCore.Tests.Mocks;
using NUnit.Framework;
using Shouldly;

Expand All @@ -20,7 +19,7 @@ public void VerifyNullGuard()
[Test]
public void VerifyNullGuard2()
{
var commit = new MockCommit();
var commit = GitToolsTestingExtensions.CreateMockCommit();
var sut = new ShaVersionFilter(new[] { commit.Sha });

Should.Throw<ArgumentNullException>(() => sut.Exclude(null, out _));
Expand All @@ -29,7 +28,7 @@ public void VerifyNullGuard2()
[Test]
public void WhenShaMatchShouldExcludeWithReason()
{
var commit = new MockCommit();
var commit = GitToolsTestingExtensions.CreateMockCommit();
var version = new BaseVersion("dummy", false, new SemanticVersion(1), commit, string.Empty);
var sut = new ShaVersionFilter(new[] { commit.Sha });

Expand All @@ -40,7 +39,7 @@ public void WhenShaMatchShouldExcludeWithReason()
[Test]
public void WhenShaMismatchShouldNotExclude()
{
var commit = new MockCommit();
var commit = GitToolsTestingExtensions.CreateMockCommit();
var version = new BaseVersion("dummy", false, new SemanticVersion(1), commit, string.Empty);
var sut = new ShaVersionFilter(new[] { "mismatched" });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ public void ShouldNotAllowIncrementOfVersion()
{
// When a branch is merged in you want to start building stable packages of that version
// So we shouldn't bump the version
var mockCommit = new MockCommit
{
MessageEx = "Merge branch 'release-0.1.5'",
ParentsEx = GetParents(true)
};
// var mockBranch = new MockBranch("master"); mockBranch.Add(mockCommit);
var mockCommit = GitToolsTestingExtensions.CreateMockCommit();
mockCommit.Message.Returns("Merge branch 'release-0.1.5'");
mockCommit.Parents.Returns(GetParents(true));

var mockBranch = GitToolsTestingExtensions.CreateMockBranch("master", mockCommit);
var branches = Substitute.For<IBranchCollection>();
branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)new[] { mockBranch }).GetEnumerator());
Expand Down Expand Up @@ -157,15 +155,13 @@ public void TakesVersionFromMergeOfConfiguredReleaseBranch(string message, strin
AssertMergeMessage(message, expectedVersion, parents, config);
}

private void AssertMergeMessage(string message, string expectedVersion, IList<ICommit> parents, Config config = null)
private static void AssertMergeMessage(string message, string expectedVersion, IEnumerable<ICommit> parents, Config config = null)
{
var commit = new MockCommit
{
MessageEx = message,
ParentsEx = parents
};
var commit = GitToolsTestingExtensions.CreateMockCommit();
commit.Message.Returns(message);
commit.Parents.Returns(parents);

var mockBranch = GitToolsTestingExtensions.CreateMockBranch("master", commit, new MockCommit());
var mockBranch = GitToolsTestingExtensions.CreateMockBranch("master", commit, GitToolsTestingExtensions.CreateMockCommit());

var contextBuilder = new GitVersionContextBuilder()
.WithConfig(config ?? new Config())
Expand Down

0 comments on commit a596c38

Please sign in to comment.