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

Set Creation, LastAccess and LastWrite time for files

Open vbreuss opened this issue 3 years ago • 2 comments

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 avatar Aug 09 '22 05:08 vbreuss

We already have the partially, see https://github.com/TestableIO/System.IO.Abstractions/pull/861.

Makes sense to implement this consistently for all operations 👍

fgreinacher avatar Aug 10 '22 17:08 fgreinacher

I would give it a try...

vbreuss avatar Aug 10 '22 18:08 vbreuss