- Wrap commands with unique fence markers (printf FENCE; cmd; printf FENCE) to isolate real output from shell init/exit noise (oh-my-zsh, macOS session restore/save, docker plugin errors, etc.) - Expand _clean_shell_noise to cover zsh/macOS patterns and strip from both beginning and end (fallback when fences are missing) - Fix BSD find compatibility: fallback to simple find when -printf produces empty output (macOS) - Fix test_terminal_disk_usage: use sys.modules to get the real module instead of the shadowed function from tools/__init__.py - Add 13 new unit tests for fence extraction and zsh noise patterns
65 lines
2.4 KiB
Python
65 lines
2.4 KiB
Python
"""Tests for get_active_environments_info disk usage calculation."""
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
import pytest
|
|
|
|
# tools/__init__.py re-exports a *function* called ``terminal_tool`` which
|
|
# shadows the module of the same name. Use sys.modules to get the real module
|
|
# so patch.object works correctly.
|
|
import sys
|
|
import tools.terminal_tool # noqa: F401 -- ensure module is loaded
|
|
_tt_mod = sys.modules["tools.terminal_tool"]
|
|
from tools.terminal_tool import get_active_environments_info
|
|
|
|
# 1 MiB of data so the rounded MB value is clearly distinguishable
|
|
_1MB = b"x" * (1024 * 1024)
|
|
|
|
|
|
@pytest.fixture()
|
|
def fake_scratch(tmp_path):
|
|
"""Create fake hermes scratch directories with known sizes."""
|
|
# Task A: 1 MiB
|
|
task_a_dir = tmp_path / "hermes-sandbox-aaaaaaaa"
|
|
task_a_dir.mkdir()
|
|
(task_a_dir / "data.bin").write_bytes(_1MB)
|
|
|
|
# Task B: 1 MiB
|
|
task_b_dir = tmp_path / "hermes-sandbox-bbbbbbbb"
|
|
task_b_dir.mkdir()
|
|
(task_b_dir / "data.bin").write_bytes(_1MB)
|
|
|
|
return tmp_path
|
|
|
|
|
|
class TestDiskUsageGlob:
|
|
def test_only_counts_matching_task_dirs(self, fake_scratch):
|
|
"""Each task should only count its own directories, not all hermes-* dirs."""
|
|
fake_envs = {
|
|
"aaaaaaaa-1111-2222-3333-444444444444": MagicMock(),
|
|
}
|
|
|
|
with patch.object(_tt_mod, "_active_environments", fake_envs), \
|
|
patch.object(_tt_mod, "_get_scratch_dir", return_value=fake_scratch):
|
|
info = get_active_environments_info()
|
|
|
|
# Task A only: ~1.0 MB. With the bug (hardcoded hermes-*),
|
|
# it would also count task B -> ~2.0 MB.
|
|
assert info["total_disk_usage_mb"] == pytest.approx(1.0, abs=0.1)
|
|
|
|
def test_multiple_tasks_no_double_counting(self, fake_scratch):
|
|
"""With 2 active tasks, each should count only its own dirs."""
|
|
fake_envs = {
|
|
"aaaaaaaa-1111-2222-3333-444444444444": MagicMock(),
|
|
"bbbbbbbb-5555-6666-7777-888888888888": MagicMock(),
|
|
}
|
|
|
|
with patch.object(_tt_mod, "_active_environments", fake_envs), \
|
|
patch.object(_tt_mod, "_get_scratch_dir", return_value=fake_scratch):
|
|
info = get_active_environments_info()
|
|
|
|
# Should be ~2.0 MB total (1 MB per task).
|
|
# With the bug, each task globs everything -> ~4.0 MB.
|
|
assert info["total_disk_usage_mb"] == pytest.approx(2.0, abs=0.1)
|