Skip to content

Commit a07c8ee

Browse files
author
Furkan Küçük
committed
Additional suggestions applied
1 parent 5a00f5f commit a07c8ee

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public void ReturnConfigurationFilePathIfCustomConfigurationIsSet()
185185
this.configFileLocator = serviceProvider.GetRequiredService<IConfigurationFileLocator>();
186186

187187
var config = this.configFileLocator.GetConfigurationFile(this.workingPath);
188-
Path.GetFileName(config).ShouldBe("CustomConfig.yaml");
188+
config.ShouldBe(Path.GetFullPath("Configuration/CustomConfig.yaml"));
189189
}
190190

191191
[TestCase(null)]

src/GitVersion.Configuration/ConfigurationFileLocator.cs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,34 @@ 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 configuration file overriden and its exists, return it
44-
if (!string.IsNullOrWhiteSpace(this.ConfigurationFile) &&
45-
PathHelper.IsPathRooted(this.ConfigurationFile) &&
46-
fileSystem.File.Exists(this.ConfigurationFile))
47-
{
48-
return this.ConfigurationFile;
49-
}
50-
51-
if (directory is null) return null;
43+
string[] configurationFilePaths = string.IsNullOrWhiteSpace(this.ConfigurationFile)
44+
? this.SupportedConfigFileNames : [this.ConfigurationFile, .. this.SupportedConfigFileNames];
5245

53-
string[] candidates = !string.IsNullOrWhiteSpace(this.ConfigurationFile)
54-
? [this.ConfigurationFile, .. this.SupportedConfigFileNames]
55-
: this.SupportedConfigFileNames;
56-
57-
foreach (var fileName in candidates)
46+
foreach (var item in configurationFilePaths)
5847
{
59-
this.log.Debug($"Trying to find configuration file {fileName} at '{directory}'");
60-
if (directory != null && fileSystem.Directory.Exists(directory))
61-
{
62-
var files = fileSystem.Directory.GetFiles(directory);
48+
this.log.Debug($"Trying to find configuration file {item} at '{directoryPath}'");
6349

64-
var matchingFile = files.FirstOrDefault(file =>
65-
string.Equals(fileSystem.Path.GetFileName(file), fileName, StringComparison.OrdinalIgnoreCase));
66-
67-
if (matchingFile != null)
50+
var configurationFilePath = item;
51+
if (!PathHelper.IsPathRooted(configurationFilePath))
52+
{
53+
if (string.IsNullOrEmpty(directoryPath))
6854
{
69-
this.log.Info($"Found configuration file at '{matchingFile}'");
70-
return matchingFile;
55+
throw new WarningException(
56+
$"The configuration file '{configurationFilePath}' is relative and no directory path known."
57+
);
7158
}
59+
configurationFilePath = Path.Combine(directoryPath, configurationFilePath);
60+
}
61+
62+
if (fileSystem.File.Exists(configurationFilePath))
63+
{
64+
this.log.Info($"Found configuration file at '{configurationFilePath}'");
65+
return configurationFilePath;
7266
}
7367

74-
this.log.Debug($"Configuration file {fileName} not found at '{directory}'");
68+
this.log.Debug($"Configuration file {configurationFilePath} not found at '{directoryPath}'");
7569
}
7670

7771
return null;

0 commit comments

Comments
 (0)