Skip to content

Commit 9078c3c

Browse files
authored
fix #8464 wrong root dir when -c is passed (#8537)
fix #8464 wrong root dir when -c is passed
1 parent d2f3d40 commit 9078c3c

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Contributors include::
55

66
Aaron Coleman
77
Abdeali JK
8+
Abdelrahman Elbehery
89
Abhijeet Kasurde
910
Adam Johnson
1011
Adam Uhlir

changelog/8464.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``-c <config file>`` now also properly defines ``rootdir`` as the directory that contains ``<config file>``.

doc/en/reference/customize.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ Finding the ``rootdir``
145145

146146
Here is the algorithm which finds the rootdir from ``args``:
147147

148+
- If ``-c`` is passed in the command-line, use that as configuration file, and its directory as ``rootdir``.
149+
148150
- Determine the common ancestor directory for the specified ``args`` that are
149151
recognised as paths that exist in the file system. If no such paths are
150152
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``:
160162
``setup.cfg`` in each of the specified ``args`` and upwards. If one is
161163
matched, it becomes the ``configfile`` and its directory becomes the ``rootdir``.
162164

163-
- If no ``configfile`` was found, use the already determined common ancestor as root
165+
- If no ``configfile`` was found and no configuration argument is passed, use the already determined common ancestor as root
164166
directory. This allows the use of pytest in structures that are not part of
165167
a package and don't have any particular configuration file.
166168

src/_pytest/config/findpaths.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def determine_setup(
176176
inipath: Optional[Path] = inipath_
177177
inicfg = load_config_dict_from_file(inipath_) or {}
178178
if rootdir_cmd_arg is None:
179-
rootdir = get_common_ancestor(dirs)
179+
rootdir = inipath_.parent
180180
else:
181181
ancestor = get_common_ancestor(dirs)
182182
rootdir, inipath, inicfg = locate_config([ancestor])

testing/test_config.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,26 @@ def test_with_specific_inifile(
14051405
assert inipath == p
14061406
assert ini_config == {"x": "10"}
14071407

1408+
def test_explicit_config_file_sets_rootdir(
1409+
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
1410+
) -> None:
1411+
tests_dir = tmp_path / "tests"
1412+
tests_dir.mkdir()
1413+
1414+
monkeypatch.chdir(tmp_path)
1415+
1416+
# No config file is explicitly given: rootdir is determined to be cwd.
1417+
rootpath, found_inipath, *_ = determine_setup(None, [str(tests_dir)])
1418+
assert rootpath == tmp_path
1419+
assert found_inipath is None
1420+
1421+
# Config file is explicitly given: rootdir is determined to be inifile's directory.
1422+
inipath = tmp_path / "pytest.ini"
1423+
inipath.touch()
1424+
rootpath, found_inipath, *_ = determine_setup(str(inipath), [str(tests_dir)])
1425+
assert rootpath == tmp_path
1426+
assert found_inipath == inipath
1427+
14081428
def test_with_arg_outside_cwd_without_inifile(
14091429
self, tmp_path: Path, monkeypatch: MonkeyPatch
14101430
) -> None:

0 commit comments

Comments
 (0)