Skip to content

Commit 596d3fe

Browse files
committed
Minor changes in troubleshooting documentation
1 parent c5b72f7 commit 596d3fe

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

docs/autopatch.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ The pyfakefs source code contains files that demonstrate this usage model:
1111
- ``example_test.py`` tests ``example.py``. During testing, the pyfakefs fake
1212
file system is used by ``example_test.py`` and ``example.py`` alike.
1313

14+
.. note:: This example uses the Python ``unittest`` module for testing, but the
15+
functionality is similar if using the ``fs`` fixture in ``pytest``,
16+
the ``patchfs`` decorator, or the ``Patcher`` class.
17+
18+
1419
Software Under Test
1520
-------------------
1621
``example.py`` contains a few functions that manipulate files. For instance:

docs/troubleshooting.rst

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ This includes a number of modules that need to start other executables to
6868
function correctly. Examples that have shown this problem include `GitPython`_
6969
and `plumbum`_.
7070

71-
The `Pillow`_ image library
72-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
71+
The `Pillow`_ Imaging Library
72+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7373
This library partly works with ``pyfakefs``, but it is known to not work at
7474
least if writing JPEG files
7575
(see `this issue <https://github.com/jmcgeheeiv/pyfakefs/issues/529>`__)
7676

77-
`pandas`_ - the Python data analysis library
78-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
77+
The `pandas`_ data analysis toolkit
78+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7979
This uses its own internal file system access written in C, thus much of
8080
``pandas`` will not work with ``pyfakefs`` out of the box. Having said that,
8181
``pyfakefs`` patches ``pandas`` to use standard file-system access instead,
@@ -86,29 +86,31 @@ system. If you use only these functions, ``pyfakefs`` should work fine with
8686

8787
`xlrd`_
8888
~~~~~~~
89-
This libary is used by ``pandas`` to read Excel files in the `.xls` format, and
90-
can also be used stand-alone. Similar to ``pandas``, it is by default patched
91-
by ``pyfakefs`` to use normal file system functions that can be patched.
89+
This library is used by ``pandas`` to read Excel files in the `.xls` format,
90+
and can also be used stand-alone. Similar to ``pandas``, it is by default
91+
patched by ``pyfakefs`` to use normal file system functions that can be
92+
patched.
9293

9394
`openpyxl`_
9495
~~~~~~~~~~~
9596
This is another library that reads and writes Excel files, and is also
9697
used by ``pandas`` if installed. ``openpyxl`` uses ``lxml`` for some file-system
97-
access if it is installed - in this case ``pyfakefs`` will not able to patch
98+
access if it is installed--in this case ``pyfakefs`` will not be able to patch
9899
it correctly (``lxml`` uses C functions for file system access). It will `not`
99-
use ``lxml`` however, if the environment variable ``OPENPYXL_LXML` is set to
100+
use ``lxml`` however, if the environment variable ``OPENPYXL_LXML`` is set to
100101
"False" (or anything other than "True"), so if you set this variable `before`
101102
running the tests, it can work fine with ``pyfakefs``.
102103

103-
Please write a new issue, if you are not sure if a module can be handled, or
104-
how to do it.
104+
If you encounter a module not working with ``pyfakefs``, and you are not sure
105+
if the module can be handled or how to do it, please write a new issue. We
106+
will check if it can be made to work, and at least add it to this list.
105107

106108
Pyfakefs behaves differently than the real filesystem
107109
-----------------------------------------------------
108110
There are at least the following kinds of deviations from the actual behavior:
109111

110112
- unwanted deviations that we didn't notice--if you find any of these, please
111-
write an issue and will try to fix it
113+
write an issue and we will try to fix it
112114
- behavior that depends on different OS versions and editions--as mentioned
113115
in :ref:`limitations`, ``pyfakefs`` uses the TravisCI systems as reference
114116
system and will not replicate all system-specific behavior
@@ -130,12 +132,21 @@ OS temporary directories
130132
------------------------
131133
Tests relying on a completely empty file system on test start will fail.
132134
As ``pyfakefs`` does not fake the ``tempfile`` module (as described above),
133-
a temporary directory is required to ensure ``tempfile`` works correctly,
135+
a temporary directory is required to ensure that ``tempfile`` works correctly,
134136
e.g., that ``tempfile.gettempdir()`` will return a valid value. This
135137
means that any newly created fake file system will always have either a
136138
directory named ``/tmp`` when running on Linux or Unix systems,
137139
``/var/folders/<hash>/T`` when running on MacOs, or
138-
``C:\Users\<user>\AppData\Local\Temp`` on Windows.
140+
``C:\Users\<user>\AppData\Local\Temp`` on Windows:
141+
142+
.. code:: python
143+
144+
import os
145+
146+
def test_something(fs):
147+
# the temp directory is always present at test start
148+
assert len(os.listdir("/")) == 1
149+
139150
140151
User rights
141152
-----------
@@ -149,7 +160,16 @@ between root user (with the user id 0) and any other user. By default,
149160
that using ``fake_filesystem.set_uid()`` in your setup. This allows to run
150161
tests as non-root user in a root user environment and vice verse.
151162
Another possibility to run tests as non-root user in a root user environment
152-
is the convenience argument :ref:`allow_root_user`.
163+
is the convenience argument :ref:`allow_root_user`:
164+
165+
.. code:: python
166+
167+
from pyfakefs.fake_filesystem_unittest import TestCase
168+
169+
class SomeTest(TestCase):
170+
def setUp(self):
171+
self.setUpPyfakefs(allow_root_user=False)
172+
153173
154174
.. _usage_with_mock_open:
155175

@@ -160,12 +180,23 @@ If you patch ``open`` using ``mock_open`` before the initialization of
160180
initialization relies on ``open`` working correctly.
161181
Generally, you should not need ``mock_open`` if using ``pyfakefs``, because you
162182
always can create the files with the needed content using ``create_file``.
163-
This is true for patching any filesystem functions - avoid patching them
183+
This is true for patching any filesystem functions--avoid patching them
164184
while working with ``pyfakefs``.
165185
If you still want to use ``mock_open``, make sure it is only used while
166186
patching is in progress. For example, if you are using ``pytest`` with the
167187
``mocker`` fixture used to patch ``open``, make sure that the ``fs`` fixture is
168-
passed before the ``mocker`` fixture to ensure this.
188+
passed before the ``mocker`` fixture to ensure this:
189+
190+
.. code:: python
191+
192+
def test_mock_open_incorrect(mocker, fs):
193+
# causes a recursion error
194+
mocker.patch('builtins.open', mocker.mock_open(read_data="content"))
195+
196+
def test_mock_open_correct(fs, mocker):
197+
# works correctly
198+
mocker.patch('builtins.open', mocker.mock_open(read_data="content"))
199+
169200
170201
.. _`multiprocessing`: https://docs.python.org/3/library/multiprocessing.html
171202
.. _`subprocess`: https://docs.python.org/3/library/subprocess.html

0 commit comments

Comments
 (0)