Skip to content

Set Creation, LastAccess and LastWrite time for files #872

@vbreuss

Description

@vbreuss

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);
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testinghelpersIssues that address the testing helpersstate: ready to pickIssues that are ready for being worked ontype: enhancementIssues that propose new functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions