Skip to content

MockFileStreamFactory.Create with FileMode.Create does not truncate an existing file #465

@rpcac

Description

@rpcac

Found in version 3.0.10.

This is very similar, though distinct from, #357. Creating a stream with FileMode.Create is fine when the file does not exist, but does not truncate if the file exists. The following test should pass, but does not:

            var fileSystem = new MockFileSystem();
            const string FilePath = "File.txt";

            using(var stream = fileSystem.FileStream.Create(FilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write))
            {
                using(var writer = new System.IO.StreamWriter(stream, System.Text.Encoding.UTF8, 4096, leaveOpen: true))
                {
                    writer.Write("1234567890");
                }
            }

            using(var stream = fileSystem.FileStream.Create(FilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write))
            {
                using(var writer = new System.IO.StreamWriter(stream, System.Text.Encoding.UTF8, 4096, leaveOpen: true))
                {
                    writer.Write("AAAAA");
                }
            }

            string text = fileSystem.File.ReadAllText(FilePath);
            Assert.AreEqual("AAAAA", text); // text == "AAAAA67890"

Note that if you substitute FileMode.Truncate when opening the second stream, the test passes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testinghelpersIssues that address the testing helpersstate: ready to pickIssues that are ready for being 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