Skip to content

Commit ee102ac

Browse files
committed
Re-create the temp directory on resetting the filesystem
- the temp dir is now created during filesystem initialization instead of at patcher setup if configured - this is only done by default if created from the Patcher - fixes #814
1 parent eff3286 commit ee102ac

File tree

5 files changed

+51
-17
lines changed

5 files changed

+51
-17
lines changed

Diff for: CHANGES.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# pyfakefs Release Notes
22
The released versions correspond to PyPI releases.
33

4+
## Unreleased
5+
6+
### Fixes
7+
* Re-create temp directory if it had been created before on resetting file system
8+
(see [#814](../../issues/814)).
9+
410
## [Version 5.2.2](https://pypi.python.org/pypi/pyfakefs/5.2.2) (2023-04-13)
511
Fixes a regression in 5.2.0
612

Diff for: pyfakefs/fake_filesystem.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import os
8686
import random
8787
import sys
88+
import tempfile
8889
from collections import namedtuple, OrderedDict
8990
from doctest import TestResults
9091
from enum import Enum
@@ -197,12 +198,17 @@ def __init__(
197198
path_separator: str = os.path.sep,
198199
total_size: Optional[int] = None,
199200
patcher: Any = None,
201+
create_temp_dir: bool = False,
200202
) -> None:
201203
"""
202204
Args:
203205
path_separator: optional substitute for os.path.sep
204206
total_size: if not None, the total size in bytes of the
205207
root filesystem.
208+
patcher: the Patcher instance if created from the Patcher
209+
create_temp_dir: If True, a temp directory is created on initialization.
210+
Under Posix, if the temp directory is not `/tmp`, a link to the temp
211+
path is additionally created at `/tmp`.
206212
207213
Example usage to use the same path separator under all systems:
208214
@@ -212,6 +218,7 @@ def __init__(
212218
self.path_separator: str = path_separator
213219
self.alternative_path_separator: Optional[str] = os.path.altsep
214220
self.patcher = patcher
221+
self.create_temp_dir = create_temp_dir
215222
if path_separator != os.sep:
216223
self.alternative_path_separator = None
217224

@@ -247,6 +254,7 @@ def __init__(
247254
self._add_root_mount_point(total_size)
248255
self._add_standard_streams()
249256
self.dev_null: Any = FakeNullFile(self)
257+
self._create_temp_dir()
250258
# set from outside if needed
251259
self.patch_open_code = PatchMode.OFF
252260
self.shuffle_listdir_results = False
@@ -342,6 +350,7 @@ def reset(self, total_size: Optional[int] = None):
342350
self.mount_points = OrderedDict()
343351
self._add_root_mount_point(total_size)
344352
self._add_standard_streams()
353+
self._create_temp_dir()
345354
from pyfakefs import fake_pathlib
346355

347356
fake_pathlib.init_module(self)
@@ -1380,7 +1389,7 @@ def exists(self, file_path: AnyPath, check_link: bool = False) -> bool:
13801389
"""
13811390
if check_link and self.islink(file_path):
13821391
return True
1383-
path = to_string(make_string_path(file_path))
1392+
path = to_string(self.make_string_path(file_path))
13841393
if path is None:
13851394
raise TypeError
13861395
if not path:
@@ -2915,6 +2924,21 @@ def _add_standard_streams(self) -> None:
29152924
self._add_open_file(StandardStreamWrapper(sys.stdout))
29162925
self._add_open_file(StandardStreamWrapper(sys.stderr))
29172926

2927+
def _create_temp_dir(self):
2928+
if not self.create_temp_dir:
2929+
return
2930+
# the temp directory is assumed to exist at least in `tempfile`,
2931+
# so we create it here for convenience
2932+
temp_dir = tempfile.gettempdir()
2933+
if not self.exists(temp_dir):
2934+
self.create_dir(temp_dir)
2935+
if sys.platform != "win32" and not self.exists("/tmp"):
2936+
# under Posix, we also create a link in /tmp if the path does not exist
2937+
self.create_symlink("/tmp", temp_dir)
2938+
# reset the used size to 0 to avoid having the link size counted
2939+
# which would make disk size tests more complicated
2940+
next(iter(self.mount_points.values()))["used_size"] = 0
2941+
29182942

29192943
def _run_doctest() -> TestResults:
29202944
import doctest

Diff for: pyfakefs/fake_filesystem_unittest.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
pyfakefs by simply changing their base class from `:py:class`unittest.TestCase`
3636
to `:py:class`pyfakefs.fake_filesystem_unittest.TestCase`.
3737
"""
38-
import _io # type:ignore [import]
38+
import _io # type:ignore[import]
3939
import doctest
4040
import functools
4141
import genericpath
@@ -844,7 +844,7 @@ def _refresh(self) -> None:
844844
self._stubs.smart_unset_all()
845845
self._stubs = mox3_stubout.StubOutForTesting()
846846

847-
self.fs = fake_filesystem.FakeFilesystem(patcher=self)
847+
self.fs = fake_filesystem.FakeFilesystem(patcher=self, create_temp_dir=True)
848848
self.fs.patch_open_code = self.patch_open_code
849849
for name in self._fake_module_classes:
850850
self.fake_modules[name] = self._fake_module_classes[name](self.fs)
@@ -876,7 +876,6 @@ def setUp(self, doctester: Any = None) -> None:
876876
if self.has_fcopy_file:
877877
shutil._HAS_FCOPYFILE = False # type: ignore[attr-defined]
878878

879-
temp_dir = tempfile.gettempdir()
880879
with warnings.catch_warnings():
881880
# ignore warnings, see #542 and #614
882881
warnings.filterwarnings("ignore")
@@ -891,17 +890,6 @@ def setUp(self, doctester: Any = None) -> None:
891890
linecache.open = self.original_open # type: ignore[attr-defined]
892891
tokenize._builtin_open = self.original_open # type: ignore
893892

894-
# the temp directory is assumed to exist at least in `tempfile`,
895-
# so we create it here for convenience
896-
assert self.fs is not None
897-
self.fs.create_dir(temp_dir)
898-
if sys.platform != "win32" and not self.fs.exists("/tmp"):
899-
# under Posix, we also create a link in /tmp if the path does not exist
900-
self.fs.create_symlink("/tmp", temp_dir)
901-
# reset the used size to 0 to avoid having the link size counted
902-
# which would make disk size tests more complicated
903-
next(iter(self.fs.mount_points.values()))["used_size"] = 0
904-
905893
def start_patching(self) -> None:
906894
if not self._patching:
907895
self._patching = True

Diff for: pyfakefs/pytest_tests/pytest_plugin_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import tempfile
44

5+
from pyfakefs.fake_filesystem import OSType
56
from pyfakefs.fake_filesystem_unittest import Pause
67
import pyfakefs.pytest_tests.io
78

@@ -60,3 +61,18 @@ def test_use_own_io_module(fs):
6061

6162
stream = pyfakefs.pytest_tests.io.InputStream(filepath)
6263
assert stream.read() == "bar"
64+
65+
66+
def test_switch_to_windows(fs):
67+
fs.os = OSType.WINDOWS
68+
assert os.path.exists(tempfile.gettempdir())
69+
70+
71+
def test_switch_to_linux(fs):
72+
fs.os = OSType.LINUX
73+
assert os.path.exists(tempfile.gettempdir())
74+
75+
76+
def test_switch_to_macos(fs):
77+
fs.os = OSType.MACOS
78+
assert os.path.exists(tempfile.gettempdir())

Diff for: pyfakefs/tests/fake_filesystem_unittest_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,11 @@ class TestTempDirCreation(fake_filesystem_unittest.TestCase):
657657
def setUp(self):
658658
self.setUpPyfakefs()
659659

660-
def testTempDirExists(self):
660+
def test_tempdir_exists(self):
661661
self.assertTrue(os.path.exists(tempfile.gettempdir()))
662662

663663
@unittest.skipIf(sys.platform == "win32", "POSIX only test")
664-
def testTmpExists(self):
664+
def test_tmp_exists(self):
665665
# directory or link under Linux, link under macOS
666666
self.assertTrue(os.path.exists("/tmp"))
667667

0 commit comments

Comments
 (0)