Skip to content

Commit ad465c3

Browse files
committed
fix(475): Fixed an issue with Mock file system directory Get Files when we use relative path.
1 parent bfa61db commit ad465c3

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,20 @@ public void MockDirectory_GetFiles_Returns_Files()
788788
Assert.AreEqual(testPath, entries.First());
789789
}
790790

791+
[Test]
792+
public void MockDirectory_GetFiles_Returns_Files_WithRelativePath()
793+
{
794+
// arrange
795+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
796+
797+
const string dir = @"C:\foo";
798+
fileSystem.Directory.SetCurrentDirectory(dir);
799+
fileSystem.AddFile($@"C:\test.txt", new MockFileData("Some ASCII text."));
800+
801+
Assert.AreEqual(fileSystem.Directory.GetFiles(@"C:\").Length, 1); // Assert with absolute path
802+
Assert.AreEqual(fileSystem.Directory.GetFiles(@"..\").Length, 1); // Assert with relative path
803+
}
804+
791805
[Test]
792806
public void MockDirectory_GetFiles_ShouldThrowAnArgumentNullException_IfSearchPatternIsNull()
793807
{

System.IO.Abstractions.TestingHelpers/MockDirectory.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,13 @@ private string[] GetFilesInternal(
212212
CheckSearchPattern(searchPattern);
213213
path = path.TrimSlashes();
214214
path = path.NormalizeSlashes();
215+
path = mockFileDataAccessor.Path.GetFullPath(path);
215216

216217
if (!Exists(path))
217218
{
218219
throw CommonExceptions.CouldNotFindPartOfPath(path);
219220
}
220221

221-
path = EnsureAbsolutePath(path);
222-
223222
if (!path.EndsWith(Path.DirectorySeparatorChar.ToString()))
224223
{
225224
path += Path.DirectorySeparatorChar;

0 commit comments

Comments
 (0)