From 7ecbd6341b9cad3dc7715125d84a8e9ad90d2c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Wed, 10 Aug 2022 22:32:34 +0200 Subject: [PATCH] Adjust times in MockFileSystem Also apply time in MockFileStream Mark MockFile_AfterSetAccessControl_ShouldUpdateLastAccessTime as Windows-Only --- .../IMockFileDataAccessor.cs | 8 + .../MockFile.cs | 42 ++- .../MockFileStream.cs | 10 +- .../MockFileSystem.cs | 42 ++- .../TimeAdjustments.cs | 30 ++ .../MockFileAdjustTimesTest.cs | 303 ++++++++++++++++++ .../MockFileGetCreationTimeTests.cs | 14 + .../MockFileGetCreationTimeUtcTests.cs | 14 + .../MockFileGetLastAccessTimeTests.cs | 14 + .../MockFileGetLastAccessTimeUtcTests.cs | 14 + .../MockFileGetLastWriteTimeTests.cs | 14 + .../MockFileGetLastWriteTimeUtcTests.cs | 14 + 12 files changed, 502 insertions(+), 17 deletions(-) create mode 100644 src/System.IO.Abstractions.TestingHelpers/TimeAdjustments.cs create mode 100644 tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileAdjustTimesTest.cs diff --git a/src/System.IO.Abstractions.TestingHelpers/IMockFileDataAccessor.cs b/src/System.IO.Abstractions.TestingHelpers/IMockFileDataAccessor.cs index 1aeeb89d2..533e28be0 100644 --- a/src/System.IO.Abstractions.TestingHelpers/IMockFileDataAccessor.cs +++ b/src/System.IO.Abstractions.TestingHelpers/IMockFileDataAccessor.cs @@ -8,6 +8,14 @@ namespace System.IO.Abstractions.TestingHelpers /// public interface IMockFileDataAccessor : IFileSystem { + /// + /// Adjust the times of the . + /// + /// The for which the times should be adjusted. + /// The adjustments to make on the . + /// The adjusted file. + MockFileData AdjustTimes(MockFileData fileData, TimeAdjustments timeAdjustments); + /// /// Gets a file. /// diff --git a/src/System.IO.Abstractions.TestingHelpers/MockFile.cs b/src/System.IO.Abstractions.TestingHelpers/MockFile.cs index a14e5d7cc..c86208a70 100644 --- a/src/System.IO.Abstractions.TestingHelpers/MockFile.cs +++ b/src/System.IO.Abstractions.TestingHelpers/MockFile.cs @@ -57,12 +57,13 @@ public override void AppendAllText(string path, string contents, Encoding encodi if (!mockFileDataAccessor.FileExists(path)) { VerifyDirectoryExists(path); - mockFileDataAccessor.AddFile(path, new MockFileData(contents, encoding)); + mockFileDataAccessor.AddFile(path, mockFileDataAccessor.AdjustTimes(new MockFileData(contents, encoding), TimeAdjustments.All)); } else { var file = mockFileDataAccessor.GetFile(path); file.CheckFileAccess(path, FileAccess.Write); + mockFileDataAccessor.AdjustTimes(file, TimeAdjustments.LastAccessTime | TimeAdjustments.LastWriteTime); var bytesToAppend = encoding.GetBytes(contents); file.Contents = file.Contents.Concat(bytesToAppend).ToArray(); } @@ -126,7 +127,7 @@ public override void Copy(string sourceFileName, string destFileName, bool overw var sourceFileData = mockFileDataAccessor.GetFile(sourceFileName); sourceFileData.CheckFileAccess(sourceFileName, FileAccess.Read); var destFileData = new MockFileData(sourceFileData); - destFileData.CreationTime = destFileData.LastAccessTime = DateTime.Now; + mockFileDataAccessor.AdjustTimes(destFileData, TimeAdjustments.CreationTime | TimeAdjustments.LastAccessTime); mockFileDataAccessor.AddFile(destFileName, destFileData); } @@ -153,6 +154,7 @@ private Stream CreateInternal(string path, FileAccess access, FileOptions option VerifyDirectoryExists(path); var mockFileData = new MockFileData(new byte[0]); + mockFileDataAccessor.AdjustTimes(mockFileData, TimeAdjustments.All); mockFileDataAccessor.AddFile(path, mockFileData); return OpenInternal(path, FileMode.Open, access, options); } @@ -396,8 +398,8 @@ public override void Move(string sourceFileName, string destFileName) throw CommonExceptions.ProcessCannotAccessFileInUse(); } VerifyDirectoryExists(destFileName); - - mockFileDataAccessor.AddFile(destFileName, new MockFileData(sourceFile)); + + mockFileDataAccessor.AddFile(destFileName, mockFileDataAccessor.AdjustTimes(new MockFileData(sourceFile), TimeAdjustments.LastAccessTime)); mockFileDataAccessor.RemoveFile(sourceFileName); } @@ -443,7 +445,7 @@ public override void Move(string sourceFileName, string destFileName, bool overw } VerifyDirectoryExists(destFileName); - mockFileDataAccessor.AddFile(destFileName, new MockFileData(sourceFile)); + mockFileDataAccessor.AddFile(destFileName, mockFileDataAccessor.AdjustTimes(new MockFileData(sourceFile), TimeAdjustments.LastAccessTime)); mockFileDataAccessor.RemoveFile(sourceFileName); } #endif @@ -501,6 +503,12 @@ private Stream OpenInternal( var mockFileData = mockFileDataAccessor.GetFile(path); mockFileData.CheckFileAccess(path, access); + var timeAdjustments = TimeAdjustments.LastAccessTime; + if (access != FileAccess.Read) + { + timeAdjustments |= TimeAdjustments.LastWriteTime; + } + mockFileDataAccessor.AdjustTimes(mockFileData, timeAdjustments); return new MockFileStream(mockFileDataAccessor, path, mode, access, options); } @@ -540,7 +548,9 @@ public override byte[] ReadAllBytes(string path) throw CommonExceptions.FileNotFound(path); } mockFileDataAccessor.GetFile(path).CheckFileAccess(path, FileAccess.Read); - return mockFileDataAccessor.GetFile(path).Contents.ToArray(); + var fileData = mockFileDataAccessor.GetFile(path); + mockFileDataAccessor.AdjustTimes(fileData, TimeAdjustments.LastAccessTime); + return fileData.Contents.ToArray(); } /// @@ -552,10 +562,11 @@ public override string[] ReadAllLines(string path) { throw CommonExceptions.FileNotFound(path); } - mockFileDataAccessor.GetFile(path).CheckFileAccess(path, FileAccess.Read); + var fileData = mockFileDataAccessor.GetFile(path); + fileData.CheckFileAccess(path, FileAccess.Read); + mockFileDataAccessor.AdjustTimes(fileData, TimeAdjustments.LastAccessTime); - return mockFileDataAccessor - .GetFile(path) + return fileData .TextContents .SplitLines(); } @@ -575,9 +586,11 @@ public override string[] ReadAllLines(string path, Encoding encoding) throw CommonExceptions.FileNotFound(path); } - mockFileDataAccessor.GetFile(path).CheckFileAccess(path, FileAccess.Read); + var fileData = mockFileDataAccessor.GetFile(path); + fileData.CheckFileAccess(path, FileAccess.Read); + mockFileDataAccessor.AdjustTimes(fileData, TimeAdjustments.LastAccessTime); - using (var ms = new MemoryStream(mockFileDataAccessor.GetFile(path).Contents)) + using (var ms = new MemoryStream(fileData.Contents)) using (var sr = new StreamReader(ms, encoding)) { return sr.ReadToEnd().SplitLines(); @@ -675,6 +688,7 @@ public override void SetAccessControl(string path, FileSecurity fileSecurity) } var fileData = mockFileDataAccessor.GetFile(path); + mockFileDataAccessor.AdjustTimes(fileData, TimeAdjustments.LastAccessTime); fileData.AccessControl = fileSecurity; } @@ -698,6 +712,7 @@ public override void SetAttributes(string path, FileAttributes fileAttributes) } else { + mockFileDataAccessor.AdjustTimes(possibleFileData, TimeAdjustments.LastAccessTime); possibleFileData.Attributes = fileAttributes; } } @@ -786,7 +801,7 @@ public override void WriteAllBytes(string path, byte[] bytes) mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path"); VerifyDirectoryExists(path); - mockFileDataAccessor.AddFile(path, new MockFileData(bytes.ToArray())); + mockFileDataAccessor.AddFile(path, mockFileDataAccessor.AdjustTimes(new MockFileData(bytes.ToArray()), TimeAdjustments.All)); } /// @@ -1056,7 +1071,7 @@ public override void WriteAllText(string path, string contents, Encoding encodin VerifyDirectoryExists(path); MockFileData data = contents == null ? new MockFileData(new byte[0]) : new MockFileData(contents, encoding); - mockFileDataAccessor.AddFile(path, data); + mockFileDataAccessor.AddFile(path, mockFileDataAccessor.AdjustTimes(data, TimeAdjustments.All)); } internal static string ReadAllBytes(byte[] contents, Encoding encoding) @@ -1072,6 +1087,7 @@ private string ReadAllTextInternal(string path, Encoding encoding) { var mockFileData = mockFileDataAccessor.GetFile(path); mockFileData.CheckFileAccess(path, FileAccess.Read); + mockFileDataAccessor.AdjustTimes(mockFileData, TimeAdjustments.LastAccessTime); return ReadAllBytes(mockFileData.Contents, encoding); } diff --git a/src/System.IO.Abstractions.TestingHelpers/MockFileStream.cs b/src/System.IO.Abstractions.TestingHelpers/MockFileStream.cs index 6b0c39303..ed7333a8d 100644 --- a/src/System.IO.Abstractions.TestingHelpers/MockFileStream.cs +++ b/src/System.IO.Abstractions.TestingHelpers/MockFileStream.cs @@ -34,6 +34,7 @@ public MockFileStream( fileData = mockFileDataAccessor.GetFile(path); fileData.CheckFileAccess(path, access); + mockFileDataAccessor.AdjustTimes(fileData, TimeAdjustments.LastAccessTime); var existingContents = fileData.Contents; var keepExistingContents = existingContents?.Length > 0 && @@ -60,7 +61,8 @@ public MockFileStream( } fileData = new MockFileData(new byte[] { }); - fileData.CreationTime = fileData.LastWriteTime = fileData.LastAccessTime = DateTime.Now; + mockFileDataAccessor.AdjustTimes(fileData, + TimeAdjustments.CreationTime | TimeAdjustments.LastAccessTime); mockFileDataAccessor.AddFile(path, fileData); } @@ -76,14 +78,16 @@ public MockFileStream( /// public override int Read(byte[] buffer, int offset, int count) { - fileData.LastAccessTime = DateTime.Now; + mockFileDataAccessor.AdjustTimes(fileData, + TimeAdjustments.LastAccessTime); return base.Read(buffer, offset, count); } /// public override void Write(byte[] buffer, int offset, int count) { - fileData.LastWriteTime = fileData.LastAccessTime = DateTime.Now; + mockFileDataAccessor.AdjustTimes(fileData, + TimeAdjustments.LastAccessTime | TimeAdjustments.LastWriteTime); base.Write(buffer, offset, count); } diff --git a/src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs b/src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs index fa3203ac9..e563633d3 100644 --- a/src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs +++ b/src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs @@ -15,6 +15,9 @@ public class MockFileSystem : FileSystemBase, IMockFileDataAccessor private readonly IDictionary files; private readonly PathVerifier pathVerifier; + [NonSerialized] + private Func dateTimeProvider = defaultDateTimeProvider; + private static Func defaultDateTimeProvider = () => DateTime.Now; /// public MockFileSystem() : this(null) { } @@ -88,6 +91,19 @@ public MockFileSystem(IDictionary files, string currentDir /// public PathVerifier PathVerifier => pathVerifier; + /// + /// Replaces the time provider with a mocked instance. This allows to influence the used time in tests. + /// + /// If not set, the default implementation returns . + /// + /// The function that returns the current . + /// + public MockFileSystem MockTime(Func dateTimeProvider) + { + this.dateTimeProvider = dateTimeProvider ?? defaultDateTimeProvider; + return this; + } + private string FixPath(string path, bool checkCaps = false) { if (path == null) @@ -124,6 +140,28 @@ private string GetPathWithCorrectDirectoryCapitalization(string fullPath) return fullPath.TrimSlashes(); } + /// + public MockFileData AdjustTimes(MockFileData fileData, TimeAdjustments timeAdjustments) + { + var now = (dateTimeProvider ?? defaultDateTimeProvider)(); + if ((timeAdjustments & TimeAdjustments.CreationTime) != TimeAdjustments.None) + { + fileData.CreationTime = now; + } + + if ((timeAdjustments & TimeAdjustments.LastAccessTime) != TimeAdjustments.None) + { + fileData.LastAccessTime = now; + } + + if ((timeAdjustments & TimeAdjustments.LastWriteTime) != TimeAdjustments.None) + { + fileData.LastWriteTime = now; + } + + return fileData; + } + /// public MockFileData GetFile(string path) { @@ -143,6 +181,7 @@ public void AddFile(string path, MockFileData mockFile) var fixedPath = FixPath(path, true); lock (files) { + mockFile ??= new MockFileData(string.Empty); var file = GetFile(fixedPath); if (file != null) @@ -155,6 +194,7 @@ public void AddFile(string path, MockFileData mockFile) throw CommonExceptions.AccessDenied(path); } file.CheckFileAccess(fixedPath, FileAccess.Write); + mockFile.CreationTime = file.CreationTime; } var directoryPath = Path.GetDirectoryName(fixedPath); @@ -164,7 +204,7 @@ public void AddFile(string path, MockFileData mockFile) AddDirectory(directoryPath); } - SetEntry(fixedPath, mockFile ?? new MockFileData(string.Empty)); + SetEntry(fixedPath, mockFile); } } diff --git a/src/System.IO.Abstractions.TestingHelpers/TimeAdjustments.cs b/src/System.IO.Abstractions.TestingHelpers/TimeAdjustments.cs new file mode 100644 index 000000000..73f814e36 --- /dev/null +++ b/src/System.IO.Abstractions.TestingHelpers/TimeAdjustments.cs @@ -0,0 +1,30 @@ +namespace System.IO.Abstractions.TestingHelpers +{ + /// + /// Flags indicating which times to adjust for a . + /// + [Flags] + public enum TimeAdjustments + { + /// + /// Adjusts no times on the + /// + None = 0, + /// + /// Adjusts the + /// + CreationTime = 1 << 0, + /// + /// Adjusts the + /// + LastAccessTime = 1 << 1, + /// + /// Adjusts the + /// + LastWriteTime = 1 << 2, + /// + /// Adjusts all times on the + /// + All = ~0 + } +} \ No newline at end of file diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileAdjustTimesTest.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileAdjustTimesTest.cs new file mode 100644 index 000000000..24af619e4 --- /dev/null +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileAdjustTimesTest.cs @@ -0,0 +1,303 @@ +using System.Runtime.Versioning; +using System.Security.AccessControl; +using System.Text; +using NUnit.Framework; + +namespace System.IO.Abstractions.TestingHelpers.Tests +{ + [TestFixture] + public class MockFileAdjustTimesTest + { + [Test] + public void MockFile_AfterAppendAllText_ShouldUpdateLastAccessAndLastWriteTime() + { + var creationTime = DateTime.Now.AddDays(10); + var updateTime = creationTime.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => creationTime); + fileSystem.File.WriteAllText("foo.txt", "abc"); + fileSystem.MockTime(() => updateTime); + fileSystem.File.AppendAllText("foo.txt", "xyz"); + + var actualCreationTime = fileSystem.File.GetCreationTime("foo.txt"); + var actualLastAccessTime = fileSystem.File.GetLastAccessTime("foo.txt"); + var actualLastWriteTime = fileSystem.File.GetLastWriteTime("foo.txt"); + + Assert.That(actualCreationTime, Is.EqualTo(creationTime)); + Assert.That(actualLastAccessTime, Is.EqualTo(updateTime)); + Assert.That(actualLastWriteTime, Is.EqualTo(updateTime)); + } + + [Test] + public void MockFile_AfterCopy_ShouldUpdateCreationAndLastAccessTimeOfDestination() + { + var creationTime = DateTime.Now.AddDays(10); + var updateTime = creationTime.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => creationTime); + fileSystem.File.WriteAllText("foo.txt", "abc"); + fileSystem.MockTime(() => updateTime); + fileSystem.File.Copy("foo.txt", "bar.txt"); + + var actualSourceCreationTime = fileSystem.File.GetCreationTime("foo.txt"); + var actualDestinationCreationTime = fileSystem.File.GetCreationTime("bar.txt"); + var actualSourceLastAccessTime = fileSystem.File.GetLastAccessTime("foo.txt"); + var actualDestinationLastAccessTime = fileSystem.File.GetLastAccessTime("bar.txt"); + var actualSourceLastWriteTime = fileSystem.File.GetLastWriteTime("foo.txt"); + var actualDestinationLastWriteTime = fileSystem.File.GetLastWriteTime("bar.txt"); + + Assert.That(actualSourceCreationTime, Is.EqualTo(creationTime)); + Assert.That(actualDestinationCreationTime, Is.EqualTo(updateTime)); + Assert.That(actualSourceLastAccessTime, Is.EqualTo(creationTime)); + Assert.That(actualDestinationLastAccessTime, Is.EqualTo(updateTime)); + Assert.That(actualSourceLastWriteTime, Is.EqualTo(creationTime)); + Assert.That(actualDestinationLastWriteTime, Is.EqualTo(creationTime)); + } + + [Test] + public void MockFile_AfterMove_ShouldUpdateLastAccessTime() + { + var creationTime = DateTime.Now.AddDays(10); + var updateTime = creationTime.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => creationTime); + fileSystem.File.WriteAllText("foo.txt", "abc"); + fileSystem.MockTime(() => updateTime); + fileSystem.File.Move("foo.txt", "bar.txt"); + + var actualCreationTime = fileSystem.File.GetCreationTime("bar.txt"); + var actualLastAccessTime = fileSystem.File.GetLastAccessTime("bar.txt"); + var actualLastWriteTime = fileSystem.File.GetLastWriteTime("bar.txt"); + + Assert.That(actualCreationTime, Is.EqualTo(creationTime)); + Assert.That(actualLastAccessTime, Is.EqualTo(updateTime)); + Assert.That(actualLastWriteTime, Is.EqualTo(creationTime)); + } + + [TestCase(FileMode.Open, FileAccess.ReadWrite)] + [TestCase(FileMode.OpenOrCreate, FileAccess.Write)] + [TestCase(FileMode.Append, FileAccess.ReadWrite)] + public void MockFile_AfterOpen_WithWriteAccess_ShouldUpdateLastAccessAndLastWriteTime(FileMode fileMode, FileAccess fileAccess) + { + var creationTime = DateTime.Now.AddDays(10); + var updateTime = creationTime.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => creationTime); + fileSystem.File.WriteAllText("foo.txt", "abc"); + fileSystem.MockTime(() => updateTime); + fileSystem.File.Open("foo.txt", fileMode, fileAccess); + + var actualCreationTime = fileSystem.File.GetCreationTime("foo.txt"); + var actualLastAccessTime = fileSystem.File.GetLastAccessTime("foo.txt"); + var actualLastWriteTime = fileSystem.File.GetLastWriteTime("foo.txt"); + + Assert.That(actualCreationTime, Is.EqualTo(creationTime)); + Assert.That(actualLastAccessTime, Is.EqualTo(updateTime)); + Assert.That(actualLastWriteTime, Is.EqualTo(updateTime)); + } + + [TestCase(FileMode.Open, FileAccess.Read)] + [TestCase(FileMode.OpenOrCreate, FileAccess.Read)] + [TestCase(FileMode.Append, FileAccess.Read)] + public void MockFile_AfterOpen_WithReadOnlyAccess_ShouldUpdateLastAccessTime(FileMode fileMode, FileAccess fileAccess) + { + var creationTime = DateTime.Now.AddDays(10); + var updateTime = creationTime.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => creationTime); + fileSystem.File.WriteAllText("foo.txt", "abc"); + fileSystem.MockTime(() => updateTime); + fileSystem.File.Open("foo.txt", fileMode, fileAccess); + + var actualCreationTime = fileSystem.File.GetCreationTime("foo.txt"); + var actualLastAccessTime = fileSystem.File.GetLastAccessTime("foo.txt"); + var actualLastWriteTime = fileSystem.File.GetLastWriteTime("foo.txt"); + + Assert.That(actualCreationTime, Is.EqualTo(creationTime)); + Assert.That(actualLastAccessTime, Is.EqualTo(updateTime)); + Assert.That(actualLastWriteTime, Is.EqualTo(creationTime)); + } + + [Test] + public void MockFile_AfterReadAllBytes_ShouldUpdateLastAccessTime() + { + var creationTime = DateTime.Now.AddDays(10); + var updateTime = creationTime.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => creationTime); + fileSystem.File.WriteAllText("foo.txt", "abc"); + fileSystem.MockTime(() => updateTime); + fileSystem.File.ReadAllBytes("foo.txt"); + + var actualCreationTime = fileSystem.File.GetCreationTime("foo.txt"); + var actualLastAccessTime = fileSystem.File.GetLastAccessTime("foo.txt"); + var actualLastWriteTime = fileSystem.File.GetLastWriteTime("foo.txt"); + + Assert.That(actualCreationTime, Is.EqualTo(creationTime)); + Assert.That(actualLastAccessTime, Is.EqualTo(updateTime)); + Assert.That(actualLastWriteTime, Is.EqualTo(creationTime)); + } + + [Test] + public void MockFile_AfterReadAllLines_ShouldUpdateLastAccessTime() + { + var creationTime = DateTime.Now.AddDays(10); + var updateTime = creationTime.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => creationTime); + fileSystem.File.WriteAllText("foo.txt", "abc"); + fileSystem.MockTime(() => updateTime); + fileSystem.File.ReadAllLines("foo.txt"); + + var actualCreationTime = fileSystem.File.GetCreationTime("foo.txt"); + var actualLastAccessTime = fileSystem.File.GetLastAccessTime("foo.txt"); + var actualLastWriteTime = fileSystem.File.GetLastWriteTime("foo.txt"); + + Assert.That(actualCreationTime, Is.EqualTo(creationTime)); + Assert.That(actualLastAccessTime, Is.EqualTo(updateTime)); + Assert.That(actualLastWriteTime, Is.EqualTo(creationTime)); + } + + [Test] + public void MockFile_AfterReadAllText_ShouldUpdateLastAccessTime() + { + var creationTime = DateTime.Now.AddDays(10); + var updateTime = creationTime.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => creationTime); + fileSystem.File.WriteAllText("foo.txt", "abc"); + fileSystem.MockTime(() => updateTime); + fileSystem.File.ReadAllText("foo.txt"); + + var actualCreationTime = fileSystem.File.GetCreationTime("foo.txt"); + var actualLastAccessTime = fileSystem.File.GetLastAccessTime("foo.txt"); + var actualLastWriteTime = fileSystem.File.GetLastWriteTime("foo.txt"); + + Assert.That(actualCreationTime, Is.EqualTo(creationTime)); + Assert.That(actualLastAccessTime, Is.EqualTo(updateTime)); + Assert.That(actualLastWriteTime, Is.EqualTo(creationTime)); + } + + [Test] + public void MockFile_AfterSetAttributes_ShouldUpdateLastAccessTime() + { + var creationTime = DateTime.Now.AddDays(10); + var updateTime = creationTime.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => creationTime); + fileSystem.File.WriteAllText("foo.txt", "abc"); + fileSystem.MockTime(() => updateTime); + fileSystem.File.SetAttributes("foo.txt", FileAttributes.Hidden); + + var actualCreationTime = fileSystem.File.GetCreationTime("foo.txt"); + var actualLastAccessTime = fileSystem.File.GetLastAccessTime("foo.txt"); + var actualLastWriteTime = fileSystem.File.GetLastWriteTime("foo.txt"); + + Assert.That(actualCreationTime, Is.EqualTo(creationTime)); + Assert.That(actualLastAccessTime, Is.EqualTo(updateTime)); + Assert.That(actualLastWriteTime, Is.EqualTo(creationTime)); + } + + [Test] + [SupportedOSPlatform("windows")] + [WindowsOnly(WindowsSpecifics.AccessControlLists)] + public void MockFile_AfterSetAccessControl_ShouldUpdateLastAccessTime() + { + var creationTime = DateTime.Now.AddDays(10); + var updateTime = creationTime.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => creationTime); + fileSystem.File.WriteAllText("foo.txt", "abc"); + fileSystem.MockTime(() => updateTime); + fileSystem.File.SetAccessControl("foo.txt", new FileSecurity()); + + var actualCreationTime = fileSystem.File.GetCreationTime("foo.txt"); + var actualLastAccessTime = fileSystem.File.GetLastAccessTime("foo.txt"); + var actualLastWriteTime = fileSystem.File.GetLastWriteTime("foo.txt"); + + Assert.That(actualCreationTime, Is.EqualTo(creationTime)); + Assert.That(actualLastAccessTime, Is.EqualTo(updateTime)); + Assert.That(actualLastWriteTime, Is.EqualTo(creationTime)); + } + + [Test] + public void MockFile_AfterWriteAllBytes_ShouldUpdateLastAccessTime() + { + var creationTime = DateTime.Now.AddDays(10); + var updateTime = creationTime.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => creationTime); + fileSystem.File.WriteAllText("foo.txt", "abc"); + fileSystem.MockTime(() => updateTime); + fileSystem.File.WriteAllBytes("foo.txt", Encoding.UTF8.GetBytes("xyz")); + + var actualCreationTime = fileSystem.File.GetCreationTime("foo.txt"); + var actualLastAccessTime = fileSystem.File.GetLastAccessTime("foo.txt"); + var actualLastWriteTime = fileSystem.File.GetLastWriteTime("foo.txt"); + + Assert.That(actualCreationTime, Is.EqualTo(creationTime)); + Assert.That(actualLastAccessTime, Is.EqualTo(updateTime)); + Assert.That(actualLastWriteTime, Is.EqualTo(updateTime)); + } + + [Test] + public void MockFile_AfterWriteAllText_ShouldUpdateLastAccessTime() + { + var creationTime = DateTime.Now.AddDays(10); + var updateTime = creationTime.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => creationTime); + fileSystem.File.WriteAllText("foo.txt", "abc"); + fileSystem.MockTime(() => updateTime); + fileSystem.File.WriteAllText("foo.txt", "xyz"); + + var actualCreationTime = fileSystem.File.GetCreationTime("foo.txt"); + var actualLastAccessTime = fileSystem.File.GetLastAccessTime("foo.txt"); + var actualLastWriteTime = fileSystem.File.GetLastWriteTime("foo.txt"); + + Assert.That(actualCreationTime, Is.EqualTo(creationTime)); + Assert.That(actualLastAccessTime, Is.EqualTo(updateTime)); + Assert.That(actualLastWriteTime, Is.EqualTo(updateTime)); + } + + [Test] + public void MockFileStream_OpenRead_ShouldUpdateLastAccessTime() + { + var creationTime = DateTime.Now.AddDays(10); + var updateTime = creationTime.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => creationTime); + fileSystem.File.WriteAllText("foo.txt", "abc"); + fileSystem.MockTime(() => updateTime); + _ = fileSystem.File.OpenRead("foo.txt"); + + var actualCreationTime = fileSystem.File.GetCreationTime("foo.txt"); + var actualLastAccessTime = fileSystem.File.GetLastAccessTime("foo.txt"); + var actualLastWriteTime = fileSystem.File.GetLastWriteTime("foo.txt"); + + Assert.That(actualCreationTime, Is.EqualTo(creationTime)); + Assert.That(actualLastAccessTime, Is.EqualTo(updateTime)); + Assert.That(actualLastWriteTime, Is.EqualTo(creationTime)); + } + + [Test] + public void MockFileStream_OpenWrite_ShouldUpdateLastAccessAndLastWriteTime() + { + var creationTime = DateTime.Now.AddDays(10); + var updateTime = creationTime.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => creationTime); + fileSystem.File.WriteAllText("foo.txt", "abc"); + fileSystem.MockTime(() => updateTime); + _ = fileSystem.File.OpenWrite("foo.txt"); + + var actualCreationTime = fileSystem.File.GetCreationTime("foo.txt"); + var actualLastAccessTime = fileSystem.File.GetLastAccessTime("foo.txt"); + var actualLastWriteTime = fileSystem.File.GetLastWriteTime("foo.txt"); + + Assert.That(actualCreationTime, Is.EqualTo(creationTime)); + Assert.That(actualLastAccessTime, Is.EqualTo(updateTime)); + Assert.That(actualLastWriteTime, Is.EqualTo(updateTime)); + } + } +} diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetCreationTimeTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetCreationTimeTests.cs index 285948272..1d213924d 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetCreationTimeTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetCreationTimeTests.cs @@ -32,5 +32,19 @@ public void MockFile_GetCreationTime_ShouldReturnDefaultTimeIfFileDoesNotExist() // Assert Assert.AreEqual(new DateTime(1601, 01, 01, 00, 00, 00, DateTimeKind.Utc).ToLocalTime(), actualCreationTime); } + + [Test] + public void MockFile_GetCreationTime_ShouldBeSet() + { + var now = DateTime.Now.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => now); + fileSystem.File.WriteAllText("foo.txt", "xyz"); + + var result = fileSystem.File.GetCreationTime("foo.txt"); + + Assert.That(result.Kind, Is.EqualTo(DateTimeKind.Local)); + Assert.That(result, Is.EqualTo(now.ToLocalTime())); + } } } diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetCreationTimeUtcTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetCreationTimeUtcTests.cs index 98f80fd8f..30cb6e1d5 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetCreationTimeUtcTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetCreationTimeUtcTests.cs @@ -32,5 +32,19 @@ public void MockFile_GetCreationTimeUtc_ShouldReturnDefaultTimeIfFileDoesNotExis // Assert Assert.AreEqual(new DateTime(1601, 01, 01, 00, 00, 00, DateTimeKind.Utc), actualCreationTime); } + + [Test] + public void MockFile_GetCreationTimeUtc_ShouldBeSet() + { + var now = DateTime.Now.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => now); + fileSystem.File.WriteAllText("foo.txt", "xyz"); + + var result = fileSystem.File.GetCreationTimeUtc("foo.txt"); + + Assert.That(result.Kind, Is.EqualTo(DateTimeKind.Utc)); + Assert.That(result, Is.EqualTo(now.ToUniversalTime())); + } } } diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastAccessTimeTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastAccessTimeTests.cs index 8b74d23d8..fbf3ef09f 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastAccessTimeTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastAccessTimeTests.cs @@ -32,5 +32,19 @@ public void MockFile_GetLastAccessTime_ShouldReturnDefaultTimeIfFileDoesNotExist // Assert Assert.AreEqual(new DateTime(1601, 01, 01, 00, 00, 00, DateTimeKind.Utc).ToLocalTime(), actualLastAccessTime); } + + [Test] + public void MockFile_GetLastAccessTime_ShouldBeSet() + { + var now = DateTime.Now.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => now); + fileSystem.File.WriteAllText("foo.txt", "xyz"); + + var result = fileSystem.File.GetLastAccessTime("foo.txt"); + + Assert.That(result.Kind, Is.EqualTo(DateTimeKind.Local)); + Assert.That(result, Is.EqualTo(now.ToLocalTime())); + } } } diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastAccessTimeUtcTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastAccessTimeUtcTests.cs index b72ff9adf..c42bc6c44 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastAccessTimeUtcTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastAccessTimeUtcTests.cs @@ -32,5 +32,19 @@ public void MockFile_GetLastAccessTimeUtc_ShouldReturnDefaultTimeIfFileDoesNotEx // Assert Assert.AreEqual(new DateTime(1601, 01, 01, 00, 00, 00, DateTimeKind.Utc), actualLastAccessTime); } + + [Test] + public void MockFile_GetLastAccessTimeUtc_ShouldBeSet() + { + var now = DateTime.Now.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => now); + fileSystem.File.WriteAllText("foo.txt", "xyz"); + + var result = fileSystem.File.GetLastAccessTimeUtc("foo.txt"); + + Assert.That(result.Kind, Is.EqualTo(DateTimeKind.Utc)); + Assert.That(result, Is.EqualTo(now.ToUniversalTime())); + } } } diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastWriteTimeTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastWriteTimeTests.cs index c6e519583..5ceaf25f7 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastWriteTimeTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastWriteTimeTests.cs @@ -32,5 +32,19 @@ public void MockFile_GetLastWriteTime_ShouldReturnDefaultTimeIfFileDoesNotExist( // Assert Assert.AreEqual(new DateTime(1601, 01, 01, 00, 00, 00, DateTimeKind.Utc).ToLocalTime(), actualLastWriteTime); } + + [Test] + public void MockFile_GetLastWriteTime_ShouldBeSet() + { + var now = DateTime.Now.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => now); + fileSystem.File.WriteAllText("foo.txt", "xyz"); + + var result = fileSystem.File.GetLastWriteTime("foo.txt"); + + Assert.That(result.Kind, Is.EqualTo(DateTimeKind.Local)); + Assert.That(result, Is.EqualTo(now.ToLocalTime())); + } } } diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastWriteTimeUtcTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastWriteTimeUtcTests.cs index 5a14d41d9..4783e7e7f 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastWriteTimeUtcTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileGetLastWriteTimeUtcTests.cs @@ -31,5 +31,19 @@ public void MockFile_GetLastWriteTimeUtc_ShouldReturnDefaultTimeIfFileDoesNotExi // Assert Assert.AreEqual(new DateTime(1601, 01, 01, 00, 00, 00, DateTimeKind.Utc), actualLastWriteTime); } + + [Test] + public void MockFile_GetLastWriteTimeUtc_ShouldBeSet() + { + var now = DateTime.Now.AddDays(10); + var fileSystem = new MockFileSystem() + .MockTime(() => now); + fileSystem.File.WriteAllText("foo.txt", "xyz"); + + var result = fileSystem.File.GetLastWriteTimeUtc("foo.txt"); + + Assert.That(result.Kind, Is.EqualTo(DateTimeKind.Utc)); + Assert.That(result, Is.EqualTo(now.ToUniversalTime())); + } } } \ No newline at end of file