Skip to content

Commit 8e00df4

Browse files
authored
Add dot prefix if file makefile extension is invalid for pathlib (#8222)
1 parent 8d16bec commit 8e00df4

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Aron Curzon
4040
Aviral Verma
4141
Aviv Palivoda
4242
Barney Gale
43+
Ben Gartner
4344
Ben Webb
4445
Benjamin Peterson
4546
Bernard Pratz

changelog/8192.bugfix.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``testdir.makefile` now silently accepts values which don't start with ``.`` to maintain backward compatibility with older pytest versions.
2+
3+
``pytester.makefile`` now issues a clearer error if the ``.`` is missing in the ``ext`` argument.

src/_pytest/pytester.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
from _pytest.tmpdir import TempPathFactory
6565
from _pytest.warning_types import PytestWarning
6666

67+
6768
if TYPE_CHECKING:
6869
from typing_extensions import Literal
6970

@@ -750,6 +751,11 @@ def _makefile(
750751
) -> Path:
751752
items = list(files.items())
752753

754+
if ext and not ext.startswith("."):
755+
raise ValueError(
756+
f"pytester.makefile expects a file extension, try .{ext} instead of {ext}"
757+
)
758+
753759
def to_text(s: Union[Any, bytes]) -> str:
754760
return s.decode(encoding) if isinstance(s, bytes) else str(s)
755761

@@ -1559,6 +1565,14 @@ def finalize(self) -> None:
15591565

15601566
def makefile(self, ext, *args, **kwargs) -> py.path.local:
15611567
"""See :meth:`Pytester.makefile`."""
1568+
if ext and not ext.startswith("."):
1569+
# pytester.makefile is going to throw a ValueError in a way that
1570+
# testdir.makefile did not, because
1571+
# pathlib.Path is stricter suffixes than py.path
1572+
# This ext arguments is likely user error, but since testdir has
1573+
# allowed this, we will prepend "." as a workaround to avoid breaking
1574+
# testdir usage that worked before
1575+
ext = "." + ext
15621576
return py.path.local(str(self._pytester.makefile(ext, *args, **kwargs)))
15631577

15641578
def makeconftest(self, source) -> py.path.local:

testing/test_pytester.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from _pytest.pytester import Pytester
1818
from _pytest.pytester import SysModulesSnapshot
1919
from _pytest.pytester import SysPathsSnapshot
20+
from _pytest.pytester import Testdir
2021

2122

2223
def test_make_hook_recorder(pytester: Pytester) -> None:
@@ -816,3 +817,33 @@ def test_makefile_joins_absolute_path(pytester: Pytester) -> None:
816817
def test_testtmproot(testdir) -> None:
817818
"""Check test_tmproot is a py.path attribute for backward compatibility."""
818819
assert testdir.test_tmproot.check(dir=1)
820+
821+
822+
def test_testdir_makefile_dot_prefixes_extension_silently(
823+
testdir: Testdir,
824+
) -> None:
825+
"""For backwards compat #8192"""
826+
p1 = testdir.makefile("foo.bar", "")
827+
assert ".foo.bar" in str(p1)
828+
829+
830+
def test_pytester_makefile_dot_prefixes_extension_with_warning(
831+
pytester: Pytester,
832+
) -> None:
833+
with pytest.raises(
834+
ValueError,
835+
match="pytester.makefile expects a file extension, try .foo.bar instead of foo.bar",
836+
):
837+
pytester.makefile("foo.bar", "")
838+
839+
840+
def test_testdir_makefile_ext_none_raises_type_error(testdir) -> None:
841+
"""For backwards compat #8192"""
842+
with pytest.raises(TypeError):
843+
testdir.makefile(None, "")
844+
845+
846+
def test_testdir_makefile_ext_empty_string_makes_file(testdir) -> None:
847+
"""For backwards compat #8192"""
848+
p1 = testdir.makefile("", "")
849+
assert "test_testdir_makefile" in str(p1)

0 commit comments

Comments
 (0)