|
| 1 | +using System; |
1 | 2 | using System.IO; |
2 | 3 | using GitVersion.Configuration; |
3 | 4 | using GitVersion.Core.Tests.Helpers; |
| 5 | +using GitVersion.Logging; |
4 | 6 | using Microsoft.Extensions.DependencyInjection; |
5 | 7 | using Microsoft.Extensions.Options; |
6 | 8 | using NUnit.Framework; |
|
9 | 11 | namespace GitVersion.Core.Tests |
10 | 12 | { |
11 | 13 | [TestFixture] |
12 | | - public class DefaultConfigFileLocatorTests : TestBase |
| 14 | + public class ConfigFileLocatorTests |
13 | 15 | { |
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"; |
16 | 20 |
|
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; |
22 | 26 |
|
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 | + }); |
29 | 38 |
|
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) |
31 | 48 | { |
32 | | - services.AddSingleton(options); |
33 | | - }); |
| 49 | + var repositoryConfigFilePath = SetupConfigFileContent(string.Empty, repoConfigFile, repoPath); |
| 50 | + var workingDirectoryConfigFilePath = SetupConfigFileContent(string.Empty, workingConfigFile, workingPath); |
34 | 51 |
|
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 | + }); |
38 | 56 |
|
39 | | - ShouldlyConfiguration.ShouldMatchApprovedDefaults.LocateTestMethodUsingAttribute<TestAttribute>(); |
40 | | - } |
| 57 | + var expecedMessage = $"Ambiguous config file selection from '{workingDirectoryConfigFilePath}' and '{repositoryConfigFilePath}'"; |
| 58 | + exception.Message.ShouldBe(expecedMessage); |
| 59 | + } |
41 | 60 |
|
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 | + } |
47 | 74 |
|
48 | | - var exception = Should.Throw<WarningException>(() => |
| 75 | + private string SetupConfigFileContent(string text, string fileName, string path) |
49 | 76 | { |
50 | | - configFileLocator.Verify(workingPath, repoPath); |
51 | | - }); |
| 77 | + var fullPath = Path.Combine(path, fileName); |
| 78 | + fileSystem.WriteAllText(fullPath, text); |
52 | 79 |
|
53 | | - var expecedMessage = $"Ambiguous config file selection from '{workingDirectoryConfigFilePath}' and '{repositoryConfigFilePath}'"; |
54 | | - exception.Message.ShouldBe(expecedMessage); |
| 80 | + return fullPath; |
| 81 | + } |
55 | 82 | } |
56 | 83 |
|
57 | | - [Test] |
58 | | - public void NoWarnOnGitVersionYmlFile() |
| 84 | + public class NamedConfigFileLocatorTests : TestBase |
59 | 85 | { |
60 | | - SetupConfigFileContent(string.Empty, ConfigFileLocator.DefaultFileName, repoPath); |
| 86 | + private const string DefaultRepoPath = @"c:\MyGitRepo"; |
| 87 | + private const string DefaultWorkingPath = @"c:\MyGitRepo\Working"; |
61 | 88 |
|
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; |
64 | 94 |
|
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>(); |
69 | 143 |
|
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 | + } |
71 | 238 | } |
72 | 239 | } |
73 | 240 | } |
0 commit comments