Skip to content

Commit

Permalink
Merge pull request #3988 from RonnyPfannschmidt/tmpdir-port-pathlib
Browse files Browse the repository at this point in the history
Tmpdir port pathlib
  • Loading branch information
nicoddemus authored Oct 12, 2018
2 parents e8348a1 + 4736b2b commit 933de16
Show file tree
Hide file tree
Showing 17 changed files with 568 additions and 134 deletions.
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ repos:
- id: check-yaml
- id: debug-statements
exclude: _pytest/debugging.py
language_version: python3
- id: flake8
language_version: python3
- repo: https://github.com/asottile/pyupgrade
rev: v1.8.0
hooks:
Expand All @@ -41,6 +43,6 @@ repos:
- id: changelogs-rst
name: changelog filenames
language: fail
entry: 'changelog files must be named ####.(feature|bugfix|doc|removal|vendor|trivial).rst'
entry: 'changelog files must be named ####.(feature|bugfix|doc|deprecation|removal|vendor|trivial).rst'
exclude: changelog/(\d+\.(feature|bugfix|doc|deprecation|removal|vendor|trivial).rst|README.rst|_template.rst)
files: ^changelog/
1 change: 1 addition & 0 deletions changelog/3985.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Introduce ``tmp_path`` as a fixture providing a Path object.
1 change: 1 addition & 0 deletions changelog/3988.deprecation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a Deprecation warning for pytest.ensuretemp as it was deprecated since a while.
1 change: 1 addition & 0 deletions changelog/3988.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port the implementation of tmpdir to pathlib.
49 changes: 49 additions & 0 deletions doc/en/tmpdir.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,55 @@
Temporary directories and files
================================================

The ``tmp_path`` fixture
------------------------

.. versionadded:: 3.9


You can use the ``tmpdir`` fixture which will
provide a temporary directory unique to the test invocation,
created in the `base temporary directory`_.

``tmpdir`` is a ``pathlib/pathlib2.Path`` object. Here is an example test usage:

.. code-block:: python
# content of test_tmp_path.py
import os
CONTENT = u"content"
def test_create_file(tmp_path):
d = tmp_path / "sub"
d.mkdir()
p = d / "hello.txt"
p.write_text(CONTENT)
assert p.read_text() == CONTENT
assert len(tmpdir.listdir()) == 1
assert 0
Running this would result in a passed test except for the last
``assert 0`` line which we use to look at values::

$ pytest test_tmp_path.py
... #fill fom regendoc



The ``tmp_path_factory`` fixture
--------------------------------

.. versionadded:: 3.9


The ``tmp_path_facotry`` is a session-scoped fixture which can be used
to create arbitrary temporary directories from any other fixture or test.

its intended to replace ``tmpdir_factory`` and returns :class:`pathlib.Path` instances.


The 'tmpdir' fixture
--------------------

Expand Down
5 changes: 3 additions & 2 deletions src/_pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
import py

from _pytest.assertion import util
from _pytest.compat import PurePath, spec_from_file_location
from _pytest.paths import fnmatch_ex
from _pytest.pathlib import PurePath
from _pytest.compat import spec_from_file_location
from _pytest.pathlib import fnmatch_ex

# pytest caches rewritten pycs in __pycache__.
if hasattr(imp, "get_tag"):
Expand Down
9 changes: 4 additions & 5 deletions src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@

import pytest
import json
import shutil

from . import paths
from .compat import _PY2 as PY2, Path
from .compat import _PY2 as PY2
from .pathlib import Path, resolve_from_str, rmtree

README_CONTENT = u"""\
# pytest cache directory #
Expand All @@ -39,13 +38,13 @@ class Cache(object):
def for_config(cls, config):
cachedir = cls.cache_dir_from_config(config)
if config.getoption("cacheclear") and cachedir.exists():
shutil.rmtree(str(cachedir))
rmtree(cachedir, force=True)
cachedir.mkdir()
return cls(cachedir, config)

@staticmethod
def cache_dir_from_config(config):
return paths.resolve_from_str(config.getini("cache_dir"), config.rootdir)
return resolve_from_str(config.getini("cache_dir"), config.rootdir)

def warn(self, fmt, **args):
from _pytest.warnings import _issue_config_warning
Expand Down
7 changes: 0 additions & 7 deletions src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
# Only available in Python 3.4+ or as a backport
enum = None

__all__ = ["Path", "PurePath"]

_PY3 = sys.version_info > (3, 0)
_PY2 = not _PY3

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

if PY36:
from pathlib import Path, PurePath
else:
from pathlib2 import Path, PurePath


if _PY3:
from collections.abc import MutableMapping as MappingMixin
Expand Down
5 changes: 5 additions & 0 deletions src/_pytest/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,8 @@
PYTEST_NAMESPACE = RemovedInPytest4Warning(
"pytest_namespace is deprecated and will be removed soon"
)

PYTEST_ENSURETEMP = RemovedInPytest4Warning(
"pytest/tmpdir_factory.ensuretemp is deprecated, \n"
"please use the tmp_path fixture or tmp_path_factory.mktemp"
)
5 changes: 4 additions & 1 deletion src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ def pytest_addoption(parser):
dest="basetemp",
default=None,
metavar="dir",
help="base temporary directory for this test run.",
help=(
"base temporary directory for this test run."
"(warning: this directory is removed if it exists)"
),
)


Expand Down
Loading

0 comments on commit 933de16

Please sign in to comment.