Closed ian-h-chamberlain closed 6 years ago
Thanks for the report! This is indeed a technical limitation - EXAMPLE_FILE
is initialized with the real pathlib
and stays that way in the test. One possibility to work around this is to add the module where EXAMPLE_FILE
is defined to modules_to_reload
, but this is not directly possible if using the fs
fixture (see the note in the link).
@ian-h-chamberlain - I added an example that shows how to work around your specific problem using a customized fixture.
@mrbean-bremen thanks for the quick response! In my case I was able to work around it by instantiating another pathlib.Path
in the code under test, e.g.
fs.create_file(EXAMPLE_FILE, contents='stuff here')
with pathlib.Path(EXAMPLE_FILE).open() as file:
assert file.read() == 'stuff here' # this succeeds now
In the future I may decide to use a fixture as in your example. This issue can probably be closed now.
Glad that you found a way to get it working!
After declaring a
pathlib.Path
at module scope, then the pytestfs
fixture tocreate_file
for that path, subsequent calls topathlib.Path.open
fail with "No such file or directory". A quick look atpathlib.Path.open
shows that it usesio.open
internally.I'm not sure if there's a straightforward way to patch methods of an object declared at module scope, so maybe this is more of a technical limitation than a bug, but I figured it was worth reporting anyway.
master
Minimal example
Output: