Skip to content

Commit f5644ac

Browse files
jozkeematouskozak
authored andcommitted
Use GetFullPath for normalization on relative files (dotnet#101083)
1 parent 202f300 commit f5644ac

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/InMemoryDirectoryInfo.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ private InMemoryDirectoryInfo(string rootDir, IEnumerable<string>? files, bool n
5151
// normalize
5252
foreach (string file in files)
5353
{
54+
string fileWithNormalSeparators = file.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
5455
if (Path.IsPathRooted(file))
5556
{
56-
fileList.Add(Path.GetFullPath(file.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)));
57+
fileList.Add(Path.GetFullPath(fileWithNormalSeparators));
5758
}
5859
else
5960
{
60-
fileList.Add(Path.Combine(normalizedRoot, file.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)));
61+
fileList.Add(Path.GetFullPath(Path.Combine(normalizedRoot, fileWithNormalSeparators)));
6162
}
6263
}
6364

src/libraries/Microsoft.Extensions.FileSystemGlobbing/tests/FunctionalTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Linq;
8+
using Microsoft.DotNet.XUnitExtensions;
89
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;
910
using Microsoft.Extensions.FileSystemGlobbing.Tests.TestUtility;
1011
using Xunit;
@@ -851,5 +852,54 @@ public void VerifyInMemoryDirectoryInfo_IsNotEmpty()
851852

852853
Assert.Equal(1, fileSystemInfos.Count());
853854
}
855+
856+
[Theory]
857+
[InlineData("./sdk/9.0.100-preview.4.24207.1/.version")]
858+
[InlineData("././sdk/9.0.100-preview.4.24207.1/.version")]
859+
public void VerifyFiles_RedundantSegment_HasMatches(string file)
860+
{
861+
foreach (string pattern in new[] { "**/*", "./", file })
862+
{
863+
var matcher = new Matcher();
864+
matcher.AddInclude(pattern);
865+
Assert.True(matcher.Match(file).HasMatches);
866+
Assert.True(matcher.Match([file]).HasMatches);
867+
Assert.True(matcher.Match("X:/foo", file).HasMatches);
868+
Assert.True(matcher.Match("X:/foo", [file]).HasMatches);
869+
}
870+
}
871+
872+
[ConditionalFact]
873+
public void VerifyFiles_ParentRedundantSegment_HasMatches()
874+
{
875+
string file = "sdk/9.0.100-preview.4.24207.1/.version";
876+
foreach (string pattern in new[] { "**/*", "./", file })
877+
{
878+
var matcher = new Matcher();
879+
matcher.AddInclude(pattern);
880+
Assert.True(matcher.Match("X:/foo", $"../foo/{file}").HasMatches);
881+
Assert.True(matcher.Match("X:/foo", [$"../foo/{file}"]).HasMatches);
882+
}
883+
}
884+
885+
[ConditionalFact]
886+
public void VerifyFiles_ParentRedundantSegment_CurrentDirectory_HasMatches()
887+
{
888+
string cwd = Environment.CurrentDirectory;
889+
string cwdFolderName = new DirectoryInfo(cwd).Name;
890+
if (cwd == cwdFolderName) // cwd is root, we can't do ../C:/
891+
{
892+
throw new SkipTestException($"CurrentDirectory {cwd} is the root directory.");
893+
}
894+
895+
string file = "sdk/9.0.100-preview.4.24207.1/.version";
896+
foreach (string pattern in new[] { "**/*", "./", file })
897+
{
898+
var matcher = new Matcher();
899+
matcher.AddInclude(pattern);
900+
Assert.True(matcher.Match($"../{cwdFolderName}/{file}").HasMatches);
901+
Assert.True(matcher.Match([$"../{cwdFolderName}/{file}"]).HasMatches);
902+
}
903+
}
854904
}
855905
}

0 commit comments

Comments
 (0)