Skip to content
Permalink
Browse files

Support modules instead of module names in additional_skip_names

- closes #482
  • Loading branch information...
mrbean-bremen committed May 10, 2019
1 parent f1cc615 commit b18174bdab6ef5018b6c7b1b33b2bf08dd7d7634
@@ -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))
@@ -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
~~~~~~~~~~~~~~~
@@ -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:
@@ -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."""
@@ -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.
You can’t perform that action at this time.