Skip to content

Commit 7d35baa

Browse files
authored
Merge pull request #10529 from pytest-dev/backport-10526-to-7.2.x
[7.2.x] Issue #10506
2 parents 3c2f90b + c3b0080 commit 7d35baa

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

changelog/10506.bugfix.rst

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

src/_pytest/config/findpaths.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ def determine_setup(
203203
else:
204204
cwd = Path.cwd()
205205
rootdir = get_common_ancestor([cwd, ancestor])
206-
is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/"
207-
if is_fs_root:
206+
if is_fs_root(rootdir):
208207
rootdir = ancestor
209208
if rootdir_cmd_arg:
210209
rootdir = absolutepath(os.path.expandvars(rootdir_cmd_arg))
@@ -216,3 +215,11 @@ def determine_setup(
216215
)
217216
assert rootdir is not None
218217
return rootdir, inipath, inicfg or {}
218+
219+
220+
def is_fs_root(p: Path) -> bool:
221+
r"""
222+
Return True if the given path is pointing to the root of the
223+
file system ("/" on Unix and "C:\\" on Windows for example).
224+
"""
225+
return os.path.splitdrive(str(p))[1] == os.sep

testing/test_findpaths.py

+17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import os
12
from pathlib import Path
23
from textwrap import dedent
34

45
import pytest
56
from _pytest.config import UsageError
67
from _pytest.config.findpaths import get_common_ancestor
78
from _pytest.config.findpaths import get_dirs_from_args
9+
from _pytest.config.findpaths import is_fs_root
810
from _pytest.config.findpaths import load_config_dict_from_file
911

1012

@@ -133,3 +135,18 @@ def test_get_dirs_from_args(tmp_path):
133135
assert get_dirs_from_args(
134136
[str(fn), str(tmp_path / "does_not_exist"), str(d), option, xdist_rsync_option]
135137
) == [fn.parent, d]
138+
139+
140+
@pytest.mark.parametrize(
141+
"path, expected",
142+
[
143+
pytest.param(
144+
f"e:{os.sep}", True, marks=pytest.mark.skipif("sys.platform != 'win32'")
145+
),
146+
(f"{os.sep}", True),
147+
(f"e:{os.sep}projects", False),
148+
(f"{os.sep}projects", False),
149+
],
150+
)
151+
def test_is_fs_root(path: Path, expected: bool) -> None:
152+
assert is_fs_root(Path(path)) is expected

0 commit comments

Comments
 (0)