Something went wrong. Try again.
nix (hat) atelier?!
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255from pathlib import Path
import pytest
from atelier.rules import ( defaults, excluded, include_max_depth, included, load, matches, prunable_excludes,)from atelier.types import ( DEFAULT_INCLUDE, DEFAULT_SYSTEMS, MAX_RECURSE_DEPTH, NIXOS_CACHE, Rules,)
def test_single_level_star_stops_at_dot() -> None: assert matches("legacyPackages.*.*", "legacyPackages.x86_64-linux.caddy") assert not matches( "legacyPackages.*.*", "legacyPackages.x86_64-linux.ocamlPackages.dune" )
def test_nested_scope_needs_its_own_segment() -> None: assert matches( "legacyPackages.*.ocamlPackages.*", "legacyPackages.x86_64-linux.ocamlPackages.dune", ) assert not matches( "legacyPackages.*.ocamlPackages.*", "legacyPackages.x86_64-linux.caddy" )
def test_within_segment_glob() -> None: assert matches( "legacyPackages.*.ripe-atlas-*", "legacyPackages.x86_64-linux.ripe-atlas-software-probe", )
def test_exact_and_segment_count() -> None: assert matches("nixosConfigurations.baldy", "nixosConfigurations.baldy") assert not matches("nixosConfigurations.baldy", "nixosConfigurations.butte") assert not matches("a.b", "a.b.c")
def test_globstar_spans_any_depth() -> None: # ** matches a package at any nesting under a system, so a single rule covers # a re-exported scope's members however deeply they sit assert matches( "legacyPackages.*.**", "legacyPackages.x86_64-linux.rocqPackages_9_2.iris" ) assert matches("legacyPackages.*.**", "legacyPackages.x86_64-linux.caddy") assert matches( "legacyPackages.*.**", "legacyPackages.x86_64-linux.a.b.c.d.e", )
def test_globstar_matches_zero_segments() -> None: # ** absorbs zero segments too, so a.** matches the bare prefix assert matches("a.**", "a") assert matches("a.**", "a.b")
def test_globstar_in_the_middle() -> None: assert matches("a.**.z", "a.z") assert matches("a.**.z", "a.b.c.z") assert not matches("a.**.z", "a.b.c")
def test_include_max_depth_counts_segments() -> None: rules = Rules( systems=(), include=("legacyPackages.*.*", "legacyPackages.*.rocqPackages_9_2.*"), exclude=(), ) assert include_max_depth(rules) == 4
def test_include_max_depth_globstar_hits_cap() -> None: rules = Rules(systems=(), include=("legacyPackages.*.**",), exclude=()) assert include_max_depth(rules) == MAX_RECURSE_DEPTH
_RULES = Rules( systems=("x86_64-linux",), include=("legacyPackages.*.*", "legacyPackages.*.ocamlPackages.*"), exclude=("legacyPackages.*.spotify",),)
def test_included() -> None: assert included("legacyPackages.x86_64-linux.caddy", _RULES) assert included("legacyPackages.x86_64-linux.ocamlPackages.dune", _RULES) assert not included("devShells.x86_64-linux.default", _RULES)
def test_excluded() -> None: assert excluded("legacyPackages.x86_64-linux.spotify", _RULES) assert not excluded("legacyPackages.x86_64-linux.caddy", _RULES)
_NIXOS_CACHE = "https://cache.nixos.org"
def test_defaults_equal_an_empty_rule_file(tmp_path: Path) -> None: # the no-file fallback must be identical to loading a rule file that sets no # keys, so "no atelier.toml" and "empty atelier.toml" evaluate the same flake assert defaults() == load(_write(tmp_path, ""))
def test_defaults_carry_every_built_in() -> None: rules = defaults() assert rules.systems == DEFAULT_SYSTEMS assert rules.include == DEFAULT_INCLUDE assert rules.exclude == () assert rules.substituters == frozenset({NIXOS_CACHE}) assert rules.root == "."
def test_load_root_names_the_flake_subdirectory(tmp_path: Path) -> None: rules = load(_write(tmp_path, 'root = "nixos"\n')) assert rules.root == "nixos"
def test_load_root_normalizes_relative_path(tmp_path: Path) -> None: assert load(_write(tmp_path, 'root = "./nixos/"\n')).root == "nixos" assert load(_write(tmp_path, 'root = "nix/os"\n')).root == "nix/os" assert load(_write(tmp_path, 'root = "./"\n')).root == "."
@pytest.mark.parametrize("value", ["7", '["nixos"]'])def test_load_rejects_nonstring_root(tmp_path: Path, value: str) -> None: with pytest.raises(TypeError, match="must be a string"): load(_write(tmp_path, f"root = {value}\n"))
@pytest.mark.parametrize( ("value", "message"), [ ('""', "must not be empty"), ('"/nixos"', "must be relative"), ('"nixos/../../flake"', "must stay within"), ],)def test_load_rejects_invalid_root( tmp_path: Path, value: str, message: str) -> None: with pytest.raises(ValueError, match=message): load(_write(tmp_path, f"root = {value}\n"))
def test_load_substituters_default_to_nixos_cache(tmp_path: Path) -> None: # an omitted key still queries the official cache, so a path already on # cache.nixos.org is skipped without any rule file configuration rules = load(_write(tmp_path, "")) assert rules.substituters == frozenset({_NIXOS_CACHE})
def test_load_substituters_union_keeps_nixos_cache(tmp_path: Path) -> None: rules = load(_write(tmp_path, 'substituters = ["https://cache.ysun.co"]\n')) assert rules.substituters == frozenset({"https://cache.ysun.co", _NIXOS_CACHE})
def test_load_substituters_dedups_explicit_nixos_cache(tmp_path: Path) -> None: rules = load( _write( tmp_path, 'substituters = ["https://cache.nixos.org", "https://cache.ysun.co"]\n', ) ) assert rules.substituters == frozenset({_NIXOS_CACHE, "https://cache.ysun.co"})
def _write(tmp_path: Path, body: str) -> Path: path = tmp_path / "atelier.toml" path.write_text(body) return path
def test_prunable_excludes_groups_by_set_and_system() -> None: rules = Rules( systems=(), include=(), exclude=( "legacyPackages.*.verus", "legacyPackages.*.spotify", "legacyPackages.aarch64-darwin.bird3", # specific system, prunable "legacyPackages.*.ripe-atlas-*", # glob leaf, stays a post filter "legacyPackages.*.ocamlPackages.*", # glob descendant, stays a post filter "nixosConfigurations.host", # config set, hosts collect under "*" ), ) assert prunable_excludes(rules) == { "legacyPackages": { "*": {"spotify": True, "verus": True}, "aarch64-darwin": {"bird3": True}, }, "nixosConfigurations": {"*": {"host": True}}, }
def test_prunable_excludes_config_hosts() -> None: rules = Rules( systems=(), include=(), exclude=( "nixosConfigurations.iso", "darwinConfigurations.laptop.**", "nixosConfigurations.cloud-*", # glob host, stays a post filter "nixosConfigurations.iso.config", # deeper than a host, prunes nothing "nixosConfigurations.**", # whole set root, stays a post filter ), ) assert prunable_excludes(rules) == { "nixosConfigurations": {"*": {"iso": True}}, "darwinConfigurations": {"*": {"laptop": True}}, }
def test_prunable_excludes_handles_nested_literals_and_trailing_globstar() -> None: rules = Rules( systems=(), include=(), exclude=( "legacyPackages.*.ocamlPackages.yocaml_unix", # nested literal, prunable "legacyPackages.*.rocqPackages.**", # drop the whole scope pre eval "legacyPackages.*.**", # whole system root, stays a post filter "legacyPackages.**.x", # mid path globstar, stays a post filter ), ) assert prunable_excludes(rules) == { "legacyPackages": { "*": {"ocamlPackages": {"yocaml_unix": True}, "rocqPackages": True} } }
def test_prunable_excludes_parent_drop_beats_subtree() -> None: # dropping the parent already drops every descendant, so the parent rule # must win regardless of the order the two rules appear in deep = "legacyPackages.*.ocamlPackages.yocaml_unix" whole = "legacyPackages.*.ocamlPackages.**" want = {"legacyPackages": {"*": {"ocamlPackages": True}}} for exclude in ((deep, whole), (whole, deep)): rules = Rules(systems=(), include=(), exclude=exclude) assert prunable_excludes(rules) == want