Skip to content

Commit

Permalink
Introduce convenience methods into MockFileSystem
Browse files Browse the repository at this point in the history
- `AddEmptyFile()` added which is a convenience wrapper for `AddFile()`
  with an empty `MockFileData`.
- `AddFile()` overload added which consumes an `IFileInfo`.
- `AddDirectory()` overload added which consumes an `IDirectoryInfo`.
- `GetFile()` overload added which consumes an `IFileInfo`.

Unit test added for `AddEmptyFile()` but not the others, as the test
code for them would have very little value.

Fixes TestableIO/System.IO.Abstractions.Extensions#32
  • Loading branch information
rcdailey committed Feb 27, 2023
1 parent ee0a5b6 commit f86f663
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,53 @@ public void AddFile(string path, MockFileData mockFile)
}
}

/// <summary>
/// Add a new file that is empty.
/// </summary>
/// <param name="path">A string representing the path of the new file to add.</param>
public void AddEmptyFile(string path)
{
AddFile(path, new MockFileData(""));
}

/// <summary>
/// Add a new file that is empty.
/// </summary>
/// <param name="path">An <see cref="IFileInfo"/> representing the path of the new file to add.</param>
public void AddEmptyFile(IFileInfo path)
{
AddEmptyFile(path.FullName);
}

/// <summary>
/// Add a new, empty directory.
/// </summary>
/// <param name="path">An <see cref="IDirectoryInfo"/> representing the path of the new directory to add.</param>
public void AddDirectory(IDirectoryInfo path)
{
AddDirectory(path.FullName);
}

/// <summary>
/// Add a new file with its contents set to a specified <see cref="MockFileData"/>.
/// </summary>
/// <param name="path">An <see cref="IFileInfo"/> representing the path of the new file to add.</param>
/// <param name="data">The data to use for the contents of the new file.</param>
public void AddFile(IFileInfo path, MockFileData data)
{
AddFile(path.FullName, data);
}

/// <summary>
/// Gets a file.
/// </summary>
/// <param name="path">The path of the file to get.</param>
/// <returns>The file. <see langword="null"/> if the file does not exist.</returns>
public MockFileData GetFile(IFileInfo path)
{
return GetFile(path.FullName);
}

/// <inheritdoc />
public void AddDirectory(string path)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ public void MockFileSystem_AddFile_ShouldReplaceExistingFile()
Assert.That(fileSystem.GetFile(path).TextContents, Is.EqualTo(newContent));
}

[Test]
public void MockFileSystem_AddEmptyFile_ShouldBeEmpty()
{
var path = XFS.Path(@"c:\some\file.txt");
var fileSystem = new MockFileSystem();

fileSystem.AddEmptyFile(path);

Assert.That(fileSystem.GetFile(path).TextContents, Is.EqualTo(""));
}

[Test]
public void MockFileSystem_ByDefault_IsSerializable()
{
Expand Down

0 comments on commit f86f663

Please sign in to comment.