Skip to content

Commit

Permalink
fix pytest-dev#5686, mktemp now fails given absolute and non-normaliz…
Browse files Browse the repository at this point in the history
…ed paths.
  • Loading branch information
gftea committed Dec 7, 2019
1 parent 985ac09 commit e667006
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/5686.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tmpdir_factory.mktemp now fails given absolute and non-normalized paths.
4 changes: 4 additions & 0 deletions src/_pytest/tmpdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .pathlib import Path
from _pytest.fixtures import FixtureRequest
from _pytest.monkeypatch import MonkeyPatch
from _pytest.outcomes import fail


@attr.s
Expand Down Expand Up @@ -47,6 +48,9 @@ def from_config(cls, config) -> "TempPathFactory":

def mktemp(self, basename: str, numbered: bool = True) -> Path:
"""makes a temporary directory managed by the factory"""

if os.path.isabs(basename):
fail("Absolute and non-normalized path is disallowed: {}".format(basename))
if not numbered:
p = self.getbasetemp().joinpath(basename)
p.mkdir()
Expand Down
21 changes: 20 additions & 1 deletion testing/test_tmpdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,33 @@ def test_basetemp(testdir):
import pytest
def test_1(tmpdir_factory):
tmpdir_factory.mktemp('hello', numbered=False)
"""
"""
)

result = testdir.runpytest(p, "--basetemp=%s" % mytemp)
assert result.ret == 0
print(mytemp)
assert mytemp.join("hello").check()


def test_basetemp_with_abs_path(testdir):
mytemp = testdir.tmpdir.mkdir("mytemp")
abs_path = "/hello"
p = testdir.makepyfile(
"""
import pytest
def test_abs_path(tmpdir_factory):
tmpdir_factory.mktemp('{}', numbered=False)
""".format(
abs_path
)
)

result = testdir.runpytest(p, "--basetemp=%s" % mytemp)
assert result.ret == 1
result.stdout.fnmatch_lines("*path is disallowed: {}*".format(abs_path))


def test_tmpdir_always_is_realpath(testdir):
# the reason why tmpdir should be a realpath is that
# when you cd to it and do "os.getcwd()" you will anyway
Expand Down

0 comments on commit e667006

Please sign in to comment.