Skip to content

Commit 4ed8f44

Browse files
committed
#2634 - fix config file locator
1 parent 9abbe00 commit 4ed8f44

File tree

3 files changed

+211
-212
lines changed

3 files changed

+211
-212
lines changed
Lines changed: 209 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using System;
12
using System.IO;
23
using GitVersion.Configuration;
34
using GitVersion.Core.Tests.Helpers;
5+
using GitVersion.Logging;
46
using Microsoft.Extensions.DependencyInjection;
57
using Microsoft.Extensions.Options;
68
using NUnit.Framework;
@@ -9,65 +11,230 @@
911
namespace GitVersion.Core.Tests
1012
{
1113
[TestFixture]
12-
public class DefaultConfigFileLocatorTests : TestBase
14+
public class ConfigFileLocatorTests
1315
{
14-
private const string DefaultRepoPath = @"c:\MyGitRepo";
15-
private const string DefaultWorkingPath = @"c:\MyGitRepo\Working";
16+
public class DefaultConfigFileLocatorTests : TestBase
17+
{
18+
private const string DefaultRepoPath = @"c:\MyGitRepo";
19+
private const string DefaultWorkingPath = @"c:\MyGitRepo\Working";
1620

17-
private string repoPath;
18-
private string workingPath;
19-
private IFileSystem fileSystem;
20-
private IConfigProvider configurationProvider;
21-
private IConfigFileLocator configFileLocator;
21+
private string repoPath;
22+
private string workingPath;
23+
private IFileSystem fileSystem;
24+
private IConfigProvider configurationProvider;
25+
private IConfigFileLocator configFileLocator;
2226

23-
[SetUp]
24-
public void Setup()
25-
{
26-
repoPath = DefaultRepoPath;
27-
workingPath = DefaultWorkingPath;
28-
var options = Options.Create(new GitVersionOptions { WorkingDirectory = repoPath });
27+
[SetUp]
28+
public void Setup()
29+
{
30+
repoPath = DefaultRepoPath;
31+
workingPath = DefaultWorkingPath;
32+
var options = Options.Create(new GitVersionOptions { WorkingDirectory = repoPath });
33+
34+
var sp = ConfigureServices(services =>
35+
{
36+
services.AddSingleton(options);
37+
});
2938

30-
var sp = ConfigureServices(services =>
39+
fileSystem = sp.GetService<IFileSystem>();
40+
configurationProvider = sp.GetService<IConfigProvider>();
41+
configFileLocator = sp.GetService<IConfigFileLocator>();
42+
43+
ShouldlyConfiguration.ShouldMatchApprovedDefaults.LocateTestMethodUsingAttribute<TestAttribute>();
44+
}
45+
46+
[TestCase(ConfigFileLocator.DefaultFileName, ConfigFileLocator.DefaultFileName)]
47+
public void ThrowsExceptionOnAmbiguousConfigFileLocation(string repoConfigFile, string workingConfigFile)
3148
{
32-
services.AddSingleton(options);
33-
});
49+
var repositoryConfigFilePath = SetupConfigFileContent(string.Empty, repoConfigFile, repoPath);
50+
var workingDirectoryConfigFilePath = SetupConfigFileContent(string.Empty, workingConfigFile, workingPath);
3451

35-
fileSystem = sp.GetService<IFileSystem>();
36-
configurationProvider = sp.GetService<IConfigProvider>();
37-
configFileLocator = sp.GetService<IConfigFileLocator>();
52+
var exception = Should.Throw<WarningException>(() =>
53+
{
54+
configFileLocator.Verify(workingPath, repoPath);
55+
});
3856

39-
ShouldlyConfiguration.ShouldMatchApprovedDefaults.LocateTestMethodUsingAttribute<TestAttribute>();
40-
}
57+
var expecedMessage = $"Ambiguous config file selection from '{workingDirectoryConfigFilePath}' and '{repositoryConfigFilePath}'";
58+
exception.Message.ShouldBe(expecedMessage);
59+
}
4160

42-
[TestCase(ConfigFileLocator.DefaultFileName, ConfigFileLocator.DefaultFileName)]
43-
public void ThrowsExceptionOnAmbiguousConfigFileLocation(string repoConfigFile, string workingConfigFile)
44-
{
45-
var repositoryConfigFilePath = SetupConfigFileContent(string.Empty, repoConfigFile, repoPath);
46-
var workingDirectoryConfigFilePath = SetupConfigFileContent(string.Empty, workingConfigFile, workingPath);
61+
[Test]
62+
public void NoWarnOnGitVersionYmlFile()
63+
{
64+
SetupConfigFileContent(string.Empty, ConfigFileLocator.DefaultFileName, repoPath);
65+
66+
Should.NotThrow(() => { configurationProvider.Provide(repoPath); });
67+
}
68+
69+
[Test]
70+
public void NoWarnOnNoGitVersionYmlFile()
71+
{
72+
Should.NotThrow(() => { configurationProvider.Provide(repoPath); });
73+
}
4774

48-
var exception = Should.Throw<WarningException>(() =>
75+
private string SetupConfigFileContent(string text, string fileName, string path)
4976
{
50-
configFileLocator.Verify(workingPath, repoPath);
51-
});
77+
var fullPath = Path.Combine(path, fileName);
78+
fileSystem.WriteAllText(fullPath, text);
5279

53-
var expecedMessage = $"Ambiguous config file selection from '{workingDirectoryConfigFilePath}' and '{repositoryConfigFilePath}'";
54-
exception.Message.ShouldBe(expecedMessage);
80+
return fullPath;
81+
}
5582
}
5683

57-
[Test]
58-
public void NoWarnOnGitVersionYmlFile()
84+
public class NamedConfigFileLocatorTests : TestBase
5985
{
60-
SetupConfigFileContent(string.Empty, ConfigFileLocator.DefaultFileName, repoPath);
86+
private const string DefaultRepoPath = @"c:\MyGitRepo";
87+
private const string DefaultWorkingPath = @"c:\MyGitRepo\Working";
6188

62-
Should.NotThrow(() => { configurationProvider.Provide(repoPath); });
63-
}
89+
private string repoPath;
90+
private string workingPath;
91+
private IFileSystem fileSystem;
92+
private IConfigFileLocator configFileLocator;
93+
private GitVersionOptions gitVersionOptions;
6494

65-
private string SetupConfigFileContent(string text, string fileName, string path)
66-
{
67-
var fullPath = Path.Combine(path, fileName);
68-
fileSystem.WriteAllText(fullPath, text);
95+
[SetUp]
96+
public void Setup()
97+
{
98+
gitVersionOptions = new GitVersionOptions { ConfigInfo = { ConfigFile = "my-config.yaml" } };
99+
repoPath = DefaultRepoPath;
100+
workingPath = DefaultWorkingPath;
101+
102+
ShouldlyConfiguration.ShouldMatchApprovedDefaults.LocateTestMethodUsingAttribute<TestAttribute>();
103+
}
104+
105+
[Test]
106+
public void ThrowsExceptionOnAmbiguousConfigFileLocation()
107+
{
108+
var sp = GetServiceProvider(gitVersionOptions);
109+
configFileLocator = sp.GetService<IConfigFileLocator>();
110+
fileSystem = sp.GetService<IFileSystem>();
111+
112+
var repositoryConfigFilePath = SetupConfigFileContent(string.Empty, path: repoPath);
113+
var workingDirectoryConfigFilePath = SetupConfigFileContent(string.Empty, path: workingPath);
114+
115+
var exception = Should.Throw<WarningException>(() => { configFileLocator.Verify(workingPath, repoPath); });
116+
117+
var expectedMessage = $"Ambiguous config file selection from '{workingDirectoryConfigFilePath}' and '{repositoryConfigFilePath}'";
118+
exception.Message.ShouldBe(expectedMessage);
119+
}
120+
121+
[Test]
122+
public void DoNotThrowWhenWorkingAndRepoPathsAreSame()
123+
{
124+
workingPath = DefaultRepoPath;
125+
126+
var sp = GetServiceProvider(gitVersionOptions);
127+
configFileLocator = sp.GetService<IConfigFileLocator>();
128+
fileSystem = sp.GetService<IFileSystem>();
129+
130+
SetupConfigFileContent(string.Empty, path: workingPath);
131+
132+
Should.NotThrow(() => { configFileLocator.Verify(workingPath, repoPath); });
133+
}
134+
135+
[Test]
136+
public void DoNotThrowWhenWorkingAndRepoPathsAreSame_WithDifferentCasing()
137+
{
138+
workingPath = DefaultRepoPath.ToLower();
139+
140+
var sp = GetServiceProvider(gitVersionOptions);
141+
configFileLocator = sp.GetService<IConfigFileLocator>();
142+
fileSystem = sp.GetService<IFileSystem>();
69143

70-
return fullPath;
144+
SetupConfigFileContent(string.Empty, path: workingPath);
145+
146+
Should.NotThrow(() => { configFileLocator.Verify(workingPath, repoPath); });
147+
}
148+
149+
[Test]
150+
public void DoNotThrowWhenConfigFileIsInSubDirectoryOfRepoPath()
151+
{
152+
workingPath = DefaultRepoPath;
153+
154+
gitVersionOptions = new GitVersionOptions { ConfigInfo = { ConfigFile = "./src/my-config.yaml" } };
155+
var sp = GetServiceProvider(gitVersionOptions);
156+
configFileLocator = sp.GetService<IConfigFileLocator>();
157+
fileSystem = sp.GetService<IFileSystem>();
158+
159+
SetupConfigFileContent(string.Empty, path: workingPath);
160+
161+
Should.NotThrow(() => { configFileLocator.Verify(workingPath, repoPath); });
162+
}
163+
164+
[Test]
165+
public void NoWarnOnCustomYmlFile()
166+
{
167+
var stringLogger = string.Empty;
168+
void Action(string info) => stringLogger = info;
169+
170+
var logAppender = new TestLogAppender(Action);
171+
var log = new Log(logAppender);
172+
173+
var sp = GetServiceProvider(gitVersionOptions, log);
174+
configFileLocator = sp.GetService<IConfigFileLocator>();
175+
fileSystem = sp.GetService<IFileSystem>();
176+
177+
SetupConfigFileContent(string.Empty);
178+
179+
var configurationProvider = sp.GetService<IConfigProvider>();
180+
181+
configurationProvider.Provide(repoPath);
182+
stringLogger.Length.ShouldBe(0);
183+
}
184+
185+
[Test]
186+
public void NoWarnOnCustomYmlFileOutsideRepoPath()
187+
{
188+
var stringLogger = string.Empty;
189+
void Action(string info) => stringLogger = info;
190+
191+
var logAppender = new TestLogAppender(Action);
192+
var log = new Log(logAppender);
193+
194+
var sp = GetServiceProvider(gitVersionOptions, log);
195+
configFileLocator = sp.GetService<IConfigFileLocator>();
196+
fileSystem = sp.GetService<IFileSystem>();
197+
198+
SetupConfigFileContent(string.Empty, path: @"c:\\Unrelated\\path");
199+
200+
var configurationProvider = sp.GetService<IConfigProvider>();
201+
202+
configurationProvider.Provide(repoPath);
203+
stringLogger.Length.ShouldBe(0);
204+
}
205+
206+
[Test]
207+
public void ThrowsExceptionOnCustomYmlFileDoesNotExist()
208+
{
209+
var sp = GetServiceProvider(gitVersionOptions);
210+
configFileLocator = sp.GetService<IConfigFileLocator>();
211+
212+
var exception = Should.Throw<WarningException>(() => { configFileLocator.Verify(workingPath, repoPath); });
213+
214+
var workingPathFileConfig = Path.Combine(workingPath, gitVersionOptions.ConfigInfo.ConfigFile);
215+
var repoPathFileConfig = Path.Combine(repoPath, gitVersionOptions.ConfigInfo.ConfigFile);
216+
var expectedMessage = $"The configuration file was not found at '{workingPathFileConfig}' or '{repoPathFileConfig}'";
217+
exception.Message.ShouldBe(expectedMessage);
218+
}
219+
220+
private string SetupConfigFileContent(string text, string fileName = null, string path = null)
221+
{
222+
if (string.IsNullOrEmpty(fileName)) fileName = configFileLocator.FilePath;
223+
var filePath = fileName;
224+
if (!string.IsNullOrEmpty(path))
225+
filePath = Path.Combine(path, filePath);
226+
fileSystem.WriteAllText(filePath, text);
227+
return filePath;
228+
}
229+
230+
private static IServiceProvider GetServiceProvider(GitVersionOptions gitVersionOptions, ILog log = null)
231+
{
232+
return ConfigureServices(services =>
233+
{
234+
if (log != null) services.AddSingleton(log);
235+
services.AddSingleton(Options.Create(gitVersionOptions));
236+
});
237+
}
71238
}
72239
}
73240
}

0 commit comments

Comments
 (0)