Skip to content
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

Add side_effect option to fake files #433

Conversation

simonfagerholm
Copy link

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:

  1. When writing an int XX to "/sys/class/gpio/export" a new directory is created "/sys/class/gpio/gpioXX" with some files
  2. when writing an int XX to "/sys/class/gpio/unexport" the directory "/sys/class/gpio/gpioXX" is removed.

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.

Simon Fagerholm added 3 commits September 25, 2018 14:44
side_effect is a function handle that takes the file object as an argument and does some side effect when the file is written.
Use case is, among others, writing to the sysfs gpio export file and expecting gpios to "pop up".
Copy link
Member

@mrbean-bremen mrbean-bremen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that - looks good to me!
@jmcgeheeiv - can you have a look and merge if it is ok?

@mrbean-bremen mrbean-bremen merged commit 1482160 into pytest-dev:master Oct 8, 2018
@jmcgeheeiv
Copy link
Contributor

Thank you for this, @simonfagerholm. This expands pyfakefs in a whole new direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants