Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tmpdir port pathlib #3988

Merged
merged 25 commits into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b48e23d
port interals of tmpdir to a basic pathlib implementation
RonnyPfannschmidt Sep 15, 2018
2e39fd8
add python27 support by using reduce instead of max
RonnyPfannschmidt Sep 16, 2018
d053cdf
factor out max and iterate on locks and cleanups
RonnyPfannschmidt Sep 18, 2018
8e00280
fix linting
RonnyPfannschmidt Sep 19, 2018
66a6909
bring in purepath and fix an assertion
RonnyPfannschmidt Sep 19, 2018
ab3637d
implement cleanup for unlocked folders
RonnyPfannschmidt Sep 20, 2018
8b4a293
fix typo
RonnyPfannschmidt Sep 21, 2018
b3a5b0e
remove path from exposure
RonnyPfannschmidt Sep 26, 2018
642cd86
shape up removal and lock destruction
RonnyPfannschmidt Sep 26, 2018
2532dc1
fix up lock consideration argument
RonnyPfannschmidt Sep 27, 2018
d76fa59
fix lock timeouts for good this time
RonnyPfannschmidt Sep 28, 2018
fed4f73
ignore rmtree errors
RonnyPfannschmidt Sep 28, 2018
85cc9b8
move all the things into _pytest.pathlib
RonnyPfannschmidt Sep 29, 2018
0071617
fix missed Path import
RonnyPfannschmidt Sep 29, 2018
2831cb9
unify paths.py and pathlib.py
RonnyPfannschmidt Oct 1, 2018
3036914
sort out rmtree expectations
RonnyPfannschmidt Oct 1, 2018
ad6f63e
add changelog
RonnyPfannschmidt Oct 1, 2018
4a436b5
resolve in code review commments
RonnyPfannschmidt Oct 2, 2018
b82d6f7
pytester: use per test tmproot
RonnyPfannschmidt Oct 2, 2018
94829c3
make tmpdir env cleanup idempotent
RonnyPfannschmidt Oct 2, 2018
ebd597b
use the constant for lock timeouts
RonnyPfannschmidt Oct 2, 2018
36c2a10
add missing docstring
RonnyPfannschmidt Oct 2, 2018
16e2737
implement tmp_path_factory and deprecate pytest.ensuretemp as intended
RonnyPfannschmidt Oct 11, 2018
584051a
extend docs with basics about tmp_path and tmp_path_facotry
RonnyPfannschmidt Oct 11, 2018
4736b2b
address review comments
RonnyPfannschmidt Oct 11, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
nicoddemus marked this conversation as resolved.
Show resolved Hide resolved
- 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'
nicoddemus marked this conversation as resolved.
Show resolved Hide resolved
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is better to use .. code-block:: python so that blaken-docs can do its thing.

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
Zac-HD marked this conversation as resolved.
Show resolved Hide resolved
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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a deprecation changelog entry for that. 👍

"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