Skip to content

Commit

Permalink
Use GetFullPath for normalization on relative files (#101083)
Browse files Browse the repository at this point in the history
  • Loading branch information
jozkee authored Apr 16, 2024
1 parent ccc74f3 commit 9d88225
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ private InMemoryDirectoryInfo(string rootDir, IEnumerable<string>? 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)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
}

0 comments on commit 9d88225

Please sign in to comment.