diff --git a/src/System.IO.Abstractions.TestingHelpers/MockFile.cs b/src/System.IO.Abstractions.TestingHelpers/MockFile.cs index a6120d13e..78155cdd2 100644 --- a/src/System.IO.Abstractions.TestingHelpers/MockFile.cs +++ b/src/System.IO.Abstractions.TestingHelpers/MockFile.cs @@ -203,8 +203,25 @@ public override bool Exists(string path) return false; } - var file = mockFileDataAccessor.GetFile(path); - return file != null && !file.IsDirectory; + if (path.Trim() == string.Empty) + { + return false; + } + + //Not handling exceptions here so that mock behaviour is as similar as possible to System.IO.File.Exists (See #810) + try + { + mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, nameof(path)); + + var file = mockFileDataAccessor.GetFile(path); + return file != null && !file.IsDirectory; + } + catch (ArgumentException) { } + catch (NotSupportedException) { } + catch (IOException) { } + catch (UnauthorizedAccessException) { } + + return false; } /// diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileExistsTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileExistsTests.cs index 3a4b37f1f..a622e7e2a 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileExistsTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileExistsTests.cs @@ -88,6 +88,32 @@ public void MockFile_Exists_ShouldReturnFalseForNullPath() Assert.IsFalse(result); } + [Test] + public void MockFile_Exists_ShouldReturnFalseForEmptyStringPath() + { + // Arrange + var fileSystem = new MockFileSystem(); + + // Act + var result = fileSystem.File.Exists(string.Empty); + + // Assert + Assert.IsFalse(result); + } + + [Test] + public void MockFile_Exists_ShouldReturnFalseForInvalidCharactersInPath() + { + // Arrange + var fileSystem = new MockFileSystem(); + + // Act + var result = fileSystem.File.Exists(@"C:""*/:<>?|abc"); + + // Assert + Assert.IsFalse(result); + } + [Test] public void MockFile_Exists_ShouldReturnFalseForDirectories() {