Add test and changelog

This commit is contained in:
Bruno Oliveira 2022-11-23 14:17:32 -03:00
parent 598eea1417
commit 5293b00ca4
3 changed files with 25 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] == os.sep 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,16 @@ 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",
[
(Path(f"e:{os.sep}"), True),
(Path(f"{os.sep}"), True),
(Path(f"e:{os.sep}projects"), False),
(Path(f"{os.sep}projects"), False),
],
)
def test_is_fs_root(path: Path, expected: bool) -> None:
assert is_fs_root(path) is expected