Skip to content

Commit 2ecca4e

Browse files
rootsus-pe
root
authored andcommitted
Add conditional ignore for build and dist dirname
Fix #12625
1 parent 67bf34a commit 2ecca4e

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

changelog/12625.improvement.rst

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Conditionally ignore collection of setuptools artifacts dirnames only if the
2+
directories reside inside a setuptools project, i.e. `setup.cfg`, is present, etc.

src/_pytest/main.py

+13
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,15 @@ def pytest_runtestloop(session: Session) -> bool:
365365
return True
366366

367367

368+
def _is_setuptools_project(path: Path) -> bool:
369+
"""Attempt to detect if ``path`` is the root of a setuptools project by
370+
checking the existence of setup.py, setup.cfg, or pyproject.toml.
371+
372+
"""
373+
indicators = ("setup.py", "setup.cfg", "pyproject.toml")
374+
return any((path / f).exists() for f in indicators)
375+
376+
368377
def _in_venv(path: Path) -> bool:
369378
"""Attempt to detect if ``path`` is the root of a Virtual Environment by
370379
checking for the existence of the pyvenv.cfg file.
@@ -421,6 +430,10 @@ def pytest_ignore_collect(collection_path: Path, config: Config) -> bool | None:
421430
if any(fnmatch_ex(pat, collection_path) for pat in norecursepatterns):
422431
return True
423432

433+
if any(fnmatch_ex(pat, collection_path) for pat in ("build", "dist")):
434+
if _is_setuptools_project(collection_path.parent):
435+
return True
436+
424437
return None
425438

426439

testing/test_collection.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def test_foo():
135135

136136

137137
class TestCollectFS:
138-
def test_build_conditional_ignore(self, pytester: Pytester) -> None:
138+
def test_build_dirs_collected(self, pytester: Pytester) -> None:
139139
tmp_path = pytester.path
140140
ensure_file(tmp_path / "build" / "test_found.py")
141141
ensure_file(tmp_path / "dist" / "test_found.py")
@@ -146,6 +146,32 @@ def test_build_conditional_ignore(self, pytester: Pytester) -> None:
146146
s = result.stdout.str()
147147
assert "test_found" in s
148148

149+
def test_setuptools_ignored_if_present(self, pytester: Pytester) -> None:
150+
tmp_path = pytester.path
151+
ensure_file(tmp_path / "build" / "test_notfound.py")
152+
ensure_file(tmp_path / "dist" / "test_notfound.py")
153+
for x in tmp_path.rglob("test_*.py"):
154+
x.write_text("def test_hello(): pass", encoding="utf-8")
155+
156+
ensure_file(tmp_path / "setup.py")
157+
158+
result = pytester.runpytest("--collect-only")
159+
s = result.stdout.str()
160+
assert "test_notfound" not in s
161+
162+
def test_setuptools_ignored_pyproject_toml(self, pytester: Pytester) -> None:
163+
tmp_path = pytester.path
164+
ensure_file(tmp_path / "build" / "test_notfound.py")
165+
ensure_file(tmp_path / "dist" / "test_notfound.py")
166+
for x in tmp_path.rglob("test_*.py"):
167+
x.write_text("def test_hello(): pass", encoding="utf-8")
168+
169+
ensure_file(tmp_path / "pyproject.toml")
170+
171+
result = pytester.runpytest("--collect-only")
172+
s = result.stdout.str()
173+
assert "test_notfound" not in s
174+
149175
def test_ignored_certain_directories(self, pytester: Pytester) -> None:
150176
tmp_path = pytester.path
151177
ensure_file(tmp_path / "_darcs" / "test_notfound.py")

0 commit comments

Comments
 (0)