fix #5686, mktemp now fails given absolute and non-normalized paths.
This commit is contained in:
parent
985ac09048
commit
226f0c48bf
|
@ -0,0 +1 @@
|
||||||
|
``tmpdir_factory.mktemp`` now fails when given absolute and non-normalized paths.
|
|
@ -45,8 +45,17 @@ class TempPathFactory:
|
||||||
given_basetemp=config.option.basetemp, trace=config.trace.get("tmpdir")
|
given_basetemp=config.option.basetemp, trace=config.trace.get("tmpdir")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _ensure_relative_to_basetemp(self, basename: str):
|
||||||
|
basename = os.path.normpath(basename)
|
||||||
|
if (self.getbasetemp() / basename).resolve().parent != self.getbasetemp():
|
||||||
|
raise ValueError(
|
||||||
|
"{} is not a normalized and relative path".format(basename)
|
||||||
|
)
|
||||||
|
return basename
|
||||||
|
|
||||||
def mktemp(self, basename: str, numbered: bool = True) -> Path:
|
def mktemp(self, basename: str, numbered: bool = True) -> Path:
|
||||||
"""makes a temporary directory managed by the factory"""
|
"""makes a temporary directory managed by the factory"""
|
||||||
|
basename = self._ensure_relative_to_basetemp(basename)
|
||||||
if not numbered:
|
if not numbered:
|
||||||
p = self.getbasetemp().joinpath(basename)
|
p = self.getbasetemp().joinpath(basename)
|
||||||
p.mkdir()
|
p.mkdir()
|
||||||
|
|
|
@ -74,19 +74,38 @@ class TestConfigTmpdir:
|
||||||
assert not mytemp.join("hello").check()
|
assert not mytemp.join("hello").check()
|
||||||
|
|
||||||
|
|
||||||
def test_basetemp(testdir):
|
testdata = [
|
||||||
|
("mypath", True),
|
||||||
|
("/mypath1", False),
|
||||||
|
("./mypath1", True),
|
||||||
|
("../mypath3", False),
|
||||||
|
("../../mypath4", False),
|
||||||
|
("mypath5/..", False),
|
||||||
|
("mypath6/../mypath6", True),
|
||||||
|
("mypath7/../mypath7/..", False),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("basename, is_ok", testdata)
|
||||||
|
def test_mktemp(testdir, basename, is_ok):
|
||||||
mytemp = testdir.tmpdir.mkdir("mytemp")
|
mytemp = testdir.tmpdir.mkdir("mytemp")
|
||||||
p = testdir.makepyfile(
|
p = testdir.makepyfile(
|
||||||
"""
|
"""
|
||||||
import pytest
|
import pytest
|
||||||
def test_1(tmpdir_factory):
|
def test_abs_path(tmpdir_factory):
|
||||||
tmpdir_factory.mktemp('hello', numbered=False)
|
tmpdir_factory.mktemp('{}', numbered=False)
|
||||||
"""
|
""".format(
|
||||||
|
basename
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
result = testdir.runpytest(p, "--basetemp=%s" % mytemp)
|
result = testdir.runpytest(p, "--basetemp=%s" % mytemp)
|
||||||
assert result.ret == 0
|
if is_ok:
|
||||||
print(mytemp)
|
assert result.ret == 0
|
||||||
assert mytemp.join("hello").check()
|
assert mytemp.join(basename).check()
|
||||||
|
else:
|
||||||
|
assert result.ret == 1
|
||||||
|
result.stdout.fnmatch_lines("*ValueError*")
|
||||||
|
|
||||||
|
|
||||||
def test_tmpdir_always_is_realpath(testdir):
|
def test_tmpdir_always_is_realpath(testdir):
|
||||||
|
|
Loading…
Reference in New Issue