Skip to content

Commit 3305021

Browse files
authored
fix: Reset Stream position after flushing MockFileStream data (#558)
Resets the internal Stream position to the original position after reading the file's data to the data array. Fixes #519
1 parent 6ca7f20 commit 3305021

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,25 @@ public void MockFileStream_Dispose_OperationsAfterDisposeThrow()
164164
// Assert
165165
Assert.Throws<ObjectDisposedException>(() => stream.WriteByte(0));
166166
}
167+
168+
[Test]
169+
public void MockFileStream_Flush_ShouldNotChangePosition()
170+
{
171+
// Arrange
172+
var fileSystem = new MockFileSystem();
173+
var path = XFS.Path("C:\\test");
174+
fileSystem.AddFile(path, new MockFileData(new byte[0]));
175+
176+
using (var stream = fileSystem.FileInfo.FromFileName(path).OpenWrite())
177+
{
178+
// Act
179+
stream.Write(new byte[400], 0, 400);
180+
stream.Seek(200, SeekOrigin.Begin);
181+
stream.Flush();
182+
183+
// Assert
184+
Assert.AreEqual(200, stream.Position);
185+
}
186+
}
167187
}
168188
}

System.IO.Abstractions.TestingHelpers/MockFileStream.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,13 @@ private void InternalFlush()
9494
{
9595
var mockFileData = mockFileDataAccessor.GetFile(path);
9696
/* reset back to the beginning .. */
97+
var position = Position;
9798
Seek(0, SeekOrigin.Begin);
9899
/* .. read everything out */
99100
var data = new byte[Length];
100101
Read(data, 0, (int)Length);
102+
/* restore to original position */
103+
Seek(position, SeekOrigin.Begin);
101104
/* .. put it in the mock system */
102105
mockFileData.Contents = data;
103106
}

0 commit comments

Comments
 (0)