You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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.
Activity
tathamoddie commentedon May 26, 2017
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! 😄
erenes commentedon Feb 19, 2019
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:
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 :)
fgreinacher commentedon Mar 10, 2019
This is released with https://github.com/System-IO-Abstractions/System.IO.Abstractions/releases/tag/v3.1.1