Skip to content

Commit 01e7b5c

Browse files
committed
Issue warning for ignored arguments in nested call
- if a fake filesystem is created inside an existing fake fs, the existing fs is reused, and any arguments ignored
1 parent c209956 commit 01e7b5c

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ The released versions correspond to PyPI releases.
1313
the real filesystem behavior
1414
* remove support for Python versions before 3.10 (if needed, patches may be backported to the 5.x branch)
1515

16+
## Unreleased
17+
18+
## Changes
19+
* a warning is now issued if trying to create a nested fake filesystem with custom arguments
20+
(custom arguments are ignored in this case, as the existing fake filesystem is used)
21+
1622
## [Version 5.9.2](https://pypi.python.org/pypi/pyfakefs/5.9.2) (2025-07-30)
1723
Fixes interaction with pytest.
1824

docs/troubleshooting.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,8 @@ Nested file system fixtures and Patcher invocations
381381
``pyfakefs`` does not support nested faked file systems. Instead, it uses reference counting
382382
on the single fake filesystem instance. That means, if you are trying to create a fake filesystem
383383
inside a fake filesystem, only the reference count will increase, and any arguments you may pass
384-
to the patcher or fixture are ignored. Likewise, if you leave a nested fake filesystem,
384+
to the patcher or fixture are ignored. In this case (e.g. if passing custom arguments to a nested
385+
invocation), a warning is issued py ``pyfakefs``. Likewise, if you leave a nested fake filesystem,
385386
only the reference count is decreased and nothing is reverted.
386387

387388
There are some situations where that may happen, probably without you noticing:

pyfakefs/fake_filesystem_unittest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,21 @@ def __init__(
655655
if self.DOC_REF_COUNT > 0:
656656
return
657657
elif self.REF_COUNT > 0:
658+
if (
659+
additional_skip_names
660+
or modules_to_reload
661+
or modules_to_patch
662+
or not allow_root_user
663+
or not use_known_patches
664+
or patch_open_code != PatchMode.OFF
665+
or patch_default_args
666+
or not use_cache
667+
or not use_dynamic_patch
668+
):
669+
warnings.warn(
670+
"Nested fake filesystem invocation using custom arguments - "
671+
"using the existing fake filesystem, discarding custom arguments!"
672+
)
658673
return
659674
if not allow_root_user:
660675
# set non-root IDs even if the real user is root

pyfakefs/tests/fake_filesystem_unittest_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ def test_context_decorator(self, fake_fs):
7171
contents = f.read()
7272
self.assertEqual("test", contents)
7373

74+
def test_nested_invocation(self):
75+
with Patcher() as patcher:
76+
patcher.fs.create_file("/foo/bar", contents="test")
77+
self.assertTrue(patcher.fs.exists("/foo/bar"))
78+
with Patcher() as patcher1:
79+
self.assertTrue(patcher1.fs.exists("/foo/bar"))
80+
patcher1.fs.create_file("/foo/baz", contents="test")
81+
self.assertTrue(patcher1.fs.exists("/foo/baz"))
82+
self.assertTrue(patcher.fs.exists("/foo/baz"))
83+
84+
def test_nested_invocation_with_args(self):
85+
with Patcher() as patcher:
86+
patcher.fs.create_file("/foo/bar", contents="test")
87+
with self.assertWarnsRegex(
88+
UserWarning, "Nested fake filesystem invocation using custom arguments"
89+
):
90+
with Patcher(allow_root_user=False):
91+
pass
92+
7493

7594
class TestPatchfsArgumentOrder(TestCase):
7695
@patchfs

0 commit comments

Comments
 (0)