Skip to content

Commit

Permalink
Recurse into build / dist directories
Browse files Browse the repository at this point in the history
See #12625 for context
  • Loading branch information
root authored and sus-pe committed Oct 26, 2024
1 parent 26215b8 commit 4e4ef2e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
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.
22 changes: 20 additions & 2 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ def pytest_addoption(parser: Parser) -> None:
"*.egg",
".*",
"_darcs",
"build",
"CVS",
"dist",
"node_modules",
"venv",
"{arch}",
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down
25 changes: 23 additions & 2 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 4e4ef2e

Please sign in to comment.