From f5edd0897b838d8e267789dd9ee1072f386c333e Mon Sep 17 00:00:00 2001 From: root Date: Fri, 25 Oct 2024 11:48:21 +0300 Subject: [PATCH 1/4] Recurse into build / dist directories See #12625 for context --- changelog/12625.improvement.rst | 2 ++ src/_pytest/main.py | 22 ++++++++++++++++++++-- testing/test_collection.py | 25 +++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 changelog/12625.improvement.rst diff --git a/changelog/12625.improvement.rst b/changelog/12625.improvement.rst new file mode 100644 index 00000000000..7823a71297e --- /dev/null +++ b/changelog/12625.improvement.rst @@ -0,0 +1,2 @@ +Conditionally ignore collection of setuptools artifacts dirnames only if the +directories reside inside a setuptools project, i.e. `setup.cfg`, is present, etc. diff --git a/src/_pytest/main.py b/src/_pytest/main.py index e5534e98d69..fc9cf696e9d 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -63,9 +63,7 @@ def pytest_addoption(parser: Parser) -> None: "*.egg", ".*", "_darcs", - "build", "CVS", - "dist", "node_modules", "venv", "{arch}", @@ -367,6 +365,22 @@ def pytest_runtestloop(session: Session) -> bool: return True +def _in_build(path: Path) -> bool: + """Attempt to detect if ``path`` is the root of a buildsystem's artifacts + by checking known dirnames patterns, and the presence of configuration in + the parent dir by checking for a setup.py, setup.cfg, or pyproject.toml. + """ + if not path.is_dir(): + return False + + if any(fnmatch_ex(pat, path) for pat in ("build", "dist")): + indicators = ("setup.py", "setup.cfg", "pyproject.toml") + if any((path.parent / f).is_file() for f in indicators): + return True + + return False + + def _in_venv(path: Path) -> bool: """Attempt to detect if ``path`` is the root of a Virtual Environment by checking for the existence of the pyvenv.cfg file. @@ -418,6 +432,10 @@ def pytest_ignore_collect(collection_path: Path, config: Config) -> bool | None: if not allow_in_venv and _in_venv(collection_path): return True + allow_in_build = False # config.getoption("collect_in_build") + if not allow_in_build and _in_build(collection_path): + return True + if collection_path.is_dir(): norecursepatterns = config.getini("norecursedirs") if any(fnmatch_ex(pat, collection_path) for pat in norecursepatterns): diff --git a/testing/test_collection.py b/testing/test_collection.py index aba8f8ea48d..2bb33796bb0 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -137,8 +137,6 @@ def test_foo(): class TestCollectFS: def test_ignored_certain_directories(self, pytester: Pytester) -> None: tmp_path = pytester.path - ensure_file(tmp_path / "build" / "test_notfound.py") - ensure_file(tmp_path / "dist" / "test_notfound.py") ensure_file(tmp_path / "_darcs" / "test_notfound.py") ensure_file(tmp_path / "CVS" / "test_notfound.py") ensure_file(tmp_path / "{arch}" / "test_notfound.py") @@ -276,6 +274,29 @@ def test_missing_permissions_on_unselected_directory_doesnt_crash( assert result.ret == ExitCode.OK result.assert_outcomes(passed=1) + known_build_dirs = pytest.mark.parametrize("build_dir", ["build", "dist"]) + known_buildsystem_env = pytest.mark.parametrize( + "buildsystem_indicator_file", ["setup.py", "setup.cfg", "pyproject.toml"] + ) + + @known_build_dirs + @known_buildsystem_env + def test_build_dirs_collected( + self, pytester: Pytester, build_dir: str, buildsystem_indicator_file: str + ) -> None: + tmp_path = pytester.path + ensure_file(tmp_path / build_dir / "test_module.py").write_text( + "def test_hello(): pass", encoding="utf-8" + ) + + result = pytester.runpytest("--collect-only").stdout.str() + assert "test_module" in result + + ensure_file(tmp_path / buildsystem_indicator_file) + + result = pytester.runpytest("--collect-only").stdout.str() + assert "test_module" not in result + class TestCollectPluginHookRelay: def test_pytest_collect_file(self, pytester: Pytester) -> None: From 8d54d89df040e5192b2f254a030c32ba070a95a0 Mon Sep 17 00:00:00 2001 From: David Peled Date: Fri, 1 Nov 2024 20:45:27 +0200 Subject: [PATCH 2/4] Add test for setup.cfg indicator only if setup.py --- testing/test_collection.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/testing/test_collection.py b/testing/test_collection.py index 2bb33796bb0..3ff2af3b6f8 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -297,6 +297,26 @@ def test_build_dirs_collected( result = pytester.runpytest("--collect-only").stdout.str() assert "test_module" not in result + @known_build_dirs + def test_build_dirs_collected_when_setuptools_configuration_present( + self, pytester: Pytester, build_dir: str + ) -> None: + tmp_path = pytester.path + ensure_file(tmp_path / build_dir / "test_module.py").write_text( + "def test_hello(): pass", encoding="utf-8" + ) + + result = pytester.runpytest("--collect-only").stdout.str() + assert "test_module" in result + + ensure_file(tmp_path / "setup.cfg") + + result = pytester.runpytest("--collect-only").stdout.str() + assert "test_module" in result + + ensure_file(tmp_path / "setup.py") + assert "test_module" not in result + class TestCollectPluginHookRelay: def test_pytest_collect_file(self, pytester: Pytester) -> None: From c8f095b70701d4a5df1048cb0cb282ff3ee54097 Mon Sep 17 00:00:00 2001 From: David Peled Date: Sun, 10 Nov 2024 14:26:06 +0200 Subject: [PATCH 3/4] fix comment --- src/_pytest/main.py | 33 ++++++++++++++++++++++++++++++--- testing/test_collection.py | 22 +++++++++++++--------- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index fc9cf696e9d..1ffe8506164 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -12,6 +12,7 @@ from pathlib import Path import sys from typing import AbstractSet +from typing import Any from typing import Callable from typing import Dict from typing import final @@ -365,6 +366,20 @@ def pytest_runtestloop(session: Session) -> bool: return True +def _decode_toml_file(toml: Path) -> dict[str, Any] | None: + """Attempt to decode a toml file into a dict, returning None if fails.""" + if sys.version_info >= (3, 11): + import tomllib + else: + import tomli as tomllib + + try: + toml_text = toml.read_text(encoding="utf-8") + return tomllib.loads(toml_text) + except tomllib.TOMLDecodeError: + return None + + def _in_build(path: Path) -> bool: """Attempt to detect if ``path`` is the root of a buildsystem's artifacts by checking known dirnames patterns, and the presence of configuration in @@ -374,9 +389,21 @@ def _in_build(path: Path) -> bool: return False if any(fnmatch_ex(pat, path) for pat in ("build", "dist")): - indicators = ("setup.py", "setup.cfg", "pyproject.toml") - if any((path.parent / f).is_file() for f in indicators): - return True + setup_cfg = path.parent / "setup.cfg" + if (setup_cfg).is_file(): + setup_py = path.parent / "setup.py" + if setup_py.is_file(): + return True + + pyproject_toml = path.parent / "pyproject.toml" + if pyproject_toml.is_file(): + config = _decode_toml_file(pyproject_toml) + if config: + if any( + "setuptools" in cfg + for cfg in config.get("build-system", {}).get("requires", {}) + ): + return True return False diff --git a/testing/test_collection.py b/testing/test_collection.py index 3ff2af3b6f8..9713afeee99 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -275,14 +275,10 @@ def test_missing_permissions_on_unselected_directory_doesnt_crash( result.assert_outcomes(passed=1) known_build_dirs = pytest.mark.parametrize("build_dir", ["build", "dist"]) - known_buildsystem_env = pytest.mark.parametrize( - "buildsystem_indicator_file", ["setup.py", "setup.cfg", "pyproject.toml"] - ) @known_build_dirs - @known_buildsystem_env - def test_build_dirs_collected( - self, pytester: Pytester, build_dir: str, buildsystem_indicator_file: str + def test_build_dirs_collected_when_setuptools_setup_py_present( + self, pytester: Pytester, build_dir: str ) -> None: tmp_path = pytester.path ensure_file(tmp_path / build_dir / "test_module.py").write_text( @@ -292,13 +288,17 @@ def test_build_dirs_collected( result = pytester.runpytest("--collect-only").stdout.str() assert "test_module" in result - ensure_file(tmp_path / buildsystem_indicator_file) + ensure_file(tmp_path / "setup.cfg") + + result = pytester.runpytest("--collect-only").stdout.str() + assert "test_module" in result + ensure_file(tmp_path / "setup.py") result = pytester.runpytest("--collect-only").stdout.str() assert "test_module" not in result @known_build_dirs - def test_build_dirs_collected_when_setuptools_configuration_present( + def test_build_dirs_collected_when_setuptools_present_in_pyproject_toml( self, pytester: Pytester, build_dir: str ) -> None: tmp_path = pytester.path @@ -314,7 +314,11 @@ def test_build_dirs_collected_when_setuptools_configuration_present( result = pytester.runpytest("--collect-only").stdout.str() assert "test_module" in result - ensure_file(tmp_path / "setup.py") + ensure_file(tmp_path / "pyproject.toml").write_text( + '[build-system]\nrequires = ["setuptools", "setuptools-scm"]\n', + encoding="utf-8", + ) + result = pytester.runpytest("--collect-only").stdout.str() assert "test_module" not in result From 8875c82a0aeb8b59613298e9bc018e127f74b7eb Mon Sep 17 00:00:00 2001 From: David Peled Date: Sun, 10 Nov 2024 14:51:34 +0200 Subject: [PATCH 4/4] simplify --- src/_pytest/main.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 1ffe8506164..ff36b85a5cd 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -12,7 +12,6 @@ from pathlib import Path import sys from typing import AbstractSet -from typing import Any from typing import Callable from typing import Dict from typing import final @@ -366,7 +365,7 @@ def pytest_runtestloop(session: Session) -> bool: return True -def _decode_toml_file(toml: Path) -> dict[str, Any] | None: +def _is_setuptools_in_pyproject_toml(toml: Path) -> bool: """Attempt to decode a toml file into a dict, returning None if fails.""" if sys.version_info >= (3, 11): import tomllib @@ -375,9 +374,14 @@ def _decode_toml_file(toml: Path) -> dict[str, Any] | None: try: toml_text = toml.read_text(encoding="utf-8") - return tomllib.loads(toml_text) - except tomllib.TOMLDecodeError: - return None + parsed_toml = tomllib.loads(toml_text) + build_system = parsed_toml.get("build-system", {}).get("requires") + if "setuptools" in build_system: + return True + except Exception: + pass + + return False def _in_build(path: Path) -> bool: @@ -395,15 +399,9 @@ def _in_build(path: Path) -> bool: if setup_py.is_file(): return True - pyproject_toml = path.parent / "pyproject.toml" - if pyproject_toml.is_file(): - config = _decode_toml_file(pyproject_toml) - if config: - if any( - "setuptools" in cfg - for cfg in config.get("build-system", {}).get("requires", {}) - ): - return True + toml = path.parent / "pyproject.toml" + if toml.is_file() and _is_setuptools_in_pyproject_toml(toml): + return True return False