Skip to content

Commit bcb94c4

Browse files
authored
Merge pull request #7822 from bluetech/backport-7813
[6.1.x] findpaths: fix regression causing incorrect rootdir to be determined
2 parents 330caac + 0f83df4 commit bcb94c4

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

changelog/7807.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed regression in pytest 6.1.0 causing incorrect rootdir to be determined in some non-trivial cases where parent directories have config files as well.

src/_pytest/config/findpaths.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import itertools
21
import os
32
from typing import Dict
43
from typing import Iterable
@@ -100,7 +99,7 @@ def locate_config(
10099
args = [Path.cwd()]
101100
for arg in args:
102101
argpath = absolutepath(arg)
103-
for base in itertools.chain((argpath,), reversed(argpath.parents)):
102+
for base in (argpath, *argpath.parents):
104103
for config_name in config_names:
105104
p = base / config_name
106105
if p.is_file():
@@ -184,9 +183,7 @@ def determine_setup(
184183
ancestor = get_common_ancestor(dirs)
185184
rootdir, inipath, inicfg = locate_config([ancestor])
186185
if rootdir is None and rootdir_cmd_arg is None:
187-
for possible_rootdir in itertools.chain(
188-
(ancestor,), reversed(ancestor.parents)
189-
):
186+
for possible_rootdir in (ancestor, *ancestor.parents):
190187
if (possible_rootdir / "setup.py").is_file():
191188
rootdir = possible_rootdir
192189
break

testing/test_config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,21 @@ def test_with_existing_file_in_subdir(
13751375
assert rootpath == tmp_path
13761376
assert inipath is None
13771377

1378+
def test_with_config_also_in_parent_directory(
1379+
self, tmp_path: Path, monkeypatch: MonkeyPatch
1380+
) -> None:
1381+
"""Regression test for #7807."""
1382+
(tmp_path / "setup.cfg").write_text("[tool:pytest]\n", "utf-8")
1383+
(tmp_path / "myproject").mkdir()
1384+
(tmp_path / "myproject" / "setup.cfg").write_text("[tool:pytest]\n", "utf-8")
1385+
(tmp_path / "myproject" / "tests").mkdir()
1386+
monkeypatch.chdir(tmp_path / "myproject")
1387+
1388+
rootpath, inipath, _ = determine_setup(None, ["tests/"])
1389+
1390+
assert rootpath == tmp_path / "myproject"
1391+
assert inipath == tmp_path / "myproject" / "setup.cfg"
1392+
13781393

13791394
class TestOverrideIniArgs:
13801395
@pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split())

0 commit comments

Comments
 (0)