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

Set Creation, LastAccess and LastWrite time for files #872

Closed
vbreuss opened this issue Aug 9, 2022 · 2 comments
Closed

Set Creation, LastAccess and LastWrite time for files #872

vbreuss opened this issue Aug 9, 2022 · 2 comments
Labels
area: testinghelpers Issues that address the testing helpers state: ready to pick Issues that are ready for being worked on type: enhancement Issues that propose new functionality

Comments

@vbreuss
Copy link
Member

vbreuss commented Aug 9, 2022

Is your feature request related to a problem? Please describe.
I want to test a functionality that relies on the "GetLastWriteTimeUtc" method to detect when a file was last changed.
This property in MockFileSystem only returns a fixed value "2010-02-03 20:00:00"

Describe the solution you'd like
MockFileSystem should set the creation, last access and last write times and return the correct values.
Ideally you would also provide an optional callback or property to set, so that I can even mock the time that is stored.

Describe alternatives you've considered
None, as the times are not even tracked anywhere internally.

Additional context
I would like e.g. the following unit test to pass:

  [Theory]
  [WithAutoData]
  public void GetCreationTimeUtc_ShouldBeSet(string file)
  {
    var now = DateTime.UtcNow;
    var sut = new MockFileSystem();
    sut.File.WriteAllText("foo.txt", "xyz");

    var result = sut.File.GetCreationTimeUtc("foo.txt");

    result.Kind.Should().Be(DateTimeKind.Utc);
    result.Should().BeOnOrAfter(now);
  }
@vbreuss vbreuss added state: needs discussion Issues that need further discussion type: enhancement Issues that propose new functionality labels Aug 9, 2022
@fgreinacher
Copy link
Contributor

We already have the partially, see #861.

Makes sense to implement this consistently for all operations 👍

@fgreinacher fgreinacher added area: testinghelpers Issues that address the testing helpers flag: good-first-issue Issues that are good for first time contributors state: ready to pick Issues that are ready for being worked on and removed state: needs discussion Issues that need further discussion flag: good-first-issue Issues that are good for first time contributors labels Aug 10, 2022
@vbreuss
Copy link
Member Author

vbreuss commented Aug 10, 2022

I would give it a try...

mergify bot pushed a commit that referenced this issue Sep 16, 2022
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.
```csharp
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 vbreuss closed this as completed Apr 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: testinghelpers Issues that address the testing helpers state: ready to pick Issues that are ready for being worked on type: enhancement Issues that propose new functionality
Projects
None yet
Development

No branches or pull requests

2 participants