diff --git a/CHANGES.md b/CHANGES.md index 78ff211a..c9b8430a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,8 @@ The release versions are PyPi releases. * removed unneeded parameter `use_dynamic_patch` #### New Features + * added possibility to use modules instead of module names for the + `additional_skip_names` argument (see [#482](../../issues/482)) * added argument `allow_root_user` to `Patcher` and `UnitTest` to allow forcing non-root access (see [#474](../../issues/474)) * added basic support for `os.pipe` (see [#473](../../issues/473)) diff --git a/docs/usage.rst b/docs/usage.rst index f2a1a765..61c1a985 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -290,11 +290,18 @@ offending module to ``additional_skip_names``: with Patcher(additional_skip_names=['pydevd']) as patcher: patcher.fs.create_file('foo') +Alternatively to the module names, the modules themselves may be used: + +.. code:: python + + import pydevd + + with Patcher(additional_skip_names=[pydevd]) as patcher: + patcher.fs.create_file('foo') + There is also the global variable ``Patcher.SKIPNAMES`` that can be extended for that purpose, though this seldom shall be needed (except for own pytest -plugins, as shown in the example mentioned above). Other than in -``additional_skip_names``, which is a list of modules names, this is a list -of modules that have to be imported before. +plugins, as shown in the example mentioned above). allow_root_user ~~~~~~~~~~~~~~~ diff --git a/pyfakefs/fake_filesystem_unittest.py b/pyfakefs/fake_filesystem_unittest.py index 8259ec31..b199dc05 100644 --- a/pyfakefs/fake_filesystem_unittest.py +++ b/pyfakefs/fake_filesystem_unittest.py @@ -120,6 +120,7 @@ class TestCaseMixin(object): additional_skip_names: names of modules inside of which no module replacement shall be performed, in addition to the names in :py:attr:`fake_filesystem_unittest.Patcher.SKIPNAMES`. + Instead of the module names, the modules themselves may be used. modules_to_reload: A list of modules that need to be reloaded to be patched dynamically; may be needed if the module imports file system modules under an alias @@ -328,7 +329,9 @@ def __init__(self, additional_skip_names=None, self.fake_open = None if additional_skip_names is not None: - self._skipNames.update(additional_skip_names) + skip_names = [m.__name__ if inspect.ismodule(m) else m + for m in additional_skip_names] + self._skipNames.update(skip_names) self.modules_to_reload = [tempfile] if modules_to_reload is not None: diff --git a/pyfakefs/tests/fake_filesystem_unittest_test.py b/pyfakefs/tests/fake_filesystem_unittest_test.py index 928583b8..052181ae 100644 --- a/pyfakefs/tests/fake_filesystem_unittest_test.py +++ b/pyfakefs/tests/fake_filesystem_unittest_test.py @@ -265,6 +265,41 @@ def test_path_exists(self): pyfakefs.tests.import_as_example.check_if_exists4(file_path)) +class NoSkipNamesTest(fake_filesystem_unittest.TestCase): + """Reference test for additional_skip_names tests: + make sure that the module is patched by default.""" + + def test_path_exists(self): + self.assertTrue( + pyfakefs.tests.import_as_example.exists_this_file()) + + +class AdditionalSkipNamesTest(fake_filesystem_unittest.TestCase): + """Make sure that modules in additional_skip_names are not patched. + Passes module name to `additional_skip_names`.""" + + def setUp(self): + self.setUpPyfakefs( + additional_skip_names=['pyfakefs.tests.import_as_example']) + + def test_path_exists(self): + self.assertFalse( + pyfakefs.tests.import_as_example.exists_this_file()) + + +class AdditionalSkipNamesModuleTest(fake_filesystem_unittest.TestCase): + """Make sure that modules in additional_skip_names are not patched. + Passes module to `additional_skip_names`.""" + + def setUp(self): + self.setUpPyfakefs( + additional_skip_names=[pyfakefs.tests.import_as_example]) + + def test_path_exists(self): + self.assertFalse( + pyfakefs.tests.import_as_example.exists_this_file()) + + class FakeExampleModule(object): """Used to patch a function that uses system-specific functions that cannot be patched automatically.""" diff --git a/pyfakefs/tests/import_as_example.py b/pyfakefs/tests/import_as_example.py index 84892549..8b79382e 100644 --- a/pyfakefs/tests/import_as_example.py +++ b/pyfakefs/tests/import_as_example.py @@ -95,3 +95,8 @@ def file_contents1(filepath): def file_contents2(filepath): with io_open(filepath) as f: return f.read() + + +def exists_this_file(): + "Returns True in real fs only" + return exists(__file__) \ No newline at end of file