System.IO.Abstractions icon indicating copy to clipboard operation
System.IO.Abstractions copied to clipboard

feat: #872 Set Creation, LastAccess and LastWrite time for files

Open vbreuss opened this issue 3 years ago • 1 comments

Implements #872 Adjust CreationTime, LastAccessTime and LastWriteTime when interacting with files in the MockFileSystem.

Provides a means to mock the used DateTime by calling e.g.

var fixedTime = new DateTime(2022, 01, 01);
var fileSystem = new MockFileSystem().MockTime(() => fixedTime);
// All times will now be set to the fixedTime.

Implementes the following logic in MockFile: All these cases are covered by unit tests in MockFileAdjustTimesTest

  • When creating files CreationTime, LastAccessTime and LastWriteTime are set to the current time
  • When changing files (WriteAllText, WriteAllBytes, AppendAllText, OpenWrite, Open) LastAccessTime and LastWriteTime are set to the current time CreationTime is left unchanged
  • When reading files (ReadAllText, ReadAllLines, ReadAllBytes, OpenRead) LastAccessTime is set to the current time CreationTime and LastWriteTime are left unchanged
  • When setting attributes or ACLs (SetAttributes, SetAccessControl) LastAccessTime is set to the current time CreationTime and LastWriteTime are left unchanged
  • When moving files (Move) LastAccessTime is set to the current time CreationTime and LastWriteTime are left unchanged
  • When copying files (Copy) CreationTime and LastAccessTime of the copied file in the destination directory is set to the current time LastWriteTime and all times of the source file are left unchanged

vbreuss avatar Aug 10 '22 20:08 vbreuss

Please note, that I changed the Interface IMockFileDataAccessor. This interface looks like it is intended for internal use, but as it is public this could be considered a breaking change...

I am not sure what I should do: I could increase the version or use default interface methods from C# 8

vbreuss avatar Aug 11 '22 18:08 vbreuss

If this is an accepted approach, I could also try to have a look at #839, which deals with the times of directories. This would probably be quite similar...

vbreuss avatar Aug 17 '22 05:08 vbreuss

Sorry @vbreuss, I'm a bit busy with other things, will try to have a look by the end of this week!

fgreinacher avatar Aug 17 '22 05:08 fgreinacher

@siprbaum : I merged the latest changes, so that this branch is up to date. All comments are either fixed and resolved, or I left a comment explaining, why I implemented it this way. Could you please have a look again at this pull request?

vbreuss avatar Aug 31 '22 07:08 vbreuss

@siprbaum : Thanks for the review! I implemented the changes and answered your findings. Can you please have a look again, and resolve the comments, that you agree with?

Regarding the user detectable behaviour change: As we are now setting the times correctly, the behaviour changes when previously in the tests you would (incorrectly) rely upon unchanged times for the files. However I am still unsure, if I should increment the version when considering my previous comment in this pull request, regarding the change in the interface IMockFileDataAccessor. What do you think?

vbreuss avatar Sep 02 '22 12:09 vbreuss

@fgreinacher : I merged the latest main branch and answered or fixed all review comments. Can you please have a look?

vbreuss avatar Sep 10 '22 13:09 vbreuss

This is addressed in release v17.2.3.

github-actions[bot] avatar Nov 18 '22 20:11 github-actions[bot]

This is addressed in release v17.2.3.

github-actions[bot] avatar Nov 28 '22 18:11 github-actions[bot]