curator/tests/test_config.py
2025-12-24 12:56:51 +02:00

164 lines
4 KiB
Python

from pathlib import Path
import datetime as dt
from curator import cli
def test_parse_config_line_format(tmp_path: Path) -> None:
config_path = tmp_path / "inventory.toml"
config_path.write_text(
"""
[curator]
version = "1.0"
last_switch = ""
[copr]
copr.fedorainfracloud.org/user/repo
[dnf]
git
vim
[brew]
wget
[flatpak]
org.mozilla.firefox
[rpm-ostree]
podman
[nix]
nixpkgs#git
[dotfiles]
".bashrc" = ".bashrc"
[options]
backup = true
backup_dir = "backup"
""".strip()
)
config = cli.parse_config(config_path)
assert config.copr == ["copr.fedorainfracloud.org/user/repo"]
assert config.packages == ["git", "vim"]
assert config.brew_packages == ["wget"]
assert config.flatpak_refs == ["org.mozilla.firefox"]
assert config.rpm_ostree_packages == ["podman"]
assert config.nix_packages == ["nixpkgs#git"]
assert config.dotfiles == {".bashrc": ".bashrc"}
assert config.options["backup"] is True
def test_parse_config_legacy_format(tmp_path: Path) -> None:
config_path = tmp_path / "inventory.toml"
config_path.write_text(
"""
[dnf]
git
vim
[brew]
wget
[flatpak]
org.mozilla.firefox
[rpm-ostree]
podman
[nix]
nixpkgs#git
[copr]
copr.fedorainfracloud.org/user/repo
[dotfiles]
".bashrc" = ".bashrc"
""".strip()
)
config = cli.parse_config(config_path)
assert config.packages == ["git", "vim"]
assert config.brew_packages == ["wget"]
assert config.flatpak_refs == ["org.mozilla.firefox"]
assert config.rpm_ostree_packages == ["podman"]
assert config.nix_packages == ["nixpkgs#git"]
assert config.copr == ["copr.fedorainfracloud.org/user/repo"]
assert config.dotfiles == {".bashrc": ".bashrc"}
def test_diff_items() -> None:
added, removed = cli.diff_items(["a", "b"], ["b", "c"])
assert added == ["a"]
assert removed == ["c"]
def test_update_last_switch_creates_backup(tmp_path: Path) -> None:
config_dir = tmp_path
config_path = config_dir / "inventory.toml"
config_path.write_text(
"""
[curator]
version = "1.0"
last_switch = ""
""".strip()
)
options = {"backup": True, "backup_dir": "backup"}
cli.update_last_switch(config_path, options)
lines = [line.strip() for line in config_path.read_text().splitlines() if line.strip()]
last_switch_line = [line for line in lines if line.startswith("last_switch")]
assert last_switch_line, "last_switch should be written"
ts_str = last_switch_line[0].split("=", 1)[1].strip().strip('"')
dt.datetime.fromisoformat(ts_str)
backup_dir = config_dir / "backup"
backups = list(backup_dir.iterdir())
assert backups, "expected a backup of inventory.toml to be created"
assert any(path.name.startswith("inventory.toml.") for path in backups)
def test_reset_previous_config(tmp_path: Path, monkeypatch) -> None:
rollback = tmp_path / "inventory.toml.rollback"
rollback.write_text("prev")
def fake_get_paths():
return tmp_path, tmp_path / "inventory.toml", tmp_path / "inventory.toml.last", rollback
monkeypatch.setattr(cli, "get_paths", fake_get_paths)
cli.reset_previous_config()
assert not rollback.exists()
def test_update_section_entries_rewrites_section(tmp_path: Path) -> None:
config_path = tmp_path / "inventory.toml"
config_path.write_text(
"""
[dnf]
git
[copr]
copr.fedorainfracloud.org/user/repo
[brew]
wget
""".strip()
)
cli.update_section_entries(config_path, "dnf", ["vim", "curl", "vim"])
cli.update_section_entries(config_path, "copr", ["copr.fedorainfracloud.org/user/repo", "copr.fedorainfracloud.org/another/repo"])
content = config_path.read_text().strip().splitlines()
dnf_index = content.index("[dnf]")
assert content[dnf_index + 1 : dnf_index + 3] == ["curl", "vim"]
assert "[copr]" in content
copr_index = content.index("[copr]")
assert content[copr_index + 1 : copr_index + 3] == [
"copr.fedorainfracloud.org/another/repo",
"copr.fedorainfracloud.org/user/repo",
]