Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make MockFile.Exists handle empty string #811

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 18 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,24 @@ 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;
}

fgreinacher marked this conversation as resolved.
Show resolved Hide resolved
try
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, nameof(path));

var file = mockFileDataAccessor.GetFile(path);
return file != null && !file.IsDirectory;
}
catch (ArgumentException) { }
fgreinacher marked this conversation as resolved.
Show resolved Hide resolved
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