Skip to content

pytest uses filesystem root (C:\) as root dir on Windows #10506

Closed
@bersbersbers

Description

@bersbersbers

My setup is a bit elaborate since I am using pyinstaller and running pytest from the bundled executable. Anyway, I can tell from the header that pytest is using C:\ as a rootdir and generates C:\.pytest_cache, which I would like to avoid.

By debugging determine_setup

def determine_setup(
inifile: Optional[str],
args: Sequence[str],
rootdir_cmd_arg: Optional[str] = None,
config: Optional["Config"] = None,
) -> Tuple[Path, Optional[Path], Dict[str, Union[str, List[str]]]]:
rootdir = None
dirs = get_dirs_from_args(args)
if inifile:
inipath_ = absolutepath(inifile)
inipath: Optional[Path] = inipath_
inicfg = load_config_dict_from_file(inipath_) or {}
if rootdir_cmd_arg is None:
rootdir = inipath_.parent
else:
ancestor = get_common_ancestor(dirs)
rootdir, inipath, inicfg = locate_config([ancestor])
if rootdir is None and rootdir_cmd_arg is None:
for possible_rootdir in (ancestor, *ancestor.parents):
if (possible_rootdir / "setup.py").is_file():
rootdir = possible_rootdir
break
else:
if dirs != [ancestor]:
rootdir, inipath, inicfg = locate_config(dirs)
if rootdir is None:
if config is not None:
cwd = config.invocation_params.dir
else:
cwd = Path.cwd()
rootdir = get_common_ancestor([cwd, ancestor])
is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/"
if is_fs_root:
rootdir = ancestor
if rootdir_cmd_arg:
rootdir = absolutepath(os.path.expandvars(rootdir_cmd_arg))
if not rootdir.is_dir():
raise UsageError(
"Directory '{}' not found. Check your '--rootdir' option.".format(
rootdir
)
)
assert rootdir is not None
return rootdir, inipath, inicfg or {}

I have found that it is being called with the following parameters:

inifile = None
args = ['C:\\Users\\bers\\AppData\\Local\\Temp\\_MEI138802\\project']
rootdir_cmd_arg = None
rootdir = None
config = <_pytest.config.Config object at 0x0000021A33D60490>

I have no relevant config in the app bundle, so the function passes via

rootdir = get_common_ancestor([cwd, ancestor])
is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/"
if is_fs_root:
rootdir = ancestor

with

cwd = WindowsPath('C:/Code/project')
ancestor = WindowsPath('C:/Users/bers/AppData/Local/Temp/_MEI138802/project')

rootdir = get_common_ancestor([cwd, ancestor]) returns WindowsPath('C:/'), but the following check returns false as os.path.splitdrive(str(rootdir))[1] is "\\", not "/". As a consequence, rootdir is not set to ancestor, but remains at WindowsPath('C:/'), which I believe is not intended.

TLDR: In

is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/"
change "/" to os.sep.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueeasy issue that is friendly to new contributorneeds backportapplied to PRs, indicates that it should be ported to the current bug-fix branchtype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions