Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jhinder opened this issue Aug 22, 2023 · 2 comments
Labels
state: needs discussion Issues that need further discussion type: bug Issues that describe misbehaving functionality

Comments

@jhinder
Copy link

jhinder commented Aug 22, 2023

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

using System.Diagnostics;
using System.IO.Abstractions.TestingHelpers;

// Setup
const string path = @"C:\test.file";
var mockFs = new MockFileSystem();
var lastWriteTime = new DateTime(2023, 08, 22, 10, 30, 00, DateTimeKind.Utc);
Console.WriteLine("Expected write time: {0}", lastWriteTime);
mockFs.AddFile(path, new MockFileData("test") { LastWriteTime = lastWriteTime });

// Act
var file = mockFs.FileInfo.New(path);
using (file.Open(FileMode.Open)) { }

// Assert
var fileInfoWriteTime = file.LastWriteTimeUtc;
Console.WriteLine("LastWriteTimeUtc from property: {0}", fileInfoWriteTime);
Debug.Assert(fileInfoWriteTime == lastWriteTime);

var fileWriteTime = 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.

@jhinder jhinder added state: needs discussion Issues that need further discussion type: bug Issues that describe misbehaving functionality labels Aug 22, 2023
@vbreuss
Copy link
Member

vbreuss commented Aug 22, 2023

@jhinder :
In #875 I implemented that on access to a file the LastWriteTime is set accordingly. Per default it uses the current DateTime, but you can overwrite this behaviour by setting MockFileSystem.MockTime().

@fgreinacher
Copy link
Contributor

I think the answer above allows you to make it work for you, so I'm closing this. Feel free to reopen if anything is missing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs discussion Issues that need further discussion type: bug Issues that describe misbehaving functionality
Projects
None yet
Development

No branches or pull requests

3 participants