Skip to content

TestingHelper.File.Copy initialises contents via object reference #353

@Will-Crabb

Description

@Will-Crabb

When you copy a file the file contents gets passed as a reference instead of being cloned. If I copy FileA.txt to FileB.txt, they will both point to the same contents object. When I then change the contents of FileA.txt, FileB.txt will also update.

        var mockFileSystem = new MockFileSystem();
        mockFileSystem.AddFile("C:\\Test1.txt", "");
        mockFileSystem.File.Copy("C:\\Test1.txt", "C:\\Test2.txt");

        var before1 = mockFileSystem.FileInfo.FromFileName("C:\\Test1.txt").Length;
        var before2 = mockFileSystem.FileInfo.FromFileName("C:\\Test2.txt").Length;

        using (var stream = mockFileSystem.File.Open("C:\\Test1.txt", FileMode.Open, FileAccess.ReadWrite))
        {
            var binaryWriter = new System.IO.BinaryWriter(stream);

            binaryWriter.Seek(0, SeekOrigin.Begin);
            binaryWriter.Write("Some text");
        }

        var after1 = mockFileSystem.FileInfo.FromFileName("C:\\Test1.txt").Length;
        var after2 = mockFileSystem.FileInfo.FromFileName("C:\\Test2.txt").Length;

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