Skip to content

Ability to lock a MockFile for testing #226

Closed

Description

@gregmcguffey

It would be really helpful to mark a MockFile as being locked, so locked file logic can easily be tested. This would related to commands like File.Delete, File.WriteAll*, ect. Or is this possible now?

Activity

  1. tathamoddie commented on May 26, 2017

    @tathamoddie
    Contributor

    This isn't currently implemented in the testing helpers.

    The fake file system doesn't track if files have been opened exclusively or not:

    https://github.com/System-IO-Abstractions/System.IO.Abstractions/blob/master/System.IO.Abstractions.TestingHelpers/MockFile.cs#L361

    Sounds like an interesting pull request though if you care to start coding something up! 😄

  2. erenes commented on Feb 19, 2019

    @erenes
    Contributor

    Hi guys, one of my customers reported an issue with file locking, so I am implementing it in my forked repo. Currently my approach is to add a FileShare property to the Mockfile that is checked when opening a stream or using one of the File methods.

    I considered implementing it with a whole dictionary of streams that are using different FileAccess/FileShare parameters, but for my scenario that seems like a lot of wasted effort.

    To illustrate, this is one of the tests:

        [Test]
        public void MockFile_Lock_FileShareNoneThrowsOnOpenRead()
        {
            string filepath = XFS.Path(@"c:\something\does\exist.txt");
            var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>
            {
                { filepath, new MockFileData("I'm here") { CurrentFileShare = FileShare.None }}
            });
    
            Assert.Throws(typeof(IOException), () => filesystem.File.OpenRead(filepath));
        }
    

    I am not entirely satisfied about the name "CurrentFileShare". Compared to writing a full File System simulation, I do think this way of marking locked files is easier to implement both in System.IO.Abstractions, as well as when writing unit tests.

    Any feedback / thoughts is welcome :)

  3. fgreinacher commented on Mar 10, 2019

    @fgreinacher
    Contributor
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: ready to pickIssues that are ready for being worked ontype: enhancementIssues that propose new functionality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions