fix #8464 wrong root dir when -c is passed (#8537)

fix #8464 wrong root dir when -c is passed
This commit is contained in:
Abdelrahman Elbehery 2021-04-16 19:38:35 +02:00 committed by GitHub
parent d2f3d40510
commit 9078c3ce23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 2 deletions

View File

@ -5,6 +5,7 @@ Contributors include::
Aaron Coleman Aaron Coleman
Abdeali JK Abdeali JK
Abdelrahman Elbehery
Abhijeet Kasurde Abhijeet Kasurde
Adam Johnson Adam Johnson
Adam Uhlir Adam Uhlir

View File

@ -0,0 +1 @@
``-c <config file>`` now also properly defines ``rootdir`` as the directory that contains ``<config file>``.

View File

@ -145,6 +145,8 @@ Finding the ``rootdir``
Here is the algorithm which finds the rootdir from ``args``: Here is the algorithm which finds the rootdir from ``args``:
- If ``-c`` is passed in the command-line, use that as configuration file, and its directory as ``rootdir``.
- Determine the common ancestor directory for the specified ``args`` that are - Determine the common ancestor directory for the specified ``args`` that are
recognised as paths that exist in the file system. If no such paths are recognised as paths that exist in the file system. If no such paths are
found, the common ancestor directory is set to the current working directory. found, the common ancestor directory is set to the current working directory.
@ -160,7 +162,7 @@ Here is the algorithm which finds the rootdir from ``args``:
``setup.cfg`` in each of the specified ``args`` and upwards. If one is ``setup.cfg`` in each of the specified ``args`` and upwards. If one is
matched, it becomes the ``configfile`` and its directory becomes the ``rootdir``. matched, it becomes the ``configfile`` and its directory becomes the ``rootdir``.
- If no ``configfile`` was found, use the already determined common ancestor as root - If no ``configfile`` was found and no configuration argument is passed, use the already determined common ancestor as root
directory. This allows the use of pytest in structures that are not part of directory. This allows the use of pytest in structures that are not part of
a package and don't have any particular configuration file. a package and don't have any particular configuration file.

View File

@ -176,7 +176,7 @@ def determine_setup(
inipath: Optional[Path] = inipath_ inipath: Optional[Path] = inipath_
inicfg = load_config_dict_from_file(inipath_) or {} inicfg = load_config_dict_from_file(inipath_) or {}
if rootdir_cmd_arg is None: if rootdir_cmd_arg is None:
rootdir = get_common_ancestor(dirs) rootdir = inipath_.parent
else: else:
ancestor = get_common_ancestor(dirs) ancestor = get_common_ancestor(dirs)
rootdir, inipath, inicfg = locate_config([ancestor]) rootdir, inipath, inicfg = locate_config([ancestor])

View File

@ -1405,6 +1405,26 @@ class TestRootdir:
assert inipath == p assert inipath == p
assert ini_config == {"x": "10"} assert ini_config == {"x": "10"}
def test_explicit_config_file_sets_rootdir(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
tests_dir = tmp_path / "tests"
tests_dir.mkdir()
monkeypatch.chdir(tmp_path)
# No config file is explicitly given: rootdir is determined to be cwd.
rootpath, found_inipath, *_ = determine_setup(None, [str(tests_dir)])
assert rootpath == tmp_path
assert found_inipath is None
# Config file is explicitly given: rootdir is determined to be inifile's directory.
inipath = tmp_path / "pytest.ini"
inipath.touch()
rootpath, found_inipath, *_ = determine_setup(str(inipath), [str(tests_dir)])
assert rootpath == tmp_path
assert found_inipath == inipath
def test_with_arg_outside_cwd_without_inifile( def test_with_arg_outside_cwd_without_inifile(
self, tmp_path: Path, monkeypatch: MonkeyPatch self, tmp_path: Path, monkeypatch: MonkeyPatch
) -> None: ) -> None: