Skip to content

MockFileSystem created file don't exist #822

Description

@milten89

Describe the bug
From version 16.0.8 of System.IO.Abstractions.TestingHelpers when I create new file in MockFileSystem property Exist return false.

To Reproduce

IFileSystem fileSystem = new MockFileSystem();
IFileInfo fileInfo = fileSystem.FileInfo.FromFileName("testFile.txt");
using (Stream fs = fileInfo.Create()) 
{
    fs.WriteByte(0);
}
Console.WriteLine($"File exist: {fileInfo.Exists}");

Version 16.0.7 return True
Version 16.0.8 return False

Expected behavior
Property Exist should return True

Activity

  1. fgreinacher commented on Mar 11, 2022

    @fgreinacher
    Contributor

    @milten89 Thanks for taking the time to report this!

    The behavior of MockFileInfo has changed in #791 to mirror the behavior of the real file system (see #661).

    Could you please check how your code behaves with the real file system?

  2. milten89 commented on Mar 15, 2022

    @milten89
    Author

    @fgreinacher there is difference in output for FileSystem and MockFileSystem.

    public static void Main(string[] args)
    {
        IFileSystem realFileSystem = new FileSystem();
        IFileSystem mockFileSystem = new MockFileSystem();
        Console.WriteLine($"Real file exist: {TestFileSystem(realFileSystem)}");
        Console.WriteLine($"Fake file exist: {TestFileSystem(mockFileSystem)}");
        Console.ReadLine();
    }
    
    private static bool TestFileSystem(IFileSystem fileSystem)
    {
        IFileInfo fileInfo = null; 
        try
        {
            fileInfo = fileSystem.FileInfo.FromFileName("testFile.txt");
            using (Stream fs = fileInfo.Create()) 
                fs.WriteByte(0);
    
            return fileInfo.Exists;
        }
        finally
        {
            if (fileInfo != null && fileInfo.Exists)
                fileInfo.Delete();
        }
    }

    Starts from version 16.0.8 FileSystem return True and MockFileSystem return False.
    In latest version 16.1.23 result is same.

    Additional to that, there is difference in paths for FileSystem and MockFileSystem.

    • FileSystem:
    C:\Users\Andrzej Ochodek\source\repos\FileSystemAbstractionTest\FileSystemAbstractionTest\bin\Debug\testFile.txt
    
    • MockFileSystem:
    C:\testFile.txt
    
  3. milten89 commented on Mar 22, 2022

    @milten89
    Author

    Are there any updates about this bug?

  4. fgreinacher commented on Mar 22, 2022

    @fgreinacher
    Contributor

    Are there any updates about this bug?

    Sorry, I lost track of this one.

    Starts from version 16.0.8 FileSystem return True and MockFileSystem return False.
    In latest version 16.1.23 result is same.

    Do I understand correctly that everything is working as expected in 16.1.23?

    Additional to that, there is difference in paths for FileSystem and MockFileSystem.

    That's expected/by-design, because the MockFileSystem is initialized with the root directory (/ on Unix, C:\ on Windows).

  5. milten89 commented on Mar 22, 2022

    @milten89
    Author

    Do I understand correctly that everything is working as expected in 16.1.23?

    Unfortunately no. Real file system returns true and mock returns false.

    That's expected/by-design, because the MockFileSystem is initialized with the root directory (/ on Unix, C:\ on Windows).

    Ok.

  6. added a commit that references this issue on Mar 22, 2022
    b2cca2c
  7. siprbaum commented on Mar 22, 2022

    @siprbaum
    Contributor

    I created a pull request to fix it, see #828

  8. github-actions commented on Mar 23, 2022

    @github-actions

    This is addressed in release v16.1.24.

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

    state: needs discussionIssues that need further discussionstate: releasedIssues that are releasedtype: bugIssues that describe misbehaving functionality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions