Skip to content

Commit

Permalink
Combine rootDir with relative files (#94528)
Browse files Browse the repository at this point in the history
  • Loading branch information
jozkee authored Nov 28, 2023
1 parent 6d8dbc7 commit b190ea8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,24 @@ private InMemoryDirectoryInfo(string rootDir, IEnumerable<string>? files, bool n
else
{
var fileList = new List<string>(files.Count());
string normalizedRoot = Path.GetFullPath(rootDir.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar));

// normalize
foreach (string file in files)
{
fileList.Add(Path.GetFullPath(file.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)));
if (Path.IsPathRooted(file))
{
fileList.Add(Path.GetFullPath(file.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)));
}
else
{
fileList.Add(Path.Combine(normalizedRoot, file.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)));
}
}

_files = fileList;

FullName = Path.GetFullPath(rootDir.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar));
FullName = normalizedRoot;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,11 @@ public void StemCorrectWithDifferentWildCards()
public void StemCorrectWithDifferentWildCards_WithInMemory()
{
var matcher = new Matcher();
matcher.AddInclude("sub/*.cs");
matcher.AddInclude("**/*.cs");
matcher.AddInclude("src/project/sub/*.cs");
matcher.AddInclude("src/project/**/*.cs");

var files = GetFileList();
var directoryPath = "src/project";
var results = matcher.Match(directoryPath, files);
var results = matcher.Match("./", files);

var actual = results.Files.Select(match => match.Stem);
var expected = new string[]
Expand Down Expand Up @@ -425,11 +424,10 @@ public void MultipleSubDirsAfterFirstWildcardMatch_HasCorrectStem()
public void MultipleSubDirsAfterFirstWildcardMatch_HasCorrectStem_WithInMemory()
{
var matcher = new Matcher();
matcher.AddInclude("compiler/**/*.cs");
matcher.AddInclude("src/project/compiler/**/*.cs");

var files = GetFileList();
var directoryPath = "src/project";
var results = matcher.Match(directoryPath, files);
var results = matcher.Match("./", files);

var actual = results.Files.Select(match => match.Stem);
var expected = new string[]
Expand All @@ -450,12 +448,10 @@ public void MultipleSubDirsAfterFirstWildcardMatch_HasCorrectStem_WithInMemory()
[InlineData(@"root", @"**/*.0", @"test.0")]
public void PathIncludesAllSegmentsFromPattern_RootDirectory(string root, string includePattern, string expectedPath)
{
var fileToFind = @"root/test.0";

var matcher = new Matcher();
matcher.AddInclude(includePattern);

var results = matcher.Match(root, new[] { fileToFind });
var results = matcher.Match(root, new[] { expectedPath });
var actualPath = results.Files.Select(file => file.Path).SingleOrDefault();

Assert.Equal(expectedPath, actualPath);
Expand All @@ -464,7 +460,7 @@ public void PathIncludesAllSegmentsFromPattern_RootDirectory(string root, string
matcher = new Matcher();
matcher.AddInclude("./" + includePattern);

results = matcher.Match(root, new[] { fileToFind });
results = matcher.Match(root, new[] { expectedPath });
actualPath = results.Files.Select(file => file.Path).SingleOrDefault();

Assert.Equal(expectedPath, actualPath);
Expand All @@ -480,12 +476,10 @@ public void PathIncludesAllSegmentsFromPattern_RootDirectory(string root, string
[InlineData(@"root", @"**/*.1", @"dir1/test.1")]
public void PathIncludesAllSegmentsFromPattern_OneDirectoryDeep(string root, string includePattern, string expectedPath)
{
var fileToFind = @"root/dir1/test.1";

var matcher = new Matcher();
matcher.AddInclude(includePattern);

var results = matcher.Match(root, new[] { fileToFind });
var results = matcher.Match(root, new[] { expectedPath });
var actualPath = results.Files.Select(file => file.Path).SingleOrDefault();

Assert.Equal(expectedPath, actualPath);
Expand All @@ -494,7 +488,7 @@ public void PathIncludesAllSegmentsFromPattern_OneDirectoryDeep(string root, str
matcher = new Matcher();
matcher.AddInclude("./" + includePattern);

results = matcher.Match(root, new[] { fileToFind });
results = matcher.Match(root, new[] { expectedPath });
actualPath = results.Files.Select(file => file.Path).SingleOrDefault();

Assert.Equal(expectedPath, actualPath);
Expand All @@ -520,12 +514,10 @@ public void PathIncludesAllSegmentsFromPattern_OneDirectoryDeep(string root, str
[InlineData(@"root", @"**/*.2", @"dir1/dir2/test.2")]
public void PathIncludesAllSegmentsFromPattern_TwoDirectoriesDeep(string root, string includePattern, string expectedPath)
{
var fileToFind = @"root/dir1/dir2/test.2";

var matcher = new Matcher();
matcher.AddInclude(includePattern);

var results = matcher.Match(root, new[] { fileToFind });
var results = matcher.Match(root, new[] { expectedPath });
var actualPath = results.Files.Select(file => file.Path).SingleOrDefault();

Assert.Equal(expectedPath, actualPath);
Expand All @@ -534,7 +526,7 @@ public void PathIncludesAllSegmentsFromPattern_TwoDirectoriesDeep(string root, s
matcher = new Matcher();
matcher.AddInclude("./" + includePattern);

results = matcher.Match(root, new[] { fileToFind });
results = matcher.Match(root, new[] { expectedPath });
actualPath = results.Files.Select(file => file.Path).SingleOrDefault();

Assert.Equal(expectedPath, actualPath);
Expand All @@ -545,7 +537,7 @@ public void PathIncludesAllSegmentsFromPattern_TwoDirectoriesDeep(string root, s
[InlineData(@"root", @"**/*.0", @"test.0")]
public void StemIncludesAllSegmentsFromPatternStartingAtWildcard_RootDirectory(string root, string includePattern, string expectedStem)
{
var fileToFind = @"root/test.0";
string fileToFind = "test.0";

var matcher = new Matcher();
matcher.AddInclude(includePattern);
Expand All @@ -565,18 +557,16 @@ public void StemIncludesAllSegmentsFromPatternStartingAtWildcard_RootDirectory(s
Assert.Equal(expectedStem, actualStem);
}

[Theory] // rootDir, includePattern, expectedStem
[InlineData(@"root/dir1", @"*.1", @"test.1")]
[InlineData(@"root/dir1", @"**/*.1", @"test.1")]
[InlineData(@"root", @"dir1/*.1", @"test.1")]
[InlineData(@"root", @"dir1/**/*.1", @"test.1")]
[InlineData(@"root", @"**/dir1/*.1", @"dir1/test.1")]
[InlineData(@"root", @"**/dir1/**/*.1", @"dir1/test.1")]
[InlineData(@"root", @"**/*.1", @"dir1/test.1")]
public void StemIncludesAllSegmentsFromPatternStartingAtWildcard_OneDirectoryDeep(string root, string includePattern, string expectedStem)
[Theory] // rootDir, includePattern, fileToFind expectedStem
[InlineData(@"root/dir1", @"*.1", @"test.1", @"test.1")]
[InlineData(@"root/dir1", @"**/*.1", @"test.1", @"test.1")]
[InlineData(@"root", @"dir1/*.1", @"dir1/test.1", @"test.1")]
[InlineData(@"root", @"dir1/**/*.1", @"dir1/test.1", @"test.1")]
[InlineData(@"root", @"**/dir1/*.1", @"dir1/test.1", @"dir1/test.1")]
[InlineData(@"root", @"**/dir1/**/*.1", @"dir1/test.1", @"dir1/test.1")]
[InlineData(@"root", @"**/*.1", @"dir1/test.1", @"dir1/test.1")]
public void StemIncludesAllSegmentsFromPatternStartingAtWildcard_OneDirectoryDeep(string root, string includePattern, string fileToFind, string expectedStem)
{
var fileToFind = @"root/dir1/test.1";

var matcher = new Matcher();
matcher.AddInclude(includePattern);

Expand All @@ -595,28 +585,26 @@ public void StemIncludesAllSegmentsFromPatternStartingAtWildcard_OneDirectoryDee
Assert.Equal(expectedStem, actualStem);
}

[Theory] // rootDir, includePattern, expectedStem
[InlineData(@"root/dir1/dir2", @"*.2", @"test.2")]
[InlineData(@"root/dir1/dir2", @"**/*.2", @"test.2")]
[InlineData(@"root/dir1", @"dir2/*.2", @"test.2")]
[InlineData(@"root/dir1", @"dir2/**/*.2", @"test.2")]
[InlineData(@"root/dir1", @"**/dir2/*.2", @"dir2/test.2")]
[InlineData(@"root/dir1", @"**/dir2/**/*.2", @"dir2/test.2")]
[InlineData(@"root/dir1", @"**/*.2", @"dir2/test.2")]
[InlineData(@"root", @"dir1/dir2/*.2", @"test.2")]
[InlineData(@"root", @"dir1/dir2/**/*.2", @"test.2")]
[InlineData(@"root", @"**/dir1/dir2/**/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir1/**/dir2/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir1/**/dir2/**/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"dir1/**/*.2", @"dir2/test.2")]
[InlineData(@"root", @"**/dir1/**/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir2/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir2/**/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/*.2", @"dir1/dir2/test.2")]
public void StemIncludesAllSegmentsFromPatternStartingAtWildcard_TwoDirectoriesDeep(string root, string includePattern, string expectedStem)
[Theory] // rootDir, includePattern, fileToFind expectedStem
[InlineData(@"root/dir1/dir2", @"*.2", @"test.2", @"test.2")]
[InlineData(@"root/dir1/dir2", @"**/*.2", @"test.2", @"test.2")]
[InlineData(@"root/dir1", @"dir2/*.2", @"dir2/test.2", @"test.2")]
[InlineData(@"root/dir1", @"dir2/**/*.2", @"dir2/test.2", @"test.2")]
[InlineData(@"root/dir1", @"**/dir2/*.2", @"dir2/test.2", @"dir2/test.2")]
[InlineData(@"root/dir1", @"**/dir2/**/*.2", @"dir2/test.2", @"dir2/test.2")]
[InlineData(@"root/dir1", @"**/*.2", @"dir2/test.2", @"dir2/test.2")]
[InlineData(@"root", @"dir1/dir2/*.2", @"dir1/dir2/test.2", @"test.2")]
[InlineData(@"root", @"dir1/dir2/**/*.2", @"dir1/dir2/test.2", @"test.2")]
[InlineData(@"root", @"**/dir1/dir2/**/*.2", @"dir1/dir2/test.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir1/**/dir2/*.2", @"dir1/dir2/test.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir1/**/dir2/**/*.2", @"dir1/dir2/test.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"dir1/**/*.2", @"dir1/dir2/test.2", @"dir2/test.2")]
[InlineData(@"root", @"**/dir1/**/*.2", @"dir1/dir2/test.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir2/*.2", @"dir1/dir2/test.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir2/**/*.2", @"dir1/dir2/test.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/*.2", @"dir1/dir2/test.2", @"dir1/dir2/test.2")]
public void StemIncludesAllSegmentsFromPatternStartingAtWildcard_TwoDirectoriesDeep(string root, string includePattern, string fileToFind, string expectedStem)
{
var fileToFind = @"root/dir1/dir2/test.2";

var matcher = new Matcher();
matcher.AddInclude(includePattern);

Expand Down Expand Up @@ -844,7 +832,6 @@ public void VerifyAbsolutePaths_HasMatches()
}

[Fact] // https://github.com/dotnet/runtime/issues/36415
[ActiveIssue("https://github.com/dotnet/runtime/issues/50648")]
public void VerifyInMemoryDirectoryInfo_IsNotEmpty()
{
IEnumerable<string> files = new[] { @"pagefile.sys" };
Expand Down

0 comments on commit b190ea8

Please sign in to comment.