Skip to content

Commit

Permalink
[7.2.x] Issue #10506
Browse files Browse the repository at this point in the history
  • Loading branch information
PrerakPatelCS authored and pytestbot committed Nov 23, 2022
1 parent 3c2f90b commit c3b0080
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/10506.bugfix.rst
@@ -0,0 +1 @@
Fix bug where sometimes pytest would use the file system root directory as :ref:`rootdir <rootdir>` on Windows.
11 changes: 9 additions & 2 deletions src/_pytest/config/findpaths.py
Expand Up @@ -203,8 +203,7 @@ def determine_setup(
else:
cwd = Path.cwd()
rootdir = get_common_ancestor([cwd, ancestor])
is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/"
if is_fs_root:
if is_fs_root(rootdir):
rootdir = ancestor
if rootdir_cmd_arg:
rootdir = absolutepath(os.path.expandvars(rootdir_cmd_arg))
Expand All @@ -216,3 +215,11 @@ def determine_setup(
)
assert rootdir is not None
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
17 changes: 17 additions & 0 deletions testing/test_findpaths.py
@@ -1,10 +1,12 @@
import os
from pathlib import Path
from textwrap import dedent

import pytest
from _pytest.config import UsageError
from _pytest.config.findpaths import get_common_ancestor
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


Expand Down Expand Up @@ -133,3 +135,18 @@ def test_get_dirs_from_args(tmp_path):
assert get_dirs_from_args(
[str(fn), str(tmp_path / "does_not_exist"), str(d), option, xdist_rsync_option]
) == [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

0 comments on commit c3b0080

Please sign in to comment.