Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/System.IO.Abstractions.TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down