88 lines
2 KiB
Python
88 lines
2 KiB
Python
import datetime as dt
|
|
from pathlib import Path
|
|
|
|
import tomlkit
|
|
|
|
from forge import cli
|
|
|
|
|
|
def test_parse_config_arrays(tmp_path: Path) -> None:
|
|
config_path = tmp_path / "forge.toml"
|
|
config_path.write_text(
|
|
"""
|
|
copr = ["copr.fedorainfracloud.org/user/repo"]
|
|
packages = ["git", "vim"]
|
|
|
|
[forge]
|
|
version = "1.0"
|
|
last_switch = ""
|
|
|
|
[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.dotfiles == {".bashrc": ".bashrc"}
|
|
assert config.options["backup"] is True
|
|
assert config.legacy is False
|
|
|
|
|
|
def test_parse_config_legacy_format(tmp_path: Path) -> None:
|
|
config_path = tmp_path / "forge.toml"
|
|
config_path.write_text(
|
|
"""
|
|
[packages]
|
|
git
|
|
vim
|
|
|
|
[copr]
|
|
copr.fedorainfracloud.org/user/repo
|
|
|
|
[dotfiles]
|
|
".bashrc" = ".bashrc"
|
|
""".strip()
|
|
)
|
|
|
|
config = cli.parse_config(config_path)
|
|
|
|
assert config.packages == ["git", "vim"]
|
|
assert config.copr == ["copr.fedorainfracloud.org/user/repo"]
|
|
assert config.dotfiles == {".bashrc": ".bashrc"}
|
|
assert config.legacy is True
|
|
|
|
|
|
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 / "forge.toml"
|
|
config_path.write_text(
|
|
"""
|
|
[forge]
|
|
version = "1.0"
|
|
last_switch = ""
|
|
""".strip()
|
|
)
|
|
|
|
options = {"backup": True, "backup_dir": "backup"}
|
|
cli.update_last_switch(config_path, options)
|
|
|
|
doc = tomlkit.parse(config_path.read_text())
|
|
assert isinstance(doc["forge"]["last_switch"], dt.datetime)
|
|
|
|
backup_dir = config_dir / "backup"
|
|
backups = list(backup_dir.iterdir())
|
|
assert backups, "expected a backup of forge.toml to be created"
|
|
assert any(path.name.startswith("forge.toml.") for path in backups)
|