From ae24de9c3496a60e3a566ac136f3da3d427b7855 Mon Sep 17 00:00:00 2001 From: hangy Date: Wed, 26 Oct 2022 01:31:33 +0200 Subject: [PATCH 1/5] chg: Make mocked data lazy-loaded --- .../MockFileInfo.cs | 3 +-- .../MockFileInfoTests.cs | 15 --------------- .../MockFileSystemTests.cs | 15 +++++++++++++++ 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs b/src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs index 96096a95d..93314ff0f 100644 --- a/src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs +++ b/src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs @@ -12,7 +12,7 @@ public class MockFileInfo : FileInfoBase private readonly string originalPath; private MockFileData cachedMockFileData; private MockFile mockFile; - private bool refreshOnNextRead; + private bool refreshOnNextRead = true; /// public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path) : base(mockFileSystem?.FileSystem) @@ -21,7 +21,6 @@ public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path) : base(mo this.originalPath = path ?? throw new ArgumentNullException(nameof(path)); this.path = mockFileSystem.Path.GetFullPath(path); this.mockFile = new MockFile(mockFileSystem); - Refresh(); } /// diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs index 2c413ef57..e52ea1da6 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs @@ -793,21 +793,6 @@ public void MockFileInfo_Replace_ShouldThrowIfDestinationFileDoesNotExist() Assert.Throws(() => fileInfo.Replace(path2, null)); } - [Test] - public void MockFileInfo_Exists_ShouldReturnCachedData() - { - // Arrange - var fileSystem = new MockFileSystem(); - var path1 = XFS.Path(@"c:\temp\file1.txt"); - var fileInfo = fileSystem.FileInfo.FromFileName(path1); - - // Act - fileSystem.AddFile(path1, new MockFileData("1")); - - // Assert - Assert.IsFalse(fileInfo.Exists); - } - [Test] public void MockFileInfo_Exists_ShouldUpdateCachedDataOnRefresh() { diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs index ac9216651..0c65ac64b 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs @@ -230,6 +230,21 @@ public void MockFileSystem_AddFile_ShouldMatchCapitalization_PartialMatch_Furthe Assert.Contains(XFS.Path(@"C:\LOUD\SUBLOUD\new\SUBDirectory"), fileSystem.AllDirectories.ToList()); } + [Test] + public void MockFileSystem_AddFile_ShouldMarkFileAsExisting() + { + // Arrange + var fileSystem = new MockFileSystem(); + var path1 = XFS.Path(@"c:\temp\file1.txt"); + var fileInfo = fileSystem.FileInfo.FromFileName(path1); + + // Act + fileSystem.AddFile(path1, new MockFileData("1")); + + // Assert + Assert.IsTrue(fileInfo.Exists); + } + [Test] public void MockFileSystem_AddFileFromEmbeddedResource_ShouldAddTheFile() { From 6111a9af4d5d048f66581da07d0888f36a5fc581 Mon Sep 17 00:00:00 2001 From: hangy Date: Wed, 26 Oct 2022 18:10:56 +0200 Subject: [PATCH 2/5] test: Re-add MockFileInfo_Exists_* test Test is renamed from MockFileInfo_Exists_ShouldReturnCachedData to MockFileInfo_Exists_LazyLoadsData, and now checks that the lazy-loading in MockFileInfo works correctly. Co-authored-by: Peter Baumann --- .../MockFileInfoTests.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs index e52ea1da6..78939737a 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs @@ -793,6 +793,24 @@ public void MockFileInfo_Replace_ShouldThrowIfDestinationFileDoesNotExist() Assert.Throws(() => fileInfo.Replace(path2, null)); } + [Test] + public void MockFileInfo_Exists_LazyLoadsData() + { + // Arrange + var fileSystem = new MockFileSystem(); + var path1 = XFS.Path(@"c:\temp\file1.txt"); + var fileInfo = fileSystem.FileInfo.FromFileName(path1); + var cachedFileInfo = fileSystem.FileInfo.FromFileName(path1); + _ = cachedFileInfo.Exists; // this forces a lazyload of the data. + + // Act + fileSystem.AddFile(path1, new MockFileData("1")); + + // Assert + Assert.IsTrue(fileInfo.Exists); + Assert.IsFalse(cachedFileInfo.Exists, "Cached MockFileInfo should still return the cached value"); + } + [Test] public void MockFileInfo_Exists_ShouldUpdateCachedDataOnRefresh() { From 4069e7121063596656322681c6b97b668d9e9165 Mon Sep 17 00:00:00 2001 From: hangy Date: Wed, 26 Oct 2022 18:21:39 +0200 Subject: [PATCH 3/5] test: Rename test to reflect that it is actually about lazy-loading --- .../MockFileSystemTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs index 0c65ac64b..15f144075 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs @@ -231,7 +231,7 @@ public void MockFileSystem_AddFile_ShouldMatchCapitalization_PartialMatch_Furthe } [Test] - public void MockFileSystem_AddFile_ShouldMarkFileAsExisting() + public void MockFileSystem_FileInfoExists_ShouldBeTrueIfAFileWasAddedBeforeMockFileDataWasLazyLoaded() { // Arrange var fileSystem = new MockFileSystem(); From 2b284356fba815127fe9029ecab62fef74148f7b Mon Sep 17 00:00:00 2001 From: hangy Date: Wed, 26 Oct 2022 18:23:39 +0200 Subject: [PATCH 4/5] fix(test): Call `Refresh()` in the Arrange phase `Refresh()` needs to be called in the Arrange phase, so that the data is not simply lazy-loaded in the assertion case. Explicitly loading MockFileData by calling `Refres()` enforces lazy-loading to be done. Co-authored-by: Peter Baumann --- .../MockFileInfoTests.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs index 78939737a..6f18a9bbe 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs @@ -818,6 +818,7 @@ public void MockFileInfo_Exists_ShouldUpdateCachedDataOnRefresh() var fileSystem = new MockFileSystem(); var path1 = XFS.Path(@"c:\temp\file1.txt"); var fileInfo = fileSystem.FileInfo.FromFileName(path1); + fileInfo.Refresh(); // Act fileSystem.AddFile(path1, new MockFileData("1")); @@ -830,9 +831,11 @@ public void MockFileInfo_Exists_ShouldUpdateCachedDataOnRefresh() [Test] public void MockFileInfo_Create_ShouldUpdateCachedDataAndReturnTrueForExists() { + // Arrange IFileSystem fileSystem = new MockFileSystem(); var path = XFS.Path(@"c:\temp\file1.txt"); IFileInfo fileInfo = fileSystem.FileInfo.FromFileName(path); + fileInfo.Refresh(); // Act fileInfo.Create().Dispose(); @@ -845,9 +848,11 @@ public void MockFileInfo_Create_ShouldUpdateCachedDataAndReturnTrueForExists() [Test] public void MockFileInfo_CreateText_ShouldUpdateCachedDataAndReturnTrueForExists() { + // Arrange IFileSystem fileSystem = new MockFileSystem(); var path = XFS.Path(@"c:\temp\file1.txt"); IFileInfo fileInfo = fileSystem.FileInfo.FromFileName(path); + fileInfo.Refresh(); // Act fileInfo.CreateText().Dispose(); @@ -859,9 +864,11 @@ public void MockFileInfo_CreateText_ShouldUpdateCachedDataAndReturnTrueForExists [Test] public void MockFileInfo_Delete_ShouldUpdateCachedDataAndReturnFalseForExists() { + // Arrange var fileSystem = new MockFileSystem(); var path = XFS.Path(@"c:\temp\file1.txt"); IFileInfo fileInfo = fileSystem.FileInfo.FromFileName(path); + fileInfo.Refresh(); // Act fileInfo.Delete(); From e769dfcd65b01898ab28db4948a4f1e7f3894098 Mon Sep 17 00:00:00 2001 From: hangy Date: Wed, 26 Oct 2022 20:38:08 +0200 Subject: [PATCH 5/5] Apply suggestions from code review Co-authored-by: Peter Baumann --- .../MockFileInfoTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs index 6f18a9bbe..e1d969bc8 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs @@ -818,7 +818,7 @@ public void MockFileInfo_Exists_ShouldUpdateCachedDataOnRefresh() var fileSystem = new MockFileSystem(); var path1 = XFS.Path(@"c:\temp\file1.txt"); var fileInfo = fileSystem.FileInfo.FromFileName(path1); - fileInfo.Refresh(); + fileInfo.Refresh(); // circumvent the lazy loading in Exists on first access by forcing a load // Act fileSystem.AddFile(path1, new MockFileData("1")); @@ -835,7 +835,7 @@ public void MockFileInfo_Create_ShouldUpdateCachedDataAndReturnTrueForExists() IFileSystem fileSystem = new MockFileSystem(); var path = XFS.Path(@"c:\temp\file1.txt"); IFileInfo fileInfo = fileSystem.FileInfo.FromFileName(path); - fileInfo.Refresh(); + fileInfo.Refresh(); // circumvent the lazy loading in Exists on first access by forcing a load // Act fileInfo.Create().Dispose(); @@ -852,7 +852,7 @@ public void MockFileInfo_CreateText_ShouldUpdateCachedDataAndReturnTrueForExists IFileSystem fileSystem = new MockFileSystem(); var path = XFS.Path(@"c:\temp\file1.txt"); IFileInfo fileInfo = fileSystem.FileInfo.FromFileName(path); - fileInfo.Refresh(); + fileInfo.Refresh(); // circumvent the lazy loading in Exists on first access by forcing a load // Act fileInfo.CreateText().Dispose(); @@ -868,7 +868,7 @@ public void MockFileInfo_Delete_ShouldUpdateCachedDataAndReturnFalseForExists() var fileSystem = new MockFileSystem(); var path = XFS.Path(@"c:\temp\file1.txt"); IFileInfo fileInfo = fileSystem.FileInfo.FromFileName(path); - fileInfo.Refresh(); + fileInfo.Refresh(); // circumvent the lazy loading in Exists on first access by forcing a load // Act fileInfo.Delete();