Skip to content

Commit

Permalink
Add conditional ignore for build and dist dirname
Browse files Browse the repository at this point in the history
  • Loading branch information
root authored and sus-pe committed Oct 25, 2024
1 parent 67bf34a commit 2ecca4e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelog/12625.improvement.rst
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 13 additions & 0 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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


Expand Down
28 changes: 27 additions & 1 deletion testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit 2ecca4e

Please sign in to comment.