Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MockFileStream unexpected behavior when used via BinaryWriter #519

Closed
RThomasHyde opened this issue Oct 23, 2019 · 3 comments · Fixed by #558
Closed

MockFileStream unexpected behavior when used via BinaryWriter #519

RThomasHyde opened this issue Oct 23, 2019 · 3 comments · Fixed by #558
Labels
area: testinghelpers Issues that address the testing helpers state: ready to pick Issues that are ready for being worked on type: bug Issues that describe misbehaving functionality

Comments

@RThomasHyde
Copy link

Using the latest stable NuGet version (7.0.7)

If I initialize a BinaryWriter with a MockFileStream (obtained e.g. by using MockFileSystem.File.Create), and then manipulate it via BinaryWriter.BaseStream, I get unexpected behavior. For instance, examine the following:

var fileSystem = new MockFileSystem();
using (var s = fileSystem.File.Create(@"C:\testfile.bin"))
using (var writer = new System.IO.BinaryWriter(s))
{
    writer.Write(new byte[400], 0, 400);
    writer.BaseStream.Seek(200, System.IO.SeekOrigin.Begin);
    Console.WriteLine(writer.BaseStream.Position);   // prints 400, expect 200
    Console.WriteLine(s.Position); // prints 400, expect 200
}

Presumably this is because BinaryWriter.BaseStream calls Flush on the underlying stream before returning it, though it's not immediately clear to me why this should be a problem. Avoiding accessing BaseStream by calling e.g. writer.Seek directly does avoid the problem, but the point is that MockFileStream does not replicate the behavior of a real FileStream in this regard:

using (var s = System.IO.File.Create(@"C:\testfile.bin")
using (var writer = new System.IO.BinaryWriter(s))
{
    writer.Write(new byte[400], 0, 400);
    writer.BaseStream.Seek(200,  System.IO.SeekOrigin.Begin);
    Console.WriteLine(writer.BaseStream.Position);   // prints 200 as expected
    Console.WriteLine(s.Position); // prints 200 as expected
}
@issue-label-bot
Copy link

Issue-Label Bot is automatically applying the label type: bug to this issue, with a confidence of 0.86. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

@issue-label-bot issue-label-bot bot added the type: bug Issues that describe misbehaving functionality label Oct 23, 2019
@fgreinacher
Copy link
Contributor

Thanks for reporting! Might have the same root cause as #464.

@fgreinacher fgreinacher added area: testinghelpers Issues that address the testing helpers state: ready to pick Issues that are ready for being worked on labels Oct 25, 2019
@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@fgreinacher fgreinacher reopened this Jan 26, 2020
fgreinacher pushed a commit that referenced this issue Feb 18, 2020
Resets the internal Stream position to the original position after reading the file's data to the data array.

Fixes #519
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: testinghelpers Issues that address the testing helpers state: ready to pick Issues that are ready for being worked on type: bug Issues that describe misbehaving functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants