124 lines
2.8 KiB
Python
124 lines
2.8 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)
|