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

Using sys.modules to patch all modules seems insufficient #199

Closed
SebastianFree opened this issue Jun 8, 2017 · 5 comments
Closed

Using sys.modules to patch all modules seems insufficient #199

SebastianFree opened this issue Jun 8, 2017 · 5 comments
Labels

Comments

@SebastianFree
Copy link

SebastianFree commented Jun 8, 2017

I am using PyFakeFS (newest version) with its unit test implementation in a Django environment and Python 3.5.
Looking at the Patcher class, it seems insufficient to iterate over sys.modules.items() since it returns only those modules that have been imported until that point.
If my unit test imports a new module after self.setUpPyfakefs(), 'os' for example will not be patched there.

I managed to patch a new module using this wild construct: self._stubber._stubs.SmartSet(new_module, 'os', self._stubber.fake_os) but only after importing it, of course.

What am I doing wrong? How can modules that are being imported after setup still be patched automatically?

Thank you

@mrbean-bremen
Copy link
Member

mrbean-bremen commented Jun 8, 2017

You are not doing anything wrong. This is indeed a shortcoming with pyfakefs and dynamic imports, and I have seen the same problem with Django views if using django.conf.urls.patterns in urls.py. If using the newer list syntax instead, e.g. instead of:

urlpatterns = patterns(
    'config.views',
    url(r'^configuration/$', 'config_page'),
    ...

use

from . import views

urlpatterns = [
    url(r'^configuration/$', views.config_page),
    ...

you have to import the view class there and the problem may go away. Note that I have not tested this, though. If this does not help (or you already use that syntax), you can import the view class in your test source - this works, but gives an "unused import" warning.

@mrbean-bremen
Copy link
Member

@SebastianFree - any progress on that? Do you need more help?

@SebastianFree
Copy link
Author

Hi,
thanks for the reminder.
Atm I have other priorities, but I will come back to pyfakefs for sure.
All I can say now is that we are using the new URL syntax (didn't even know there was the old one).
Importing everything in advance kind of beats the purpose for me because it would mean individual efforts for every single test which is exactly what i wanted to avoid (only wanted one custom super class that sets up pyfakefs in setUp method).
But it might still be of value nevertheless. I"ll let you know.

@mrbean-bremen
Copy link
Member

@SebastianFree - if you are still at this, can you please check if the problem persists with the current master branch? I added a fix for this, but I'm not completely sue that this resolves the problem.

@mrbean-bremen
Copy link
Member

I could reproduce the problem in Django code, and it's now fixed for me. Closing this, please reopen if it doesn't work for you.

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

No branches or pull requests

2 participants