From 8ab1d64e3f82372c7ec4f3cf4cda3e3e1e504b01 Mon Sep 17 00:00:00 2001 From: benjotron Date: Sat, 2 Jan 2021 16:29:44 -0600 Subject: [PATCH 1/4] Prepend dot if testdir.makefile ext arg is invalid for pathlib (#8192) --- AUTHORS | 1 + changelog/8192.bugfix.rst | 1 + src/_pytest/pytester.py | 14 ++++++++++++++ testing/test_pytester.py | 19 +++++++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 changelog/8192.bugfix.rst diff --git a/AUTHORS b/AUTHORS index abac9f010a1..e75baa8cd92 100644 --- a/AUTHORS +++ b/AUTHORS @@ -40,6 +40,7 @@ Aron Curzon Aviral Verma Aviv Palivoda Barney Gale +Ben Gartner Ben Webb Benjamin Peterson Bernard Pratz diff --git a/changelog/8192.bugfix.rst b/changelog/8192.bugfix.rst new file mode 100644 index 00000000000..2ce6a258a9a --- /dev/null +++ b/changelog/8192.bugfix.rst @@ -0,0 +1 @@ +Prepend '.' to ext argument in makefile before passing to pathlib and raise warning diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index 4544d2c2bbb..95b22b3b23e 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -64,6 +64,7 @@ from _pytest.tmpdir import TempPathFactory from _pytest.warning_types import PytestWarning + if TYPE_CHECKING: from typing_extensions import Literal @@ -750,6 +751,11 @@ def _makefile( ) -> Path: 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: return s.decode(encoding) if isinstance(s, bytes) else str(s) @@ -1559,6 +1565,14 @@ def finalize(self) -> None: def makefile(self, ext, *args, **kwargs) -> py.path.local: """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))) def makeconftest(self, source) -> py.path.local: diff --git a/testing/test_pytester.py b/testing/test_pytester.py index 57d6f4fd9eb..5a0bedc2230 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -17,6 +17,7 @@ from _pytest.pytester import Pytester from _pytest.pytester import SysModulesSnapshot from _pytest.pytester import SysPathsSnapshot +from _pytest.pytester import Testdir def test_make_hook_recorder(pytester: Pytester) -> None: @@ -816,3 +817,21 @@ def test_makefile_joins_absolute_path(pytester: Pytester) -> None: def test_testtmproot(testdir) -> None: """Check test_tmproot is a py.path attribute for backward compatibility.""" assert testdir.test_tmproot.check(dir=1) + + +def test_testdir_makefile_dot_prefixes_extension_silently( + testdir: Testdir, +) -> None: + 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", + ): + p1 = pytester.makefile("foo.bar", "") + assert ".foo.bar" in str(p1) From ad0c4378bdbec4b07aeff4b34a519ae33eb5af5d Mon Sep 17 00:00:00 2001 From: benjotron Date: Sun, 3 Jan 2021 14:53:07 -0600 Subject: [PATCH 2/4] Test ext is Falsey paths for testdir.makefile() to prevent testdir regression (#8192) --- testing/test_pytester.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/testing/test_pytester.py b/testing/test_pytester.py index 5a0bedc2230..774d69f019b 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -822,6 +822,7 @@ def test_testtmproot(testdir) -> None: 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) @@ -835,3 +836,15 @@ def test_pytester_makefile_dot_prefixes_extension_with_warning( ): p1 = pytester.makefile("foo.bar", "") assert ".foo.bar" in str(p1) + + +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) From 5b6a3775cd1ffb12be56ad3bfad87c8457b06d60 Mon Sep 17 00:00:00 2001 From: benjotron Date: Sun, 3 Jan 2021 15:43:24 -0600 Subject: [PATCH 3/4] Unreachable line in test_pytester (#8192) --- testing/test_pytester.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/testing/test_pytester.py b/testing/test_pytester.py index 774d69f019b..5823d51155c 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -834,8 +834,7 @@ def test_pytester_makefile_dot_prefixes_extension_with_warning( ValueError, match="pytester.makefile expects a file extension, try .foo.bar instead of foo.bar", ): - p1 = pytester.makefile("foo.bar", "") - assert ".foo.bar" in str(p1) + pytester.makefile("foo.bar", "") def test_testdir_makefile_ext_none_raises_type_error(testdir) -> None: From 89781ccf0a30c0554b857505b460ebb2e8098b8e Mon Sep 17 00:00:00 2001 From: bengartner Date: Mon, 4 Jan 2021 07:38:15 -0600 Subject: [PATCH 4/4] Update changelog/8192.bugfix.rst Co-authored-by: Ran Benita --- changelog/8192.bugfix.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/changelog/8192.bugfix.rst b/changelog/8192.bugfix.rst index 2ce6a258a9a..5b26ecbe45c 100644 --- a/changelog/8192.bugfix.rst +++ b/changelog/8192.bugfix.rst @@ -1 +1,3 @@ -Prepend '.' to ext argument in makefile before passing to pathlib and raise warning +``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.