diff --git a/AUTHORS b/AUTHORS index 9b3ec1533..f219c0c83 100644 --- a/AUTHORS +++ b/AUTHORS @@ -5,6 +5,7 @@ Contributors include:: Aaron Coleman Abdeali JK +Abdelrahman Elbehery Abhijeet Kasurde Adam Johnson Adam Uhlir diff --git a/changelog/8464.bugfix.rst b/changelog/8464.bugfix.rst new file mode 100644 index 000000000..017c4c078 --- /dev/null +++ b/changelog/8464.bugfix.rst @@ -0,0 +1 @@ +``-c `` now also properly defines ``rootdir`` as the directory that contains ````. diff --git a/doc/en/reference/customize.rst b/doc/en/reference/customize.rst index 9f7c365dc..24d2ec931 100644 --- a/doc/en/reference/customize.rst +++ b/doc/en/reference/customize.rst @@ -145,6 +145,8 @@ Finding the ``rootdir`` 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 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. @@ -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 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 a package and don't have any particular configuration file. diff --git a/src/_pytest/config/findpaths.py b/src/_pytest/config/findpaths.py index 05f21ece5..7dde4b92d 100644 --- a/src/_pytest/config/findpaths.py +++ b/src/_pytest/config/findpaths.py @@ -176,7 +176,7 @@ def determine_setup( inipath: Optional[Path] = inipath_ inicfg = load_config_dict_from_file(inipath_) or {} if rootdir_cmd_arg is None: - rootdir = get_common_ancestor(dirs) + rootdir = inipath_.parent else: ancestor = get_common_ancestor(dirs) rootdir, inipath, inicfg = locate_config([ancestor]) diff --git a/testing/test_config.py b/testing/test_config.py index 1d1a80aa4..5f3ff1ea8 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -1405,6 +1405,26 @@ class TestRootdir: assert inipath == p 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( self, tmp_path: Path, monkeypatch: MonkeyPatch ) -> None: