| 1 |
"""Test for issue #252, fixed in PR #254. |
| 2 |
|
| 3 |
Issue #252: ConfigParser delimiter inconsistency between save_settings() and |
| 4 |
_load_config() causes config files with colons in values to be corrupted. |
| 5 |
|
| 6 |
PR #254 fixed this by ensuring both save_settings() and _load_config() use |
| 7 |
the same delimiter setting (delimiters="="). |
| 8 |
|
| 9 |
This test verifies that config files can be saved and loaded correctly even |
| 10 |
when values contain colons (e.g., time strings like "12:34:56"). |
| 11 |
""" |
| 12 |
|
| 13 |
import configparser |
| 14 |
import os |
| 15 |
from collections import defaultdict |
| 16 |
from unittest.mock import MagicMock, patch |
| 17 |
|
| 18 |
import pytest |
| 19 |
|
| 20 |
from s_tui.helper_functions import get_user_config_file |
| 21 |
|
| 22 |
|
| 23 |
class TestConfigDelimiter: |
| 24 |
"""Test that config files with colons in values work correctly. |
| 25 |
|
| 26 |
These tests verify the fix for issue #252 (PR #254) which ensures |
| 27 |
ConfigParser delimiter consistency between save_settings() and _load_config(). |
| 28 |
""" |
| 29 |
|
| 30 |
@pytest.fixture |
| 31 |
def temp_config_dir(self, tmp_path, mocker): |
| 32 |
"""Create a temporary config directory for testing.""" |
| 33 |
config_dir = tmp_path / "s-tui" |
| 34 |
config_dir.mkdir() |
| 35 |
mocker.patch( |
| 36 |
"s_tui.helper_functions.get_user_config_dir", |
| 37 |
return_value=str(config_dir), |
| 38 |
) |
| 39 |
mocker.patch( |
| 40 |
"s_tui.helper_functions.user_config_dir_exists", |
| 41 |
return_value=True, |
| 42 |
) |
| 43 |
mocker.patch( |
| 44 |
"s_tui.helper_functions.make_user_config_dir", |
| 45 |
return_value=str(config_dir), |
| 46 |
) |
| 47 |
return config_dir |
| 48 |
|
| 49 |
def test_save_settings_uses_correct_delimiter(self, temp_config_dir, mocker): |
| 50 |
"""Test that save_settings() uses delimiters="=" (fix from PR #254). |
| 51 |
|
| 52 |
Issue #252: save_settings() used ConfigParser() without delimiters="=", |
| 53 |
causing values with colons to be corrupted when saved. |
| 54 |
|
| 55 |
PR #254 fixed this by ensuring save_settings() uses delimiters="=" |
| 56 |
to match _load_config(). |
| 57 |
""" |
| 58 |
from s_tui.s_tui import GraphController |
| 59 |
from s_tui.sources.util_source import UtilSource |
| 60 |
|
| 61 |
|
| 62 |
util_source = UtilSource() |
| 63 |
|
| 64 |
|
| 65 |
mocker.patch("s_tui.s_tui.which", return_value=None) |
| 66 |
mocker.patch("argparse.ArgumentParser.parse_args").return_value = MagicMock( |
| 67 |
debug=False, |
| 68 |
debug_file=None, |
| 69 |
terminal=False, |
| 70 |
json=False, |
| 71 |
csv=False, |
| 72 |
csv_file=None, |
| 73 |
no_mouse=False, |
| 74 |
t_thresh=None, |
| 75 |
refresh_rate="2.0", |
| 76 |
) |
| 77 |
|
| 78 |
|
| 79 |
with patch("s_tui.s_tui.GraphView") as mock_view_class: |
| 80 |
mock_view = MagicMock() |
| 81 |
mock_view.graphs_menu = MagicMock() |
| 82 |
mock_view.graphs_menu.active_sensors = { |
| 83 |
"CPU Util": [True, True], |
| 84 |
} |
| 85 |
mock_view.summary_menu = MagicMock() |
| 86 |
mock_view.summary_menu.active_sensors = { |
| 87 |
"CPU Util": [True, True], |
| 88 |
} |
| 89 |
mock_view_class.return_value = mock_view |
| 90 |
|
| 91 |
controller = GraphController(MagicMock()) |
| 92 |
controller.sources = [util_source] |
| 93 |
controller.view = mock_view |
| 94 |
controller.refresh_rate = "2.0" |
| 95 |
controller.smooth_graph_mode = False |
| 96 |
controller.temp_thresh = None |
| 97 |
|
| 98 |
|
| 99 |
controller.save_settings() |
| 100 |
|
| 101 |
|
| 102 |
config_file = get_user_config_file() |
| 103 |
assert os.path.exists(config_file) |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
with open(config_file) as f: |
| 108 |
config_content = f.read() |
| 109 |
|
| 110 |
|
| 111 |
assert "refresh" in config_content |
| 112 |
|
| 113 |
lines = [ |
| 114 |
line.strip() |
| 115 |
for line in config_content.split("\n") |
| 116 |
if "refresh" in line and "=" in line |
| 117 |
] |
| 118 |
assert len(lines) > 0, "Config file should contain refresh setting" |
| 119 |
|
| 120 |
assert any( |
| 121 |
"=" in line and ":" not in line.split("=")[0] for line in lines |
| 122 |
), "Config should use '=' as delimiter, not ':' (PR #254 fix)" |
| 123 |
|
| 124 |
def test_configparser_delimiter_consistency(self): |
| 125 |
"""Test that ConfigParser with delimiters="=" preserves colons in values. |
| 126 |
|
| 127 |
This is the core fix from PR #254: using delimiters="=" ensures that |
| 128 |
colons in values are not treated as key-value separators. |
| 129 |
|
| 130 |
This test verifies the fix works by writing and reading a config with |
| 131 |
a colon in the value. |
| 132 |
""" |
| 133 |
|
| 134 |
conf_write = configparser.ConfigParser(delimiters="=") |
| 135 |
conf_write.add_section("GraphControl") |
| 136 |
conf_write.set("GraphControl", "refresh", "12:34:56") |
| 137 |
|
| 138 |
|
| 139 |
import tempfile |
| 140 |
|
| 141 |
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".conf") as f: |
| 142 |
conf_write.write(f) |
| 143 |
temp_file = f.name |
| 144 |
|
| 145 |
try: |
| 146 |
|
| 147 |
conf_read = configparser.ConfigParser(delimiters="=") |
| 148 |
conf_read.read(temp_file) |
| 149 |
|
| 150 |
|
| 151 |
refresh_value = conf_read.get("GraphControl", "refresh") |
| 152 |
assert refresh_value == "12:34:56", ( |
| 153 |
f"Expected '12:34:56' but got '{refresh_value}'. " |
| 154 |
"This verifies that delimiters='=' preserves colons in values." |
| 155 |
) |
| 156 |
finally: |
| 157 |
os.unlink(temp_file) |
| 158 |
|
| 159 |
|
| 160 |
class TestConfigSensorNameMatching: |
| 161 |
"""Test that sensor config is matched by name, not position. |
| 162 |
|
| 163 |
Verifies the fix for config loading that uses sensor names as keys |
| 164 |
instead of positional lists, so reordering/adding/removing sensors |
| 165 |
between restarts doesn't misalign visibility settings. |
| 166 |
""" |
| 167 |
|
| 168 |
def test_config_fewer_sensors_than_system(self): |
| 169 |
"""Config has fewer sensors than currently on the system. |
| 170 |
|
| 171 |
New sensors should default to visible (True). |
| 172 |
""" |
| 173 |
from s_tui.sensors_menu import SensorsMenu |
| 174 |
|
| 175 |
|
| 176 |
source = MagicMock() |
| 177 |
source.get_source_name.return_value = "Temp" |
| 178 |
source.get_sensor_list.return_value = ["Core0,0", "Core1,0", "Core2,0"] |
| 179 |
|
| 180 |
|
| 181 |
default_conf = {"Temp": {"core0,0": True, "core1,0": False}} |
| 182 |
|
| 183 |
menu = SensorsMenu(MagicMock(), [source], default_conf) |
| 184 |
active = menu.active_sensors["Temp"] |
| 185 |
assert active[0] is True |
| 186 |
assert active[1] is False |
| 187 |
assert active[2] is True |
| 188 |
|
| 189 |
def test_config_more_sensors_than_system(self): |
| 190 |
"""Config has more sensors than currently on the system. |
| 191 |
|
| 192 |
Extra config entries should be silently ignored. |
| 193 |
""" |
| 194 |
from s_tui.sensors_menu import SensorsMenu |
| 195 |
|
| 196 |
|
| 197 |
source = MagicMock() |
| 198 |
source.get_source_name.return_value = "Temp" |
| 199 |
source.get_sensor_list.return_value = ["Core0,0"] |
| 200 |
|
| 201 |
|
| 202 |
default_conf = {"Temp": {"core0,0": False, "core1,0": True, "core2,0": True}} |
| 203 |
|
| 204 |
menu = SensorsMenu(MagicMock(), [source], default_conf) |
| 205 |
active = menu.active_sensors["Temp"] |
| 206 |
assert len(active) == 1 |
| 207 |
assert active[0] is False |
| 208 |
|
| 209 |
def test_config_empty_defaults_all_visible(self): |
| 210 |
"""No config for a source means all sensors default to visible.""" |
| 211 |
from s_tui.sensors_menu import SensorsMenu |
| 212 |
|
| 213 |
source = MagicMock() |
| 214 |
source.get_source_name.return_value = "Fan" |
| 215 |
source.get_sensor_list.return_value = ["fan0", "fan1"] |
| 216 |
|
| 217 |
default_conf = defaultdict(dict) |
| 218 |
|
| 219 |
menu = SensorsMenu(MagicMock(), [source], default_conf) |
| 220 |
active = menu.active_sensors["Fan"] |
| 221 |
assert active == [True, True] |