Skip to content

Commit 11ec3c8

Browse files
mannetathamoddie
authored andcommitted
fixed Directory.Exists
1 parent c7339b2 commit 11ec3c8

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

TestHelpers.Tests/MockDirectoryTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,25 @@ public void MockDirectory_Exists_ShouldReturnTrueForFolderContainingFileAddedToM
377377
Assert.IsTrue(result);
378378
}
379379

380+
[TestCase(@"\\s")]
381+
[TestCase(@"<")]
382+
[TestCase("\t")]
383+
public void MockDirectory_Exists_ShouldReturnFalseForIllegalPath(string path)
384+
{
385+
// Arrange
386+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
387+
{
388+
{ @"c:\foo\bar.txt", new MockFileData("Demo text content") },
389+
{ @"c:\baz.txt", new MockFileData("Demo text content") }
390+
});
391+
392+
// Act
393+
var result = fileSystem.Directory.Exists(path);
394+
395+
// Assert
396+
Assert.IsFalse(result);
397+
}
398+
380399
[Test]
381400
public void MockDirectory_CreateDirectory_ShouldCreateFolderInMemoryFileSystem()
382401
{

TestingHelpers/MockDirectory.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,17 @@ public override void Delete(string path, bool recursive)
8585

8686
public override bool Exists(string path)
8787
{
88-
path = EnsurePathEndsWithDirectorySeparator(path);
88+
try
89+
{
90+
path = EnsurePathEndsWithDirectorySeparator(path);
8991

90-
path = mockFileDataAccessor.Path.GetFullPath(path);
91-
return mockFileDataAccessor.AllDirectories.Any(p => p.Equals(path, StringComparison.OrdinalIgnoreCase));
92+
path = mockFileDataAccessor.Path.GetFullPath(path);
93+
return mockFileDataAccessor.AllDirectories.Any(p => p.Equals(path, StringComparison.OrdinalIgnoreCase));
94+
}
95+
catch (Exception)
96+
{
97+
return false;
98+
}
9299
}
93100

94101
public override DirectorySecurity GetAccessControl(string path)

0 commit comments

Comments
 (0)