Skip to content

Commit

Permalink
Support modules instead of module names in additional_skip_names
Browse files Browse the repository at this point in the history
- closes #482
  • Loading branch information
mrbean-bremen committed May 10, 2019
1 parent f1cc615 commit b18174b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -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))
Expand Down
13 changes: 10 additions & 3 deletions docs/usage.rst
Expand Up @@ -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
~~~~~~~~~~~~~~~
Expand Down
5 changes: 4 additions & 1 deletion pyfakefs/fake_filesystem_unittest.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
35 changes: 35 additions & 0 deletions pyfakefs/tests/fake_filesystem_unittest_test.py
Expand Up @@ -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."""
Expand Down
5 changes: 5 additions & 0 deletions pyfakefs/tests/import_as_example.py
Expand Up @@ -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__)

0 comments on commit b18174b

Please sign in to comment.