|
| 1 | +"""Unit tests for utils.log_paths.resolve_log_dir.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from utils.log_paths import resolve_log_dir |
| 10 | + |
| 11 | + |
| 12 | +class TestEnvOverride: |
| 13 | + def test_override_wins_on_macos(self): |
| 14 | + path = resolve_log_dir( |
| 15 | + platform="darwin", |
| 16 | + env={"UNITY_MCP_LOG_DIR": "/tmp/custom-logs"}, |
| 17 | + ) |
| 18 | + assert path == "/tmp/custom-logs" |
| 19 | + |
| 20 | + def test_override_wins_on_windows(self): |
| 21 | + path = resolve_log_dir( |
| 22 | + platform="win32", |
| 23 | + env={"UNITY_MCP_LOG_DIR": "/tmp/custom-logs", "LOCALAPPDATA": r"C:\ignored"}, |
| 24 | + ) |
| 25 | + assert path == "/tmp/custom-logs" |
| 26 | + |
| 27 | + def test_override_wins_on_linux(self): |
| 28 | + path = resolve_log_dir( |
| 29 | + platform="linux", |
| 30 | + env={"UNITY_MCP_LOG_DIR": "/tmp/custom-logs", "XDG_STATE_HOME": "/ignored"}, |
| 31 | + ) |
| 32 | + assert path == "/tmp/custom-logs" |
| 33 | + |
| 34 | + def test_override_expands_user(self): |
| 35 | + path = resolve_log_dir( |
| 36 | + platform="linux", |
| 37 | + env={"UNITY_MCP_LOG_DIR": "~/my-logs"}, |
| 38 | + ) |
| 39 | + assert path == os.path.expanduser("~/my-logs") |
| 40 | + assert "~" not in path |
| 41 | + |
| 42 | + |
| 43 | +def _norm(p: str) -> str: |
| 44 | + """Normalize for cross-host comparison: os.path.expanduser can leave |
| 45 | + mixed separators when a foreign platform is injected during testing.""" |
| 46 | + return os.path.normpath(p) |
| 47 | + |
| 48 | + |
| 49 | +class TestPlatformDefaults: |
| 50 | + def test_macos_uses_application_support(self): |
| 51 | + path = resolve_log_dir(platform="darwin", env={}) |
| 52 | + assert _norm(path).endswith(_norm( |
| 53 | + "Library/Application Support/UnityMCP/Logs")) |
| 54 | + assert "~" not in path |
| 55 | + |
| 56 | + def test_windows_uses_localappdata_env(self): |
| 57 | + path = resolve_log_dir( |
| 58 | + platform="win32", |
| 59 | + env={"LOCALAPPDATA": r"C:\Users\alice\AppData\Local"}, |
| 60 | + ) |
| 61 | + assert _norm(path) == _norm(os.path.join( |
| 62 | + r"C:\Users\alice\AppData\Local", "UnityMCP", "Logs")) |
| 63 | + |
| 64 | + def test_windows_falls_back_when_localappdata_missing(self): |
| 65 | + path = resolve_log_dir(platform="win32", env={}) |
| 66 | + # Fallback expands ~/AppData/Local; assert shape, not the exact user home. |
| 67 | + assert _norm(path).endswith(_norm("AppData/Local/UnityMCP/Logs")) |
| 68 | + assert "~" not in path |
| 69 | + |
| 70 | + def test_linux_uses_xdg_state_home(self): |
| 71 | + path = resolve_log_dir( |
| 72 | + platform="linux", |
| 73 | + env={"XDG_STATE_HOME": "/home/alice/.local/state"}, |
| 74 | + ) |
| 75 | + assert _norm(path) == _norm(os.path.join( |
| 76 | + "/home/alice/.local/state", "UnityMCP", "Logs")) |
| 77 | + |
| 78 | + def test_linux_falls_back_to_default_xdg_path(self): |
| 79 | + path = resolve_log_dir(platform="linux", env={}) |
| 80 | + assert _norm(path).endswith(_norm(".local/state/UnityMCP/Logs")) |
| 81 | + assert "~" not in path |
| 82 | + |
| 83 | + @pytest.mark.parametrize("unix_like", ["freebsd", "openbsd", "sunos5"]) |
| 84 | + def test_unknown_unix_variants_follow_linux_branch(self, unix_like): |
| 85 | + path = resolve_log_dir( |
| 86 | + platform=unix_like, |
| 87 | + env={"XDG_STATE_HOME": "/var/state"}, |
| 88 | + ) |
| 89 | + assert _norm(path) == _norm(os.path.join( |
| 90 | + "/var/state", "UnityMCP", "Logs")) |
| 91 | + |
| 92 | + |
| 93 | +class TestDefaults: |
| 94 | + def test_no_args_reads_live_environment(self, monkeypatch): |
| 95 | + """With no args, should read sys.platform and os.environ directly.""" |
| 96 | + monkeypatch.setenv("UNITY_MCP_LOG_DIR", "/tmp/live-env-test") |
| 97 | + assert resolve_log_dir() == "/tmp/live-env-test" |
0 commit comments