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 1 commit
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 @@ -793,21 +793,6 @@ public void MockFileInfo_Replace_ShouldThrowIfDestinationFileDoesNotExist()
Assert.Throws<FileNotFoundException>(() => 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);
}

hangy marked this conversation as resolved.
Show resolved Hide resolved
[Test]
public void MockFileInfo_Exists_ShouldUpdateCachedDataOnRefresh()
hangy marked this conversation as resolved.
Show resolved Hide resolved
{
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_AddFile_ShouldMarkFileAsExisting()
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);

// Act
fileSystem.AddFile(path1, new MockFileData("1"));

// Assert
Assert.IsTrue(fileInfo.Exists);
}

[Test]
public void MockFileSystem_AddFileFromEmbeddedResource_ShouldAddTheFile()
{
Expand Down