Skip to content

MockFileSystem: IFile.GetLastWriteTimeUtc may return wrong last write time after calling IFileInfo.Open #1029

Description

@jhinder

Describe the bug
IFile.GetLastWriteTimeUtc returns the system time instead of MockFileData's LastWriteTime value if IFileInfo.Open was called with File.Open but without FileAccess.Read.

To Reproduce

usingSystem.Diagnostics;usingSystem.IO.Abstractions.TestingHelpers;// Setupconststringpath=@"C:\test.file";varmockFs=newMockFileSystem();varlastWriteTime=newDateTime(2023,08,22,10,30,00,DateTimeKind.Utc);Console.WriteLine("Expected write time: {0}",lastWriteTime);mockFs.AddFile(path,newMockFileData("test"){LastWriteTime=lastWriteTime});// Actvarfile=mockFs.FileInfo.New(path);using(file.Open(FileMode.Open)){}// AssertvarfileInfoWriteTime=file.LastWriteTimeUtc;Console.WriteLine("LastWriteTimeUtc from property: {0}",fileInfoWriteTime);Debug.Assert(fileInfoWriteTime==lastWriteTime);varfileWriteTime=mockFs.File.GetLastWriteTimeUtc(file.FullName);Console.WriteLine("LastWriteTimeUtc from call: {0}",fileWriteTime);Debug.Assert(fileWriteTime==lastWriteTime);

The code returns an output similar to this, and the last assertion is hit:

Expected write time: 22.08.2023 10:30:00
LastWriteTimeUtc from property: 22.08.2023 10:30:00
LastWriteTimeUtc from call: 22.08.2023 08:44:14

Expected behavior
The correct write time (= the time set in the MockFileData initializer) is returned in all cases.

Additional context
Removing the call to Open or adding the FileAccess.Read argument both lead to correct behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: needs discussionIssues that need further discussiontype: 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