Add side_effect option to fake files #433
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background:
On linux, parts of the file system have other uses than being actual files. One example is "/sys/class/gpio".
To mock the expected behaviour in this example a "side effect" of writing to the files is needed:
This "side effect" needs to happen in the same way during testing as in real environment to not affect the SUT. For example, if the folder "/sys/class/gpio/gpioXX" is created in the setup of the test the SUT can see that gpioXX is already available and skip writing to "/sys/class/gpio/export".
Another solution could have been polling the files if export has been written to, however this problems as the polling must be much faster than the SUT the be able to cause the side_effect to simulate the real environment.
Pull request:
The proposed solution in this pull request is to add a side_effect argument to fake files which can contain a function handle that is invoked, with the file object as argument, after the file is written. This function then causes the "side effects" needed.
The default is to not have any "side effect", preserving legacy.
I also added 2 unittests for the new functionality.