Skip to content

Commit 04e117c

Browse files
feat: introduce convenience methods into MockFileSystem (#951)
- `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 Co-authored-by: Florian Greinacher <florian@greinacher.de>
1 parent f1f37cd commit 04e117c

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,53 @@ public void AddFile(string path, MockFileData mockFile)
209209
}
210210
}
211211

212+
/// <summary>
213+
/// Add a new file that is empty.
214+
/// </summary>
215+
/// <param name="path">A string representing the path of the new file to add.</param>
216+
public void AddEmptyFile(string path)
217+
{
218+
AddFile(path, new MockFileData(""));
219+
}
220+
221+
/// <summary>
222+
/// Add a new file that is empty.
223+
/// </summary>
224+
/// <param name="path">An <see cref="IFileInfo"/> representing the path of the new file to add.</param>
225+
public void AddEmptyFile(IFileInfo path)
226+
{
227+
AddEmptyFile(path.FullName);
228+
}
229+
230+
/// <summary>
231+
/// Add a new, empty directory.
232+
/// </summary>
233+
/// <param name="path">An <see cref="IDirectoryInfo"/> representing the path of the new directory to add.</param>
234+
public void AddDirectory(IDirectoryInfo path)
235+
{
236+
AddDirectory(path.FullName);
237+
}
238+
239+
/// <summary>
240+
/// Add a new file with its contents set to a specified <see cref="MockFileData"/>.
241+
/// </summary>
242+
/// <param name="path">An <see cref="IFileInfo"/> representing the path of the new file to add.</param>
243+
/// <param name="data">The data to use for the contents of the new file.</param>
244+
public void AddFile(IFileInfo path, MockFileData data)
245+
{
246+
AddFile(path.FullName, data);
247+
}
248+
249+
/// <summary>
250+
/// Gets a file.
251+
/// </summary>
252+
/// <param name="path">The path of the file to get.</param>
253+
/// <returns>The file. <see langword="null"/> if the file does not exist.</returns>
254+
public MockFileData GetFile(IFileInfo path)
255+
{
256+
return GetFile(path.FullName);
257+
}
258+
212259
/// <inheritdoc />
213260
public void AddDirectory(string path)
214261
{

tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ public void MockFileSystem_AddFile_ShouldReplaceExistingFile()
119119
Assert.That(fileSystem.GetFile(path).TextContents, Is.EqualTo(newContent));
120120
}
121121

122+
[Test]
123+
public void MockFileSystem_AddEmptyFile_ShouldBeEmpty()
124+
{
125+
var path = XFS.Path(@"c:\some\file.txt");
126+
var fileSystem = new MockFileSystem();
127+
128+
fileSystem.AddEmptyFile(path);
129+
130+
Assert.That(fileSystem.GetFile(path).TextContents, Is.EqualTo(""));
131+
}
132+
122133
[Test]
123134
public void MockFileSystem_ByDefault_IsSerializable()
124135
{

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "19.1",
3+
"version": "19.2",
44
"assemblyVersion": {
55
"precision": "major"
66
},

0 commit comments

Comments
 (0)