Merge pull request #4427 from RonnyPfannschmidt/fix-4425
fix 4425: resolve --basetemp to absolute paths
This commit is contained in:
commit
9424d88843
|
@ -0,0 +1 @@
|
||||||
|
Ensure we resolve the absolute path when the given ``--basetemp`` is a relative path.
|
|
@ -13,8 +13,9 @@ import six
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from _pytest.fixtures import fixture
|
from _pytest.fixtures import fixture
|
||||||
|
from _pytest.pathlib import Path
|
||||||
|
|
||||||
RE_IMPORT_ERROR_NAME = re.compile("^No module named (.*)$")
|
RE_IMPORT_ERROR_NAME = re.compile(r"^No module named (.*)$")
|
||||||
|
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
|
@ -267,6 +268,9 @@ class MonkeyPatch(object):
|
||||||
self._cwd = os.getcwd()
|
self._cwd = os.getcwd()
|
||||||
if hasattr(path, "chdir"):
|
if hasattr(path, "chdir"):
|
||||||
path.chdir()
|
path.chdir()
|
||||||
|
elif isinstance(path, Path):
|
||||||
|
# modern python uses the fspath protocol here LEGACY
|
||||||
|
os.chdir(str(path))
|
||||||
else:
|
else:
|
||||||
os.chdir(path)
|
os.chdir(path)
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ import warnings
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
import py
|
import py
|
||||||
|
import six
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from .pathlib import ensure_reset_dir
|
from .pathlib import ensure_reset_dir
|
||||||
|
@ -26,7 +27,14 @@ class TempPathFactory(object):
|
||||||
|
|
||||||
The base directory can be configured using the ``--basetemp`` option."""
|
The base directory can be configured using the ``--basetemp`` option."""
|
||||||
|
|
||||||
_given_basetemp = attr.ib()
|
_given_basetemp = attr.ib(
|
||||||
|
# using os.path.abspath() to get absolute path instead of resolve() as it
|
||||||
|
# does not work the same in all platforms (see #4427)
|
||||||
|
# Path.absolute() exists, but it is not public (see https://bugs.python.org/issue25012)
|
||||||
|
convert=attr.converters.optional(
|
||||||
|
lambda p: Path(os.path.abspath(six.text_type(p)))
|
||||||
|
)
|
||||||
|
)
|
||||||
_trace = attr.ib()
|
_trace = attr.ib()
|
||||||
_basetemp = attr.ib(default=None)
|
_basetemp = attr.ib(default=None)
|
||||||
|
|
||||||
|
@ -53,7 +61,7 @@ class TempPathFactory(object):
|
||||||
""" return base temporary directory. """
|
""" return base temporary directory. """
|
||||||
if self._basetemp is None:
|
if self._basetemp is None:
|
||||||
if self._given_basetemp is not None:
|
if self._given_basetemp is not None:
|
||||||
basetemp = Path(self._given_basetemp)
|
basetemp = self._given_basetemp
|
||||||
ensure_reset_dir(basetemp)
|
ensure_reset_dir(basetemp)
|
||||||
else:
|
else:
|
||||||
from_env = os.environ.get("PYTEST_DEBUG_TEMPROOT")
|
from_env = os.environ.get("PYTEST_DEBUG_TEMPROOT")
|
||||||
|
|
|
@ -4,6 +4,7 @@ from __future__ import print_function
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import attr
|
||||||
import six
|
import six
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -25,12 +26,29 @@ def test_ensuretemp(recwarn):
|
||||||
assert d1.check(dir=1)
|
assert d1.check(dir=1)
|
||||||
|
|
||||||
|
|
||||||
|
@attr.s
|
||||||
|
class FakeConfig(object):
|
||||||
|
basetemp = attr.ib()
|
||||||
|
trace = attr.ib(default=None)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def trace(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def get(self, key):
|
||||||
|
return lambda *k: None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def option(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
class TestTempdirHandler(object):
|
class TestTempdirHandler(object):
|
||||||
def test_mktemp(self, testdir):
|
def test_mktemp(self, tmp_path):
|
||||||
|
|
||||||
from _pytest.tmpdir import TempdirFactory, TempPathFactory
|
from _pytest.tmpdir import TempdirFactory, TempPathFactory
|
||||||
|
|
||||||
config = testdir.parseconfig()
|
config = FakeConfig(tmp_path)
|
||||||
config.option.basetemp = testdir.mkdir("hello")
|
|
||||||
t = TempdirFactory(TempPathFactory.from_config(config))
|
t = TempdirFactory(TempPathFactory.from_config(config))
|
||||||
tmp = t.mktemp("world")
|
tmp = t.mktemp("world")
|
||||||
assert tmp.relto(t.getbasetemp()) == "world0"
|
assert tmp.relto(t.getbasetemp()) == "world0"
|
||||||
|
@ -40,6 +58,15 @@ class TestTempdirHandler(object):
|
||||||
assert tmp2.relto(t.getbasetemp()).startswith("this")
|
assert tmp2.relto(t.getbasetemp()).startswith("this")
|
||||||
assert tmp2 != tmp
|
assert tmp2 != tmp
|
||||||
|
|
||||||
|
@pytest.mark.issue(4425)
|
||||||
|
def test_tmppath_relative_basetemp_absolute(self, tmp_path, monkeypatch):
|
||||||
|
from _pytest.tmpdir import TempPathFactory
|
||||||
|
|
||||||
|
monkeypatch.chdir(tmp_path)
|
||||||
|
config = FakeConfig("hello")
|
||||||
|
t = TempPathFactory.from_config(config)
|
||||||
|
assert t.getbasetemp().resolve() == (tmp_path / "hello").resolve()
|
||||||
|
|
||||||
|
|
||||||
class TestConfigTmpdir(object):
|
class TestConfigTmpdir(object):
|
||||||
def test_getbasetemp_custom_removes_old(self, testdir):
|
def test_getbasetemp_custom_removes_old(self, testdir):
|
||||||
|
|
Loading…
Reference in New Issue