Skip to content

fix(tests): hermetic HOME for unit tests on Windows runner#1270

Merged
danielmeppiel merged 1 commit into
mainfrom
fix/windows-home-runner
May 11, 2026
Merged

fix(tests): hermetic HOME for unit tests on Windows runner#1270
danielmeppiel merged 1 commit into
mainfrom
fix/windows-home-runner

Conversation

@danielmeppiel
Copy link
Copy Markdown
Collaborator

TL;DR

The post-tag CI/CD Pipeline run on v0.13.0 (25660187642) failed Windows unit tests with 57 failures:

  • 56: RuntimeError: Could not determine home directory from Path.home() in tests/unit/test_runtime_manager.py and tests/unit/test_runtime_detection.py.
  • 1: test_update_command_respects_disabled_policy -- requests.get called once when it should not have been.

Both were latent test-infra issues exposed by GitHub redirecting windows-latest from windows-2025 to windows-2025-vs2026. No product code change.

Problem

1. Path.home() on the new Windows image

Path.home() on Windows reads USERPROFILE first, then HOMEDRIVE+HOMEPATH. The windows-2025-vs2026 image does not seed any of these for pytest worker subprocesses. Any unit test that touches Path.home() (directly or via get_user_runtime_dir() etc.) raises.

2. Missed mock in disabled-self-update test

apm_cli/commands/_helpers.py does from ..update_policy import ... is_self_update_enabled, rebinding the symbol locally. _check_and_notify_updates() runs on every cli() invocation. The test only patched apm_cli.commands.self_update.is_self_update_enabled, leaving the _helpers reference live. On Linux xdist workers an earlier test primed ~/.cache/apm/last_version_check so should_check_for_updates() skipped the network call; Windows workers were cold, so requests.get fired.

Fix

  1. tests/unit/conftest.py (new) -- session-scoped autouse _hermetic_home fixture pins Path.home() to a tmp dir via tmp_path_factory.mktemp. Sets HOME on POSIX and USERPROFILE/HOMEDRIVE/HOMEPATH on Windows. Restores prior values on teardown. Per-test monkeypatch.setenv fixtures (_set_home etc.) keep working for their scope.
  2. tests/unit/test_update_command.py -- stack a second @patch("apm_cli.commands._helpers.is_self_update_enabled", return_value=False) on test_update_command_respects_disabled_policy and add the matching mock arg.

Validation

Ran locally on macOS:

uv run --extra dev pytest tests/unit tests/test_console.py -n auto --dist worksteal -q
# 8281 passed, 29 subtests passed in 70.94s

Lint mirror clean:

uv run --extra dev ruff check src/ tests/    # All checks passed!
uv run --extra dev ruff format --check src/ tests/   # 746 files already formatted

Cannot reproduce the USERPROFILE drift on macOS, but the conftest fix unifies behaviour with the existing per-test _set_home helper at tests/unit/integration/test_scope_integration.py:25-40, so the Windows runner will now see a stable Path.home() for every unit test.

How to test

Triggered on push to fix/windows-home-runner; the merge will re-trigger the CI/CD Pipeline job on Windows after we re-tag v0.13.0.

Copilot AI review requested due to automatic review settings May 11, 2026 09:03
…d mock gap

The new windows-2025-vs2026 GitHub-hosted runner image does not seed
USERPROFILE/HOMEDRIVE+HOMEPATH for pytest workers, causing 56 unit-test
failures with 'RuntimeError: Could not determine home directory' from
Path.home(). A session-scoped autouse conftest fixture pins HOME (POSIX)
and USERPROFILE/HOMEDRIVE/HOMEPATH (Windows) to a tmp dir for the entire
unit-test session, restoring previous values on teardown. Per-test
monkeypatch fixtures still win for their tests.

Separately, test_update_command_respects_disabled_policy patched only
apm_cli.commands.self_update.is_self_update_enabled but missed the
matching import in apm_cli.commands._helpers (used by
_check_and_notify_updates on every CLI invocation), letting requests.get
fire whenever the version-check cache was cold. Linux xdist workers
inherited a primed cache from neighbouring tests; Windows workers did
not, surfacing the latent gap. Add the second @patch.

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 fixes Windows CI unit test failures by making Path.home() deterministic in unit tests and by correctly patching an import-bound is_self_update_enabled() symbol so the disabled-self-update test cannot accidentally hit the network.

Changes:

  • Add a session-scoped autouse unit-test fixture that sets a hermetic HOME/USERPROFILE (and related Windows vars) for the duration of tests/unit.
  • Fix test_update_command_respects_disabled_policy by also patching apm_cli.commands._helpers.is_self_update_enabled.
  • Add a changelog entry under Unreleased.
Show a summary per file
File Description
tests/unit/conftest.py Adds session-wide hermetic HOME environment setup so Path.home() works reliably (including on Windows runners).
tests/unit/test_update_command.py Ensures the disabled-self-update test patches the correct import binding to prevent unintended network calls.
CHANGELOG.md Documents the test-infra fix under Unreleased.

Copilot's findings

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

Comment thread CHANGELOG.md

### Fixed

- Pin `Path.home()` under unit tests via a session-scoped autouse conftest fixture, fixing 56 Windows runner failures on the new `windows-2025-vs2026` GitHub-hosted image where `USERPROFILE`/`HOMEDRIVE`+`HOMEPATH` are not seeded for pytest workers; also patch the `_check_and_notify_updates` import binding in the disabled-self-update test so it no longer races on the version-check cache. (#1270)
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