Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure custom configuration file path is returned when custom configuration is set #4483

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,39 @@ public void DoNotThrowWhenWorkingAndRepoPathsAreSame_WithDifferentCasing()
}

[Test]
public void DoNotThrowWhenFileNameAreSame_WithDifferentCasing()
public void ReturnConfigurationFilePathIfCustomConfigurationIsSet()
{
this.workingPath = this.repoPath;
string configurationFilePath = Path.Combine(this.workingPath, "Configuration", "CustomConfig.yaml");

this.gitVersionOptions = new() { ConfigurationInfo = { ConfigurationFile = "MyConfig.yaml" } };
this.gitVersionOptions = new() { ConfigurationInfo = { ConfigurationFile = configurationFilePath } };

var serviceProvider = GetServiceProvider(this.gitVersionOptions);
this.fileSystem = serviceProvider.GetRequiredService<IFileSystem>();

using var _ = this.fileSystem.SetupConfigFile(
path: Path.Combine(this.workingPath, "Configuration"), fileName: "CustomConfig.yaml"
);
this.configFileLocator = serviceProvider.GetRequiredService<IConfigurationFileLocator>();

var config = this.configFileLocator.GetConfigurationFile(this.workingPath);
config.ShouldBe(configurationFilePath);
}

[TestCase(null)]
[TestCase("")]
[TestCase(" ")]
[TestCase("Configuration/CustomConfig2.yaml")]
public void ReturnConfigurationFilePathIfCustomConfigurationIsSet_InvalidConfigurationFilePaths(string? configFile)
{
this.workingPath = this.repoPath;

this.gitVersionOptions = new() { ConfigurationInfo = { ConfigurationFile = configFile } };
var sp = GetServiceProvider(this.gitVersionOptions);
this.configFileLocator = sp.GetRequiredService<IConfigurationFileLocator>();
this.fileSystem = sp.GetRequiredService<IFileSystem>();

using var _ = this.fileSystem.SetupConfigFile(path: this.workingPath, fileName: ConfigFile.ToLower());

var config = Should.NotThrow(() => this.configFileLocator.GetConfigurationFile(this.workingPath));
config.ShouldNotBe(null);
var config = this.configFileLocator.GetConfigurationFile(this.workingPath);
config.ShouldBe(null);
}

[Test]
Expand Down
50 changes: 33 additions & 17 deletions src/GitVersion.Configuration/ConfigurationFileLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,48 @@ public void Verify(string? workingDirectory, string? projectRootDirectory)
WarnAboutAmbiguousConfigFileSelection(workingDirectory, projectRootDirectory);
}

public string? GetConfigurationFile(string? directory)
public string? GetConfigurationFile(string? directoryPath)
{
if (directory is null) return null;
string? customConfigurationFile = GetCustomConfigurationFilePathIfEligable(directoryPath);
if (!string.IsNullOrWhiteSpace(customConfigurationFile))
{
return customConfigurationFile;
}

string[] candidates = !string.IsNullOrWhiteSpace(this.ConfigurationFile)
? [this.ConfigurationFile, .. this.SupportedConfigFileNames]
: this.SupportedConfigFileNames;
if (string.IsNullOrWhiteSpace(directoryPath) || !fileSystem.Directory.Exists(directoryPath))
{
return null;
}

foreach (var fileName in candidates)
string[] files = fileSystem.Directory.GetFiles(directoryPath);
foreach (var fileName in this.SupportedConfigFileNames)
{
this.log.Debug($"Trying to find configuration file {fileName} at '{directory}'");
if (directory != null && fileSystem.Directory.Exists(directory))
this.log.Debug($"Trying to find configuration file {fileName} at '{directoryPath}'");
string? matchingFile = files.FirstOrDefault(file => string.Equals(PathHelper.GetFileName(file), fileName, StringComparison.OrdinalIgnoreCase));
if (matchingFile != null)
{
var files = fileSystem.Directory.GetFiles(directory);
this.log.Info($"Found configuration file at '{matchingFile}'");
return matchingFile;
}
}

var matchingFile = files.FirstOrDefault(file =>
string.Equals(fileSystem.Path.GetFileName(file), fileName, StringComparison.OrdinalIgnoreCase));
return null;
}

if (matchingFile != null)
{
this.log.Info($"Found configuration file at '{matchingFile}'");
return matchingFile;
}
private string? GetCustomConfigurationFilePathIfEligable(string? directoryPath)
{
if (!string.IsNullOrWhiteSpace(this.ConfigurationFile))
{
string configurationFilePath = this.ConfigurationFile;
if (!string.IsNullOrWhiteSpace(directoryPath))
{
configurationFilePath = Path.Combine(directoryPath, this.ConfigurationFile);
}

this.log.Debug($"Configuration file {fileName} not found at '{directory}'");
if (fileSystem.File.Exists(configurationFilePath))
{
return configurationFilePath;
}
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ namespace GitVersion.Configuration;
public interface IConfigurationFileLocator
{
void Verify(string? workingDirectory, string? projectRootDirectory);
string? GetConfigurationFile(string? directory);
string? GetConfigurationFile(string? directoryPath);
}
2 changes: 1 addition & 1 deletion src/GitVersion.Core/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ GitVersion.Configuration.IConfigurationBuilder
GitVersion.Configuration.IConfigurationBuilder.AddOverride(System.Collections.Generic.IReadOnlyDictionary<object!, object?>! value) -> void
GitVersion.Configuration.IConfigurationBuilder.Build() -> GitVersion.Configuration.IGitVersionConfiguration!
GitVersion.Configuration.IConfigurationFileLocator
GitVersion.Configuration.IConfigurationFileLocator.GetConfigurationFile(string? directory) -> string?
GitVersion.Configuration.IConfigurationFileLocator.GetConfigurationFile(string? directoryPath) -> string?
GitVersion.Configuration.IConfigurationFileLocator.Verify(string? workingDirectory, string? projectRootDirectory) -> void
GitVersion.Configuration.IConfigurationProvider
GitVersion.Configuration.IConfigurationProvider.Provide(System.Collections.Generic.IReadOnlyDictionary<object!, object?>? overrideConfiguration = null) -> GitVersion.Configuration.IGitVersionConfiguration!
Expand Down