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
|
||||
|
||||
|
||||
def _max(iterable, default):
|
||||
"""needed due to python2.7 lacking the default argument for max"""
|
||||
return reduce(max, iterable, default)
|
||||
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
|
||||
|
||||
|
||||
def make_numbered_dir(root, prefix):
|
||||
"""create a directory with a increased number as suffix for the given prefix"""
|
||||
for i in range(10):
|
||||
# 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_path = root.joinpath("{}{}".format(prefix, new_number))
|
||||
try:
|
||||
|
@ -109,9 +115,8 @@ def make_numbered_dir(root, prefix):
|
|||
return new_path
|
||||
else:
|
||||
raise EnvironmentError(
|
||||
"could not create numbered dir with prefix {prefix} in {root})".format(
|
||||
prefix=prefix, root=root
|
||||
)
|
||||
"could not create numbered dir with prefix "
|
||||
"{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):
|
||||
"""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
|
||||
paths = find_prefixed(root, prefix)
|
||||
paths, paths2 = itertools.tee(paths)
|
||||
|
|
|
@ -17,7 +17,9 @@ from .pathlib import (
|
|||
|
||||
@attr.s
|
||||
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()
|
||||
trace = attr.ib()
|
||||
|
@ -25,11 +27,15 @@ class TempPathFactory(object):
|
|||
|
||||
@classmethod
|
||||
def from_config(cls, config):
|
||||
"""
|
||||
:param config: a pytest configuration
|
||||
"""
|
||||
return cls(
|
||||
given_basetemp=config.option.basetemp, trace=config.trace.get("tmpdir")
|
||||
)
|
||||
|
||||
def mktemp(self, basename, numbered=True):
|
||||
"""makes a temporary directory managed by the factory"""
|
||||
if not numbered:
|
||||
p = self.getbasetemp().joinpath(basename)
|
||||
p.mkdir()
|
||||
|
@ -64,12 +70,12 @@ class TempPathFactory(object):
|
|||
|
||||
@attr.s
|
||||
class TempdirFactory(object):
|
||||
"""Factory for temporary directories under the common base temp directory.
|
||||
|
||||
The base directory can be configured using the ``--basetemp`` option.
|
||||
"""
|
||||
backward comptibility wrapper that implements
|
||||
:class:``py.path.local`` for :class:``TempPathFactory``
|
||||
"""
|
||||
|
||||
tmppath_factory = attr.ib()
|
||||
_tmppath_factory = attr.ib()
|
||||
|
||||
def ensuretemp(self, string, dir=1):
|
||||
""" (deprecated) return temporary directory path with
|
||||
|
@ -86,13 +92,13 @@ class TempdirFactory(object):
|
|||
If ``numbered``, ensure the directory is unique by adding a number
|
||||
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):
|
||||
return py.path.local(self.tmppath_factory.getbasetemp().resolve())
|
||||
return py.path.local(self._tmppath_factory.getbasetemp().resolve())
|
||||
|
||||
def finish(self):
|
||||
self.tmppath_factory.trace("finish")
|
||||
self._tmppath_factory.trace("finish")
|
||||
|
||||
|
||||
def get_user():
|
||||
|
@ -150,4 +156,15 @@ def tmpdir(request, tmpdir_factory):
|
|||
|
||||
@pytest.fixture
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue