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

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

Open
grotzs opened this issue Oct 13, 2022 · 1 comment
Open

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

grotzs opened this issue Oct 13, 2022 · 1 comment
Labels
area: testinghelpers Issues that address the testing helpers state: ready to pick Issues that are ready for being worked on type: bug Issues that describe misbehaving functionality

Comments

@grotzs
Copy link

grotzs commented Oct 13, 2022

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 grotzs added state: needs discussion Issues that need further discussion type: bug Issues that describe misbehaving functionality labels Oct 13, 2022
@fgreinacher
Copy link
Contributor

Thanks for reporting with a test case 👍

@fgreinacher fgreinacher added state: ready to pick Issues that are ready for being worked on area: testinghelpers Issues that address the testing helpers and removed state: needs discussion Issues that need further discussion labels Oct 14, 2022
vbreuss added a commit to Testably/Testably.Abstractions that referenced this issue Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: testinghelpers Issues that address the testing helpers state: ready to pick Issues that are ready for being worked on type: bug Issues that describe misbehaving functionality
Projects
None yet
Development

No branches or pull requests

2 participants