1
+ using System ;
1
2
using System . IO ;
2
3
using NUnit . Framework ;
3
4
using Shouldly ;
4
5
using GitVersion . Configuration ;
5
6
using GitVersion . Exceptions ;
6
7
using GitVersion ;
7
- using GitVersion . Configuration . Init . Wizard ;
8
8
using GitVersion . Logging ;
9
9
using GitVersionCore . Tests . Helpers ;
10
+ using Microsoft . Extensions . DependencyInjection ;
10
11
using Microsoft . Extensions . Options ;
11
12
12
13
namespace GitVersionCore . Tests
@@ -20,29 +21,26 @@ public class NamedConfigFileLocatorTests : TestBase
20
21
private string repoPath ;
21
22
private string workingPath ;
22
23
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 ;
27
26
28
27
[ SetUp ]
29
28
public void Setup ( )
30
29
{
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" } ;
36
31
repoPath = DefaultRepoPath ;
37
32
workingPath = DefaultWorkingPath ;
38
- stepFactory = new ConfigInitStepFactory ( ) ;
39
33
40
34
ShouldlyConfiguration . ShouldMatchApprovedDefaults . LocateTestMethodUsingAttribute < TestAttribute > ( ) ;
41
35
}
42
36
43
37
[ Test ]
44
38
public void ThrowsExceptionOnAmbiguousConfigFileLocation ( )
45
39
{
40
+ var sp = GetServiceProvider ( arguments ) ;
41
+ configFileLocator = sp . GetService < IConfigFileLocator > ( ) ;
42
+ fileSystem = sp . GetService < IFileSystem > ( ) ;
43
+
46
44
var repositoryConfigFilePath = SetupConfigFileContent ( string . Empty , path : repoPath ) ;
47
45
var workingDirectoryConfigFilePath = SetupConfigFileContent ( string . Empty , path : workingPath ) ;
48
46
@@ -56,6 +54,11 @@ public void ThrowsExceptionOnAmbiguousConfigFileLocation()
56
54
public void DoNotThrowWhenWorkingAndRepoPathsAreSame ( )
57
55
{
58
56
workingPath = DefaultRepoPath ;
57
+
58
+ var sp = GetServiceProvider ( arguments ) ;
59
+ configFileLocator = sp . GetService < IConfigFileLocator > ( ) ;
60
+ fileSystem = sp . GetService < IFileSystem > ( ) ;
61
+
59
62
SetupConfigFileContent ( string . Empty , path : workingPath ) ;
60
63
61
64
Should . NotThrow ( ( ) => { configFileLocator . Verify ( workingPath , repoPath ) ; } ) ;
@@ -66,6 +69,11 @@ public void DoNotThrowWhenWorkingAndRepoPathsAreSame()
66
69
public void DoNotThrowWhenWorkingAndRepoPathsAreSame_WithDifferentCasing ( )
67
70
{
68
71
workingPath = DefaultRepoPath . ToLower ( ) ;
72
+
73
+ var sp = GetServiceProvider ( arguments ) ;
74
+ configFileLocator = sp . GetService < IConfigFileLocator > ( ) ;
75
+ fileSystem = sp . GetService < IFileSystem > ( ) ;
76
+
69
77
SetupConfigFileContent ( string . Empty , path : workingPath ) ;
70
78
71
79
Should . NotThrow ( ( ) => { configFileLocator . Verify ( workingPath , repoPath ) ; } ) ;
@@ -76,8 +84,11 @@ public void DoNotThrowWhenConfigFileIsInSubDirectoryOfRepoPath()
76
84
{
77
85
workingPath = DefaultRepoPath ;
78
86
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
+
81
92
SetupConfigFileContent ( string . Empty , path : workingPath ) ;
82
93
83
94
Should . NotThrow ( ( ) => { configFileLocator . Verify ( workingPath , repoPath ) ; } ) ;
@@ -86,19 +97,19 @@ public void DoNotThrowWhenConfigFileIsInSubDirectoryOfRepoPath()
86
97
[ Test ]
87
98
public void NoWarnOnCustomYmlFile ( )
88
99
{
89
- SetupConfigFileContent ( string . Empty ) ;
90
-
91
100
var stringLogger = string . Empty ;
92
101
void Action ( string info ) => stringLogger = info ;
93
102
94
103
var logAppender = new TestLogAppender ( Action ) ;
95
- log = new Log ( logAppender ) ;
104
+ var log = new Log ( logAppender ) ;
96
105
97
- configFileLocator = new NamedConfigFileLocator ( fileSystem , log , options ) ;
106
+ var sp = GetServiceProvider ( arguments , log ) ;
107
+ configFileLocator = sp . GetService < IConfigFileLocator > ( ) ;
108
+ fileSystem = sp . GetService < IFileSystem > ( ) ;
98
109
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 > ( ) ;
102
113
103
114
configurationProvider . Provide ( repoPath ) ;
104
115
stringLogger . Length . ShouldBe ( 0 ) ;
@@ -107,31 +118,41 @@ public void NoWarnOnCustomYmlFile()
107
118
[ Test ]
108
119
public void NoWarnOnCustomYmlFileOutsideRepoPath ( )
109
120
{
110
- SetupConfigFileContent ( string . Empty , path : @"c:\\Unrelated\\path" ) ;
111
-
112
121
var stringLogger = string . Empty ;
113
122
void Action ( string info ) => stringLogger = info ;
114
123
115
124
var logAppender = new TestLogAppender ( Action ) ;
116
- log = new Log ( logAppender ) ;
125
+ var log = new Log ( logAppender ) ;
117
126
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 > ( ) ;
122
134
123
135
configurationProvider . Provide ( repoPath ) ;
124
136
stringLogger . Length . ShouldBe ( 0 ) ;
125
137
}
126
138
127
139
private string SetupConfigFileContent ( string text , string fileName = null , string path = null )
128
140
{
129
- if ( string . IsNullOrEmpty ( fileName ) ) fileName = configFileLocator . FilePath ;
141
+ if ( string . IsNullOrEmpty ( fileName ) ) fileName = ( ( NamedConfigFileLocator ) configFileLocator ) . FilePath ;
130
142
var filePath = fileName ;
131
143
if ( ! string . IsNullOrEmpty ( path ) )
132
144
filePath = Path . Combine ( path , filePath ) ;
133
145
fileSystem . WriteAllText ( filePath , text ) ;
134
146
return filePath ;
135
147
}
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
+ }
136
157
}
137
158
}
0 commit comments