Skip to content

Commit

Permalink
fix: improve cache invalidation in MockDirectoryInfo (#862)
Browse files Browse the repository at this point in the history
When invoking the `Create()` and `Delete()` methods on
`MockDirectoryInfo`, the `Exists` property should intrinsically invoke
`Refresh()` the next time it is called. This is necessary to prevent
returning stale state about the directory after those operations.

Additional test cases have been added for `MoveTo()` as well, to ensure
it also not returning stale state.

For context, this change was motivated by PR #828 where similar issues
were solved in `MockFileInfo`.

Co-authored-by: Florian Greinacher <florian@greinacher.de>
  • Loading branch information
rcdailey and fgreinacher committed Jul 19, 2022
1 parent fc2d041 commit 96f9886
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string dire
public override void Delete()
{
mockFileDataAccessor.Directory.Delete(directoryPath);
refreshOnNextRead = true;
}

/// <inheritdoc />
Expand Down Expand Up @@ -160,12 +161,14 @@ public override string Name
public override void Create()
{
mockFileDataAccessor.Directory.CreateDirectory(FullName);
refreshOnNextRead = true;
}

/// <inheritdoc />
public override void Create(DirectorySecurity directorySecurity)
{
mockFileDataAccessor.Directory.CreateDirectory(FullName, directorySecurity);
refreshOnNextRead = true;
}

/// <inheritdoc />
Expand All @@ -178,6 +181,7 @@ public override IDirectoryInfo CreateSubdirectory(string path)
public override void Delete(bool recursive)
{
mockFileDataAccessor.Directory.Delete(directoryPath, recursive);
refreshOnNextRead = true;
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using NUnit.Framework;

namespace System.IO.Abstractions.TestingHelpers.Tests
Expand Down Expand Up @@ -444,6 +445,76 @@ public void MockDirectoryInfo_Exists_ShouldUpdateCachedDataOnRefresh()
Assert.IsTrue(directoryInfo.Exists);
}

[Test]
public void Directory_exists_after_creation()
{
// Arrange
var fileSystem = new MockFileSystem();
var directoryInfo = fileSystem.DirectoryInfo.FromDirectoryName(XFS.Path(@"c:\abc"));

// Act
directoryInfo.Create();

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

[Test, WindowsOnly(WindowsSpecifics.AccessControlLists)]
public void Directory_exists_after_creation_with_security()
{
// Arrange
var fileSystem = new MockFileSystem();
var directoryInfo = fileSystem.DirectoryInfo.FromDirectoryName(XFS.Path(@"c:\abc"));

// Act
directoryInfo.Create(new DirectorySecurity());

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

[Test]
public void Directory_does_not_exist_after_delete()
{
// Arrange
var fileSystem = new MockFileSystem();
var directoryInfo = fileSystem.Directory.CreateDirectory(XFS.Path(@"c:\abc"));

// Act
directoryInfo.Delete();

// Assert
Assert.IsFalse(directoryInfo.Exists);
}

[Test]
public void Directory_does_not_exist_after_recursive_delete()
{
// Arrange
var fileSystem = new MockFileSystem();
var directoryInfo = fileSystem.Directory.CreateDirectory(XFS.Path(@"c:\abc"));

// Act
directoryInfo.Delete(true);

// Assert
Assert.IsFalse(directoryInfo.Exists);
}

[Test]
public void Directory_still_exists_after_move()
{
// Arrange
var fileSystem = new MockFileSystem();
var directoryInfo = fileSystem.Directory.CreateDirectory(XFS.Path(@"c:\abc"));

// Act
directoryInfo.MoveTo(XFS.Path(@"c:\abc2"));

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

[Test]
public void MockDirectoryInfo_LastAccessTime_ShouldReflectChangedValue()
{
Expand Down Expand Up @@ -584,4 +655,4 @@ public void MockDirectoryInfo_LastWriteTimeUtc_SetterShouldThrowDirectoryNotFoun
}

}
}
}

0 comments on commit 96f9886

Please sign in to comment.