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 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -793,13 +793,32 @@ public void MockFileInfo_Replace_ShouldThrowIfDestinationFileDoesNotExist()
Assert.Throws<FileNotFoundException>(() => 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()
hangy marked this conversation as resolved.
Show resolved Hide resolved
{
// Arrange
var fileSystem = new MockFileSystem();
var path1 = XFS.Path(@"c:\temp\file1.txt");
var fileInfo = fileSystem.FileInfo.FromFileName(path1);
fileInfo.Refresh();
hangy marked this conversation as resolved.
Show resolved Hide resolved

// Act
fileSystem.AddFile(path1, new MockFileData("1"));
Expand All @@ -812,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();
hangy marked this conversation as resolved.
Show resolved Hide resolved

// Act
fileInfo.Create().Dispose();
Expand All @@ -827,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();
hangy marked this conversation as resolved.
Show resolved Hide resolved

// Act
fileInfo.CreateText().Dispose();
Expand All @@ -841,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();
hangy marked this conversation as resolved.
Show resolved Hide resolved

// Act
fileInfo.Delete();
Expand Down
Expand Up @@ -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();
Expand Down