resolve in code review commments
This commit is contained in:
parent
ad6f63edda
commit
4a436b5470
|
@ -89,16 +89,22 @@ def parse_num(maybe_num):
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
|
||||||
def _max(iterable, default):
|
if six.PY2:
|
||||||
"""needed due to python2.7 lacking the default argument for max"""
|
|
||||||
return reduce(max, iterable, default)
|
def _max(iterable, default):
|
||||||
|
"""needed due to python2.7 lacking the default argument for max"""
|
||||||
|
return reduce(max, iterable, default)
|
||||||
|
|
||||||
|
|
||||||
|
else:
|
||||||
|
_max = max
|
||||||
|
|
||||||
|
|
||||||
def make_numbered_dir(root, prefix):
|
def make_numbered_dir(root, prefix):
|
||||||
"""create a directory with a increased number as suffix for the given prefix"""
|
"""create a directory with a increased number as suffix for the given prefix"""
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
# try up to 10 times to create the folder
|
# try up to 10 times to create the folder
|
||||||
max_existing = _max(map(parse_num, find_suffixes(root, prefix)), -1)
|
max_existing = _max(map(parse_num, find_suffixes(root, prefix)), default=-1)
|
||||||
new_number = max_existing + 1
|
new_number = max_existing + 1
|
||||||
new_path = root.joinpath("{}{}".format(prefix, new_number))
|
new_path = root.joinpath("{}{}".format(prefix, new_number))
|
||||||
try:
|
try:
|
||||||
|
@ -109,9 +115,8 @@ def make_numbered_dir(root, prefix):
|
||||||
return new_path
|
return new_path
|
||||||
else:
|
else:
|
||||||
raise EnvironmentError(
|
raise EnvironmentError(
|
||||||
"could not create numbered dir with prefix {prefix} in {root})".format(
|
"could not create numbered dir with prefix "
|
||||||
prefix=prefix, root=root
|
"{prefix} in {root} after 10 tries".format(prefix=prefix, root=root)
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -191,7 +196,7 @@ def try_cleanup(path, consider_lock_dead_if_created_before):
|
||||||
|
|
||||||
def cleanup_candidates(root, prefix, keep):
|
def cleanup_candidates(root, prefix, keep):
|
||||||
"""lists candidates for numbered directories to be removed - follows py.path"""
|
"""lists candidates for numbered directories to be removed - follows py.path"""
|
||||||
max_existing = _max(map(parse_num, find_suffixes(root, prefix)), -1)
|
max_existing = _max(map(parse_num, find_suffixes(root, prefix)), default=-1)
|
||||||
max_delete = max_existing - keep
|
max_delete = max_existing - keep
|
||||||
paths = find_prefixed(root, prefix)
|
paths = find_prefixed(root, prefix)
|
||||||
paths, paths2 = itertools.tee(paths)
|
paths, paths2 = itertools.tee(paths)
|
||||||
|
|
|
@ -17,7 +17,9 @@ from .pathlib import (
|
||||||
|
|
||||||
@attr.s
|
@attr.s
|
||||||
class TempPathFactory(object):
|
class TempPathFactory(object):
|
||||||
"""docstring for ClassName"""
|
"""Factory for temporary directories under the common base temp directory.
|
||||||
|
|
||||||
|
The base directory can be configured using the ``--basetemp`` option."""
|
||||||
|
|
||||||
given_basetemp = attr.ib()
|
given_basetemp = attr.ib()
|
||||||
trace = attr.ib()
|
trace = attr.ib()
|
||||||
|
@ -25,11 +27,15 @@ class TempPathFactory(object):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_config(cls, config):
|
def from_config(cls, config):
|
||||||
|
"""
|
||||||
|
:param config: a pytest configuration
|
||||||
|
"""
|
||||||
return cls(
|
return cls(
|
||||||
given_basetemp=config.option.basetemp, trace=config.trace.get("tmpdir")
|
given_basetemp=config.option.basetemp, trace=config.trace.get("tmpdir")
|
||||||
)
|
)
|
||||||
|
|
||||||
def mktemp(self, basename, numbered=True):
|
def mktemp(self, basename, numbered=True):
|
||||||
|
"""makes a temporary directory managed by the factory"""
|
||||||
if not numbered:
|
if not numbered:
|
||||||
p = self.getbasetemp().joinpath(basename)
|
p = self.getbasetemp().joinpath(basename)
|
||||||
p.mkdir()
|
p.mkdir()
|
||||||
|
@ -64,12 +70,12 @@ class TempPathFactory(object):
|
||||||
|
|
||||||
@attr.s
|
@attr.s
|
||||||
class TempdirFactory(object):
|
class TempdirFactory(object):
|
||||||
"""Factory for temporary directories under the common base temp directory.
|
"""
|
||||||
|
backward comptibility wrapper that implements
|
||||||
The base directory can be configured using the ``--basetemp`` option.
|
:class:``py.path.local`` for :class:``TempPathFactory``
|
||||||
"""
|
"""
|
||||||
|
|
||||||
tmppath_factory = attr.ib()
|
_tmppath_factory = attr.ib()
|
||||||
|
|
||||||
def ensuretemp(self, string, dir=1):
|
def ensuretemp(self, string, dir=1):
|
||||||
""" (deprecated) return temporary directory path with
|
""" (deprecated) return temporary directory path with
|
||||||
|
@ -86,13 +92,13 @@ class TempdirFactory(object):
|
||||||
If ``numbered``, ensure the directory is unique by adding a number
|
If ``numbered``, ensure the directory is unique by adding a number
|
||||||
prefix greater than any existing one.
|
prefix greater than any existing one.
|
||||||
"""
|
"""
|
||||||
return py.path.local(self.tmppath_factory.mktemp(basename, numbered).resolve())
|
return py.path.local(self._tmppath_factory.mktemp(basename, numbered).resolve())
|
||||||
|
|
||||||
def getbasetemp(self):
|
def getbasetemp(self):
|
||||||
return py.path.local(self.tmppath_factory.getbasetemp().resolve())
|
return py.path.local(self._tmppath_factory.getbasetemp().resolve())
|
||||||
|
|
||||||
def finish(self):
|
def finish(self):
|
||||||
self.tmppath_factory.trace("finish")
|
self._tmppath_factory.trace("finish")
|
||||||
|
|
||||||
|
|
||||||
def get_user():
|
def get_user():
|
||||||
|
@ -150,4 +156,15 @@ def tmpdir(request, tmpdir_factory):
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def tmp_path(tmpdir):
|
def tmp_path(tmpdir):
|
||||||
|
"""Return a temporary directory path object
|
||||||
|
which is unique to each test function invocation,
|
||||||
|
created as a sub directory of the base temporary
|
||||||
|
directory. The returned object is a :class:`pathlib.Path`
|
||||||
|
object.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
in python < 3.6 this is a pathlib2.Path
|
||||||
|
"""
|
||||||
|
|
||||||
return Path(tmpdir)
|
return Path(tmpdir)
|
||||||
|
|
Loading…
Reference in New Issue