diff --git a/src/GitVersionCore.Tests/Extensions/GitToolsTestingExtensions.cs b/src/GitVersionCore.Tests/Extensions/GitToolsTestingExtensions.cs index 4cffda3a7b..6a0cf05d07 100644 --- a/src/GitVersionCore.Tests/Extensions/GitToolsTestingExtensions.cs +++ b/src/GitVersionCore.Tests/Extensions/GitToolsTestingExtensions.cs @@ -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(); + objectId.Sha.Returns(Guid.NewGuid().ToString().Replace("-", "") + "00000000"); + + var commit = Substitute.For(); + commit.Id.Returns(objectId); + commit.Sha.Returns(objectId.Sha); + commit.Message.Returns("Commit " + commitCount++); + commit.Parents.Returns(Enumerable.Empty()); + commit.CommitterWhen.Returns(when.AddSeconds(1)); + return commit; + } public static IBranch CreateMockBranch(string name, params ICommit[] commits) { var branch = Substitute.For(); diff --git a/src/GitVersionCore.Tests/Helpers/GitVersionContextBuilder.cs b/src/GitVersionCore.Tests/Helpers/GitVersionContextBuilder.cs index 4c85af057c..cb087fae0a 100644 --- a/src/GitVersionCore.Tests/Helpers/GitVersionContextBuilder.cs +++ b/src/GitVersionCore.Tests/Helpers/GitVersionContextBuilder.cs @@ -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); @@ -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(); branches.GetEnumerator().Returns(_ => ((IEnumerable)new[] { mockBranch }).GetEnumerator()); diff --git a/src/GitVersionCore.Tests/Mocks/MockCommit.cs b/src/GitVersionCore.Tests/Mocks/MockCommit.cs deleted file mode 100644 index 2f20844b09..0000000000 --- a/src/GitVersionCore.Tests/Mocks/MockCommit.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using GitVersion; -using LibGit2Sharp; -using NSubstitute; - -namespace GitVersionCore.Tests.Mocks -{ - [DebuggerDisplay("{" + nameof(DebuggerDisplay) + "}")] - internal class MockCommit : ICommit - { - private static int commitCount = 1; - private static DateTimeOffset when = DateTimeOffset.Now; - - public MockCommit() - { - idEx = Substitute.For(); - idEx.Sha.Returns(Guid.NewGuid().ToString().Replace("-", "") + "00000000"); - MessageEx = "Commit " + commitCount++; - ParentsEx = new List { null }; - CommitterEx = new Signature("Joe", "Joe@bloggs.net", when); - // Make sure each commit is a different time - when = when.AddSeconds(1); - } - - public string MessageEx; - public string Message => MessageEx; - - public Signature CommitterEx; - public DateTimeOffset CommitterWhen => CommitterEx.When; - - private readonly IObjectId idEx; - public IObjectId Id => idEx; - - public string Sha => idEx.Sha; - - public IList ParentsEx; - public IEnumerable Parents => ParentsEx; - - // ReSharper disable once UnusedMember.Local - private string DebuggerDisplay => MessageEx; - public bool Equals(ICommit other) - { - throw new NotImplementedException(); - } - public int CompareTo(ICommit other) - { - throw new NotImplementedException(); - } - } -} diff --git a/src/GitVersionCore.Tests/Model/GitVersionContextTests.cs b/src/GitVersionCore.Tests/Model/GitVersionContextTests.cs index a4bbd5466a..8c95c79834 100644 --- a/src/GitVersionCore.Tests/Model/GitVersionContextTests.cs +++ b/src/GitVersionCore.Tests/Model/GitVersionContextTests.cs @@ -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(); @@ -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(); branches.GetEnumerator().Returns(_ => ((IEnumerable)new[] { master, develop }).GetEnumerator()); @@ -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(); branches.GetEnumerator().Returns(_ => ((IEnumerable)new[] { releaseLatestBranch, releaseVersionBranch }).GetEnumerator()); diff --git a/src/GitVersionCore.Tests/VersionCalculation/BaseVersionCalculatorTests.cs b/src/GitVersionCore.Tests/VersionCalculation/BaseVersionCalculatorTests.cs index a92cc32ce1..6dc03dd008 100644 --- a/src/GitVersionCore.Tests/VersionCalculation/BaseVersionCalculatorTests.cs +++ b/src/GitVersionCore.Tests/VersionCalculation/BaseVersionCalculatorTests.cs @@ -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; @@ -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 => { @@ -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 => { @@ -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, @@ -148,7 +147,7 @@ public void ShouldIgnorePreReleaseVersionInMainlineMode() Number = 1 } }, - new MockCommit(), + GitToolsTestingExtensions.CreateMockCommit(), null ); @@ -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 GetVersions() @@ -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 GetVersions() diff --git a/src/GitVersionCore.Tests/VersionCalculation/MinDateVersionFilterTests.cs b/src/GitVersionCore.Tests/VersionCalculation/MinDateVersionFilterTests.cs index b2322e173a..a67c09fc1d 100644 --- a/src/GitVersionCore.Tests/VersionCalculation/MinDateVersionFilterTests.cs +++ b/src/GitVersionCore.Tests/VersionCalculation/MinDateVersionFilterTests.cs @@ -2,7 +2,6 @@ using GitVersion; using GitVersion.VersionCalculation; using GitVersionCore.Tests.Helpers; -using GitVersionCore.Tests.Mocks; using NUnit.Framework; using Shouldly; @@ -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); @@ -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); diff --git a/src/GitVersionCore.Tests/VersionCalculation/NextVersionCalculatorTests.cs b/src/GitVersionCore.Tests/VersionCalculation/NextVersionCalculatorTests.cs index 3cfa9ebc25..e844a829f0 100644 --- a/src/GitVersionCore.Tests/VersionCalculation/NextVersionCalculatorTests.cs +++ b/src/GitVersionCore.Tests/VersionCalculation/NextVersionCalculatorTests.cs @@ -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; @@ -26,7 +25,8 @@ public void ShouldIncrementVersionBasedOnConfig() contextBuilder .OverrideServices(services => { - services.AddSingleton(new TestBaseVersionCalculator(true, new SemanticVersion(1), new MockCommit())); + var testBaseVersionCalculator = new TestBaseVersionCalculator(true, new SemanticVersion(1), GitToolsTestingExtensions.CreateMockCommit()); + services.AddSingleton(testBaseVersionCalculator); services.AddSingleton(new TestMainlineVersionCalculator(semanticVersionBuildMetaData)); }) .WithConfig(new Config()) @@ -50,7 +50,8 @@ public void DoesNotIncrementWhenBaseVersionSaysNotTo() contextBuilder .OverrideServices(services => { - services.AddSingleton(new TestBaseVersionCalculator(false, new SemanticVersion(1), new MockCommit())); + var testBaseVersionCalculator = new TestBaseVersionCalculator(false, new SemanticVersion(1), GitToolsTestingExtensions.CreateMockCommit()); + services.AddSingleton(testBaseVersionCalculator); services.AddSingleton(new TestMainlineVersionCalculator(semanticVersionBuildMetaData)); }) .WithConfig(new Config()) @@ -74,7 +75,8 @@ public void AppliesBranchPreReleaseTag() contextBuilder .OverrideServices(services => { - services.AddSingleton(new TestBaseVersionCalculator(false, new SemanticVersion(1), new MockCommit())); + var testBaseVersionCalculator = new TestBaseVersionCalculator(false, new SemanticVersion(1), GitToolsTestingExtensions.CreateMockCommit()); + services.AddSingleton(testBaseVersionCalculator); services.AddSingleton(new TestMainlineVersionCalculator(semanticVersionBuildMetaData)); }) .WithDevelopBranch() diff --git a/src/GitVersionCore.Tests/VersionCalculation/ShaVersionFilterTests.cs b/src/GitVersionCore.Tests/VersionCalculation/ShaVersionFilterTests.cs index 65abf61f20..6511c786c4 100644 --- a/src/GitVersionCore.Tests/VersionCalculation/ShaVersionFilterTests.cs +++ b/src/GitVersionCore.Tests/VersionCalculation/ShaVersionFilterTests.cs @@ -2,7 +2,6 @@ using GitVersion; using GitVersion.VersionCalculation; using GitVersionCore.Tests.Helpers; -using GitVersionCore.Tests.Mocks; using NUnit.Framework; using Shouldly; @@ -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(() => sut.Exclude(null, out _)); @@ -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 }); @@ -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" }); diff --git a/src/GitVersionCore.Tests/VersionCalculation/Strategies/MergeMessageBaseVersionStrategyTests.cs b/src/GitVersionCore.Tests/VersionCalculation/Strategies/MergeMessageBaseVersionStrategyTests.cs index 558f0d7e42..c10d566754 100644 --- a/src/GitVersionCore.Tests/VersionCalculation/Strategies/MergeMessageBaseVersionStrategyTests.cs +++ b/src/GitVersionCore.Tests/VersionCalculation/Strategies/MergeMessageBaseVersionStrategyTests.cs @@ -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(); branches.GetEnumerator().Returns(_ => ((IEnumerable)new[] { mockBranch }).GetEnumerator()); @@ -157,15 +155,13 @@ public void TakesVersionFromMergeOfConfiguredReleaseBranch(string message, strin AssertMergeMessage(message, expectedVersion, parents, config); } - private void AssertMergeMessage(string message, string expectedVersion, IList parents, Config config = null) + private static void AssertMergeMessage(string message, string expectedVersion, IEnumerable 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())