Skip to content

IsReadOnly is true when file does not exist #942

Closed

Description

@brinkdinges

Describe the bug
When you get the FileInfo of a file that does not exist, IsReadOnly is true while the Exists property is false.

To Reproduce

FileSystem = new MockFileSystem();
var fileInfo = FileSystem.FileInfo.New("C:\\Folder\\Donuts.sldasm");
var isReadOnly = fileInfo.IsReadOnly;

Expected behavior
According to the docs, IsReadOnly should throw a FileNotFoundException when the file does not exist.

Additional context
I've looked through previous issues but could not find any mentioning this.

In my code, I was checking the FileInfo attributes to see if it was read-only. If a file does not exist, the attributes return -1, which, when you bitwise AND with any value, returns the positive value, apparently. Maybe that's happening internally as well.

image

Activity

  1. added
    state: ready to pickIssues that are ready for being worked on
    and removed on Feb 8, 2023
  2. fgreinacher commented on Feb 8, 2023

    @fgreinacher
    Contributor

    Thanks for reporting! PRs welcome 🤗

  3. brinkdinges commented on Feb 9, 2023

    @brinkdinges
    ContributorAuthor

    I created a console app in .NET 7 to let me compare System.IO with System.IO.Abstractions. It looks like the official docs are not the same as the current .NET code because IsReadOnly just returns true when the file does not exist:

    image

    When the file does exist:
    image

    So is this a .NET issue? That doesn't seem right 😄

  4. vbreuss commented on Feb 9, 2023

    @vbreuss
    Member

    @brinkdinges : I tested this issue in Testably/Testably.Abstractions#233:
    The tests run on Windows, MacOS and Linux and a wide range of .NET versions. In all circumstances IsReadOnly returns true when it does not exist.

    I agree with you, that it looks strange, but as we try to mimic the real file system, this is the intended behaviour...

  5. brinkdinges commented on Feb 9, 2023

    @brinkdinges
    ContributorAuthor

    Thanks for checking it out. When I reread the docs, they probably only throw a FileNotFoundException when setting the property, not when getting it. I've sent in feedback on that page.

  6. fgreinacher commented on Feb 14, 2023

    @fgreinacher
    Contributor

    Thanks for investigating. I think we can close this then. Feel free to reopen if I missed something.

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: testinghelpersIssues that address the testing helpersstate: ready to pickIssues that are ready for being worked ontype: 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