Skip to content

Commit ccdc24d

Browse files
authored
feat: Add Flush(bool) to FileSystemStream. (#985)
`FileSystemStream` was missing the implementation of `FileStream.Flush(bool)` which allows the caller to specify whether `Flush` should ensure all buffers are written to disk.
1 parent 2777be0 commit ccdc24d

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ public override void Flush()
223223
InternalFlush();
224224
}
225225

226+
/// <inheritdoc />
227+
public override void Flush(bool flushToDisk)
228+
=> InternalFlush();
229+
226230
/// <inheritdoc />
227231
public override Task FlushAsync(CancellationToken cancellationToken)
228232
{

src/TestableIO.System.IO.Abstractions.Wrappers/FileStreamWrapper.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,9 @@ public void SetAccessControl(object value)
4141
throw new ArgumentException("value must be of type `FileSecurity`");
4242
}
4343
}
44+
45+
/// <inheritdoc />
46+
public override void Flush(bool flushToDisk)
47+
=> fileStream.Flush(flushToDisk);
4448
}
4549
}

src/TestableIO.System.IO.Abstractions/FileSystemStream.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ public override void EndWrite(IAsyncResult asyncResult)
128128
public override void Flush()
129129
=> _stream.Flush();
130130

131+
/// <inheritDoc cref="FileStream.Flush(bool)" />
132+
public virtual void Flush(bool flushToDisk)
133+
=> _stream.Flush();
134+
131135
/// <inheritdoc cref="Stream.FlushAsync(CancellationToken)" />
132136
public override Task FlushAsync(CancellationToken cancellationToken)
133137
=> _stream.FlushAsync(cancellationToken);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,25 @@ public void MockFileStream_Flush_ShouldNotChangePosition()
206206
Assert.AreEqual(200, stream.Position);
207207
}
208208
}
209+
210+
[Test]
211+
public void MockFileStream_FlushBool_ShouldNotChangePosition([Values] bool flushToDisk)
212+
{
213+
// Arrange
214+
var fileSystem = new MockFileSystem();
215+
var path = XFS.Path("C:\\test");
216+
fileSystem.AddFile(path, new MockFileData(new byte[0]));
217+
218+
using (var stream = fileSystem.FileInfo.New(path).OpenWrite())
219+
{
220+
// Act
221+
stream.Write(new byte[400], 0, 400);
222+
stream.Seek(200, SeekOrigin.Begin);
223+
stream.Flush(flushToDisk);
224+
225+
// Assert
226+
Assert.AreEqual(200, stream.Position);
227+
}
228+
}
209229
}
210230
}

0 commit comments

Comments
 (0)