Skip to content

fix(tests): override Path.home() in root conftest so Windows xdist gw2 cannot crash#1272

Merged
danielmeppiel merged 2 commits into
mainfrom
fix/windows-home-monkeypatch
May 11, 2026
Merged

fix(tests): override Path.home() in root conftest so Windows xdist gw2 cannot crash#1272
danielmeppiel merged 2 commits into
mainfrom
fix/windows-home-monkeypatch

Conversation

@danielmeppiel
Copy link
Copy Markdown
Collaborator

Problem

CI/CD Pipeline run 25667668848 -- the run that fired after #1271 merged -- still failed the Windows unit-test job with 46 RuntimeError: Could not determine home directory failures, all on xdist worker gw2 on the new windows-2025-vs2026 GitHub-hosted image.

The session-scoped autouse fixture (PR #1270) and the import-time env mutation in tests/unit/conftest.py (PR #1271) both turned out to be insufficient: on gw2, something resolves Path.home() before the unit conftest's setup takes effect on that worker.

Fix

Override Path.home() itself in tests/conftest.py (root). Properties:

  • Applied at import time of the root conftest, which every xdist worker loads before any test in any directory runs.
  • The override returns Path(env['HOME']) when set, then the Windows trio (USERPROFILE / HOMEDRIVE+HOMEPATH), and only falls back to a hermetic tmp dir when nothing is set. So per-test monkeypatch.setenv("HOME", ...) keeps working (verified locally with the tests/unit/integration/test_scope_* suite).
  • The override never raises, so any production or test code path calling Path.home() during the test run gets a usable path even on runners that would otherwise leave the worker subprocess with an empty USERPROFILE.

The previous tests/unit/conftest.py import-time mutation is now redundant -- deleted.

Validation

  • uv run pytest tests/unit tests/test_console.py -n auto --dist worksteal -- 8293 passed locally (incl. the scope-resolution tests that exercise monkeypatch.setenv("HOME")).
  • uv run --extra dev ruff check src/ tests/ && ruff format --check src/ tests/ -- silent.

Notes

This is required to unblock the v0.13.0 retag.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

…2 cannot crash

PR #1271 set HOME / USERPROFILE / HOMEDRIVE / HOMEPATH at conftest
import time, but a single xdist worker (gw2) on windows-2025-vs2026
still hit 46 'RuntimeError: Could not determine home directory'
failures (run 25667668848). Whatever the cause -- worker subprocess
spawning order, conftest discovery quirks, or some other code path
calling Path.home() before the unit-conftest mutation took effect
on that worker -- the env-mutation approach is not robust enough.

Override Path.home() directly in tests/conftest.py (root). The
override:

  - is applied at import time of the root conftest, which every
    xdist worker loads before any test in any directory runs;
  - returns Path(env['HOME']) when set, then the Windows trio,
    falling back to a hermetic tmp dir only when nothing is set;
  - never raises, so any production or test code path calling
    Path.home() during the test run gets a usable path even on
    runners that would otherwise leave the worker subprocess with
    an empty HOME / USERPROFILE.

Per-test 'monkeypatch.setenv("HOME", ...)' (e.g. the scope-resolution
tests under tests/unit/integration/) keeps working because the
override reads from os.environ on every call.

The previous tests/unit/conftest.py import-time mutation is now
redundant -- delete it.

Refs run https://github.com/microsoft/apm/actions/runs/25667668848

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 11, 2026 11:44
Locks in the contract: Path.home() must not raise even when every env
var ntpath.expanduser / posixpath.expanduser consults is unset. This
is exactly the windows-2025-vs2026 xdist worker case that produced
the 56 / 53 / 46 failure cascades.

Also asserts that per-test monkeypatch.setenv("HOME", ...) keeps
working (so the scope-resolution tests under tests/unit/integration/
don't regress).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the test harness against Windows pytest-xdist worker subprocesses that can crash when pathlib.Path.home() cannot resolve a home directory (notably on the windows-2025-vs2026 runner), by applying a deterministic, hermetic Path.home() behavior at the earliest shared hook (root tests/conftest.py).

Changes:

  • Override Path.home() in the root tests/conftest.py at import time, backed by HOME/Windows-home env vars with a tmp fallback.
  • Remove the now-redundant tests/unit/conftest.py import-time HOME mutation.
  • Add a CHANGELOG.md Unreleased fix entry for the test stability change.
Show a summary per file
File Description
tests/unit/conftest.py Deletes redundant unit-only import-time HOME mutation.
tests/conftest.py Adds import-time hermetic HOME env setup and overrides Path.home() for all test runs/workers.
CHANGELOG.md Documents the Windows test-flake fix under Unreleased -> Fixed.

Copilot's findings

  • Files reviewed: 4/4 changed files
  • Comments generated: 3

Comment thread tests/conftest.py

import pytest

_TMP_HOME = Path(tempfile.mkdtemp(prefix="apm-test-home-"))
Comment thread tests/conftest.py
_ensure_home_env(_TMP_HOME)


def _hermetic_home(_cls=Path) -> Path:
Comment thread tests/conftest.py
Comment on lines +51 to +58
Honors HOME / USERPROFILE / HOMEDRIVE+HOMEPATH so per-test
`monkeypatch.setenv("HOME", ...)` (or its Windows-trio equivalent)
keeps working. Falls back to a hermetic tmp dir only when the env
is empty -- which is the windows-2025-vs2026 xdist worker case.
"""
home = os.environ.get("HOME")
if not home and os.name == "nt":
home = os.environ.get("USERPROFILE")
@danielmeppiel danielmeppiel merged commit a82f4f9 into main May 11, 2026
9 checks passed
@danielmeppiel danielmeppiel deleted the fix/windows-home-monkeypatch branch May 11, 2026 11:54
danielmeppiel added a commit that referenced this pull request May 11, 2026
…1276)

* fix(tests): wrap Path.expanduser() to never raise on Windows runner

Companion to #1272. After the Path.home() override landed, the
windows-2025-vs2026 runner still red-marked two tests:

  tests/unit/install/test_user_scope_rejection_reason.py
    ::test_user_scope_accepts_tilde_local_path[~/pkg]
    ::test_user_scope_accepts_tilde_local_path[~/sub/pkg]

These hit a different code path: production code in
install.package_resolution.user_scope_rejection_reason calls
Path('~/pkg').expanduser() to detect that the path is absolute.
ntpath.expanduser raises RuntimeError('Could not determine home
directory.') when both USERPROFILE and HOMEPATH are absent --
exactly the windows-2025-vs2026 worker env state.

Fix in tests/conftest.py (root, loaded by every xdist worker before
any test):

  - Wrap Path.expanduser. On RuntimeError, fall back to _TMP_HOME
    (the same hermetic dir Path.home() uses). The expanded path is
    still absolute, so production code's .is_absolute() check keeps
    behaving correctly.

Regression trap added in tests/unit/test_path_home_override.py: clears
HOME/USERPROFILE/HOMEDRIVE/HOMEPATH and asserts Path('~/pkg').expanduser()
returns an absolute path without raising.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* chore: correct PR number in CHANGELOG entry

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Daniel Meppiel <copilot-rework@github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants