@@ -17,19 +17,23 @@ Here is an example for a simple test:
17
17
18
18
.. code :: python
19
19
20
- def my_fakefs_test (fs ):
20
+ import os
21
+
22
+
23
+ def test_fakefs (fs ):
21
24
# "fs" is the reference to the fake file system
22
25
fs.create_file(" /var/data/xx1.txt" )
23
26
assert os.path.exists(" /var/data/xx1.txt" )
24
27
25
28
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:
30
31
31
32
.. code :: python
32
33
34
+ import pytest
35
+
36
+
33
37
@pytest.fixture
34
38
def fake_filesystem (fs ): # pylint:disable=invalid-name
35
39
""" Variable name 'fs' causes a pylint warning. Provide a longer name
@@ -60,6 +64,7 @@ with the fake file system functions and modules:
60
64
61
65
.. code :: python
62
66
67
+ import os
63
68
from pyfakefs.fake_filesystem_unittest import TestCase
64
69
65
70
@@ -81,6 +86,8 @@ method ``setUpClassPyfakefs`` instead:
81
86
82
87
.. code :: python
83
88
89
+ import os
90
+ import pathlib
84
91
from pyfakefs.fake_filesystem_unittest import TestCase
85
92
86
93
@@ -89,7 +96,9 @@ method ``setUpClassPyfakefs`` instead:
89
96
def setUpClass (cls ):
90
97
cls .setUpClassPyfakefs()
91
98
# 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()
93
102
# you can also access the fake fs via fake_fs() if needed
94
103
cls .fake_fs().create_file(" /test/file2.txt" , contents = " test" )
95
104
@@ -381,6 +390,7 @@ an example from a related issue):
381
390
.. code :: python
382
391
383
392
import pathlib
393
+ import click
384
394
385
395
386
396
@click.command ()
@@ -396,7 +406,7 @@ dynamically. All modules loaded after the initial patching described above
396
406
will be patched using this second mechanism.
397
407
398
408
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) :
400
410
401
411
.. code :: python
402
412
@@ -457,6 +467,9 @@ has now been been integrated into ``pyfakefs``):
457
467
458
468
.. code :: python
459
469
470
+ import django
471
+
472
+
460
473
class FakeLocks :
461
474
""" django.core.files.locks uses low level OS functions, fake it."""
462
475
@@ -526,6 +539,7 @@ Alternatively to the module names, the modules themselves may be used:
526
539
.. code :: python
527
540
528
541
import pydevd
542
+ from pyfakefs.fake_filesystem_unittest import Patcher
529
543
530
544
with Patcher(additional_skip_names = [pydevd]) as patcher:
531
545
patcher.fs.create_file(" foo" )
@@ -592,7 +606,7 @@ search for this kind of default arguments and patch them automatically.
592
606
You could also use the :ref: `modules_to_reload ` option with the module that
593
607
contains the default argument instead, if you want to avoid the overhead.
594
608
595
- .. note :: There are some cases where this option dees not work:
609
+ .. note :: There are some cases where this option does * not* work:
596
610
597
611
- if default arguments are *computed * using file system functions:
598
612
@@ -674,7 +688,7 @@ solution for the problem is found.
674
688
675
689
The setting defines how the dynamically loaded modules are cleaned up after the test
676
690
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,
678
692
DELETE will delete all dynamically loaded modules and RELOAD will reload them.
679
693
Under some rare conditions, changing this setting may help to avoid problems related
680
694
to incorrect test cleanup.
@@ -684,7 +698,7 @@ to incorrect test cleanup.
684
698
Using convenience methods
685
699
-------------------------
686
700
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
688
702
help you setting up your tests. The methods can be accessed via the
689
703
``fake_filesystem `` instance in your tests: ``Patcher.fs ``, the ``fs ``
690
704
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.
753
767
754
768
.. code :: python
755
769
770
+ import os
756
771
from pyfakefs.fake_filesystem_unittest import TestCase
757
772
758
773
@@ -853,6 +868,8 @@ and you may fail to create new files if the fake file system is full.
853
868
854
869
.. code :: python
855
870
871
+ import errno
872
+ import os
856
873
from pyfakefs.fake_filesystem_unittest import TestCase
857
874
858
875
@@ -862,10 +879,11 @@ and you may fail to create new files if the fake file system is full.
862
879
self .fs.set_disk_usage(100 )
863
880
864
881
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:
867
885
f.write(" a" * 200 )
868
- f.flush( )
886
+ self .assertEqual(errno. ENOSPC , e.exception.errno )
869
887
870
888
To get the file system size, you may use :py:meth: `get_disk_usage()<pyfakefs.fake_filesystem.FakeFilesystem.get_disk_usage> `, which is
871
889
modeled after ``shutil.disk_usage() ``.
@@ -883,6 +901,8 @@ Here is an example that tests the usage with the ``pyfakefs`` pytest fixture:
883
901
884
902
.. code :: python
885
903
904
+ import os
905
+ import tempfile
886
906
from pyfakefs.fake_filesystem_unittest import Pause
887
907
888
908
@@ -901,6 +921,8 @@ Here is the same code using a context manager:
901
921
902
922
.. code :: python
903
923
924
+ import os
925
+ import tempfile
904
926
from pyfakefs.fake_filesystem_unittest import Pause
905
927
906
928
@@ -944,6 +966,7 @@ The following test works both under Windows and Linux:
944
966
945
967
.. code :: python
946
968
969
+ import os
947
970
from pyfakefs.fake_filesystem import OSType
948
971
949
972
@@ -964,12 +987,17 @@ possibility to use the ``force_unix_mode`` argument to ``FakeFilesystem.chmod``:
964
987
965
988
.. code :: python
966
989
990
+ import pathlib
991
+ import pytest
992
+ from pyfakefs.fake_filesystem import OSType
993
+
994
+
967
995
def test_is_file_for_unreadable_dir_windows (fs ):
968
996
fs.os = OSType.WINDOWS
969
997
path = pathlib.Path(" /foo/bar" )
970
998
fs.create_file(path)
971
999
# normal chmod does not really set the mode to 0
972
- self . fs.chmod(" /foo" , 0o 000 )
1000
+ fs.chmod(" /foo" , 0o 000 )
973
1001
assert path.is_file()
974
1002
# but it does in forced UNIX mode
975
1003
fs.chmod(" /foo" , 0o 000 , force_unix_mode = True )
0 commit comments