Skip to content

fix: isolate test_api focus resolution from ancestor .pt-snap/focus.json - #124

Merged
codeasier merged 1 commit into
mainfrom
fix/issue-117-test-api-cwd-isolation
Sep 17, 2026
Merged

codeasier merged 1 commit into
mainfrom
fix/issue-117-test-api-cwd-isolation

Conversation

@codeasier

Copy link
Copy Markdown
Owner

📝 Description

Config.resolve_focus() walks up from Path.cwd() looking for .pt-snap/focus.json, but the autouse fixture in tests/test_api.py only neutralized PT_SNAP_DB_PATH and Path.home(). Any focus file in the repository root (or another ancestor) — exactly what pt-snap focus / pt-snap import write to the current directory — leaked into test_set_focus_device_requires_database, which then raised FileNotFoundError for the stale database instead of the expected RuntimeError("... Set db_path ...").

This is the same defect class as #52; #53 fixed tests/test_completion.py and every other CLI/MCP/service test module has since adopted monkeypatch.chdir(tmp_path). tests/test_api.py was the last one still using the old fixture.

Changes:

  • Replace the hand-rolled _isolate_config with a monkeypatch-based fixture that also chdirs into tmp_path and uses a per-test home directory (the old fixture leaked an uncleaned tempfile.mkdtemp() per test).
  • Add test_isolated_environment_has_no_inherited_focus, which pins the invariant that a fresh SnapshotAnalyzer() in this module resolves to source == "none" regardless of the developer's machine.
  • Drop the now-unused os import.

No production code changes.

🔗 Related Issue

Fixes #117

🧪 Testing

  • Tests added/updated
  • Manual testing completed

Reproduction and verification, run from the branch checkout with a leaked focus file placed at the repository root (.pt-snap/focus.json → /nonexistent/leaked.db):

Test file Result
tests/test_api.py from main 1 failed, 17 passed (test_set_focus_device_requires_database)
tests/test_api.py from this branch 19 passed

Full suite on this branch (macOS, Python 3.13.11): 770 passed, 1 xfailed. ruff check and black --check clean.

✅ Checklist

  • Code follows project guidelines
  • Documentation updated (if applicable) — not applicable, test-only change
  • No new warnings introduced
  • Changes tested locally

Snapshot Runtime Provenance

  • Snapshot provenance evaluation completed when snapshot runtime files change
    Snapshot provenance decision: no-update
    Snapshot provenance no-update reason: This PR only changes tests/test_api.py; no files under src/pt_snap_cli/snapshot/ are touched.

📋 Additional Notes

AGENTS.md change routing: "MCP or Python API behavior" → tests/test_api.py, tests/test_mcp_server.py, tests/test_contract_cli_mcp.py; all three pass.

Config.resolve_focus() walks up from the current directory looking for
.pt-snap/focus.json, but the autouse fixture in tests/test_api.py only
neutralized PT_SNAP_DB_PATH and Path.home(). A focus file written to the
repository root by `pt-snap focus` or `pt-snap import` therefore leaked
into test_set_focus_device_requires_database, which expected no database
to be configured.

Switch the fixture to monkeypatch and chdir into tmp_path, matching the
isolation already used by every other CLI/MCP/service test module since
#53, and add a test that pins the invariant that a fresh analyzer sees no
inherited focus.

Fixes #117
@codeasier
codeasier merged commit a115a3b into main Sep 17, 2026
6 checks passed
@codeasier
codeasier deleted the fix/issue-117-test-api-cwd-isolation branch October 3, 2026 19:18
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] tests/test_api.py 未隔离工作目录,祖先目录存在 .pt-snap/focus.json 时 test_set_focus_device_requires_database 失败

1 participant