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

fix: Make mocked data lazy-loaded #900

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <inheritdoc />
public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path) : base(mockFileSystem?.FileSystem)
Expand All @@ -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();
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,18 +794,21 @@ public void MockFileInfo_Replace_ShouldThrowIfDestinationFileDoesNotExist()
}

[Test]
public void MockFileInfo_Exists_ShouldReturnCachedData()
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.IsFalse(fileInfo.Exists);
Assert.IsTrue(fileInfo.Exists);
Assert.IsFalse(cachedFileInfo.Exists, "Cached MockFileInfo should still return the cached value");
}

[Test]
Expand All @@ -815,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(); // circumvent the lazy loading in Exists on first access by forcing a load

// Act
fileSystem.AddFile(path1, new MockFileData("1"));
Expand All @@ -827,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(); // circumvent the lazy loading in Exists on first access by forcing a load

// Act
fileInfo.Create().Dispose();
Expand All @@ -842,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(); // circumvent the lazy loading in Exists on first access by forcing a load

// Act
fileInfo.CreateText().Dispose();
Expand All @@ -856,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(); // circumvent the lazy loading in Exists on first access by forcing a load

// Act
fileInfo.Delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_FileInfoExists_ShouldBeTrueIfAFileWasAddedBeforeMockFileDataWasLazyLoaded()
{
// 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()
{
Expand Down