Skip to content

Commit

Permalink
Merge pull request #757 from jimmylewis/isUnderRoot
Browse files Browse the repository at this point in the history
Fix bug in FileHelpers.IsUnderRootDirectory for separator
  • Loading branch information
phil-allen-msft authored May 28, 2024
2 parents fb1dbe0 + bc49e4b commit 6a3fb4c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/LibraryManager.Contracts/FileHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,9 @@ public static bool IsUnderRootDirectory(string filePath, string rootDirectory)
string normalizedFilePath = NormalizePath(filePath);
string normalizedRootDirectory = NormalizePath(rootDirectory);

return normalizedFilePath.Length > normalizedRootDirectory.Length
return normalizedFilePath.Length > normalizedRootDirectory.Length + 1
// normalization has edge cases where either / or \ may be retained
&& normalizedFilePath[normalizedRootDirectory.Length] is '/' or '\\'
&& normalizedFilePath.StartsWith(normalizedRootDirectory, StringComparison.OrdinalIgnoreCase);
}

Expand Down
6 changes: 4 additions & 2 deletions test/LibraryManager.Test/FileHelpersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ public class FileHelpersTest
[DataRow("C:\\dir\\file1.js", "C:\\dir", true)]
[DataRow("C:\\dir\\", "C:\\dir\\", false)]
[DataRow("/abc/def/ghi", "\\abc\\def", true)]
public void UnderRootDirectory(string path1, string path2, bool expectedResult)
[DataRow("abc/def", "abc", true)]
[DataRow("abcdef", "abc", false)]
public void UnderRootDirectory(string file, string directory, bool expectedResult)
{
Assert.AreEqual(expectedResult, FileHelpers.IsUnderRootDirectory(path1, path2));
Assert.AreEqual(expectedResult, FileHelpers.IsUnderRootDirectory(file, directory));
}
}
}

0 comments on commit 6a3fb4c

Please sign in to comment.