Skip to content

Commit 6a3fb4c

Browse files
Merge pull request #757 from jimmylewis/isUnderRoot
Fix bug in FileHelpers.IsUnderRootDirectory for separator
2 parents fb1dbe0 + bc49e4b commit 6a3fb4c

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/LibraryManager.Contracts/FileHelpers.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,9 @@ public static bool IsUnderRootDirectory(string filePath, string rootDirectory)
375375
string normalizedFilePath = NormalizePath(filePath);
376376
string normalizedRootDirectory = NormalizePath(rootDirectory);
377377

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

test/LibraryManager.Test/FileHelpersTest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ public class FileHelpersTest
1414
[DataRow("C:\\dir\\file1.js", "C:\\dir", true)]
1515
[DataRow("C:\\dir\\", "C:\\dir\\", false)]
1616
[DataRow("/abc/def/ghi", "\\abc\\def", true)]
17-
public void UnderRootDirectory(string path1, string path2, bool expectedResult)
17+
[DataRow("abc/def", "abc", true)]
18+
[DataRow("abcdef", "abc", false)]
19+
public void UnderRootDirectory(string file, string directory, bool expectedResult)
1820
{
19-
Assert.AreEqual(expectedResult, FileHelpers.IsUnderRootDirectory(path1, path2));
21+
Assert.AreEqual(expectedResult, FileHelpers.IsUnderRootDirectory(file, directory));
2022
}
2123
}
2224
}

0 commit comments

Comments
 (0)