Skip to content

Commit 2b6dba4

Browse files
authored
fix: ensure FlushAsync behaves like Flush (#960)
Added an overwrite for `Stream.FlushAsync` to the `MockFileStream` class, which ensures that the internal flush implementation is called. This way `FlushAsync` will correctly project the changes to the underlying `MockFile`, exactly like `Flush` does. Closes #959
1 parent 9dc2c3a commit 2b6dba4

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ public override void Flush()
198198
InternalFlush();
199199
}
200200

201+
/// <inheritdoc />
202+
public override Task FlushAsync(CancellationToken cancellationToken)
203+
{
204+
InternalFlush();
205+
return Task.CompletedTask;
206+
}
207+
201208
/// <inheritdoc cref="IFileSystemAclSupport.GetAccessControl()" />
202209
[SupportedOSPlatform("windows")]
203210
public object GetAccessControl()

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace System.IO.Abstractions.TestingHelpers.Tests
22
{
33
using System.Collections.Generic;
4+
using System.Threading.Tasks;
45

56
using NUnit.Framework;
67

@@ -27,6 +28,27 @@ public void MockFileStream_Flush_WritesByteToFile()
2728
CollectionAssert.AreEqual(new byte[] { 255 }, fileSystem.GetFile(filepath).Contents);
2829
}
2930

31+
[Test]
32+
public async Task MockFileStream_FlushAsync_WritesByteToFile()
33+
{
34+
// bug replication test for issue
35+
// https://github.com/TestableIO/System.IO.Abstractions/issues/959
36+
37+
// Arrange
38+
var filepath = XFS.Path(@"C:\something\foo.txt");
39+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
40+
fileSystem.AddDirectory(XFS.Path(@"C:\something"));
41+
42+
var cut = new MockFileStream(fileSystem, filepath, FileMode.Create);
43+
44+
// Act
45+
await cut.WriteAsync(new byte[] { 255 }, 0, 1);
46+
await cut.FlushAsync();
47+
48+
// Assert
49+
CollectionAssert.AreEqual(new byte[] { 255 }, fileSystem.GetFile(filepath).Contents);
50+
}
51+
3052
[Test]
3153
public void MockFileStream_Dispose_ShouldNotResurrectFile()
3254
{

0 commit comments

Comments
 (0)