Add dot prefix if file makefile extension is invalid for pathlib (#8222)
This commit is contained in:
parent
8d16bec329
commit
8e00df4c4b
1
AUTHORS
1
AUTHORS
|
@ -40,6 +40,7 @@ Aron Curzon
|
||||||
Aviral Verma
|
Aviral Verma
|
||||||
Aviv Palivoda
|
Aviv Palivoda
|
||||||
Barney Gale
|
Barney Gale
|
||||||
|
Ben Gartner
|
||||||
Ben Webb
|
Ben Webb
|
||||||
Benjamin Peterson
|
Benjamin Peterson
|
||||||
Bernard Pratz
|
Bernard Pratz
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
``testdir.makefile` now silently accepts values which don't start with ``.`` to maintain backward compatibility with older pytest versions.
|
||||||
|
|
||||||
|
``pytester.makefile`` now issues a clearer error if the ``.`` is missing in the ``ext`` argument.
|
|
@ -64,6 +64,7 @@ from _pytest.reports import TestReport
|
||||||
from _pytest.tmpdir import TempPathFactory
|
from _pytest.tmpdir import TempPathFactory
|
||||||
from _pytest.warning_types import PytestWarning
|
from _pytest.warning_types import PytestWarning
|
||||||
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from typing_extensions import Literal
|
from typing_extensions import Literal
|
||||||
|
|
||||||
|
@ -750,6 +751,11 @@ class Pytester:
|
||||||
) -> Path:
|
) -> Path:
|
||||||
items = list(files.items())
|
items = list(files.items())
|
||||||
|
|
||||||
|
if ext and not ext.startswith("."):
|
||||||
|
raise ValueError(
|
||||||
|
f"pytester.makefile expects a file extension, try .{ext} instead of {ext}"
|
||||||
|
)
|
||||||
|
|
||||||
def to_text(s: Union[Any, bytes]) -> str:
|
def to_text(s: Union[Any, bytes]) -> str:
|
||||||
return s.decode(encoding) if isinstance(s, bytes) else str(s)
|
return s.decode(encoding) if isinstance(s, bytes) else str(s)
|
||||||
|
|
||||||
|
@ -1559,6 +1565,14 @@ class Testdir:
|
||||||
|
|
||||||
def makefile(self, ext, *args, **kwargs) -> py.path.local:
|
def makefile(self, ext, *args, **kwargs) -> py.path.local:
|
||||||
"""See :meth:`Pytester.makefile`."""
|
"""See :meth:`Pytester.makefile`."""
|
||||||
|
if ext and not ext.startswith("."):
|
||||||
|
# pytester.makefile is going to throw a ValueError in a way that
|
||||||
|
# testdir.makefile did not, because
|
||||||
|
# pathlib.Path is stricter suffixes than py.path
|
||||||
|
# This ext arguments is likely user error, but since testdir has
|
||||||
|
# allowed this, we will prepend "." as a workaround to avoid breaking
|
||||||
|
# testdir usage that worked before
|
||||||
|
ext = "." + ext
|
||||||
return py.path.local(str(self._pytester.makefile(ext, *args, **kwargs)))
|
return py.path.local(str(self._pytester.makefile(ext, *args, **kwargs)))
|
||||||
|
|
||||||
def makeconftest(self, source) -> py.path.local:
|
def makeconftest(self, source) -> py.path.local:
|
||||||
|
|
|
@ -17,6 +17,7 @@ from _pytest.pytester import LineMatcher
|
||||||
from _pytest.pytester import Pytester
|
from _pytest.pytester import Pytester
|
||||||
from _pytest.pytester import SysModulesSnapshot
|
from _pytest.pytester import SysModulesSnapshot
|
||||||
from _pytest.pytester import SysPathsSnapshot
|
from _pytest.pytester import SysPathsSnapshot
|
||||||
|
from _pytest.pytester import Testdir
|
||||||
|
|
||||||
|
|
||||||
def test_make_hook_recorder(pytester: Pytester) -> None:
|
def test_make_hook_recorder(pytester: Pytester) -> None:
|
||||||
|
@ -816,3 +817,33 @@ def test_makefile_joins_absolute_path(pytester: Pytester) -> None:
|
||||||
def test_testtmproot(testdir) -> None:
|
def test_testtmproot(testdir) -> None:
|
||||||
"""Check test_tmproot is a py.path attribute for backward compatibility."""
|
"""Check test_tmproot is a py.path attribute for backward compatibility."""
|
||||||
assert testdir.test_tmproot.check(dir=1)
|
assert testdir.test_tmproot.check(dir=1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_testdir_makefile_dot_prefixes_extension_silently(
|
||||||
|
testdir: Testdir,
|
||||||
|
) -> None:
|
||||||
|
"""For backwards compat #8192"""
|
||||||
|
p1 = testdir.makefile("foo.bar", "")
|
||||||
|
assert ".foo.bar" in str(p1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_pytester_makefile_dot_prefixes_extension_with_warning(
|
||||||
|
pytester: Pytester,
|
||||||
|
) -> None:
|
||||||
|
with pytest.raises(
|
||||||
|
ValueError,
|
||||||
|
match="pytester.makefile expects a file extension, try .foo.bar instead of foo.bar",
|
||||||
|
):
|
||||||
|
pytester.makefile("foo.bar", "")
|
||||||
|
|
||||||
|
|
||||||
|
def test_testdir_makefile_ext_none_raises_type_error(testdir) -> None:
|
||||||
|
"""For backwards compat #8192"""
|
||||||
|
with pytest.raises(TypeError):
|
||||||
|
testdir.makefile(None, "")
|
||||||
|
|
||||||
|
|
||||||
|
def test_testdir_makefile_ext_empty_string_makes_file(testdir) -> None:
|
||||||
|
"""For backwards compat #8192"""
|
||||||
|
p1 = testdir.makefile("", "")
|
||||||
|
assert "test_testdir_makefile" in str(p1)
|
||||||
|
|
Loading…
Reference in New Issue