added env var support
This commit is contained in:
parent
7d51de1052
commit
25c80fdf3b
3 changed files with 390 additions and 10 deletions
|
|
@ -67,11 +67,14 @@ wget
|
|||
[flatpak]
|
||||
org.mozilla.firefox
|
||||
|
||||
[rpm-ostree]
|
||||
podman
|
||||
[rpm-ostree]
|
||||
podman
|
||||
|
||||
[nix]
|
||||
nixpkgs#git
|
||||
[nix]
|
||||
nixpkgs#git
|
||||
|
||||
[variables]
|
||||
EDITOR = "nvim"
|
||||
|
||||
[copr]
|
||||
copr.fedorainfracloud.org/user/repo
|
||||
|
|
@ -90,6 +93,7 @@ copr.fedorainfracloud.org/user/repo
|
|||
assert config.nix_packages == ["nixpkgs#git"]
|
||||
assert config.copr == ["copr.fedorainfracloud.org/user/repo"]
|
||||
assert config.dotfiles == {".bashrc": ".bashrc"}
|
||||
assert config.variables == {"EDITOR": "nvim"}
|
||||
|
||||
|
||||
def test_diff_items() -> None:
|
||||
|
|
@ -162,3 +166,94 @@ wget
|
|||
"copr.fedorainfracloud.org/another/repo",
|
||||
"copr.fedorainfracloud.org/user/repo",
|
||||
]
|
||||
|
||||
|
||||
def test_merge_section_entries_preserves_spacing(tmp_path: Path) -> None:
|
||||
config_path = tmp_path / "inventory.toml"
|
||||
config_path.write_text(
|
||||
"""
|
||||
[dnf]
|
||||
git
|
||||
|
||||
[brew]
|
||||
wget
|
||||
""".strip()
|
||||
)
|
||||
|
||||
cli.merge_section_entries(config_path, "dnf", {"vim"}, set())
|
||||
|
||||
lines = config_path.read_text().splitlines()
|
||||
assert "" in lines, "expected to keep a blank line between sections"
|
||||
dnf_index = lines.index("[dnf]")
|
||||
brew_index = lines.index("[brew]")
|
||||
assert lines[dnf_index + 1 : dnf_index + 3] == ["git", "vim"]
|
||||
assert lines[brew_index - 1] == ""
|
||||
|
||||
|
||||
def test_apply_environment_variables(tmp_path: Path, monkeypatch) -> None:
|
||||
curator_dir = tmp_path / "curator"
|
||||
curator_dir.mkdir()
|
||||
env_home = tmp_path / "xdg"
|
||||
monkeypatch.setenv("XDG_CONFIG_HOME", str(env_home))
|
||||
|
||||
config = cli.empty_config()
|
||||
config.variables = {"EDITOR": "nvim", "WITH_SPACE": "some value"}
|
||||
config.options["backup"] = False
|
||||
|
||||
written, removed = cli.apply_environment_variables(config, curator_dir)
|
||||
|
||||
env_file = env_home / "environment.d" / "20-curator.conf"
|
||||
assert env_file.exists()
|
||||
lines = env_file.read_text().splitlines()
|
||||
assert "EDITOR=nvim" in lines
|
||||
assert 'WITH_SPACE="some value"' in lines
|
||||
assert written == {"EDITOR": "nvim", "WITH_SPACE": "some value"}
|
||||
assert removed == set()
|
||||
|
||||
|
||||
def test_apply_environment_variables_removes_file_when_empty(tmp_path: Path, monkeypatch) -> None:
|
||||
curator_dir = tmp_path / "curator"
|
||||
curator_dir.mkdir()
|
||||
env_home = tmp_path / "xdg"
|
||||
env_dir = env_home / "environment.d"
|
||||
env_dir.mkdir(parents=True, exist_ok=True)
|
||||
env_file = env_dir / "20-curator.conf"
|
||||
env_file.write_text("OLD=1\n")
|
||||
monkeypatch.setenv("XDG_CONFIG_HOME", str(env_home))
|
||||
|
||||
config = cli.empty_config()
|
||||
config.options["backup"] = False
|
||||
|
||||
variables, removed = cli.apply_environment_variables(config, curator_dir)
|
||||
|
||||
assert not env_file.exists()
|
||||
assert variables == {}
|
||||
assert removed == {"OLD"}
|
||||
|
||||
|
||||
def test_env_command_eval_outputs_exports(tmp_path: Path, monkeypatch, capsys) -> None:
|
||||
curator_dir = tmp_path / "curator"
|
||||
curator_dir.mkdir()
|
||||
env_file = curator_dir / "inventory.toml"
|
||||
env_file.write_text(
|
||||
"""
|
||||
[variables]
|
||||
EDITOR = "nvim"
|
||||
OLD = "1"
|
||||
""".strip()
|
||||
)
|
||||
|
||||
def fake_get_paths():
|
||||
return curator_dir, env_file, curator_dir / "last", curator_dir / "rollback"
|
||||
|
||||
def fake_apply_environment_variables(config, path, quiet=False, propagate=False):
|
||||
assert quiet is True
|
||||
assert propagate is True
|
||||
return {"EDITOR": "nvim"}, {"OLD"}
|
||||
|
||||
monkeypatch.setattr(cli, "get_paths", fake_get_paths)
|
||||
monkeypatch.setattr(cli, "apply_environment_variables", fake_apply_environment_variables)
|
||||
|
||||
cli.env_command(eval_mode=True)
|
||||
out = capsys.readouterr().out.strip().splitlines()
|
||||
assert out == ["export EDITOR=nvim", "unset OLD"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue