Skip to content

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

Open

Description

@grotzs

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

Activity

  1. added
    state: needs discussion
    type: bug
    on Oct 13, 2022
  2. fgreinacher commented on Oct 14, 2022

    @fgreinacher
    Contributor

    Thanks for reporting with a test case 👍

  3. added
    state: ready to pick
    area: testinghelpers
    and removed
    state: needs discussion
    on Oct 14, 2022
  4. CaringDev commented on Nov 24, 2026

    @CaringDev
    Contributor

    @vbreuss I take it this could be closed given your fix?

  5. vbreuss commented on Nov 24, 2026

    @vbreuss
    Member

    @CaringDev : It fixes this issue in Testably.Abstractions, not here in System.IO.Abstractions (for more background on these two libraries, see here).

  6. CaringDev commented on Nov 24, 2026

    @CaringDev
    Contributor

    @vbreuss 🙈 I haven't had enough coffee yet
    Would you still want to tackle that here / a PR, given "active development and new features are primarily focused on the Testably.Abstractions project"?
    If so, what should the Linux vs. Windows story look like?

  7. vbreuss commented on Nov 24, 2026

    @vbreuss
    Member

    I personally do not invest much effort in this library anymore. I keep it alive, but my focus is on Testably.Abstractions. But I am open for PRs and will review them, merge them and create releases.

  8. CaringDev commented on Nov 24, 2026

    @CaringDev
    Contributor

    Given the scarce resources I do not even want to waste yours for PR reviews here... at least not more than I already did with my comments 🙈
    We'll switch to the Testably stack completely (partial migration is already done) and I'll contribute there if need be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testinghelpersstate: ready to picktype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions