Skip to content

Commit 49747fa

Browse files
authored
fix: Refresh FileInfo & DirectoryInfo after adding to MockFileSystem (#984)
Modify the `IFileInfo` and `IDirectoryInfo` overloads of `AddFile()`, `AddEmptyFile()`, or `AddDirectory()` in `MockFileSystem`, to call `Refresh()` on those objects after the paths they refer to are created.
1 parent ccdc24d commit 49747fa

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ public void AddEmptyFile(string path)
225225
public void AddEmptyFile(IFileInfo path)
226226
{
227227
AddEmptyFile(path.FullName);
228+
path.Refresh();
228229
}
229230

230231
/// <summary>
@@ -234,6 +235,7 @@ public void AddEmptyFile(IFileInfo path)
234235
public void AddDirectory(IDirectoryInfo path)
235236
{
236237
AddDirectory(path.FullName);
238+
path.Refresh();
237239
}
238240

239241
/// <summary>
@@ -244,6 +246,7 @@ public void AddDirectory(IDirectoryInfo path)
244246
public void AddFile(IFileInfo path, MockFileData data)
245247
{
246248
AddFile(path.FullName, data);
249+
path.Refresh();
247250
}
248251

249252
/// <summary>

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,39 @@ public void MockFileSystem_AddEmptyFile_ShouldBeEmpty()
130130
Assert.That(fileSystem.GetFile(path).TextContents, Is.EqualTo(""));
131131
}
132132

133+
[Test]
134+
public void MockFileSystem_AddEmptyFile_ShouldExist()
135+
{
136+
var fileSystem = new MockFileSystem();
137+
var path = fileSystem.FileInfo.New(XFS.Path(@"c:\some\file.txt"));
138+
139+
fileSystem.AddEmptyFile(path);
140+
141+
Assert.That(path.Exists, Is.True);
142+
}
143+
144+
[Test]
145+
public void MockFileSystem_AddFile_ShouldExist()
146+
{
147+
var fileSystem = new MockFileSystem();
148+
var path = fileSystem.FileInfo.New(XFS.Path(@"c:\some\file.txt"));
149+
150+
fileSystem.AddFile(path, new MockFileData("stuff"));
151+
152+
Assert.That(path.Exists, Is.True);
153+
}
154+
155+
[Test]
156+
public void MockFileSystem_AddDirectory_ShouldExist()
157+
{
158+
var fileSystem = new MockFileSystem();
159+
var path = fileSystem.DirectoryInfo.New(XFS.Path(@"c:\thedir"));
160+
161+
fileSystem.AddDirectory(path);
162+
163+
Assert.That(path.Exists, Is.True);
164+
}
165+
133166
[Test]
134167
public void MockFileSystem_ByDefault_IsSerializable()
135168
{

0 commit comments

Comments
 (0)