System.IO.Abstractions icon indicating copy to clipboard operation
System.IO.Abstractions copied to clipboard

IFileInfo.OpenRead() does not lock file when using MockFileSystem

Open grotzs opened this issue 3 years ago • 1 comments

Describe the bug If you want to test if a file handle is released correctly after calling IFileInfo.OpenRead() the test does not fail when using MockFileSystem.

To Reproduce Steps to reproduce the behavior:

  1. Run the following Test and see that it should fail but greens
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using FluentAssertions;
using NUnit.Framework;

namespace InstallerServiceTests.Package;

public class TestClass
{
  [Test]
  public void TestThatStreamIsDisposedAfterOpenRead()
  {
    // Given
    var mockFileSystem = new MockFileSystem();
    var fileInfo = mockFileSystem.FileInfo.FromFileName("aPath.txt");
    mockFileSystem.AddFile(fileInfo.FullName,
                           "");
    ClassUnderTest.MethodUnderTest(fileInfo);
    // When
    var action = () => fileInfo.Delete(); // write access on the still opened file
    // Then
    action.Should()
          .NotThrow();
  }
}

public static class ClassUnderTest
{
  public static void MethodUnderTest(IFileInfo fileInfo)
  {
    var stream = fileInfo.OpenRead();
    // stream is not disposed as it should be correctly
  }
}
  1. Change the MockFileSystem to the real FileSystem and you will see that the test fails correctly

Expected behavior MockFileSystem should behave like the real FileSystem so that the test fails

Additional context I am working on Windows 10

Versions: System.IO.Abstractions = 17.2.3

grotzs avatar Oct 13 '22 08:10 grotzs

Thanks for reporting with a test case 👍

fgreinacher avatar Oct 14 '22 12:10 fgreinacher