Skip to content

Commit

Permalink
fix: ensure FlushAsync behaves like Flush (#960)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cryocz authored Mar 13, 2023
1 parent 9dc2c3a commit 2b6dba4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ public override void Flush()
InternalFlush();
}

/// <inheritdoc />
public override Task FlushAsync(CancellationToken cancellationToken)
{
InternalFlush();
return Task.CompletedTask;
}

/// <inheritdoc cref="IFileSystemAclSupport.GetAccessControl()" />
[SupportedOSPlatform("windows")]
public object GetAccessControl()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace System.IO.Abstractions.TestingHelpers.Tests
{
using System.Collections.Generic;
using System.Threading.Tasks;

using NUnit.Framework;

Expand All @@ -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<string, MockFileData>());
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()
{
Expand Down

0 comments on commit 2b6dba4

Please sign in to comment.