|
| 1 | +"""Unit tests for session helper functions.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from codetracer_python_recorder import session |
| 9 | + |
| 10 | + |
| 11 | +def test_coerce_format_accepts_supported_aliases() -> None: |
| 12 | + assert session._coerce_format("json") == "json" |
| 13 | + assert session._coerce_format("JSON") == "json" |
| 14 | + assert session._coerce_format("binary") == "binary" |
| 15 | + |
| 16 | + |
| 17 | +def test_coerce_format_rejects_unknown_value() -> None: |
| 18 | + with pytest.raises(ValueError) as excinfo: |
| 19 | + session._coerce_format("yaml") |
| 20 | + assert "unsupported trace format" in str(excinfo.value) |
| 21 | + |
| 22 | + |
| 23 | +def test_validate_trace_path_expands_user(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 24 | + home_dir = tmp_path / "home" |
| 25 | + home_dir.mkdir() |
| 26 | + target = home_dir / "traces" |
| 27 | + |
| 28 | + monkeypatch.setenv("HOME", str(home_dir)) |
| 29 | + path = session._validate_trace_path(Path("~/traces")) |
| 30 | + |
| 31 | + assert path == target |
| 32 | + assert path.parent == home_dir |
| 33 | + |
| 34 | + |
| 35 | +def test_validate_trace_path_rejects_file(tmp_path: Path) -> None: |
| 36 | + file_path = tmp_path / "trace.bin" |
| 37 | + file_path.write_text("stub") |
| 38 | + with pytest.raises(ValueError): |
| 39 | + session._validate_trace_path(file_path) |
| 40 | + |
| 41 | + |
| 42 | +def test_normalize_activation_path_handles_none() -> None: |
| 43 | + assert session._normalize_activation_path(None) is None |
| 44 | + |
| 45 | + |
| 46 | +def test_normalize_activation_path_expands_user(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 47 | + home_dir = tmp_path / "home" |
| 48 | + home_dir.mkdir() |
| 49 | + script = home_dir / "script.py" |
| 50 | + script.write_text("print('hi')\n") |
| 51 | + |
| 52 | + monkeypatch.setenv("HOME", str(home_dir)) |
| 53 | + result = session._normalize_activation_path("~/script.py") |
| 54 | + |
| 55 | + assert result == str(script) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture(autouse=True) |
| 59 | +def clear_active_session() -> None: |
| 60 | + session._active_session = None |
| 61 | + yield |
| 62 | + session._active_session = None |
| 63 | + |
| 64 | + |
| 65 | +def test_trace_session_stop_clears_global(monkeypatch: pytest.MonkeyPatch) -> None: |
| 66 | + called = {"stop": False, "start": False} |
| 67 | + |
| 68 | + def fake_start(*args, **kwargs) -> None: |
| 69 | + called["start"] = True |
| 70 | + |
| 71 | + def fake_stop() -> None: |
| 72 | + called["stop"] = True |
| 73 | + |
| 74 | + monkeypatch.setattr(session, "_start_backend", fake_start) |
| 75 | + monkeypatch.setattr(session, "_stop_backend", fake_stop) |
| 76 | + monkeypatch.setattr(session, "_is_tracing_backend", lambda: session._active_session is not None) |
| 77 | + |
| 78 | + session._active_session = session.TraceSession(path=Path("/tmp"), format="json") |
| 79 | + session.stop() |
| 80 | + assert session._active_session is None |
| 81 | + assert called["stop"] is True |
| 82 | + |
| 83 | + |
| 84 | +def test_trace_session_flush_noop_when_inactive(monkeypatch: pytest.MonkeyPatch) -> None: |
| 85 | + monkeypatch.setattr(session, "_is_tracing_backend", lambda: False) |
| 86 | + flushed = [] |
| 87 | + |
| 88 | + def fake_flush() -> None: |
| 89 | + flushed.append(True) |
| 90 | + |
| 91 | + monkeypatch.setattr(session, "_flush_backend", fake_flush) |
| 92 | + session.flush() |
| 93 | + assert flushed == [] |
| 94 | + |
| 95 | + |
| 96 | +def test_trace_context_manager_starts_and_stops(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: |
| 97 | + calls = {"start": [], "stop": []} |
| 98 | + |
| 99 | + trace_state = {"active": False} |
| 100 | + |
| 101 | + def fake_start(path: str, fmt: str, activation: str | None) -> None: |
| 102 | + trace_state["active"] = True |
| 103 | + calls["start"].append((Path(path), fmt, activation)) |
| 104 | + |
| 105 | + def fake_stop() -> None: |
| 106 | + trace_state["active"] = False |
| 107 | + calls["stop"].append(True) |
| 108 | + |
| 109 | + monkeypatch.setattr(session, "_start_backend", fake_start) |
| 110 | + monkeypatch.setattr(session, "_stop_backend", fake_stop) |
| 111 | + monkeypatch.setattr(session, "_is_tracing_backend", lambda: trace_state["active"]) |
| 112 | + monkeypatch.setattr(session, "_flush_backend", lambda: None) |
| 113 | + |
| 114 | + target = tmp_path / "trace" |
| 115 | + target.mkdir() |
| 116 | + with session.trace(target, format="binary") as handle: |
| 117 | + assert isinstance(handle, session.TraceSession) |
| 118 | + assert handle.path == target.expanduser() |
| 119 | + assert handle.format == "binary" |
| 120 | + |
| 121 | + assert calls["start"] == [(target, "binary", None)] |
| 122 | + assert calls["stop"] == [True] |
0 commit comments