Skip to content

Commit b2cca2c

Browse files
committed
fix: MockFileInfo.Exists no longer returns stale cached data
In 5216e0d ("fix: make Mock{File,Directory}Info cache file attributes (TestableIO#791)", 2022-01-09) a cache of the MockFileData was introduced, to mimic the behaviour that a FileInfo object will not read the data from disk but instead also remembers its internal state, which can be updated by calling its `Refresh` method. This internal (or cached state) is invalidated when calling the methods `Create`, `Delete` and `MoveTo` methods, as can be seen by the `Invalidate` calls in https://github.com/dotnet/runtime/blob/8766a1cf1ed6c3b69cef50154139699b72fb52c5/src/libraries/System.Private.CoreLib/src/System/IO/FileInfo.cs In the PR TestableIO#791 it was overlooked to also invalidate the cached data in these methods. This commit fixes it by invalidating the cached data only for `Create`, `CreateText` and `Delete`. The two `MoveTo` variants are not changed as I could not manage to find any behaviour which is broken without that invalidation. Most likely reason is that the MockFileData is updated to represent the destination file ofter the move, so invalidating is not needed. Fixes TestableIO#822
1 parent 74ef961 commit b2cca2c

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path) : base(mo
2828
/// <inheritdoc />
2929
public override void Delete()
3030
{
31+
refreshOnNextRead = true;
3132
mockFile.Delete(path);
3233
}
3334

@@ -221,13 +222,17 @@ public override IFileInfo CopyTo(string destFileName, bool overwrite)
221222
/// <inheritdoc />
222223
public override Stream Create()
223224
{
224-
return mockFile.Create(FullName);
225+
var result = mockFile.Create(FullName);
226+
refreshOnNextRead = true;
227+
return result;
225228
}
226229

227230
/// <inheritdoc />
228231
public override StreamWriter CreateText()
229232
{
230-
return mockFile.CreateText(FullName);
233+
var result = mockFile.CreateText(FullName);
234+
refreshOnNextRead = true;
235+
return result;
231236
}
232237

233238
/// <inheritdoc />

tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,49 @@ public void MockFileInfo_Exists_ShouldUpdateCachedDataOnRefresh()
750750
Assert.IsTrue(fileInfo.Exists);
751751
}
752752

753+
[Test]
754+
public void MockFileInfo_Create_ShouldUpdateCachedDataAndReturnTrueForExists()
755+
{
756+
IFileSystem fileSystem = new MockFileSystem();
757+
var path = XFS.Path(@"c:\temp\file1.txt");
758+
IFileInfo fileInfo = fileSystem.FileInfo.FromFileName(path);
759+
760+
// Act
761+
fileInfo.Create().Dispose();
762+
763+
// Assert
764+
var result = fileInfo.Exists;
765+
Assert.IsTrue(result);
766+
}
767+
768+
[Test]
769+
public void MockFileInfo_CreateText_ShouldUpdateCachedDataAndReturnTrueForExists()
770+
{
771+
IFileSystem fileSystem = new MockFileSystem();
772+
var path = XFS.Path(@"c:\temp\file1.txt");
773+
IFileInfo fileInfo = fileSystem.FileInfo.FromFileName(path);
774+
775+
// Act
776+
fileInfo.CreateText().Dispose();
777+
778+
// Assert
779+
Assert.IsTrue(fileInfo.Exists);
780+
}
781+
782+
[Test]
783+
public void MockFileInfo_Delete_ShouldUpdateCachedDataAndReturnFalseForExists()
784+
{
785+
var fileSystem = new MockFileSystem();
786+
var path = XFS.Path(@"c:\temp\file1.txt");
787+
IFileInfo fileInfo = fileSystem.FileInfo.FromFileName(path);
788+
789+
// Act
790+
fileInfo.Delete();
791+
792+
// Assert
793+
Assert.IsFalse(fileInfo.Exists);
794+
}
795+
753796
[Test]
754797
public void MockFileInfo_Delete_ShouldThrowIfFileAccessShareHasNoWriteOrDeleteAccess()
755798
{

0 commit comments

Comments
 (0)