Skip to content

Commit 933de16

Browse files
authored
Merge pull request #3988 from RonnyPfannschmidt/tmpdir-port-pathlib
Tmpdir port pathlib
2 parents e8348a1 + 4736b2b commit 933de16

17 files changed

+568
-134
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ repos:
2020
- id: check-yaml
2121
- id: debug-statements
2222
exclude: _pytest/debugging.py
23+
language_version: python3
2324
- id: flake8
25+
language_version: python3
2426
- repo: https://github.com/asottile/pyupgrade
2527
rev: v1.8.0
2628
hooks:
@@ -41,6 +43,6 @@ repos:
4143
- id: changelogs-rst
4244
name: changelog filenames
4345
language: fail
44-
entry: 'changelog files must be named ####.(feature|bugfix|doc|removal|vendor|trivial).rst'
46+
entry: 'changelog files must be named ####.(feature|bugfix|doc|deprecation|removal|vendor|trivial).rst'
4547
exclude: changelog/(\d+\.(feature|bugfix|doc|deprecation|removal|vendor|trivial).rst|README.rst|_template.rst)
4648
files: ^changelog/

changelog/3985.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Introduce ``tmp_path`` as a fixture providing a Path object.

changelog/3988.deprecation.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a Deprecation warning for pytest.ensuretemp as it was deprecated since a while.

changelog/3988.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port the implementation of tmpdir to pathlib.

doc/en/tmpdir.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,55 @@
55
Temporary directories and files
66
================================================
77

8+
The ``tmp_path`` fixture
9+
------------------------
10+
11+
.. versionadded:: 3.9
12+
13+
14+
You can use the ``tmpdir`` fixture which will
15+
provide a temporary directory unique to the test invocation,
16+
created in the `base temporary directory`_.
17+
18+
``tmpdir`` is a ``pathlib/pathlib2.Path`` object. Here is an example test usage:
19+
20+
.. code-block:: python
21+
22+
# content of test_tmp_path.py
23+
import os
24+
25+
CONTENT = u"content"
26+
27+
28+
def test_create_file(tmp_path):
29+
d = tmp_path / "sub"
30+
d.mkdir()
31+
p = d / "hello.txt"
32+
p.write_text(CONTENT)
33+
assert p.read_text() == CONTENT
34+
assert len(tmpdir.listdir()) == 1
35+
assert 0
36+
37+
Running this would result in a passed test except for the last
38+
``assert 0`` line which we use to look at values::
39+
40+
$ pytest test_tmp_path.py
41+
... #fill fom regendoc
42+
43+
44+
45+
The ``tmp_path_factory`` fixture
46+
--------------------------------
47+
48+
.. versionadded:: 3.9
49+
50+
51+
The ``tmp_path_facotry`` is a session-scoped fixture which can be used
52+
to create arbitrary temporary directories from any other fixture or test.
53+
54+
its intended to replace ``tmpdir_factory`` and returns :class:`pathlib.Path` instances.
55+
56+
857
The 'tmpdir' fixture
958
--------------------
1059

src/_pytest/assertion/rewrite.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
import py
1818

1919
from _pytest.assertion import util
20-
from _pytest.compat import PurePath, spec_from_file_location
21-
from _pytest.paths import fnmatch_ex
20+
from _pytest.pathlib import PurePath
21+
from _pytest.compat import spec_from_file_location
22+
from _pytest.pathlib import fnmatch_ex
2223

2324
# pytest caches rewritten pycs in __pycache__.
2425
if hasattr(imp, "get_tag"):

src/_pytest/cacheprovider.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313

1414
import pytest
1515
import json
16-
import shutil
1716

18-
from . import paths
19-
from .compat import _PY2 as PY2, Path
17+
from .compat import _PY2 as PY2
18+
from .pathlib import Path, resolve_from_str, rmtree
2019

2120
README_CONTENT = u"""\
2221
# pytest cache directory #
@@ -39,13 +38,13 @@ class Cache(object):
3938
def for_config(cls, config):
4039
cachedir = cls.cache_dir_from_config(config)
4140
if config.getoption("cacheclear") and cachedir.exists():
42-
shutil.rmtree(str(cachedir))
41+
rmtree(cachedir, force=True)
4342
cachedir.mkdir()
4443
return cls(cachedir, config)
4544

4645
@staticmethod
4746
def cache_dir_from_config(config):
48-
return paths.resolve_from_str(config.getini("cache_dir"), config.rootdir)
47+
return resolve_from_str(config.getini("cache_dir"), config.rootdir)
4948

5049
def warn(self, fmt, **args):
5150
from _pytest.warnings import _issue_config_warning

src/_pytest/compat.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
# Only available in Python 3.4+ or as a backport
2424
enum = None
2525

26-
__all__ = ["Path", "PurePath"]
27-
2826
_PY3 = sys.version_info > (3, 0)
2927
_PY2 = not _PY3
3028

@@ -41,11 +39,6 @@
4139
PY36 = sys.version_info[:2] >= (3, 6)
4240
MODULE_NOT_FOUND_ERROR = "ModuleNotFoundError" if PY36 else "ImportError"
4341

44-
if PY36:
45-
from pathlib import Path, PurePath
46-
else:
47-
from pathlib2 import Path, PurePath
48-
4942

5043
if _PY3:
5144
from collections.abc import MutableMapping as MappingMixin

src/_pytest/deprecated.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,8 @@
109109
PYTEST_NAMESPACE = RemovedInPytest4Warning(
110110
"pytest_namespace is deprecated and will be removed soon"
111111
)
112+
113+
PYTEST_ENSURETEMP = RemovedInPytest4Warning(
114+
"pytest/tmpdir_factory.ensuretemp is deprecated, \n"
115+
"please use the tmp_path fixture or tmp_path_factory.mktemp"
116+
)

src/_pytest/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ def pytest_addoption(parser):
156156
dest="basetemp",
157157
default=None,
158158
metavar="dir",
159-
help="base temporary directory for this test run.",
159+
help=(
160+
"base temporary directory for this test run."
161+
"(warning: this directory is removed if it exists)"
162+
),
160163
)
161164

162165

0 commit comments

Comments
 (0)