From 2b6dba4529047c68576fd1c42354bfa970c19bac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Pokorn=C3=BD?= Date: Mon, 13 Mar 2023 14:51:14 +0100 Subject: [PATCH] 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 --- .../MockFileStream.cs | 7 ++++++ .../MockFileStreamTests.cs | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileStream.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileStream.cs index adcb4ac0e..ba157b967 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileStream.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileStream.cs @@ -198,6 +198,13 @@ public override void Flush() InternalFlush(); } + /// + public override Task FlushAsync(CancellationToken cancellationToken) + { + InternalFlush(); + return Task.CompletedTask; + } + /// [SupportedOSPlatform("windows")] public object GetAccessControl() diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileStreamTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileStreamTests.cs index 6993cb99f..69f541c59 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileStreamTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileStreamTests.cs @@ -1,6 +1,7 @@ namespace System.IO.Abstractions.TestingHelpers.Tests { using System.Collections.Generic; + using System.Threading.Tasks; using NUnit.Framework; @@ -27,6 +28,27 @@ public void MockFileStream_Flush_WritesByteToFile() CollectionAssert.AreEqual(new byte[] { 255 }, fileSystem.GetFile(filepath).Contents); } + [Test] + public async Task MockFileStream_FlushAsync_WritesByteToFile() + { + // bug replication test for issue + // https://github.com/TestableIO/System.IO.Abstractions/issues/959 + + // Arrange + var filepath = XFS.Path(@"C:\something\foo.txt"); + var fileSystem = new MockFileSystem(new Dictionary()); + fileSystem.AddDirectory(XFS.Path(@"C:\something")); + + var cut = new MockFileStream(fileSystem, filepath, FileMode.Create); + + // Act + await cut.WriteAsync(new byte[] { 255 }, 0, 1); + await cut.FlushAsync(); + + // Assert + CollectionAssert.AreEqual(new byte[] { 255 }, fileSystem.GetFile(filepath).Contents); + } + [Test] public void MockFileStream_Dispose_ShouldNotResurrectFile() {