Skip to content

Reading from a file via MockFileStream resets creation time #122

@kodfodrasz

Description

@kodfodrasz

when having a mocked file in a mockfilesystem, and readfing from it mutates the LastWriteTime of the mocked file.
Here is an XUnit fact which can be used as a regression test to verify the problem, and the solution.

        [Fact]
        public void ReadingFromAFileDoesNotOverwriteIt() {
            // Arrange
            var fs = new MockFileSystem();
            var filename = @"C:\TestData\test.txt";
            var file = new MockFileData(@"This is just a test file

with test contents, which are not modified!");
            var lastmod = new DateTime(2012, 03, 21);
            file.LastWriteTime = lastmod;
            fs.AddFile(filename, file);

            // Act
            using (var reader = fs.File.OpenText(filename)) {
                var firstline = reader.ReadLine();
            }

            // Assert
            Assert.Equal(lastmod, fs.FileInfo.FromFileName(filename).LastWriteTime);
        }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions