Sort host lists in state file for consistent output (#174)
Some checks failed
CI / test (macos-latest, 3.11) (push) Has been cancelled
CI / test (macos-latest, 3.12) (push) Has been cancelled
CI / test (macos-latest, 3.13) (push) Has been cancelled
CI / test (ubuntu-latest, 3.11) (push) Has been cancelled
CI / test (ubuntu-latest, 3.12) (push) Has been cancelled
CI / test (ubuntu-latest, 3.13) (push) Has been cancelled
CI / browser-tests (push) Has been cancelled
CI / lint (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
TOC Generator / TOC Generator (push) Has been cancelled
Update README.md / update_readme (push) Has been cancelled

* Sort host lists in state file for consistent output

Multi-host stacks (like glances) now have their host lists sorted
alphabetically when saving state. This makes git diffs cleaner by
avoiding spurious reordering changes.
This commit is contained in:
Bas Nijholt
2026-01-30 15:29:13 -08:00
committed by GitHub
parent 596a05e39d
commit 4d65702868
3 changed files with 45 additions and 32 deletions

View File

@@ -64,8 +64,11 @@ def load_state(config: Config) -> dict[str, str | list[str]]:
def _sorted_dict(d: dict[str, str | list[str]]) -> dict[str, str | list[str]]: def _sorted_dict(d: dict[str, str | list[str]]) -> dict[str, str | list[str]]:
"""Return a dictionary sorted by keys.""" """Return a dictionary sorted by keys, with list values also sorted."""
return dict(sorted(d.items(), key=lambda item: item[0])) return {
k: sorted(v) if isinstance(v, list) else v
for k, v in sorted(d.items(), key=lambda item: item[0])
}
def save_state(config: Config, deployed: dict[str, str | list[str]]) -> None: def save_state(config: Config, deployed: dict[str, str | list[str]]) -> None:

View File

@@ -67,6 +67,16 @@ class TestSaveState:
assert "plex: nas01" in content assert "plex: nas01" in content
assert "jellyfin: nas02" in content assert "jellyfin: nas02" in content
def test_save_state_sorts_host_lists(self, config: Config) -> None:
"""Saves state with sorted host lists for consistent output."""
# Pass hosts in unsorted order
save_state(config, {"glances": ["pc", "nas", "hp", "anton"]})
state_file = config.get_state_path()
content = state_file.read_text()
# Hosts should be sorted alphabetically
assert "- anton\n - hp\n - nas\n - pc" in content
class TestGetStackHost: class TestGetStackHost:
"""Tests for get_stack_host function.""" """Tests for get_stack_host function."""