Skip to content

Commit b5f4404

Browse files
authored
Merge pull request #4483 from kucuk-furkan/main
Make sure custom configuration file path is returned when custom configuration is set
2 parents cc19461 + cb0743f commit b5f4404

File tree

4 files changed

+62
-26
lines changed

4 files changed

+62
-26
lines changed

src/GitVersion.Configuration.Tests/Configuration/ConfigurationFileLocatorTests.cs

+27-7
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,39 @@ public void DoNotThrowWhenWorkingAndRepoPathsAreSame_WithDifferentCasing()
154154
}
155155

156156
[Test]
157-
public void DoNotThrowWhenFileNameAreSame_WithDifferentCasing()
157+
public void ReturnConfigurationFilePathIfCustomConfigurationIsSet()
158158
{
159159
this.workingPath = this.repoPath;
160+
string configurationFilePath = Path.Combine(this.workingPath, "Configuration", "CustomConfig.yaml");
160161

161-
this.gitVersionOptions = new() { ConfigurationInfo = { ConfigurationFile = "MyConfig.yaml" } };
162+
this.gitVersionOptions = new() { ConfigurationInfo = { ConfigurationFile = configurationFilePath } };
163+
164+
var serviceProvider = GetServiceProvider(this.gitVersionOptions);
165+
this.fileSystem = serviceProvider.GetRequiredService<IFileSystem>();
166+
167+
using var _ = this.fileSystem.SetupConfigFile(
168+
path: Path.Combine(this.workingPath, "Configuration"), fileName: "CustomConfig.yaml"
169+
);
170+
this.configFileLocator = serviceProvider.GetRequiredService<IConfigurationFileLocator>();
171+
172+
var config = this.configFileLocator.GetConfigurationFile(this.workingPath);
173+
config.ShouldBe(configurationFilePath);
174+
}
175+
176+
[TestCase(null)]
177+
[TestCase("")]
178+
[TestCase(" ")]
179+
[TestCase("Configuration/CustomConfig2.yaml")]
180+
public void ReturnConfigurationFilePathIfCustomConfigurationIsSet_InvalidConfigurationFilePaths(string? configFile)
181+
{
182+
this.workingPath = this.repoPath;
183+
184+
this.gitVersionOptions = new() { ConfigurationInfo = { ConfigurationFile = configFile } };
162185
var sp = GetServiceProvider(this.gitVersionOptions);
163186
this.configFileLocator = sp.GetRequiredService<IConfigurationFileLocator>();
164-
this.fileSystem = sp.GetRequiredService<IFileSystem>();
165-
166-
using var _ = this.fileSystem.SetupConfigFile(path: this.workingPath, fileName: ConfigFile.ToLower());
167187

168-
var config = Should.NotThrow(() => this.configFileLocator.GetConfigurationFile(this.workingPath));
169-
config.ShouldNotBe(null);
188+
var config = this.configFileLocator.GetConfigurationFile(this.workingPath);
189+
config.ShouldBe(null);
170190
}
171191

172192
[Test]

src/GitVersion.Configuration/ConfigurationFileLocator.cs

+33-17
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,48 @@ public void Verify(string? workingDirectory, string? projectRootDirectory)
3838
WarnAboutAmbiguousConfigFileSelection(workingDirectory, projectRootDirectory);
3939
}
4040

41-
public string? GetConfigurationFile(string? directory)
41+
public string? GetConfigurationFile(string? directoryPath)
4242
{
43-
if (directory is null) return null;
43+
string? customConfigurationFile = GetCustomConfigurationFilePathIfEligable(directoryPath);
44+
if (!string.IsNullOrWhiteSpace(customConfigurationFile))
45+
{
46+
return customConfigurationFile;
47+
}
4448

45-
string[] candidates = !string.IsNullOrWhiteSpace(this.ConfigurationFile)
46-
? [this.ConfigurationFile, .. this.SupportedConfigFileNames]
47-
: this.SupportedConfigFileNames;
49+
if (string.IsNullOrWhiteSpace(directoryPath) || !fileSystem.Directory.Exists(directoryPath))
50+
{
51+
return null;
52+
}
4853

49-
foreach (var fileName in candidates)
54+
string[] files = fileSystem.Directory.GetFiles(directoryPath);
55+
foreach (var fileName in this.SupportedConfigFileNames)
5056
{
51-
this.log.Debug($"Trying to find configuration file {fileName} at '{directory}'");
52-
if (directory != null && fileSystem.Directory.Exists(directory))
57+
this.log.Debug($"Trying to find configuration file {fileName} at '{directoryPath}'");
58+
string? matchingFile = files.FirstOrDefault(file => string.Equals(PathHelper.GetFileName(file), fileName, StringComparison.OrdinalIgnoreCase));
59+
if (matchingFile != null)
5360
{
54-
var files = fileSystem.Directory.GetFiles(directory);
61+
this.log.Info($"Found configuration file at '{matchingFile}'");
62+
return matchingFile;
63+
}
64+
}
5565

56-
var matchingFile = files.FirstOrDefault(file =>
57-
string.Equals(fileSystem.Path.GetFileName(file), fileName, StringComparison.OrdinalIgnoreCase));
66+
return null;
67+
}
5868

59-
if (matchingFile != null)
60-
{
61-
this.log.Info($"Found configuration file at '{matchingFile}'");
62-
return matchingFile;
63-
}
69+
private string? GetCustomConfigurationFilePathIfEligable(string? directoryPath)
70+
{
71+
if (!string.IsNullOrWhiteSpace(this.ConfigurationFile))
72+
{
73+
string configurationFilePath = this.ConfigurationFile;
74+
if (!string.IsNullOrWhiteSpace(directoryPath))
75+
{
76+
configurationFilePath = Path.Combine(directoryPath, this.ConfigurationFile);
6477
}
6578

66-
this.log.Debug($"Configuration file {fileName} not found at '{directory}'");
79+
if (fileSystem.File.Exists(configurationFilePath))
80+
{
81+
return configurationFilePath;
82+
}
6783
}
6884

6985
return null;

src/GitVersion.Core/Configuration/IConfigurationFileLocator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ namespace GitVersion.Configuration;
33
public interface IConfigurationFileLocator
44
{
55
void Verify(string? workingDirectory, string? projectRootDirectory);
6-
string? GetConfigurationFile(string? directory);
6+
string? GetConfigurationFile(string? directoryPath);
77
}

src/GitVersion.Core/PublicAPI.Shipped.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ GitVersion.Configuration.IConfigurationBuilder
113113
GitVersion.Configuration.IConfigurationBuilder.AddOverride(System.Collections.Generic.IReadOnlyDictionary<object!, object?>! value) -> void
114114
GitVersion.Configuration.IConfigurationBuilder.Build() -> GitVersion.Configuration.IGitVersionConfiguration!
115115
GitVersion.Configuration.IConfigurationFileLocator
116-
GitVersion.Configuration.IConfigurationFileLocator.GetConfigurationFile(string? directory) -> string?
116+
GitVersion.Configuration.IConfigurationFileLocator.GetConfigurationFile(string? directoryPath) -> string?
117117
GitVersion.Configuration.IConfigurationFileLocator.Verify(string? workingDirectory, string? projectRootDirectory) -> void
118118
GitVersion.Configuration.IConfigurationProvider
119119
GitVersion.Configuration.IConfigurationProvider.Provide(System.Collections.Generic.IReadOnlyDictionary<object!, object?>? overrideConfiguration = null) -> GitVersion.Configuration.IGitVersionConfiguration!

0 commit comments

Comments
 (0)