diff --git a/src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/InMemoryDirectoryInfo.cs b/src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/InMemoryDirectoryInfo.cs index 4d41468a173eb..8d6c9c3f33e4a 100644 --- a/src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/InMemoryDirectoryInfo.cs +++ b/src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/InMemoryDirectoryInfo.cs @@ -51,13 +51,14 @@ private InMemoryDirectoryInfo(string rootDir, IEnumerable? files, bool n // normalize foreach (string file in files) { + string fileWithNormalSeparators = file.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); if (Path.IsPathRooted(file)) { - fileList.Add(Path.GetFullPath(file.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar))); + fileList.Add(Path.GetFullPath(fileWithNormalSeparators)); } else { - fileList.Add(Path.Combine(normalizedRoot, file.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar))); + fileList.Add(Path.GetFullPath(Path.Combine(normalizedRoot, fileWithNormalSeparators))); } } diff --git a/src/libraries/Microsoft.Extensions.FileSystemGlobbing/tests/FunctionalTests.cs b/src/libraries/Microsoft.Extensions.FileSystemGlobbing/tests/FunctionalTests.cs index 0d9ca6ce1e20a..e8bcdacdb9558 100644 --- a/src/libraries/Microsoft.Extensions.FileSystemGlobbing/tests/FunctionalTests.cs +++ b/src/libraries/Microsoft.Extensions.FileSystemGlobbing/tests/FunctionalTests.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using Microsoft.DotNet.XUnitExtensions; using Microsoft.Extensions.FileSystemGlobbing.Abstractions; using Microsoft.Extensions.FileSystemGlobbing.Tests.TestUtility; using Xunit; @@ -851,5 +852,54 @@ public void VerifyInMemoryDirectoryInfo_IsNotEmpty() Assert.Equal(1, fileSystemInfos.Count()); } + + [Theory] + [InlineData("./sdk/9.0.100-preview.4.24207.1/.version")] + [InlineData("././sdk/9.0.100-preview.4.24207.1/.version")] + public void VerifyFiles_RedundantSegment_HasMatches(string file) + { + foreach (string pattern in new[] { "**/*", "./", file }) + { + var matcher = new Matcher(); + matcher.AddInclude(pattern); + Assert.True(matcher.Match(file).HasMatches); + Assert.True(matcher.Match([file]).HasMatches); + Assert.True(matcher.Match("X:/foo", file).HasMatches); + Assert.True(matcher.Match("X:/foo", [file]).HasMatches); + } + } + + [ConditionalFact] + public void VerifyFiles_ParentRedundantSegment_HasMatches() + { + string file = "sdk/9.0.100-preview.4.24207.1/.version"; + foreach (string pattern in new[] { "**/*", "./", file }) + { + var matcher = new Matcher(); + matcher.AddInclude(pattern); + Assert.True(matcher.Match("X:/foo", $"../foo/{file}").HasMatches); + Assert.True(matcher.Match("X:/foo", [$"../foo/{file}"]).HasMatches); + } + } + + [ConditionalFact] + public void VerifyFiles_ParentRedundantSegment_CurrentDirectory_HasMatches() + { + string cwd = Environment.CurrentDirectory; + string cwdFolderName = new DirectoryInfo(cwd).Name; + if (cwd == cwdFolderName) // cwd is root, we can't do ../C:/ + { + throw new SkipTestException($"CurrentDirectory {cwd} is the root directory."); + } + + string file = "sdk/9.0.100-preview.4.24207.1/.version"; + foreach (string pattern in new[] { "**/*", "./", file }) + { + var matcher = new Matcher(); + matcher.AddInclude(pattern); + Assert.True(matcher.Match($"../{cwdFolderName}/{file}").HasMatches); + Assert.True(matcher.Match([$"../{cwdFolderName}/{file}"]).HasMatches); + } + } } }