Debian Codegraph @

tests/conftest.py

raw symbols SCIP
1 """Shared fixtures, markers, and psutil mock helpers for the s-tui test suite."""
2
3 import os
4 from collections import OrderedDict, namedtuple
5
6 import pytest
7
8 # ---------------------------------------------------------------------------
9 # Marker registration
10 # ---------------------------------------------------------------------------
11
12
13 def pytest_configure(config):
14 config.addinivalue_line(
15 "markers", "hardware: test requires real hardware sensors (skipped in CI)"
16 )
17
18
19 def _is_ci():
20 return os.environ.get("CI") == "true" or os.environ.get("GITHUB_ACTIONS") == "true"
21
22
23 def pytest_collection_modifyitems(config, items):
24 """Auto-skip hardware-marked tests when running inside CI."""
25 if _is_ci():
26 skip_hw = pytest.mark.skip(reason="Hardware not available in CI")
27 for item in items:
28 if "hardware" in item.keywords:
29 item.add_marker(skip_hw)
30
31
32 # ---------------------------------------------------------------------------
33 # Named-tuple helpers (mirror psutil's own types)
34 # ---------------------------------------------------------------------------
35
36 CpuFreq = namedtuple("scpufreq", ["current", "min", "max"])
37 SensorTemperature = namedtuple("shwtemp", ["label", "current", "high", "critical"])
38 SensorFan = namedtuple("sfan", ["label", "current"])
39 RaplStats = namedtuple("rapl", ["label", "current", "max"])
40
41
42 # ---------------------------------------------------------------------------
43 # Default fake data builders
44 # ---------------------------------------------------------------------------
45
46
47 def make_cpu_freq_list(count=4, current=2400.0, min_freq=800.0, max_freq=3600.0):
48 """Return a list of *count* CpuFreq named-tuples."""
49 return [CpuFreq(current=current, min=min_freq, max=max_freq) for _ in range(count)]
50
51
52 def make_cpu_freq_overall(current=2400.0, min_freq=800.0, max_freq=3600.0):
53 """Return a single CpuFreq named-tuple (overall)."""
54 return CpuFreq(current=current, min=min_freq, max=max_freq)
55
56
57 def make_temperatures_dict(
58 count=2,
59 label_prefix="Core",
60 current=55.0,
61 high=80.0,
62 critical=100.0,
63 group_name="coretemp",
64 ):
65 """Return an OrderedDict matching psutil.sensors_temperatures() shape."""
66 sensors = []
67 for i in range(count):
68 sensors.append(
69 SensorTemperature(
70 label=f"{label_prefix} {i}",
71 current=current,
72 high=high,
73 critical=critical,
74 )
75 )
76 return OrderedDict([(group_name, sensors)])
77
78
79 def make_fans_dict(count=1, label_prefix="fan", current=1200, group_name="thinkpad"):
80 """Return a dict matching psutil.sensors_fans() shape."""
81 fans = []
82 for i in range(count):
83 fans.append(SensorFan(label=f"{label_prefix}{i}", current=current))
84 return {group_name: fans}
85
86
87 # ---------------------------------------------------------------------------
88 # Reusable psutil mock fixtures
89 # ---------------------------------------------------------------------------
90
91
92 @pytest.fixture
93 def mock_cpu_count(mocker):
94 """Mock psutil.cpu_count to return 4, with matching topology helpers."""
95 mocker.patch("s_tui.sources.source.Source._get_total_core_count", return_value=4)
96 mocker.patch(
97 "s_tui.sources.source.Source._get_online_cpu_ids",
98 return_value=[0, 1, 2, 3],
99 )
100 return mocker.patch("psutil.cpu_count", return_value=4)
101
102
103 @pytest.fixture
104 def mock_cpu_percent(mocker):
105 """Mock psutil.cpu_percent to return per-cpu values for 4 cores."""
106 return mocker.patch("psutil.cpu_percent", return_value=[25.0, 30.0, 20.0, 15.0])
107
108
109 @pytest.fixture
110 def mock_cpu_freq(mocker):
111 """Mock psutil.cpu_freq for 4 cores + overall, with matching topology helpers."""
112 per_cpu = make_cpu_freq_list(4)
113 overall = make_cpu_freq_overall()
114
115 def _cpu_freq(percpu=False):
116 if percpu:
117 return per_cpu
118 return overall
119
120 mocker.patch("s_tui.sources.source.Source._get_total_core_count", return_value=4)
121 mocker.patch(
122 "s_tui.sources.source.Source._get_online_cpu_ids",
123 return_value=[0, 1, 2, 3],
124 )
125 return mocker.patch("psutil.cpu_freq", side_effect=_cpu_freq)
126
127
128 @pytest.fixture
129 def mock_sensors_temperatures(mocker):
130 """Mock psutil.sensors_temperatures with 2 coretemp sensors."""
131 temps = make_temperatures_dict(count=2)
132 return mocker.patch("psutil.sensors_temperatures", return_value=temps)
133
134
135 @pytest.fixture
136 def mock_sensors_fans(mocker):
137 """Mock psutil.sensors_fans with 1 fan."""
138 fans = make_fans_dict(count=1)
139 return mocker.patch("psutil.sensors_fans", return_value=fans)