Skip to content

Commit 6d9cb54

Browse files
committed
fix comment
1 parent bf44537 commit 6d9cb54

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

src/_pytest/main.py

+30-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pathlib import Path
1313
import sys
1414
from typing import AbstractSet
15+
from typing import Any
1516
from typing import Callable
1617
from typing import Dict
1718
from typing import final
@@ -365,6 +366,20 @@ def pytest_runtestloop(session: Session) -> bool:
365366
return True
366367

367368

369+
def _decode_toml_file(toml: Path) -> dict[str, Any] | None:
370+
"""Attempt to decode a toml file into a dict, returning None if fails."""
371+
if sys.version_info >= (3, 11):
372+
import tomllib
373+
else:
374+
import tomli as tomllib
375+
376+
try:
377+
toml_text = toml.read_text(encoding="utf-8")
378+
return tomllib.loads(toml_text)
379+
except tomllib.TOMLDecodeError:
380+
return None
381+
382+
368383
def _in_build(path: Path) -> bool:
369384
"""Attempt to detect if ``path`` is the root of a buildsystem's artifacts
370385
by checking known dirnames patterns, and the presence of configuration in
@@ -374,9 +389,21 @@ def _in_build(path: Path) -> bool:
374389
return False
375390

376391
if any(fnmatch_ex(pat, path) for pat in ("build", "dist")):
377-
indicators = ("setup.py", "setup.cfg", "pyproject.toml")
378-
if any((path.parent / f).is_file() for f in indicators):
379-
return True
392+
setup_cfg = path.parent / "setup.cfg"
393+
if (setup_cfg).is_file():
394+
setup_py = path.parent / "setup.py"
395+
if setup_py.is_file():
396+
return True
397+
398+
pyproject_toml = path.parent / "pyproject.toml"
399+
if pyproject_toml.is_file():
400+
config = _decode_toml_file(pyproject_toml)
401+
if config:
402+
if any(
403+
"setuptools" in cfg
404+
for cfg in config.get("build-system", {}).get("requires", {})
405+
):
406+
return True
380407

381408
return False
382409

testing/test_collection.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,10 @@ def test_missing_permissions_on_unselected_directory_doesnt_crash(
275275
result.assert_outcomes(passed=1)
276276

277277
known_build_dirs = pytest.mark.parametrize("build_dir", ["build", "dist"])
278-
known_buildsystem_env = pytest.mark.parametrize(
279-
"buildsystem_indicator_file", ["setup.py", "setup.cfg", "pyproject.toml"]
280-
)
281278

282279
@known_build_dirs
283-
@known_buildsystem_env
284-
def test_build_dirs_collected(
285-
self, pytester: Pytester, build_dir: str, buildsystem_indicator_file: str
280+
def test_build_dirs_collected_when_setuptools_setup_py_present(
281+
self, pytester: Pytester, build_dir: str
286282
) -> None:
287283
tmp_path = pytester.path
288284
ensure_file(tmp_path / build_dir / "test_module.py").write_text(
@@ -292,13 +288,17 @@ def test_build_dirs_collected(
292288
result = pytester.runpytest("--collect-only").stdout.str()
293289
assert "test_module" in result
294290

295-
ensure_file(tmp_path / buildsystem_indicator_file)
291+
ensure_file(tmp_path / "setup.cfg")
292+
293+
result = pytester.runpytest("--collect-only").stdout.str()
294+
assert "test_module" in result
296295

296+
ensure_file(tmp_path / "setup.py")
297297
result = pytester.runpytest("--collect-only").stdout.str()
298298
assert "test_module" not in result
299299

300300
@known_build_dirs
301-
def test_build_dirs_collected_when_setuptools_configuration_present(
301+
def test_build_dirs_collected_when_setuptools_present_in_pyproject_toml(
302302
self, pytester: Pytester, build_dir: str
303303
) -> None:
304304
tmp_path = pytester.path
@@ -314,7 +314,11 @@ def test_build_dirs_collected_when_setuptools_configuration_present(
314314
result = pytester.runpytest("--collect-only").stdout.str()
315315
assert "test_module" in result
316316

317-
ensure_file(tmp_path / "setup.py")
317+
ensure_file(tmp_path / "pyproject.toml").write_text(
318+
'[build-system]\nrequires = ["setuptools", "setuptools-scm"]\n',
319+
encoding="utf-8",
320+
)
321+
result = pytester.runpytest("--collect-only").stdout.str()
318322
assert "test_module" not in result
319323

320324

0 commit comments

Comments
 (0)