-
Notifications
You must be signed in to change notification settings - Fork 91
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
Patcher.tearDown() issue with pathlib #518
Comments
Good point. This is a general problem for modules loaded during patching, that is not specific to from pyfakefs.fake_filesystem_unittest import Patcher
import shutil
TMP_DIR = '/tmp/delmedir/somedir'
shutil.rmtree(TMP_DIR, ignore_errors=True)
with Patcher() as patcher:
import os
# We should not be patched here
os.makedirs(TMP_DIR, exist_ok=True)
new_file = os.path.join(TMP_DIR, 'new-file')
file_obj = open(new_file, mode='w') This never came up until now (the use case is not very common) - I will check how to resolve this, thanks for another bug report! |
The obvious workaround is to import the module ( |
Thanks for the quick reply, but I'm having troubles using this workaround with pytest.
Do you know what am I missing here? |
I have a bit trouble to reproduce your scenario. In the simple example you have shown the problem goes away if The best would be to fix the problem, of course, but I had no good idea how to handle this yet, and due to time constraints this may take some time, so it would be nice to have a workaround that actually works for the time being... |
Okay, so it turns out the problem is not directly pytest related. We have in the code a kind of global class with a pathlib.Path initialized once. from pyfakefs.fake_filesystem_unittest import Patcher
import os
import shutil
# Use this directory as a showcase, remove it before testing
TMP_DIR = '/tmp/delmedir/somedir'
shutil.rmtree(TMP_DIR, ignore_errors=True)
import pathlib # not imported while patched
class PathResolver:
def __init__(self):
self.p = pathlib.Path(TMP_DIR)
def get_cache_dir(self, sub_dir):
ret = self.p.joinpath(sub_dir)
ret.mkdir(parents=True, exist_ok=True)
return ret
# Patch setUp and teadDown
with Patcher():
path_resolver = PathResolver()
new_dir = path_resolver.get_cache_dir('my-plugin')
print(new_dir._accessor.mkdir) # Note this is FakeFilesystem.makedir
new_file = os.path.join(str(new_dir), 'new-file')
file_obj = open(new_file, mode='w') Without the Patcher this works fine. |
Ok, thanks - I will have a closer look tonight. |
Ok, I understand. I'm afraid that this will not work as is. The problem is that What you try is to use an object created in the fake filesystem in the real file system. This will not work with any such object, be it a path, a file object, or a function pointer. You have to decide if the tests using the resolver run in the real or the fake file system. For example, you could map a part of the real file system into the fake file system, if you are able to run the whole test there, though without knowing your concrete use case it is difficult to say. |
I see. |
I will see what I can do with these issues. I already had a go, and they turned out to be a bit more tricky than expected. I don't have much free time at the moment, so it may take a bit... |
After thinking a bit more about this, I do not consider this a bug - objects created in a fake environment cannot be used outside that environment due to side effects (e.g. fake objects assigned and processed). There is nothing we can do here - closing the issue. Thanks anyway for the report that helped to understand the problem, and the other bug reports! |
Describe the bug
If pathlib is imported between Patcher.setUp() and Patcher.tearDown() for the first time then
Patcher.tearDown() does not cleanup pathlib static accessors correctly.
How To Reproduce
Basically I'm using the 'fs' fixture plugin for pytest. I have tests which use pyfakefs and then some tests which don't use it. A simplified example of the problem decoupled from pytest is:
Your enviroment
Please run the following and paste the output.
Linux-4.15.0-76-generic-x86_64-with-debian-buster-sid
Python 3.7.3 (default, Nov 21 2019, 15:51:40)
[GCC 7.4.0]
pyfakefs 3.7.1
The text was updated successfully, but these errors were encountered: