Skip to content

test(audit): patch run_replay where audit_replay uses it (#4876) - #4881

Closed
Daniel Meppiel (danielmeppiel) wants to merge 1 commit into
mainfrom
danielmeppiel-ubiquitous-invention
Closed

Daniel Meppiel (danielmeppiel) wants to merge 1 commit into
mainfrom
danielmeppiel-ubiquitous-invention

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

Description

Fixes #4876

Cause: audit_replay.py binds run_replay at import time (from apm_cli.install.drift import run_replay). When test_audit_ci_command.py patched apm_cli.install.drift.run_replay and audit_replay was first imported lazily inside that patch (via commands/audit.py), the Mock was captured in audit_replay's module globals for good, tripping the session-end leak guard. Order-dependent under xdist.

Fix: import apm_cli.install.audit_replay at test-module top so the binding is the real function before any patch, and assert audit_replay.run_replay is drift.run_replay after each patched block (fails deterministically without the import). The patch target stays apm_cli.install.drift.run_replay: for this fixture the drift check runs via policy/ci_checks._check_drift, which looks run_replay up lazily from drift; retargeting to audit_replay.run_replay makes the three tests exit 0. The other drift.run_replay monkeypatch sites (test_ci_checks, test_export_patch, test_drift_check e10) never import audit_replay, so they are not affected.

Issue and approved scope

Issue: Fixes #4876

Human scope-approval comment: none on the issue yet; fix requested via the maintainer's QL session as a bounded test-only change.

Completes the issue.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally

  • All existing tests pass

  • Added tests for new functionality (if applicable)

  • uv run --extra dev ruff check tests/unit/test_audit_ci_command.py -- passed

  • uv run --extra dev ruff format --check tests/unit/test_audit_ci_command.py -- passed

  • uv run --extra dev pytest tests/unit/test_audit_ci_command.py -q -p no:xdist -- 17 passed

  • Regression check: with the assertion but without the top-level import, -k "native_drift_boundary or policy_blocked_drift" -- 3 failed (leak reproduced)

Full suites not run.

Changelog

  • One fragment per section, changelog.d/<pr>.<section>.md.
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • N/A -- this PR does not change OpenAPM-observable behaviour.

Import apm_cli.install.audit_replay at test-module top so its run_replay
binding is the real function before any test patches drift.run_replay,
and assert the binding is intact after each patched block.

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block approval of the test-isolation change.

No open issues 🎉

What changed in this PR

Addresses the audit CI test-isolation flake in #4876 by preventing a patched run_replay mock from persisting in audit_replay module globals. Production behavior is unchanged.

Changes:

  • Imports audit_replay before test patches can capture a mock during lazy loading.
  • Adds binding-identity assertions after the affected patch blocks while retaining the patch target used by the CI drift checks.
File Description
tests/​unit/​test_audit_ci_command.py Eagerly imports replay modules and checks that patched tests leave the real run_replay binding intact.

Review effort: Balanced

@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

Superseded by #4884 (conftest preload of apm_cli.install.audit_replay). This PR's approach trips AQ004_patches_owner_it_imports in scripts/check_test_assertions.py (tests/unit/test_audit_ci_command.py:228,257).

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.

[BUG] Flaky unit shard: test_audit_ci_command patches drift.run_replay and leaks a Mock into audit_replay

2 participants