Skip to content

Commit a249222

Browse files
committed
(test) use DI for tests
1 parent dde2129 commit a249222

18 files changed

+411
-396
lines changed

src/GitVersionCore.Tests/GitVersionExecutorTests.cs

Lines changed: 212 additions & 272 deletions
Large diffs are not rendered by default.

src/GitVersionCore.Tests/Helpers/ServiceCollectionExtensions.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
using System;
12
using System.IO;
23
using NUnit.Framework;
34
using Shouldly;
45
using GitVersion.Configuration;
56
using GitVersion.Exceptions;
67
using GitVersion;
7-
using GitVersion.Configuration.Init.Wizard;
88
using GitVersion.Logging;
99
using GitVersionCore.Tests.Helpers;
10+
using Microsoft.Extensions.DependencyInjection;
1011
using Microsoft.Extensions.Options;
1112

1213
namespace GitVersionCore.Tests
@@ -20,29 +21,26 @@ public class NamedConfigFileLocatorTests : TestBase
2021
private string repoPath;
2122
private string workingPath;
2223
private IFileSystem fileSystem;
23-
private NamedConfigFileLocator configFileLocator;
24-
private ILog log;
25-
private IConfigInitStepFactory stepFactory;
26-
private IOptions<Arguments> options;
24+
private IConfigFileLocator configFileLocator;
25+
private Arguments arguments;
2726

2827
[SetUp]
2928
public void Setup()
3029
{
31-
fileSystem = new TestFileSystem();
32-
log = new NullLog();
33-
34-
options = Options.Create(new Arguments { ConfigFile = "my-config.yaml" });
35-
configFileLocator = new NamedConfigFileLocator(fileSystem, log, options);
30+
arguments = new Arguments { ConfigFile = "my-config.yaml" };
3631
repoPath = DefaultRepoPath;
3732
workingPath = DefaultWorkingPath;
38-
stepFactory = new ConfigInitStepFactory();
3933

4034
ShouldlyConfiguration.ShouldMatchApprovedDefaults.LocateTestMethodUsingAttribute<TestAttribute>();
4135
}
4236

4337
[Test]
4438
public void ThrowsExceptionOnAmbiguousConfigFileLocation()
4539
{
40+
var sp = GetServiceProvider(arguments);
41+
configFileLocator = sp.GetService<IConfigFileLocator>();
42+
fileSystem = sp.GetService<IFileSystem>();
43+
4644
var repositoryConfigFilePath = SetupConfigFileContent(string.Empty, path: repoPath);
4745
var workingDirectoryConfigFilePath = SetupConfigFileContent(string.Empty, path: workingPath);
4846

@@ -56,6 +54,11 @@ public void ThrowsExceptionOnAmbiguousConfigFileLocation()
5654
public void DoNotThrowWhenWorkingAndRepoPathsAreSame()
5755
{
5856
workingPath = DefaultRepoPath;
57+
58+
var sp = GetServiceProvider(arguments);
59+
configFileLocator = sp.GetService<IConfigFileLocator>();
60+
fileSystem = sp.GetService<IFileSystem>();
61+
5962
SetupConfigFileContent(string.Empty, path: workingPath);
6063

6164
Should.NotThrow(() => { configFileLocator.Verify(workingPath, repoPath); });
@@ -66,6 +69,11 @@ public void DoNotThrowWhenWorkingAndRepoPathsAreSame()
6669
public void DoNotThrowWhenWorkingAndRepoPathsAreSame_WithDifferentCasing()
6770
{
6871
workingPath = DefaultRepoPath.ToLower();
72+
73+
var sp = GetServiceProvider(arguments);
74+
configFileLocator = sp.GetService<IConfigFileLocator>();
75+
fileSystem = sp.GetService<IFileSystem>();
76+
6977
SetupConfigFileContent(string.Empty, path: workingPath);
7078

7179
Should.NotThrow(() => { configFileLocator.Verify(workingPath, repoPath); });
@@ -76,8 +84,11 @@ public void DoNotThrowWhenConfigFileIsInSubDirectoryOfRepoPath()
7684
{
7785
workingPath = DefaultRepoPath;
7886

79-
options = Options.Create(new Arguments { ConfigFile = "./src/my-config.yaml" });
80-
configFileLocator = new NamedConfigFileLocator(fileSystem, log, options);
87+
arguments = new Arguments { ConfigFile = "./src/my-config.yaml" };
88+
var sp = GetServiceProvider(arguments);
89+
configFileLocator = sp.GetService<IConfigFileLocator>();
90+
fileSystem = sp.GetService<IFileSystem>();
91+
8192
SetupConfigFileContent(string.Empty, path: workingPath);
8293

8394
Should.NotThrow(() => { configFileLocator.Verify(workingPath, repoPath); });
@@ -86,19 +97,19 @@ public void DoNotThrowWhenConfigFileIsInSubDirectoryOfRepoPath()
8697
[Test]
8798
public void NoWarnOnCustomYmlFile()
8899
{
89-
SetupConfigFileContent(string.Empty);
90-
91100
var stringLogger = string.Empty;
92101
void Action(string info) => stringLogger = info;
93102

94103
var logAppender = new TestLogAppender(Action);
95-
log = new Log(logAppender);
104+
var log = new Log(logAppender);
96105

97-
configFileLocator = new NamedConfigFileLocator(fileSystem, log, options);
106+
var sp = GetServiceProvider(arguments, log);
107+
configFileLocator = sp.GetService<IConfigFileLocator>();
108+
fileSystem = sp.GetService<IFileSystem>();
98109

99-
var gitPreparer = new GitPreparer(log, new TestEnvironment(), Options.Create(new Arguments { TargetPath = repoPath }));
100-
var configInitWizard = new ConfigInitWizard(new ConsoleAdapter(), stepFactory);
101-
var configurationProvider = new ConfigProvider(fileSystem, log, configFileLocator, gitPreparer, configInitWizard);
110+
SetupConfigFileContent(string.Empty);
111+
112+
var configurationProvider = sp.GetService<IConfigProvider>();
102113

103114
configurationProvider.Provide(repoPath);
104115
stringLogger.Length.ShouldBe(0);
@@ -107,31 +118,41 @@ public void NoWarnOnCustomYmlFile()
107118
[Test]
108119
public void NoWarnOnCustomYmlFileOutsideRepoPath()
109120
{
110-
SetupConfigFileContent(string.Empty, path: @"c:\\Unrelated\\path");
111-
112121
var stringLogger = string.Empty;
113122
void Action(string info) => stringLogger = info;
114123

115124
var logAppender = new TestLogAppender(Action);
116-
log = new Log(logAppender);
125+
var log = new Log(logAppender);
117126

118-
configFileLocator = new NamedConfigFileLocator(fileSystem, log, options);
119-
var gitPreparer = new GitPreparer(log, new TestEnvironment(), Options.Create(new Arguments { TargetPath = repoPath }));
120-
var configInitWizard = new ConfigInitWizard(new ConsoleAdapter(), stepFactory);
121-
var configurationProvider = new ConfigProvider(fileSystem, log, configFileLocator, gitPreparer, configInitWizard);
127+
var sp = GetServiceProvider(arguments, log);
128+
configFileLocator = sp.GetService<IConfigFileLocator>();
129+
fileSystem = sp.GetService<IFileSystem>();
130+
131+
SetupConfigFileContent(string.Empty, path: @"c:\\Unrelated\\path");
132+
133+
var configurationProvider = sp.GetService<IConfigProvider>();
122134

123135
configurationProvider.Provide(repoPath);
124136
stringLogger.Length.ShouldBe(0);
125137
}
126138

127139
private string SetupConfigFileContent(string text, string fileName = null, string path = null)
128140
{
129-
if (string.IsNullOrEmpty(fileName)) fileName = configFileLocator.FilePath;
141+
if (string.IsNullOrEmpty(fileName)) fileName = ((NamedConfigFileLocator)configFileLocator).FilePath;
130142
var filePath = fileName;
131143
if (!string.IsNullOrEmpty(path))
132144
filePath = Path.Combine(path, filePath);
133145
fileSystem.WriteAllText(filePath, text);
134146
return filePath;
135147
}
148+
149+
private static IServiceProvider GetServiceProvider(Arguments arguments, ILog log = null)
150+
{
151+
return ConfigureServices(services =>
152+
{
153+
if (log != null) services.AddSingleton(log);
154+
services.AddSingleton(Options.Create(arguments));
155+
});
156+
}
136157
}
137158
}

src/GitVersionCore.Tests/VersionCalculation/BaseVersionCalculatorTests.cs

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@
1010
using LibGit2Sharp;
1111
using NUnit.Framework;
1212
using Shouldly;
13-
using GitVersion.Logging;
1413
using GitVersion.Extensions;
1514
using GitVersionCore.Tests.Helpers;
15+
using Microsoft.Extensions.DependencyInjection;
16+
using Microsoft.Extensions.DependencyInjection.Extensions;
1617

1718
namespace GitVersionCore.Tests.VersionCalculation
1819
{
1920
[TestFixture]
2021
public class BaseVersionCalculatorTests : TestBase
2122
{
22-
private readonly ILog log;
23-
24-
public BaseVersionCalculatorTests()
25-
{
26-
log = new NullLog();
27-
}
28-
2923
[Test]
3024
public void ChoosesHighestVersionReturnedFromStrategies()
3125
{
3226
var context = new GitVersionContextBuilder().Build();
3327
var dateTimeOffset = DateTimeOffset.Now;
3428

35-
var versionStrategies = new IVersionStrategy[] { new V1Strategy(DateTimeOffset.Now), new V2Strategy(dateTimeOffset) };
36-
var sut = new BaseVersionCalculator(log, versionStrategies);
29+
var sp = ConfigureServices(services =>
30+
{
31+
services.RemoveAll<IVersionStrategy>();
32+
services.AddSingleton<IVersionStrategy>(new V1Strategy(DateTimeOffset.Now));
33+
services.AddSingleton<IVersionStrategy>(new V2Strategy(dateTimeOffset));
34+
});
35+
36+
var versionCalculator = sp.GetService<IBaseVersionCalculator>();
3737

38-
var baseVersion = sut.GetBaseVersion(context);
38+
var baseVersion = versionCalculator.GetBaseVersion(context);
3939

4040
baseVersion.SemanticVersion.ToString().ShouldBe("2.0.0");
4141
baseVersion.ShouldIncrement.ShouldBe(true);
@@ -48,10 +48,16 @@ public void UsesWhenFromNextBestMatchIfHighestDoesntHaveWhen()
4848
var context = new GitVersionContextBuilder().Build();
4949
var when = DateTimeOffset.Now;
5050

51-
var versionStrategies = new IVersionStrategy[] { new V1Strategy(when), new V2Strategy(null) };
52-
var sut = new BaseVersionCalculator(log, versionStrategies);
51+
var sp = ConfigureServices(services =>
52+
{
53+
services.RemoveAll<IVersionStrategy>();
54+
services.AddSingleton<IVersionStrategy>(new V1Strategy(when));
55+
services.AddSingleton<IVersionStrategy>(new V2Strategy(null));
56+
});
57+
58+
var versionCalculator = sp.GetService<IBaseVersionCalculator>();
5359

54-
var baseVersion = sut.GetBaseVersion(context);
60+
var baseVersion = versionCalculator.GetBaseVersion(context);
5561

5662
baseVersion.SemanticVersion.ToString().ShouldBe("2.0.0");
5763
baseVersion.ShouldIncrement.ShouldBe(true);
@@ -64,10 +70,16 @@ public void UsesWhenFromNextBestMatchIfHighestDoesntHaveWhenReversedOrder()
6470
var context = new GitVersionContextBuilder().Build();
6571
var when = DateTimeOffset.Now;
6672

67-
var versionStrategies = new IVersionStrategy[] { new V1Strategy(null), new V2Strategy(when) };
68-
var sut = new BaseVersionCalculator(log, versionStrategies);
73+
var sp = ConfigureServices(services =>
74+
{
75+
services.RemoveAll<IVersionStrategy>();
76+
services.AddSingleton<IVersionStrategy>(new V1Strategy(null));
77+
services.AddSingleton<IVersionStrategy>(new V2Strategy(when));
78+
});
79+
80+
var versionCalculator = sp.GetService<IBaseVersionCalculator>();
6981

70-
var baseVersion = sut.GetBaseVersion(context);
82+
var baseVersion = versionCalculator.GetBaseVersion(context);
7183

7284
baseVersion.SemanticVersion.ToString().ShouldBe("2.0.0");
7385
baseVersion.ShouldIncrement.ShouldBe(true);
@@ -81,10 +93,15 @@ public void ShouldNotFilterVersion()
8193
var context = new GitVersionContextBuilder().WithConfig(new Config() { Ignore = fakeIgnoreConfig }).Build();
8294
var version = new BaseVersion(context, "dummy", false, new SemanticVersion(2), new MockCommit(), null);
8395

84-
var versionStrategies = new IVersionStrategy[] { new TestVersionStrategy(version) };
85-
var sut = new BaseVersionCalculator(log, versionStrategies);
96+
var sp = ConfigureServices(services =>
97+
{
98+
services.RemoveAll<IVersionStrategy>();
99+
services.AddSingleton<IVersionStrategy>(new TestVersionStrategy(version));
100+
});
86101

87-
var baseVersion = sut.GetBaseVersion(context);
102+
var versionCalculator = sp.GetService<IBaseVersionCalculator>();
103+
104+
var baseVersion = versionCalculator.GetBaseVersion(context);
88105

89106
baseVersion.Source.ShouldBe(version.Source);
90107
baseVersion.ShouldIncrement.ShouldBe(version.ShouldIncrement);
@@ -99,10 +116,15 @@ public void ShouldFilterVersion()
99116
var higherVersion = new BaseVersion(context, "exclude", false, new SemanticVersion(2), new MockCommit(), null);
100117
var lowerVersion = new BaseVersion(context, "dummy", false, new SemanticVersion(1), new MockCommit(), null);
101118

102-
var versionStrategies = new IVersionStrategy[] { new TestVersionStrategy(higherVersion, lowerVersion) };
103-
var sut = new BaseVersionCalculator(log, versionStrategies);
119+
var sp = ConfigureServices(services =>
120+
{
121+
services.RemoveAll<IVersionStrategy>();
122+
services.AddSingleton<IVersionStrategy>(new TestVersionStrategy(higherVersion, lowerVersion));
123+
});
124+
125+
var versionCalculator = sp.GetService<IBaseVersionCalculator>();
104126

105-
var baseVersion = sut.GetBaseVersion(context);
127+
var baseVersion = versionCalculator.GetBaseVersion(context);
106128

107129
baseVersion.Source.ShouldNotBe(higherVersion.Source);
108130
baseVersion.SemanticVersion.ShouldNotBe(higherVersion.SemanticVersion);

src/GitVersionCore.Tests/VersionCalculation/Strategies/MergeMessageBaseVersionStrategyTests.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using GitVersion.Configuration;
4+
using GitVersion.Logging;
45
using GitVersion.VersionCalculation.BaseVersionCalculators;
56
using GitVersionCore.Tests.Helpers;
67
using GitVersionCore.Tests.Mocks;
78
using LibGit2Sharp;
9+
using Microsoft.Extensions.DependencyInjection;
810
using NUnit.Framework;
911
using Shouldly;
1012

@@ -13,6 +15,14 @@ namespace GitVersionCore.Tests.VersionCalculation.Strategies
1315
[TestFixture]
1416
public class MergeMessageBaseVersionStrategyTests : TestBase
1517
{
18+
private readonly ILog log;
19+
20+
public MergeMessageBaseVersionStrategyTests()
21+
{
22+
var sp = ConfigureServices();
23+
log = sp.GetService<ILog>();
24+
}
25+
1626
[Test]
1727
public void ShouldNotAllowIncrementOfVersion()
1828
{
@@ -26,7 +36,7 @@ public void ShouldNotAllowIncrementOfVersion()
2636
ParentsEx = GetParents(true)
2737
} }
2838
}).Build();
29-
var strategy = new MergeMessageVersionStrategy();
39+
var strategy = new MergeMessageVersionStrategy(log);
3040

3141
var baseVersion = strategy.GetVersions(context).Single();
3242

@@ -144,7 +154,7 @@ public void TakesVersionFromMergeOfConfiguredReleaseBranch(string message, strin
144154
AssertMergeMessage(message, expectedVersion, parents, config);
145155
}
146156

147-
private static void AssertMergeMessage(string message, string expectedVersion, List<Commit> parents, Config config = null)
157+
private void AssertMergeMessage(string message, string expectedVersion, List<Commit> parents, Config config = null)
148158
{
149159
var commit = new MockCommit
150160
{
@@ -163,7 +173,7 @@ private static void AssertMergeMessage(string message, string expectedVersion, L
163173
}
164174
})
165175
.Build();
166-
var strategy = new MergeMessageVersionStrategy();
176+
var strategy = new MergeMessageVersionStrategy(log);
167177

168178
var baseVersion = strategy.GetVersions(context).SingleOrDefault();
169179

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using GitVersion;
2-
using GitVersion.Logging;
32
using GitVersion.VersionCalculation;
43
using GitVersion.VersionCalculation.BaseVersionCalculators;
54
using LibGit2Sharp;
@@ -24,20 +23,4 @@ public BaseVersion GetBaseVersion(GitVersionContext context)
2423
return new BaseVersion(context, "Test source", shouldIncrement, semanticVersion, source, null);
2524
}
2625
}
27-
28-
public class TestBaseVersionStrategiesCalculator : BaseVersionCalculator
29-
{
30-
private static readonly IVersionStrategy[] VersionStrategies =
31-
{
32-
new FallbackVersionStrategy(),
33-
new ConfigNextVersionVersionStrategy(),
34-
new TaggedCommitVersionStrategy(),
35-
new MergeMessageVersionStrategy(),
36-
new VersionInBranchNameVersionStrategy(),
37-
new TrackReleaseBranchesVersionStrategy()
38-
};
39-
public TestBaseVersionStrategiesCalculator(ILog log) : base(log, VersionStrategies)
40-
{
41-
}
42-
}
4326
}

0 commit comments

Comments
 (0)