From 2ecca4ef11559d1c11e3cc092d8901dc9fdad323 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 25 Oct 2024 11:57:32 +0300 Subject: [PATCH] Add conditional ignore for build and dist dirname Fix #12625 --- changelog/12625.improvement.rst | 2 ++ src/_pytest/main.py | 13 +++++++++++++ testing/test_collection.py | 28 +++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) 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 534b60114e7..d007df3d55a 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -365,6 +365,15 @@ def pytest_runtestloop(session: Session) -> bool: return True +def _is_setuptools_project(path: Path) -> bool: + """Attempt to detect if ``path`` is the root of a setuptools project by + checking the existence of setup.py, setup.cfg, or pyproject.toml. + + """ + indicators = ("setup.py", "setup.cfg", "pyproject.toml") + return any((path / f).exists() for f in indicators) + + 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. @@ -421,6 +430,10 @@ def pytest_ignore_collect(collection_path: Path, config: Config) -> bool | None: if any(fnmatch_ex(pat, collection_path) for pat in norecursepatterns): return True + if any(fnmatch_ex(pat, collection_path) for pat in ("build", "dist")): + if _is_setuptools_project(collection_path.parent): + return True + return None diff --git a/testing/test_collection.py b/testing/test_collection.py index 60673dce297..f5368168291 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -135,7 +135,7 @@ def test_foo(): class TestCollectFS: - def test_build_conditional_ignore(self, pytester: Pytester) -> None: + def test_build_dirs_collected(self, pytester: Pytester) -> None: tmp_path = pytester.path ensure_file(tmp_path / "build" / "test_found.py") ensure_file(tmp_path / "dist" / "test_found.py") @@ -146,6 +146,32 @@ def test_build_conditional_ignore(self, pytester: Pytester) -> None: s = result.stdout.str() assert "test_found" in s + def test_setuptools_ignored_if_present(self, pytester: Pytester) -> None: + tmp_path = pytester.path + ensure_file(tmp_path / "build" / "test_notfound.py") + ensure_file(tmp_path / "dist" / "test_notfound.py") + for x in tmp_path.rglob("test_*.py"): + x.write_text("def test_hello(): pass", encoding="utf-8") + + ensure_file(tmp_path / "setup.py") + + result = pytester.runpytest("--collect-only") + s = result.stdout.str() + assert "test_notfound" not in s + + def test_setuptools_ignored_pyproject_toml(self, pytester: Pytester) -> None: + tmp_path = pytester.path + ensure_file(tmp_path / "build" / "test_notfound.py") + ensure_file(tmp_path / "dist" / "test_notfound.py") + for x in tmp_path.rglob("test_*.py"): + x.write_text("def test_hello(): pass", encoding="utf-8") + + ensure_file(tmp_path / "pyproject.toml") + + result = pytester.runpytest("--collect-only") + s = result.stdout.str() + assert "test_notfound" not in s + def test_ignored_certain_directories(self, pytester: Pytester) -> None: tmp_path = pytester.path ensure_file(tmp_path / "_darcs" / "test_notfound.py")