Skip to content

Commit 4404c50

Browse files
committed
Fix some documentation examples
- add needed imports - fix a few incorrect tests
1 parent 56e5911 commit 4404c50

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

docs/troubleshooting.rst

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ or directories from the real into the fake filesystem as described in
145145
info_dir = Path(pytz.__file__).parent / "zoneinfo"
146146
self.fs.add_real_directory(info_dir)
147147

148+
.. note:: In newer django versions, `tzdata` is used instead of `pytz`, but the usage will be the same.
149+
148150
If you are using Django, various dependencies may expect both the project
149151
directory and the ``site-packages`` installation to exist in the fake filesystem.
150152

docs/usage.rst

+42-14
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@ Here is an example for a simple test:
1717

1818
.. code:: python
1919
20-
def my_fakefs_test(fs):
20+
import os
21+
22+
23+
def test_fakefs(fs):
2124
# "fs" is the reference to the fake file system
2225
fs.create_file("/var/data/xx1.txt")
2326
assert os.path.exists("/var/data/xx1.txt")
2427
2528
If you are bothered by the ``pylint`` warning,
26-
``C0103: Argument name "fs" doesn't conform to snake_case naming style
27-
(invalid-name)``,
28-
you can define a longer name in your ``conftest.py`` and use that in your
29-
tests:
29+
``C0103: Argument name "fs" doesn't conform to snake_case naming style (invalid-name)``,
30+
you can define a longer name in your ``conftest.py`` and use that in your tests:
3031

3132
.. code:: python
3233
34+
import pytest
35+
36+
3337
@pytest.fixture
3438
def fake_filesystem(fs): # pylint:disable=invalid-name
3539
"""Variable name 'fs' causes a pylint warning. Provide a longer name
@@ -60,6 +64,7 @@ with the fake file system functions and modules:
6064

6165
.. code:: python
6266
67+
import os
6368
from pyfakefs.fake_filesystem_unittest import TestCase
6469
6570
@@ -81,6 +86,8 @@ method ``setUpClassPyfakefs`` instead:
8186

8287
.. code:: python
8388
89+
import os
90+
import pathlib
8491
from pyfakefs.fake_filesystem_unittest import TestCase
8592
8693
@@ -89,7 +96,9 @@ method ``setUpClassPyfakefs`` instead:
8996
def setUpClass(cls):
9097
cls.setUpClassPyfakefs()
9198
# setup the fake filesystem using standard functions
92-
pathlib.Path("/test/file1.txt").touch()
99+
path = pathlib.Path("/test")
100+
path.mkdir()
101+
(path / "file1.txt").touch()
93102
# you can also access the fake fs via fake_fs() if needed
94103
cls.fake_fs().create_file("/test/file2.txt", contents="test")
95104
@@ -381,6 +390,7 @@ an example from a related issue):
381390
.. code:: python
382391
383392
import pathlib
393+
import click
384394
385395
386396
@click.command()
@@ -396,7 +406,7 @@ dynamically. All modules loaded after the initial patching described above
396406
will be patched using this second mechanism.
397407

398408
Given that the example function ``check_if_exists`` shown above is located in
399-
the file ``example/sut.py``, the following code will work:
409+
the file ``example/sut.py``, the following code will work (imports are omitted):
400410

401411
.. code:: python
402412
@@ -457,6 +467,9 @@ has now been been integrated into ``pyfakefs``):
457467

458468
.. code:: python
459469
470+
import django
471+
472+
460473
class FakeLocks:
461474
"""django.core.files.locks uses low level OS functions, fake it."""
462475
@@ -526,6 +539,7 @@ Alternatively to the module names, the modules themselves may be used:
526539
.. code:: python
527540
528541
import pydevd
542+
from pyfakefs.fake_filesystem_unittest import Patcher
529543
530544
with Patcher(additional_skip_names=[pydevd]) as patcher:
531545
patcher.fs.create_file("foo")
@@ -592,7 +606,7 @@ search for this kind of default arguments and patch them automatically.
592606
You could also use the :ref:`modules_to_reload` option with the module that
593607
contains the default argument instead, if you want to avoid the overhead.
594608

595-
.. note:: There are some cases where this option dees not work:
609+
.. note:: There are some cases where this option does *not* work:
596610

597611
- if default arguments are *computed* using file system functions:
598612

@@ -674,7 +688,7 @@ solution for the problem is found.
674688

675689
The setting defines how the dynamically loaded modules are cleaned up after the test
676690
to ensure that no patched modules can be used after the test has finished.
677-
The default (AUTO) currently depends on the availability of the `django` module,
691+
The default (ModuleCleanupMode.AUTO) currently depends on the availability of the `django` module,
678692
DELETE will delete all dynamically loaded modules and RELOAD will reload them.
679693
Under some rare conditions, changing this setting may help to avoid problems related
680694
to incorrect test cleanup.
@@ -684,7 +698,7 @@ to incorrect test cleanup.
684698
Using convenience methods
685699
-------------------------
686700
While ``pyfakefs`` can be used just with the standard Python file system
687-
functions, there are few convenience methods in ``fake_filesystem`` that can
701+
functions, there are a few convenience methods in ``fake_filesystem`` that can
688702
help you setting up your tests. The methods can be accessed via the
689703
``fake_filesystem`` instance in your tests: ``Patcher.fs``, the ``fs``
690704
fixture in pytest, ``TestCase.fs`` for ``unittest``, and the ``fs`` argument
@@ -753,6 +767,7 @@ would be overwritten by a file from the real filesystem, an exception is raised.
753767

754768
.. code:: python
755769
770+
import os
756771
from pyfakefs.fake_filesystem_unittest import TestCase
757772
758773
@@ -853,6 +868,8 @@ and you may fail to create new files if the fake file system is full.
853868

854869
.. code:: python
855870
871+
import errno
872+
import os
856873
from pyfakefs.fake_filesystem_unittest import TestCase
857874
858875
@@ -862,10 +879,11 @@ and you may fail to create new files if the fake file system is full.
862879
self.fs.set_disk_usage(100)
863880
864881
def test_disk_full(self):
865-
with open("/foo/bar.txt", "w") as f:
866-
with self.assertRaises(OSError):
882+
os.mkdir("/foo")
883+
with self.assertRaises(OSError) as e:
884+
with open("/foo/bar.txt", "w") as f:
867885
f.write("a" * 200)
868-
f.flush()
886+
self.assertEqual(errno.ENOSPC, e.exception.errno)
869887
870888
To get the file system size, you may use :py:meth:`get_disk_usage()<pyfakefs.fake_filesystem.FakeFilesystem.get_disk_usage>`, which is
871889
modeled after ``shutil.disk_usage()``.
@@ -883,6 +901,8 @@ Here is an example that tests the usage with the ``pyfakefs`` pytest fixture:
883901

884902
.. code:: python
885903
904+
import os
905+
import tempfile
886906
from pyfakefs.fake_filesystem_unittest import Pause
887907
888908
@@ -901,6 +921,8 @@ Here is the same code using a context manager:
901921

902922
.. code:: python
903923
924+
import os
925+
import tempfile
904926
from pyfakefs.fake_filesystem_unittest import Pause
905927
906928
@@ -944,6 +966,7 @@ The following test works both under Windows and Linux:
944966

945967
.. code:: python
946968
969+
import os
947970
from pyfakefs.fake_filesystem import OSType
948971
949972
@@ -964,12 +987,17 @@ possibility to use the ``force_unix_mode`` argument to ``FakeFilesystem.chmod``:
964987

965988
.. code:: python
966989
990+
import pathlib
991+
import pytest
992+
from pyfakefs.fake_filesystem import OSType
993+
994+
967995
def test_is_file_for_unreadable_dir_windows(fs):
968996
fs.os = OSType.WINDOWS
969997
path = pathlib.Path("/foo/bar")
970998
fs.create_file(path)
971999
# normal chmod does not really set the mode to 0
972-
self.fs.chmod("/foo", 0o000)
1000+
fs.chmod("/foo", 0o000)
9731001
assert path.is_file()
9741002
# but it does in forced UNIX mode
9751003
fs.chmod("/foo", 0o000, force_unix_mode=True)

0 commit comments

Comments
 (0)