Skip to content

Tmpdir port pathlib - #3988

Merged
nicoddemus merged 25 commits into
pytest-dev:featuresfrom
RonnyPfannschmidt:tmpdir-port-pathlib
Oct 12, 2018
Merged

nicoddemus merged 25 commits into
pytest-dev:featuresfrom
RonnyPfannschmidt:tmpdir-port-pathlib

Conversation

@RonnyPfannschmidt

@RonnyPfannschmidt RonnyPfannschmidt commented Sep 15, 2018 •

Copy link
Copy Markdown
Member

this pr is to port tmpdir over to pathlib

the following steps have to be implemented

Fix #3985

@coveralls

coveralls commented Sep 16, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.007%) to 93.773% when pulling 4736b2b on RonnyPfannschmidt:tmpdir-port-pathlib into 8ecdd4e on pytest-dev:features.

Comment thread src/_pytest/tmpdir.py Outdated
from __future__ import absolute_import, division, print_function

import re
from six.moves import reduce

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

in py27+ you can consistently import this from functools import reduce -- would be a regression of #3609

Comment thread src/_pytest/tmpdir.py Outdated
self._basetemp = t = basetemp.realpath()
self.trace("new basetemp", t)
return t
return py.path.local(self.tmppath_factory.getbasetemp().resolve())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ah ok, so it still returns a py.path for now?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

the shim and the old api must stay compatible

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

makes sense, another approach might be to write an adapter class which has the interface of py.path._Path but keeps a pathlib object internally -- I suspect we'll want something like this anyway to make a path forward for methods like as_cwd() which have no equivalent in pathlib

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@asottile that stuff should be using monkeypatch - i am perfectly fine with not replicating py.path.local - it has major issues with miss-alinged feature creep and bad api, and i really dont want to inherit that into a new api

my plan is to support pathlib everywhere, and then make it possible to run without py.path.local

@asottile asottile Sep 16, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd be a little sad to see some of the apis go. I for one use:

with tmpdir.join('somepath').ensure_dir().as_cwd():
    ...

quite a lot

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ensure is going to go away as well - i wonder where you use that patter, since its a bit pathological in common usage and i consider it a anti-pattern

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

most of my tests, actually 😆

$ all-repos-grep -l -E '\.(as_cwd|ensure(|_dir))\(\)'
repos/asottile/all-repos:tests/push/github_pull_request_test.py
repos/asottile/all-repos-depends:tests/conftest.py
repos/asottile/all-repos-depends:tests/depends_test.py
repos/asottile/all-repos-depends:tests/lang/python_test.py
repos/asottile/all-repos-depends:tests/packages_test.py
repos/asottile/aspy.refactor_imports:tests/classify_test.py
repos/asottile/aspy.refactor_imports:tests/conftest.py
repos/asottile/aspy.refactor_imports:tests/sort_test.py
repos/asottile/git-code-debt:tests/server/app_test.py
repos/asottile/no-manylinux1:tests/integration_test.py
repos/asottile/reorder_python_imports:tests/reorder_python_imports_test.py
repos/asottile/rubyvenv:tests/rubyvenv_test.py
repos/asottile/seed-isort-config:tests/seed_isort_config_test.py
repos/asottile/yesqa:tests/yesqa_test.py
repos/pre-commit/pre-commit:tests/commands/run_test.py
repos/pre-commit/pre-commit:tests/git_test.py
repos/pre-commit/pre-commit:tests/languages/pygrep_test.py
repos/pre-commit/pre-commit:tests/parse_shebang_test.py
repos/pre-commit/pre-commit:tests/repository_test.py
repos/pre-commit/pre-commit:tests/staged_files_only_test.py
repos/pre-commit/pre-commit-hooks:tests/autopep8_wrapper_test.py
repos/pre-commit/pre-commit-hooks:tests/check_added_large_files_test.py
repos/pre-commit/pre-commit-hooks:tests/check_case_conflict_test.py
repos/pre-commit/pre-commit-hooks:tests/check_merge_conflict_test.py
repos/pre-commit/pre-commit-hooks:tests/check_symlinks_test.py
repos/pre-commit/pre-commit-hooks:tests/check_vcs_permalinks_test.py
repos/pre-commit/pre-commit-hooks:tests/forbid_new_submodules_test.py
repos/pre-commit/pre-commit-hooks:tests/no_commit_to_branch_test.py
repos/pre-commit/pre-commit-mirror-maker:tests/make_repo_test.py
repos/sass/libsass-python:sasstests.py

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@asottile should i fix them?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ehhhhhh I can fix them if the apis go away

Comment thread src/_pytest/tmpdir.py Outdated
if x.name.lower().startswith(l_prefix)
),
-1,
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this code was a major head scratcher for me, maybe refactor it to not use reduce?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

there is not max(iterable, default=...) on python2

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤷‍♂️ the for loop might be more readable, or try: n = max(...) except TypeError: n = ...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

i extracted it a bit - the for loop is slower due to executing on the interpreter, impending push after i have a first iteration of folder cleanup

Comment thread .pre-commit-config.yaml Resolved
@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

just a note on the lack of progress, i caught a cold

@nicoddemus

Copy link
Copy Markdown
Member

Sorry to hear, get better soon!

@codecov

codecov Bot commented Sep 18, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #3988 into features will increase coverage by <.01%.
The diff coverage is 92.98%.

Impacted file tree graph

@@             Coverage Diff              @@
##           features    #3988      +/-   ##
============================================
+ Coverage     94.52%   94.53%   +<.01%     
============================================
  Files           109      109              
  Lines         23939    24142     +203     
  Branches       2362     2382      +20     
============================================
+ Hits          22629    22822     +193     
- Misses         1001     1009       +8     
- Partials        309      311       +2
Flag Coverage Δ
#doctesting 28.53% <50.35%> (-0.57%) ⬇️
#linux 94.38% <92.28%> (ø) ⬆️
#nobyte 0% <0%> (ø) ⬆️
#numpy 28.25% <52.46%> (+0.25%) ⬆️
#pexpect 0% <0%> (ø) ⬆️
#py27 92.63% <90.87%> (ø) ⬆️
#py34 92.06% <90.87%> (ø) ⬆️
#py35 92.07% <90.87%> (ø) ⬆️
#py36 92.63% <90.87%> (ø) ⬆️
#py37 92.27% <90.87%> (-0.01%) ⬇️
#trial 31.45% <52.46%> (+0.22%) ⬆️
#windows 93.8% <92.98%> (-0.08%) ⬇️
#xdist 18.95% <50%> (+0.28%) ⬆️
Impacted Files Coverage Δ
src/_pytest/compat.py 96.55% <ø> (-0.08%) ⬇️
src/_pytest/main.py 96.19% <ø> (ø) ⬆️
src/_pytest/assertion/rewrite.py 95.35% <100%> (ø) ⬆️
src/_pytest/pytester.py 86.43% <100%> (+0.05%) ⬆️
testing/python/fixture.py 99.23% <100%> (ø) ⬆️
testing/test_paths.py 100% <100%> (ø) ⬆️
src/_pytest/tmpdir.py 100% <100%> (ø) ⬆️
src/_pytest/cacheprovider.py 96.9% <100%> (-0.02%) ⬇️
src/_pytest/deprecated.py 100% <100%> (ø) ⬆️
testing/test_tmpdir.py 100% <100%> (ø) ⬆️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8ecdd4e...4736b2b. Read the comment docs.

@nicoddemus nicoddemus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looking pretty good so far. 👍

Comment thread src/_pytest/tmpdir.py Outdated
yield x


def extract_suffixees(iter, prefix):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

typo: "suffixees"

Comment thread src/_pytest/tmpdir.py Outdated


def make_numbered_dir_with_cleanup(root, prefix, keep, consider_lock_dead_after):
for i in range(10):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why the loop?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this is part of the currency preparation

creating a folder may fail at the first level

a the second level in the timeframe between creating a folder and creating the lock cleanup might rename the folder

im toying with some ideas to limit the need for the loops but for practical purposes i am following the implementation of py.path.local.make_numbered_dir closely in that regard

@RonnyPfannschmidt RonnyPfannschmidt changed the title [WIP] Tmpdir port pathlib Tmpdir port pathlib Sep 26, 2018
@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

removing the wip, a few more exception handlers/ignorers might be needed

Comment thread src/_pytest/tmpdir.py Resolved
Comment thread src/_pytest/tmpdir.py Outdated

def make_numbered_dir_with_cleanup(root, prefix, keep, lock_timeout):
for i in range(10):
try:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this try/except seems unnecessary given that it only re-raises any caught exception

Comment thread src/_pytest/tmpdir.py Resolved
Comment thread src/_pytest/tmpdir.py Outdated
root=root,
prefix=prefix,
keep=keep,
consider_lock_dead_after=consider_lock_dead_after,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this correct? We are computing a consider_lock_dead_after as the current time (we just created p) + some time in the future, and we are using this date in the future to decide if a lock file can be considered dead in ensure_deletable. It seems to me we should be passing the current time plus a lock_timeout, and if the last mtime of a lock is less than current_time - lock_timeout, the lock can be considered dead.

Or am I missing something?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

good catch, i accidentally reversed the calculation

Comment thread src/_pytest/tmpdir.py


@attr.s
class TempPathFactory(object):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add docstrings to all the public functions here as they will appear in the docs.

Comment thread src/_pytest/tmpdir.py Outdated
def __init__(self, config):
self.config = config
self.trace = config.trace.get("tmpdir")
tmppath_factory = attr.ib()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We probably want to keep this private no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yup

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems you missed this comment

Comment thread src/_pytest/tmpdir.py Outdated


@pytest.fixture
def tmp_path(tmpdir):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

docstring please

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

missed this one too

Comment thread testing/test_tmpdir.py Outdated
monkeypatch.delenv(envvar, raising=False)


@pytest.mark.skip(reason="creates random tmpdirs as part of a system level test")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are this skip and the next one in this file temporary?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This still needs to be addressed it seems

Comment thread testing/test_tmpdir.py Resolved
@RonnyPfannschmidt RonnyPfannschmidt changed the title Tmpdir port pathlib [WIP] Tmpdir port pathlib Sep 29, 2018
@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

the basic structural details are finished, but shutil.rmtree is insufficient since its rather limited,
i must reimplement rm -rf

Comment thread src/_pytest/pathlib.py
delete_a_numbered_dir(path)


def cleanup_candidates(root, prefix, keep):

@nicoddemus nicoddemus Sep 29, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This will not actually keep the number of directories, but will remove all directories with prefix smaller than the maximum one found minus keep. For example:

fun-10
fun-11
fun-12
fun-16
fun-20

With keep=3, only fun-20 will be left in the directory. keep=3 could be considered "leave the last 3 folders", in which case fun-12, fun-16 and fun-20 should be kept.

I don't mind either way, just mentioning this to make sure we are getting the behavior we want (and we should document it to be clear as well).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@nicoddemus its the current behaviour

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see, thanks. Let's make sure to document it later then. 👍

@RonnyPfannschmidt RonnyPfannschmidt changed the title [WIP] Tmpdir port pathlib Tmpdir port pathlib Oct 1, 2018
@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

i'll have to leave out forced delete, the python stdlib is too broken to do it reliably

@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

aka this will be a followup

@nicoddemus nicoddemus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Besides the comments below, I think that before merging we also need:

  • Changelog entry
  • Update the tmpdir docs and reference

Comment thread src/_pytest/pathlib.py Resolved
Comment thread src/_pytest/pathlib.py Outdated

def _max(iterable, default):
"""needed due to python2.7 lacking the default argument for max"""
return reduce(max, iterable, default)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I suggest to change this to:

if six.PY2:
    def _max(iterable, default):
        """needed due to python2.7 lacking the default argument for max"""
        return reduce(max, iterable, default)
else:
    _max = max

So it is easier to find when we start to cleanup the codebase after dropping Python 2. 😁

Comment thread src/_pytest/pathlib.py Resolved
Comment thread src/_pytest/tmpdir.py Resolved
Comment thread src/_pytest/tmpdir.py Resolved
Comment thread src/_pytest/tmpdir.py Resolved
Comment thread src/_pytest/tmpdir.py Outdated
def __init__(self, config):
self.config = config
self.trace = config.trace.get("tmpdir")
tmppath_factory = attr.ib()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems you missed this comment

Comment thread src/_pytest/tmpdir.py Outdated


@pytest.fixture
def tmp_path(tmpdir):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

missed this one too

Comment thread testing/test_tmpdir.py Outdated
monkeypatch.delenv(envvar, raising=False)


@pytest.mark.skip(reason="creates random tmpdirs as part of a system level test")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This still needs to be addressed it seems

Comment thread testing/test_tmpdir.py Outdated
reprec.assertoutcome(passed=1)


@pytest.mark.skip(reason="creates random tmpdirs as part of a system level test")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ditto

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great, awesome work @RonnyPfannschmidt!

The only thing missing are docs (reference and updating the current tmpdir docs to mention the new tmp_path fixture).

Comment thread src/_pytest/deprecated.py
"pytest_namespace is deprecated and will be removed soon"
)

PYTEST_ENSURETEMP = RemovedInPytest4Warning(

ghost Oct 11, 2018

Copy link
Copy Markdown
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. 👍

Comment thread doc/en/tmpdir.rst Resolved
Comment thread doc/en/tmpdir.rst

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

# content of test_tmp_path.py

ghost Oct 11, 2018

Copy link
Copy Markdown
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.

Comment thread src/_pytest/tmpdir.py Outdated
The base directory can be configured using the ``--basetemp`` option.
The base directory can be configured using the ``--basetemp`` option."""

given_basetemp = attr.ib()

ghost Oct 11, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is still public, I think it should really be private

Comment thread src/_pytest/tmpdir.py Outdated
"""docstring for ClassName"""

given_basetemp = attr.ib()
trace = attr.ib()

ghost Oct 11, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is still public, I think it should really be private

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Awesome work, thanks!

Comment thread .pre-commit-config.yaml Resolved
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the tmpdir-port-pathlib branch 3 times, most recently from 65398ab to 4534614 Compare October 11, 2018 15:29
@nicoddemus nicoddemus mentioned this pull request Oct 11, 2018
@nicoddemus
nicoddemus merged commit 933de16 into pytest-dev:features Oct 12, 2018
@nicoddemus

ghost commented Oct 12, 2018

Copy link
Copy Markdown
Member

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

5 participants