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

Globbing - Use GetFullPath for normalization on relative files #101083

Merged
merged 1 commit into from
Apr 16, 2024
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 @@ -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);
}
}
}
}
Loading