From 0f83df4f5547a6f72ef5ef070dab30f2ee372eb4 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Wed, 30 Sep 2020 13:21:18 +0300 Subject: [PATCH] Merge pull request #7813 from bluetech/findpaths-confusion findpaths: fix regression causing incorrect rootdir to be determined (cherry picked from commit b250c9d6157fe6ae6d308d0fbd7625b280b793fc) --- changelog/7807.bugfix.rst | 1 + src/_pytest/config/findpaths.py | 7 ++----- testing/test_config.py | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 changelog/7807.bugfix.rst diff --git a/changelog/7807.bugfix.rst b/changelog/7807.bugfix.rst new file mode 100644 index 00000000000..93f3e56dc11 --- /dev/null +++ b/changelog/7807.bugfix.rst @@ -0,0 +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. diff --git a/src/_pytest/config/findpaths.py b/src/_pytest/config/findpaths.py index facf30a87a2..167b9e7a006 100644 --- a/src/_pytest/config/findpaths.py +++ b/src/_pytest/config/findpaths.py @@ -1,4 +1,3 @@ -import itertools import os from typing import Dict from typing import Iterable @@ -100,7 +99,7 @@ def locate_config( args = [Path.cwd()] for arg in args: argpath = absolutepath(arg) - for base in itertools.chain((argpath,), reversed(argpath.parents)): + for base in (argpath, *argpath.parents): for config_name in config_names: p = base / config_name if p.is_file(): @@ -184,9 +183,7 @@ def determine_setup( ancestor = get_common_ancestor(dirs) rootdir, inipath, inicfg = locate_config([ancestor]) if rootdir is None and rootdir_cmd_arg is None: - for possible_rootdir in itertools.chain( - (ancestor,), reversed(ancestor.parents) - ): + for possible_rootdir in (ancestor, *ancestor.parents): if (possible_rootdir / "setup.py").is_file(): rootdir = possible_rootdir break diff --git a/testing/test_config.py b/testing/test_config.py index 0cfd11fd525..89fbbf8c957 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -1375,6 +1375,21 @@ def test_with_existing_file_in_subdir( assert rootpath == tmp_path assert inipath is None + def test_with_config_also_in_parent_directory( + self, tmp_path: Path, monkeypatch: MonkeyPatch + ) -> None: + """Regression test for #7807.""" + (tmp_path / "setup.cfg").write_text("[tool:pytest]\n", "utf-8") + (tmp_path / "myproject").mkdir() + (tmp_path / "myproject" / "setup.cfg").write_text("[tool:pytest]\n", "utf-8") + (tmp_path / "myproject" / "tests").mkdir() + monkeypatch.chdir(tmp_path / "myproject") + + rootpath, inipath, _ = determine_setup(None, ["tests/"]) + + assert rootpath == tmp_path / "myproject" + assert inipath == tmp_path / "myproject" / "setup.cfg" + class TestOverrideIniArgs: @pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split())