-
Notifications
You must be signed in to change notification settings - Fork 256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Support event callbacks and custom exceptions in MockFileSystem #883
feat: Support event callbacks and custom exceptions in MockFileSystem #883
Conversation
…es, so that also file access can be triggered
This draft is to gather feedback about the suggested approach in general. |
I like the general direction here 👍 Any reason to not expose this as regular events, as e.g. suggested here?
Maybe we could use the *ing/*ed scheme e.g. ( |
It probably boils down to personal preference :-)
I think this is a good idea, but I am not sure about the naming. What are your thoughts on this? Do you think the use cases are more specific and I should add multiple different events? |
Ah sorry @vbreuss, I got derailed by other topics 🙇 The main problem I see with the callback approach is that it's not easily possible to unregister the handlers. I think we should for something like this: var fs = new MockFileSystem();
fs.Events.DirectoryCreating += (sender, args) => {}
fs.Events.FileCreated += (sender, args) => {}
... Yes, it's a bit verbose if you want to register the same handler to different events. But on the other hand it's quite intuitive and future-proof (no breaking change when adding new events). WDYT? |
@fgreinacher : Unfortunately, I won't be able to continue working on this pull request! There I already implemented notification handling as follows: var fileSystem = new FileSystemMock();
fileSystem.Intercept.Creating(FileSystemTypes.Directory, _ => throw new Exception("custom exception"));
fileSystem.Notify.OnCreated(FileSystemTypes.Directory, c => DirectoryWasCreated(c.Path)); Maybe a similar approach would also work for you? |
@vbreuss All good, fully understood! I'm maintaining this at super low capacity and want to keep changes as non-invasive as possible. I personally also like the fluent syntax a lot, but none of the existing APIs use it, and if in doubt I favor consistency over anything else. I like your library a lot! I'm wondering whether it would make sense if you implement the System.IO.Abstractions interfaces instead of duplicating them. In that case I'd be happy to link your lib from the README here, as an alternative, way of mocking things. Let me know what you think 🙇 |
@fgreinacher :
All in all I really like the idea, but my goal with Testably.Abstractions is for a broader approach to unit testing helpers and I don't see, how we could make these two libraries 100% compatible :-( Do you have an idea how to overcome these challenges? |
I would have no problem changing that so that the interface are separate.
I tried to mirror what .NET does and I think on modern versions we don’t actually need that reference. Or at least it is a no-op.
As said the PR I like this, just a matter of getting it merged IMO 😀
Agreed, this is not something I’d like to have in SIOA. But is this really a problem at the Interfaxe level?
I would have no problem with changing this. Yes, it’s breaking, but easy to adapt.
Yes, new things need to go with the All in all I’d say nothing really blocking. Let me know what you think @vbreuss |
Would this be possible? I think you can't create new projects in the And I fear this would become quite confusing, especially in my library:
If I were to split my interface DLL, so that one only contains the file-related interfaces (e.g.
In modern versions it was extracted to extension methods. I implemented the same behaviour in Testably.Abstractions.AccessControl. For this to work, I had to implement the IFileSystemExtensionContainer interface, but this would be quite a trivial change in "System.IO.Abstractions", so no blocker...
Agreed :-)
Agreed, as said above, I think I could split my interface to only contain the file-related interfaces. However the |
True, we cannot upload new NuGet packages with the I think a simple way would be keep the namespaces as-is and change the NuGet package structure a bit:
You could then reference just |
I'm open to other solutions as long as the breaking change is minimal. From a gut feeling I don't think it's worth the refactoring effort though - I don't see the big problem with an extra reference, and up to now nobody complained. |
Agreed that it helps with the implementation, but I'd like to keep it out of the interfaces and implementation here. But that should be fine, right? |
@fgreinacher : |
Remove the ACL-Features from the interfaces and instead implement them as extension methods implemented in "TestableIO.System.IO.Abstractions.Wrappers". This removes the dependency on `System.IO.FileSystem.AccessControl` from the interface project. This is part of the approach discussed in #883 BREAKING CHANGE: This refactoring removes ACL-related methods from the interface project and replaces them with extension methods in `TestableIO.System.IO.Abstractions.Wrappers`.
I now released v2.0.0 which references the interfaces from TestableIO.System.IO.Abstractions (see Testably/Testably.Abstractions#225) and also mentioned the System.IO.Abstractions in the Readme.md... Would it make sense to merge the two projects further together? |
Looks great @vbreuss, I think we're good then. I'll prepare a small PR to mention the Testably library. |
Observing Changes
Implements #805
Allows registering a callback to be executed whenever a file or directory event is triggered in the file system:
The callback parameter provides the path of the file or directory and the event type.
Throw custom exceptions
Implements #877
Additionally the callback allows throwing a custom exception. This callback is executed before the actual change in the
MockFileSystem
, thus allowing simulating e.g.OutOfMemoryException
or other edge cases...