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
28 changes: 26 additions & 2 deletions System.IO.Abstractions.TestingHelpers.Tests/MockFileDeleteTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace System.IO.Abstractions.TestingHelpers.Tests
{
using NUnit.Framework;
using System.Collections.Generic;
using NUnit.Framework;

using XFS = MockUnixSupport;

Expand All @@ -10,7 +11,7 @@ public class MockFileDeleteTests
public void MockFile_Delete_ShouldDeleteFile()
{
var fileSystem = new MockFileSystem();
var path = XFS.Path("C:\\test");
var path = XFS.Path("C:\\some_folder\\test");
var directory = fileSystem.Path.GetDirectoryName(path);
fileSystem.AddFile(path, new MockFileData("Bla"));

Expand All @@ -35,5 +36,28 @@ public void MockFile_Delete_ShouldThrowArgumentExceptionIfPathContainsOnlyWhites
// Assert
Assert.Throws<ArgumentException>(action);
}

[Test]
public void MockFile_Delete_ShouldThrowDirectoryNotFoundExceptionIfParentFolderAbsent()
{
var fileSystem = new MockFileSystem();
var path = XFS.Path("C:\\test\\somefile.txt");

Assert.Throws<DirectoryNotFoundException>(() => fileSystem.File.Delete(path));
}

[Test]
public void MockFile_Delete_ShouldSilentlyReturnIfNonExistingFileInExistingFolder()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()
{
{ XFS.Path("C:\\temp\\exist.txt"), new MockFileData("foobar") },
});

string filePath = XFS.Path("C:\\temp\\somefile.txt");

// Delete() returns void, so there is nothing to check here beside absense of an exception
Assert.DoesNotThrow(() => fileSystem.File.Delete(filePath));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public void MockFileStream_Flush_WritesByteToFile()
[Test]
public void MockFileStream_Dispose_ShouldNotResurrectFile()
{
// path in this test case is a subject to Directory.GetParent(path) Linux issue
// https://github.com/System-IO-Abstractions/System.IO.Abstractions/issues/395
var fileSystem = new MockFileSystem();
var path = XFS.Path("C:\\test");
var path = XFS.Path("C:\\some_folder\\test");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of potential bug. Consider this code:

            var fileSystem = new MockFileSystem();
            var path = XFS.Path("C:\\test.txt");
            DirectoryInfoBase dir = fileSystem.Directory.GetParent(path);

It works ok on Windows but throws NullReferenceException on Linux. And after my changes GetParent() method is called inside File.Delete(), so this test started to fail. Adding a new intermediate folder "some_folder" is the fastest way to make the test work again.

I can create a new issue with this if you think it is a real defect.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK, thanks. Could you please create an issue for this problem and add a comment to the changed test case that refers to the issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
New issue is #395

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

var directory = fileSystem.Path.GetDirectoryName(path);
fileSystem.AddFile(path, new MockFileData("Bla"));
var stream = fileSystem.File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Delete);
Expand Down
11 changes: 8 additions & 3 deletions System.IO.Abstractions.TestingHelpers.Tests/MockFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,14 @@ public void MockFile_Delete_Should_RemoveFiles()

[Test]
public void MockFile_Delete_No_File_Does_Nothing()
{
string filePath = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem();
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()
{
{ XFS.Path(@"c:\something\exist.txt"), new MockFileData("Demo text content") },
});

string filePath = XFS.Path(@"c:\something\not_exist.txt");

fileSystem.File.Delete(filePath);
}

Expand Down
11 changes: 10 additions & 1 deletion System.IO.Abstractions.TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ public override void Delete(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

// We mimic exact behavior of the standard File.Delete() method
// which throws exception only if the folder does not exist,
// but silently returns if deleting a non-existing file in an existing folder.
VerifyDirectoryExists(path);

mockFileDataAccessor.RemoveFile(path);
}

Expand Down Expand Up @@ -991,7 +996,11 @@ private void VerifyDirectoryExists(string path)
DirectoryInfoBase dir = mockFileDataAccessor.Directory.GetParent(path);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), dir));
throw new DirectoryNotFoundException(
string.Format(
CultureInfo.InvariantCulture,
StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"),
dir));
}
}
}
Expand Down