Fix bug where file system root was erroneously be used as rootdir on Windows

Fix #10506
This commit is contained in:
Prerak Patel 2022-11-23 12:46:00 -05:00 committed by GitHub
parent 99dfc19fe6
commit 857e34ef85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 2 deletions

View File

@ -0,0 +1 @@
Fix bug where sometimes pytest would use the file system root directory as :ref:`rootdir <rootdir>` on Windows.

View File

@ -203,8 +203,7 @@ def determine_setup(
else: else:
cwd = Path.cwd() cwd = Path.cwd()
rootdir = get_common_ancestor([cwd, ancestor]) rootdir = get_common_ancestor([cwd, ancestor])
is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/" if is_fs_root(rootdir):
if is_fs_root:
rootdir = ancestor rootdir = ancestor
if rootdir_cmd_arg: if rootdir_cmd_arg:
rootdir = absolutepath(os.path.expandvars(rootdir_cmd_arg)) rootdir = absolutepath(os.path.expandvars(rootdir_cmd_arg))
@ -216,3 +215,11 @@ def determine_setup(
) )
assert rootdir is not None assert rootdir is not None
return rootdir, inipath, inicfg or {} return rootdir, inipath, inicfg or {}
def is_fs_root(p: Path) -> bool:
r"""
Return True if the given path is pointing to the root of the
file system ("/" on Unix and "C:\\" on Windows for example).
"""
return os.path.splitdrive(str(p))[1] == os.sep

View File

@ -1,3 +1,4 @@
import os
from pathlib import Path from pathlib import Path
from textwrap import dedent from textwrap import dedent
@ -5,6 +6,7 @@ import pytest
from _pytest.config import UsageError from _pytest.config import UsageError
from _pytest.config.findpaths import get_common_ancestor from _pytest.config.findpaths import get_common_ancestor
from _pytest.config.findpaths import get_dirs_from_args from _pytest.config.findpaths import get_dirs_from_args
from _pytest.config.findpaths import is_fs_root
from _pytest.config.findpaths import load_config_dict_from_file from _pytest.config.findpaths import load_config_dict_from_file
@ -133,3 +135,18 @@ def test_get_dirs_from_args(tmp_path):
assert get_dirs_from_args( assert get_dirs_from_args(
[str(fn), str(tmp_path / "does_not_exist"), str(d), option, xdist_rsync_option] [str(fn), str(tmp_path / "does_not_exist"), str(d), option, xdist_rsync_option]
) == [fn.parent, d] ) == [fn.parent, d]
@pytest.mark.parametrize(
"path, expected",
[
pytest.param(
f"e:{os.sep}", True, marks=pytest.mark.skipif("sys.platform != 'win32'")
),
(f"{os.sep}", True),
(f"e:{os.sep}projects", False),
(f"{os.sep}projects", False),
],
)
def test_is_fs_root(path: Path, expected: bool) -> None:
assert is_fs_root(Path(path)) is expected