Skip to content

Commit 6772195

Browse files
committed
refactor: make API private and return list of ignored files
This will let users know exactly which files have been ignored in case multiple of them contain pytest configuration.
1 parent 9bdbec5 commit 6772195

File tree

4 files changed

+53
-27
lines changed

4 files changed

+53
-27
lines changed

src/_pytest/config/__init__.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,14 +1107,6 @@ def inipath(self) -> pathlib.Path | None:
11071107
"""
11081108
return self._inipath
11091109

1110-
@property
1111-
def should_warn(self) -> bool:
1112-
"""Whether a warning should be emitted for the configuration.
1113-
1114-
.. versionadded:: 8.6
1115-
"""
1116-
return self._should_warn
1117-
11181110
def add_cleanup(self, func: Callable[[], None]) -> None:
11191111
"""Add a function to be called when the config object gets out of
11201112
use (usually coinciding with pytest_unconfigure).
@@ -1250,15 +1242,15 @@ def _initini(self, args: Sequence[str]) -> None:
12501242
ns, unknown_args = self._parser.parse_known_and_unknown_args(
12511243
args, namespace=copy.copy(self.option)
12521244
)
1253-
rootpath, inipath, inicfg, should_warn = determine_setup(
1245+
rootpath, inipath, inicfg, ignored_files = determine_setup(
12541246
inifile=ns.inifilename,
12551247
args=ns.file_or_dir + unknown_args,
12561248
rootdir_cmd_arg=ns.rootdir or None,
12571249
invocation_dir=self.invocation_params.dir,
12581250
)
12591251
self._rootpath = rootpath
12601252
self._inipath = inipath
1261-
self._should_warn = should_warn
1253+
self._ignored_files = ignored_files
12621254
self.inicfg = inicfg
12631255
self._parser.extra_info["rootdir"] = str(self.rootpath)
12641256
self._parser.extra_info["inifile"] = str(self.inipath)

src/_pytest/config/findpaths.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def make_scalar(v: object) -> str | list[str]:
9191
def locate_config(
9292
invocation_dir: Path,
9393
args: Iterable[Path],
94-
) -> tuple[Path | None, Path | None, ConfigDict, bool]:
94+
) -> tuple[Path | None, Path | None, ConfigDict, list[str]]:
9595
"""Search in the list of arguments for a valid ini-file for pytest,
9696
and return a tuple of (rootdir, inifile, cfg-dict)."""
9797
config_names = [
@@ -105,6 +105,7 @@ def locate_config(
105105
if not args:
106106
args = [invocation_dir]
107107
found_pyproject_toml: Path | None = None
108+
ignored_files: list[str] = []
108109

109110
for arg in args:
110111
argpath = absolutepath(arg)
@@ -116,17 +117,17 @@ def locate_config(
116117
found_pyproject_toml = p
117118
ini_config = load_config_dict_from_file(p)
118119
if ini_config is not None:
119-
should_warn = False
120-
if ".ini" in p.suffixes:
121-
pyproject = base / "pyproject.toml"
122-
if pyproject.is_file():
123-
should_warn = (
124-
load_config_dict_from_file(pyproject) is not None
125-
)
126-
return base, p, ini_config, should_warn
120+
index = config_names.index(config_name)
121+
if index < len(config_names) - 1:
122+
for remainder in config_names[index + 1 :]:
123+
p2 = base / remainder
124+
if p2.is_file():
125+
if load_config_dict_from_file(p2) is not None:
126+
ignored_files.append(remainder)
127+
return base, p, ini_config, ignored_files
127128
if found_pyproject_toml is not None:
128-
return found_pyproject_toml.parent, found_pyproject_toml, {}, False
129-
return None, None, {}, False
129+
return found_pyproject_toml.parent, found_pyproject_toml, {}, []
130+
return None, None, {}, []
130131

131132

132133
def get_common_ancestor(
@@ -186,7 +187,7 @@ def determine_setup(
186187
args: Sequence[str],
187188
rootdir_cmd_arg: str | None,
188189
invocation_dir: Path,
189-
) -> tuple[Path, Path | None, ConfigDict, bool]:
190+
) -> tuple[Path, Path | None, ConfigDict, list[str]]:
190191
"""Determine the rootdir, inifile and ini configuration values from the
191192
command line arguments.
192193
@@ -201,7 +202,8 @@ def determine_setup(
201202
"""
202203
rootdir = None
203204
dirs = get_dirs_from_args(args)
204-
should_warn = False
205+
ignored_files: list[str] = []
206+
205207
if inifile:
206208
inipath_ = absolutepath(inifile)
207209
inipath: Path | None = inipath_
@@ -210,7 +212,7 @@ def determine_setup(
210212
rootdir = inipath_.parent
211213
else:
212214
ancestor = get_common_ancestor(invocation_dir, dirs)
213-
rootdir, inipath, inicfg, should_warn = locate_config(
215+
rootdir, inipath, inicfg, ignored_files = locate_config(
214216
invocation_dir, [ancestor]
215217
)
216218
if rootdir is None and rootdir_cmd_arg is None:
@@ -234,7 +236,7 @@ def determine_setup(
234236
f"Directory '{rootdir}' not found. Check your '--rootdir' option."
235237
)
236238
assert rootdir is not None
237-
return rootdir, inipath, inicfg or {}, should_warn
239+
return rootdir, inipath, inicfg or {}, ignored_files
238240

239241

240242
def is_fs_root(p: Path) -> bool:

src/_pytest/terminal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,8 @@ def pytest_report_header(self, config: Config) -> list[str]:
880880

881881
if config.inipath:
882882
warning = ""
883-
if config.should_warn:
884-
warning = " (WARNING: ignoring pytest config in pyproject.toml!)"
883+
if config._ignored_files:
884+
warning = f" (WARNING: ignoring pytest config in {', '.join(config._ignored_files)}!)"
885885
result.append(
886886
"configfile: " + bestrelpath(config.rootpath, config.inipath) + warning
887887
)

testing/test_terminal.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,38 @@ def test_warning_when_init_trumps_pyproject_toml(
29142914
)
29152915

29162916

2917+
def test_warning_when_init_trumps_multiple_files(
2918+
pytester: Pytester, monkeypatch: MonkeyPatch
2919+
) -> None:
2920+
"""Regression test for #7814."""
2921+
tests = pytester.path.joinpath("tests")
2922+
tests.mkdir()
2923+
pytester.makepyprojecttoml(
2924+
f"""
2925+
[tool.pytest.ini_options]
2926+
testpaths = ['{tests}']
2927+
"""
2928+
)
2929+
pytester.makefile(".ini", pytest="")
2930+
pytester.makeini(
2931+
"""
2932+
# tox.ini
2933+
[pytest]
2934+
minversion = 6.0
2935+
addopts = -ra -q
2936+
testpaths =
2937+
tests
2938+
integration
2939+
"""
2940+
)
2941+
result = pytester.runpytest()
2942+
result.stdout.fnmatch_lines(
2943+
[
2944+
"configfile: pytest.ini (WARNING: ignoring pytest config in pyproject.toml, tox.ini!)",
2945+
]
2946+
)
2947+
2948+
29172949
def test_no_warning_when_init_but_pyproject_toml_has_no_entry(
29182950
pytester: Pytester, monkeypatch: MonkeyPatch
29192951
) -> None:

0 commit comments

Comments
 (0)