diff --git a/src/GitVersionCore.Tests/NamedConfigFileLocatorTests.cs b/src/GitVersionCore.Tests/NamedConfigFileLocatorTests.cs index 4331e9c062..df2cd36c3e 100644 --- a/src/GitVersionCore.Tests/NamedConfigFileLocatorTests.cs +++ b/src/GitVersionCore.Tests/NamedConfigFileLocatorTests.cs @@ -51,6 +51,37 @@ public void ThrowsExceptionOnAmbiguousConfigFileLocation() exception.Message.ShouldBe(expectedMessage); } + [Test] + public void DoNotThrowWhenWorkingAndRepoPathsAreSame() + { + workingPath = DefaultRepoPath; + SetupConfigFileContent(string.Empty, path: workingPath); + + Should.NotThrow(() => { configFileLocator.Verify(workingPath, repoPath); }); + } + + [Test] + [Platform(Exclude = "Linux,Unix")] + public void DoNotThrowWhenWorkingAndRepoPathsAreSame_WithDifferentCasing() + { + workingPath = DefaultRepoPath.ToLower(); + SetupConfigFileContent(string.Empty, path: workingPath); + + Should.NotThrow(() => { configFileLocator.Verify(workingPath, repoPath); }); + } + + [Test] + public void DoNotThrowWhenConfigFileIsInSubDirectoryOfRepoPath() + { + workingPath = DefaultRepoPath; + + options = Options.Create(new Arguments { ConfigFile = "./src/my-config.yaml" }); + configFileLocator = new NamedConfigFileLocator(fileSystem, log, options); + SetupConfigFileContent(string.Empty, path: workingPath); + + Should.NotThrow(() => { configFileLocator.Verify(workingPath, repoPath); }); + } + [Test] public void NoWarnOnCustomYmlFile() { diff --git a/src/GitVersionCore.Tests/TestFileSystem.cs b/src/GitVersionCore.Tests/TestFileSystem.cs index 2dff623698..95a0fac562 100644 --- a/src/GitVersionCore.Tests/TestFileSystem.cs +++ b/src/GitVersionCore.Tests/TestFileSystem.cs @@ -9,42 +9,49 @@ namespace GitVersionCore.Tests { public class TestFileSystem : IFileSystem { - private readonly Dictionary fileSystem = new Dictionary(); + private static IEqualityComparer fileSystemCasingComparer = System.Environment.OSVersion.Platform == PlatformID.Unix ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase; + private readonly Dictionary fileSystem = new Dictionary(fileSystemCasingComparer); public void Copy(string @from, string to, bool overwrite) { - if (fileSystem.ContainsKey(to)) + var fromPath = Path.GetFullPath(@from); + var toPath = Path.GetFullPath(to); + if (fileSystem.ContainsKey(toPath)) { if (overwrite) - fileSystem.Remove(to); + fileSystem.Remove(toPath); else throw new IOException("File already exists"); } - if (!fileSystem.TryGetValue(from, out var source)) - throw new FileNotFoundException($"The source file '{@from}' was not found", from); + if (!fileSystem.TryGetValue(fromPath, out var source)) + throw new FileNotFoundException($"The source file '{fromPath}' was not found", from); - fileSystem.Add(to, source); + fileSystem.Add(toPath, source); } public void Move(string @from, string to) { + var fromPath = Path.GetFullPath(@from); Copy(from, to, false); - fileSystem.Remove(from); + fileSystem.Remove(fromPath); } public bool Exists(string file) { - return fileSystem.ContainsKey(file); + var path = Path.GetFullPath(file); + return fileSystem.ContainsKey(path); } public void Delete(string path) { - fileSystem.Remove(path); + var fullPath = Path.GetFullPath(path); + fileSystem.Remove(fullPath); } - public string ReadAllText(string path) + public string ReadAllText(string file) { + var path = Path.GetFullPath(file); if (!fileSystem.TryGetValue(path, out var content)) throw new FileNotFoundException($"The file '{path}' was not found", path); @@ -54,15 +61,17 @@ public string ReadAllText(string path) public void WriteAllText(string file, string fileContents) { - var encoding = fileSystem.ContainsKey(file) - ? EncodingHelper.DetectEncoding(fileSystem[file]) ?? Encoding.UTF8 + var path = Path.GetFullPath(file); + var encoding = fileSystem.ContainsKey(path) + ? EncodingHelper.DetectEncoding(fileSystem[path]) ?? Encoding.UTF8 : Encoding.UTF8; - WriteAllText(file, fileContents, encoding); + WriteAllText(path, fileContents, encoding); } public void WriteAllText(string file, string fileContents, Encoding encoding) { - fileSystem[file] = encoding.GetBytes(fileContents); + var path = Path.GetFullPath(file); + fileSystem[path] = encoding.GetBytes(fileContents); } public IEnumerable DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption) @@ -75,8 +84,9 @@ public Stream OpenWrite(string path) return new TestStream(path, this); } - public Stream OpenRead(string path) + public Stream OpenRead(string file) { + var path = Path.GetFullPath(file); if (fileSystem.ContainsKey(path)) { var content = fileSystem[path]; @@ -86,8 +96,9 @@ public Stream OpenRead(string path) throw new FileNotFoundException("File not found.", path); } - public void CreateDirectory(string path) + public void CreateDirectory(string directory) { + var path = Path.GetFullPath(directory); if (fileSystem.ContainsKey(path)) { fileSystem[path] = new byte[0]; @@ -98,8 +109,9 @@ public void CreateDirectory(string path) } } - public bool DirectoryExists(string path) + public bool DirectoryExists(string directory) { + var path = Path.GetFullPath(directory); return fileSystem.ContainsKey(path); } diff --git a/src/GitVersionCore/Configuration/NamedConfigFileLocator.cs b/src/GitVersionCore/Configuration/NamedConfigFileLocator.cs index 97faff0d44..09c994c502 100644 --- a/src/GitVersionCore/Configuration/NamedConfigFileLocator.cs +++ b/src/GitVersionCore/Configuration/NamedConfigFileLocator.cs @@ -36,6 +36,10 @@ private void WarnAboutAmbiguousConfigFileSelection(string workingDirectory, stri var workingConfigFile = GetConfigFilePath(workingDirectory); var projectRootConfigFile = GetConfigFilePath(projectRootDirectory); + var fileSystemCasingComparer = System.Environment.OSVersion.Platform == PlatformID.Unix ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; + if (Path.GetFullPath(workingConfigFile).Equals(Path.GetFullPath(projectRootConfigFile), fileSystemCasingComparer)) + return; + var hasConfigInWorkingDirectory = FileSystem.Exists(workingConfigFile); var hasConfigInProjectRootDirectory = FileSystem.Exists(projectRootConfigFile); if (hasConfigInProjectRootDirectory && hasConfigInWorkingDirectory)