Skip to content

MockFileSystem doesn't handle FileMode.Truncate as expected #357

@ckarras

Description

@ckarras

Using System.IO.Abstractions.TestingHelpers 2.1.0.231

FileStream.Create with FileMode.Truncate doesn't correctly truncate existing files. If the new content written to the file is smaller than the old content, the smaller content will overwrite the begining of the file, but then the old content will remain.

Example:

            using (var stream = fileSystem.FileStream.Create("c:\\t\\test", FileMode.Create, FileAccess.Write))
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.Write("test1234TEST5678");
                }
            }

            using (var stream = fileSystem.FileStream.Create("c:\\t\\test", FileMode.Truncate, FileAccess.Write))
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.Write("ab");
                }
            }

            fileSystem.File.ReadAllLines("c:\\t\\test").Should().Equal("ab");
  • If fileSystem = new FileSystem(), then the test passes.
  • But if fileSystem = new MockFileSystem(), then the test fails because the file contents is: abst1234TEST5678 (first 2 characters overwritten, followed by characters from the original file that shouldn't be there anymore)

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: in workIssues that are currently worked ontype: bugIssues that describe misbehaving functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions