#!/usr/bin/env python3 """Tests for nixflake.py, register-host.py and list-hosts.py. Run: python3 -m unittest -v""" from __future__ import annotations import importlib.util import io import os import random import re import stat import sys import tempfile import textwrap import unittest from contextlib import redirect_stderr, redirect_stdout from pathlib import Path from unittest import mock SCRIPTS = Path(__file__).resolve().parent.parent sys.path.insert(0, str(SCRIPTS)) import nixflake # noqa: E402 def load(filename: str, modname: str): spec = importlib.util.spec_from_file_location(modname, SCRIPTS / filename) mod = importlib.util.module_from_spec(spec) sys.modules[modname] = mod # dataclasses (with deferred annotations) look themselves up here spec.loader.exec_module(mod) return mod reg = load("register-host.py", "register_host") lh = load("list-hosts.py", "list_hosts") BASIC = textwrap.dedent("""\ { inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; outputs = { self, nixpkgs, ... }@inputs: let system = "x86_64-linux"; in { nixosConfigurations = { alpha = nixpkgs.lib.nixosSystem { inherit system; modules = [ ./hosts/alpha ]; }; beta = nixpkgs.lib.nixosSystem { inherit system; modules = [ ./hosts/beta ./hosts/beta-extras ]; }; }; }; } """) def names(src: str) -> list[str]: return [h.name for h in nixflake.find_hosts(src)] class MaskTests(unittest.TestCase): def test_length_and_newlines_preserved(self): src = 'a = "x\n{";\n# c {\n/* {\n} */ b = \'\'\n${y}\n\'\';\n' m = nixflake.mask_non_code(src) self.assertEqual(len(m), len(src)) self.assertEqual([i for i, c in enumerate(src) if c == "\n"], [i for i, c in enumerate(m) if c == "\n"]) def test_braces_in_non_code_ignored(self): for src in ( '{ a = "}"; }', "{ a = 1; # }\n}", "{ a = 1; /* } */ }", "{ a = ''}''; }", "{ a = ''x'''}''; }", '{ a = "\\"}"; }', "{ a = ''x''${}''; }", ): self.assertTrue(nixflake.braces_balanced(src), src) m = nixflake.mask_non_code(src) self.assertEqual(nixflake.find_matching_brace(m, 0), len(src) - 1, src) def test_interpolation_is_code(self): src = '{ a = "x ${ { b = 1; }.b } y"; c = "${"nested ${ "}" }"}"; }' self.assertTrue(nixflake.braces_balanced(src)) m = nixflake.mask_non_code(src) self.assertEqual(nixflake.find_matching_brace(m, 0), len(src) - 1) def test_indented_string_escapes(self): # ''$ and ''' and ''\n are escapes, not terminators. src = "{ a = ''x ''${notinterp} ''' still ''\\n in ''; b = 1; }" m = nixflake.mask_non_code(src) self.assertEqual(nixflake.find_matching_brace(m, 0), len(src) - 1) self.assertIn("b = 1", m) def test_unterminated_constructs_do_not_crash(self): for src in ('{ a = "oops', "{ a = ''oops", "{ /* oops", "{ # oops", '{ a = "${ x', "\\", "''", '"', "${"): nixflake.mask_non_code(src) # must not raise def test_fuzz_never_crashes_and_preserves_length(self): rng = random.Random(1234) alphabet = ['"', "'", "''", "$", "${", "}", "{", "#", "/*", "*/", "\\", "\n", " ", "a", "=", ";"] for _ in range(3000): src = "".join(rng.choice(alphabet) for _ in range(rng.randint(0, 40))) m = nixflake.mask_non_code(src) self.assertEqual(len(m), len(src), repr(src)) nixflake.braces_balanced(src) class FindHostsTests(unittest.TestCase): def test_basic(self): self.assertEqual(names(BASIC), ["alpha", "beta"]) def test_ignores_commented_and_stringified_hosts(self): src = textwrap.dedent("""\ { outputs = { nixosConfigurations = { # ghost = nixpkgs.lib.nixosSystem { }; /* ghost2 = nixpkgs.lib.nixosSystem { }; */ doc = "ghost3 = nixpkgs.lib.nixosSystem { };"; real = nixpkgs.lib.nixosSystem { }; }; }; } """) self.assertEqual(names(src), ["real"]) def test_brace_in_comment_does_not_end_block(self): src = textwrap.dedent("""\ { nixosConfigurations = { a = nixpkgs.lib.nixosSystem { x = 1; }; # } closes nothing b = nixpkgs.lib.nixosSystem { y = "}"; }; }; } """) self.assertEqual(names(src), ["a", "b"]) def test_nested_nixossystem_not_a_host(self): src = textwrap.dedent("""\ { nixosConfigurations = { outer = nixpkgs.lib.nixosSystem { modules = [ { inner = nixpkgs.lib.nixosSystem { }; } ]; other = let deep = nixpkgs.lib.nixosSystem { }; in deep; }; }; } """) self.assertEqual(names(src), ["outer"]) def test_function_path_variants(self): src = textwrap.dedent("""\ { nixosConfigurations = { a = nixpkgs.lib.nixosSystem { }; b = inputs.nixpkgs.lib.nixosSystem { }; c = lib.nixosSystem { }; d = nixosSystem { }; e-f_g' = nixpkgs.lib.nixosSystem { }; not-a-host = somethingElse { }; }; } """) self.assertEqual(names(src), ["a", "b", "c", "d", "e-f_g'"]) def test_errors(self): with self.assertRaises(nixflake.FlakeError): names("{ outputs = { }; }") with self.assertRaises(nixflake.FlakeError): names("{ nixosConfigurations = { a = nixpkgs.lib.nixosSystem { };") with self.assertRaises(nixflake.FlakeError): # entry missing its `;` names("{ nixosConfigurations = { a = nixpkgs.lib.nixosSystem { } }; }") def test_empty_block(self): self.assertEqual(names("{ nixosConfigurations = { }; }"), []) self.assertEqual(names("{ nixosConfigurations = {}; }"), []) def test_crlf(self): self.assertEqual(names(BASIC.replace("\n", "\r\n")), ["alpha", "beta"]) def test_dotted_prefix_still_found(self): self.assertEqual(names("{ outputs.nixosConfigurations = { a = nixpkgs.lib.nixosSystem { }; }; }"), ["a"]) # but a lookalike attribute is not nixosConfigurations with self.assertRaises(nixflake.FlakeError): names("{ fakenixosConfigurations = { a = nixpkgs.lib.nixosSystem { }; }; }") class PlanEditTests(unittest.TestCase): def test_clone_renames_and_rewrites_paths(self): e = reg.plan_flake_edit(BASIC, "gamma", "beta") self.assertEqual(names(e.new_src), ["alpha", "beta", "gamma"]) self.assertIn("gamma = nixpkgs.lib.nixosSystem {", e.new_src) self.assertIn("./hosts/gamma ./hosts/beta-extras", e.new_src.split("gamma = ")[1]) # -extras untouched self.assertTrue(nixflake.braces_balanced(e.new_src)) self.assertEqual(e.template_used, "beta") self.assertTrue(any("beta-extras" in ln for ln in e.stale_template_lines) is False) def test_stale_mentions_reported(self): src = BASIC.replace("modules = [ ./hosts/beta ", 'networking.hostName = "beta"; modules = [ ./hosts/beta ') self.assertIn('hostName = "beta"', src) e = reg.plan_flake_edit(src, "gamma", "beta") self.assertTrue(any('"beta"' in ln for ln in e.stale_template_lines), e.stale_template_lines) def test_default_entry_empty_blocks(self): for src in ( "{ outputs = { nixosConfigurations = { }; }; }", "{ outputs = { nixosConfigurations = {}; }; }", "{\n nixosConfigurations = {\n };\n}\n", "{\n nixosConfigurations = {\n };\n}", ): e = reg.plan_flake_edit(src, "gamma", None) self.assertEqual(names(e.new_src), ["gamma"], src) self.assertTrue(nixflake.braces_balanced(e.new_src), src) self.assertIn("./hosts/gamma", e.new_src) def test_one_line_attrset_with_existing_host(self): src = "{ nixosConfigurations = { a = nixpkgs.lib.nixosSystem { }; }; }" e = reg.plan_flake_edit(src, "b", "a") self.assertEqual(names(e.new_src), ["a", "b"]) self.assertTrue(nixflake.braces_balanced(e.new_src)) def test_crlf_is_preserved_everywhere(self): src = BASIC.replace("\n", "\r\n") for template in ("alpha", None): e = reg.plan_flake_edit(src, "gamma", template) self.assertNotRegex(e.new_src, r"(? bool: """True if `new` equals `old` with one contiguous chunk inserted.""" if len(new) < len(old): return False p = 0 while p < len(old) and old[p] == new[p]: p += 1 s = 0 while s < len(old) - p and old[-1 - s] == new[-1 - s]: s += 1 return p + s >= len(old) def random_flake(rng: random.Random) -> tuple[str, list[str]]: junk = [ '# } { comment\n', '/* } { */', '"}{"', "''}${\"{\"}{''", '"${ "}" }"', "x = \"a\\\"}\";", "# x = nixpkgs.lib.nixosSystem { };\n", ] shape = rng.choice([0, 1, 2]) if shape == 1: # newlines get collapsed below, so line comments would swallow code junk = [j for j in junk if not j.startswith("#")] nm = [f"h{i}" for i in range(rng.randint(0, 4))] entries = [] for n in nm: body = " ".join(rng.choice(junk) for _ in range(rng.randint(0, 3))) entries.append(f" {n} = nixpkgs.lib.nixosSystem {{ modules = [ ./hosts/{n} ]; {body}\n }};\n") sep = rng.choice(["\n", "\n\n", "\r\n"]) inner = sep.join(entries) if shape == 0: src = f"{{\n nixosConfigurations = {{\n{inner} }};\n}}\n" elif shape == 1: src = f"{{ nixosConfigurations = {{ {re.sub(chr(13) + '?' + chr(10), ' ', inner)} }}; }}" else: src = f"# {{\n{{ a = 1; outputs = {{ nixosConfigurations = {{\n{inner}}}; }}; }}" return src, nm class FuzzEditTests(unittest.TestCase): def test_random_flakes(self): rng = random.Random(42) for i in range(800): src, expected = random_flake(rng) if nixflake.find_hosts(src) != [] or expected: self.assertEqual(names(src), expected, repr(src)) template = rng.choice([None, *expected]) if expected else None e = reg.plan_flake_edit(src, "newhost", template) reg.verify_edit(src, e.new_src, "newhost") ok = is_pure_insertion(src, e.new_src) if not ok and "\n" not in src: # One-line attrsets: the blank gap may be replaced by the new entry. def squash(t): return re.sub(r"\s+", "", t) ok = is_pure_insertion(squash(src), squash(e.new_src)) self.assertTrue(ok, f"case {i}: {src!r} -> {e.new_src!r}") class HostnameTests(unittest.TestCase): def test_valid(self): for n in ("a", "willow", "white-grizzly", "h1", "a" * 63): reg.validate_hostname(n) def test_invalid(self): for n in ("", "-a", "a-", "1abc", "Willow", "a_b", "a.b", "../x", "a/b", "a b", "a" * 64, "in", "let", "rec", "é", "a\n", "--help"): with self.assertRaises(reg.RegisterError, msg=repr(n)): reg.validate_hostname(n) class RenderTests(unittest.TestCase): def test_braces_survive(self): out = reg.render("@NAME@ = { a = [ \"@NAME@\" ]; };", name="x") self.assertEqual(out, 'x = { a = [ "x" ]; };') def test_leftover_token_is_a_bug(self): with self.assertRaises(RuntimeError): reg.render("@NAME@ @OTHER@", name="x") def test_real_templates_render_and_are_brace_balanced(self): # This is the exact regression for the reported KeyError. for t in (reg.DEFAULT_NIX_TEMPLATE, reg.PLACEHOLDER_HARDWARE, reg.DEFAULT_FLAKE_ENTRY): out = reg.render(t, name="willow", i=" ") self.assertTrue(nixflake.braces_balanced(out)) self.assertNotRegex(out, r"@[A-Z_]+@") self.assertIn('polkitPolicyOwners = [ "alex" ];', reg.render(reg.DEFAULT_NIX_TEMPLATE, name="willow")) def test_hardware_placeholder_marker_matches_capture_script(self): cap = (SCRIPTS / "capture-hardware.sh").read_text() self.assertIn(reg.HARDWARE_PLACEHOLDER_MARKER, cap) self.assertIn(reg.HARDWARE_PLACEHOLDER_MARKER, reg.render(reg.PLACEHOLDER_HARDWARE, name="x")) class RepoCase(unittest.TestCase): """Runs register-host.main() against a throwaway repo.""" def setUp(self): self._td = tempfile.TemporaryDirectory() self.addCleanup(self._td.cleanup) self.repo = Path(self._td.name) (self.repo / "flake.nix").write_text(BASIC) for m in ("core", "plasma", "security"): (self.repo / "modules").mkdir(exist_ok=True) (self.repo / "modules" / f"{m}.nix").write_text("{ }") def run_reg(self, *argv: str) -> tuple[int, str]: err = io.StringIO() with redirect_stderr(err), redirect_stdout(io.StringIO()): try: code = reg.main([*argv, "--repo-root", str(self.repo)] if "--" not in argv else list(argv)) except SystemExit as e: # argparse code = int(e.code or 0) return code, err.getvalue() @property def flake(self) -> str: return (self.repo / "flake.nix").read_text() class RegisterEndToEnd(RepoCase): def test_success(self): code, err = self.run_reg("willow") self.assertEqual(code, 0, err) self.assertEqual(names(self.flake), ["alpha", "beta", "willow"]) d = self.repo / "hosts" / "willow" self.assertIn('networking.hostName = "willow";', (d / "default.nix").read_text()) self.assertIn("PLACEHOLDER hardware.nix", (d / "hardware.nix").read_text()) self.assertNotIn("does not exist", err) # modules/ present in this fixture def test_second_registration_of_same_host_refused_and_changes_nothing(self): self.assertEqual(self.run_reg("willow")[0], 0) before = self.flake code, err = self.run_reg("willow") self.assertEqual(code, 1) self.assertEqual(self.flake, before) def test_dry_run_writes_nothing(self): before = self.flake code, err = self.run_reg("willow", "--dry-run") self.assertEqual(code, 0) self.assertEqual(self.flake, before) self.assertFalse((self.repo / "hosts").exists()) self.assertIn("dry-run", err) def test_dry_run_still_validates(self): code, err = self.run_reg("alpha", "--dry-run") self.assertEqual(code, 1) self.assertIn("already defined", err) def test_invalid_names_rejected_without_touching_anything(self): before = self.flake for bad in ("../evil", "Willow", "1x", "a b", "if"): code, err = self.run_reg(bad) self.assertEqual(code, 1, bad) self.assertEqual(self.flake, before) self.assertFalse((self.repo / "hosts").exists()) self.assertFalse((self.repo.parent / "evil").exists()) def test_leading_dash_name_after_double_dash(self): err = io.StringIO() with redirect_stderr(err): code = reg.main(["--repo-root", str(self.repo), "--", "-rf"]) self.assertEqual(code, 1) self.assertIn("invalid hostname '-rf'", err.getvalue()) def test_reuses_empty_dir_but_refuses_nonempty(self): (self.repo / "hosts" / "willow").mkdir(parents=True) self.assertEqual(self.run_reg("willow")[0], 0) (self.repo / "hosts" / "other").mkdir() (self.repo / "hosts" / "other" / "keep.txt").write_text("precious") before = self.flake code, err = self.run_reg("other") self.assertEqual(code, 1) self.assertIn("not empty", err) self.assertEqual(self.flake, before) self.assertEqual((self.repo / "hosts" / "other" / "keep.txt").read_text(), "precious") def test_hosts_path_is_a_file(self): (self.repo / "hosts").mkdir() (self.repo / "hosts" / "willow").write_text("i am a file") self.assertEqual(self.run_reg("willow")[0], 1) self.assertEqual((self.repo / "hosts" / "willow").read_text(), "i am a file") def test_missing_flake(self): (self.repo / "flake.nix").unlink() code, err = self.run_reg("willow") self.assertEqual(code, 1) self.assertIn("flake.nix not found", err) def test_flake_without_configurations(self): (self.repo / "flake.nix").write_text("{ outputs = { }; }") code, err = self.run_reg("willow") self.assertEqual(code, 1) self.assertIn("nixosConfigurations", err) self.assertFalse((self.repo / "hosts").exists()) def test_invalid_utf8_flake(self): (self.repo / "flake.nix").write_bytes(b"{ a = \xff\xfe; }") code, err = self.run_reg("willow") self.assertEqual(code, 1) self.assertIn("UTF-8", err) def test_template_options(self): self.assertEqual(self.run_reg("g1", "--template", "beta")[0], 0) self.assertEqual(self.run_reg("g2", "--no-template")[0], 0) self.assertEqual(self.run_reg("g3", "--template", "nope")[0], 1) self.assertEqual(self.run_reg("g4", "--template", "alpha", "--no-template")[0], 2) self.assertEqual(names(self.flake), ["alpha", "beta", "g1", "g2"]) def test_crlf_flake_stays_crlf(self): (self.repo / "flake.nix").write_bytes(BASIC.replace("\n", "\r\n").encode()) self.assertEqual(self.run_reg("willow")[0], 0) raw = (self.repo / "flake.nix").read_bytes() self.assertNotRegex(raw, rb"(? str: bindir = self.repo / "fakebin" bindir.mkdir(exist_ok=True) exe = bindir / "nix-instantiate" exe.write_text(f"#!/bin/sh\necho '{msg}' >&2\nexit {exit_code}\n") exe.chmod(0o755) return str(bindir) def test_parse_failure_blocks_all_writes(self): before = self.flake with mock.patch.dict(os.environ, {"PATH": self._fake_nix(1, "error: syntax error, unexpected '}'") + os.pathsep + os.environ["PATH"]}): code, err = self.run_reg("willow") self.assertEqual(code, 1) self.assertIn("would not parse", err) self.assertIn("syntax error", err) self.assertEqual(self.flake, before) self.assertFalse((self.repo / "hosts").exists()) def test_parse_success_allows_write(self): with mock.patch.dict(os.environ, {"PATH": self._fake_nix(0, "") + os.pathsep + os.environ["PATH"]}): self.assertEqual(self.run_reg("willow")[0], 0) def test_absent_nix_is_fine(self): with mock.patch.dict(os.environ, {"PATH": "/nonexistent"}): reg.nix_parse_check("{ }") # must not raise class ListHostsTests(RepoCase): def test_lists_in_order(self): out = io.StringIO() with redirect_stdout(out): self.assertEqual(lh.main([str(self.repo / "flake.nix")]), 0) self.assertEqual(out.getvalue().split(), ["alpha", "beta"]) def test_errors(self): with redirect_stderr(io.StringIO()) as err: self.assertEqual(lh.main([str(self.repo / "nope.nix")]), 1) (self.repo / "bad.nix").write_text("{ }") self.assertEqual(lh.main([str(self.repo / "bad.nix")]), 1) (self.repo / "bin.nix").write_bytes(b"\xff\xff") self.assertEqual(lh.main([str(self.repo / "bin.nix")]), 1) self.assertIn("nixosConfigurations", err.getvalue()) def test_empty_block_warns_but_succeeds(self): (self.repo / "e.nix").write_text("{ nixosConfigurations = { }; }") with redirect_stderr(io.StringIO()) as err, redirect_stdout(io.StringIO()) as out: self.assertEqual(lh.main([str(self.repo / "e.nix")]), 0) self.assertEqual(out.getvalue(), "") self.assertIn("no hosts", err.getvalue()) class CliSubprocess(RepoCase): """Exercise the real executables the way `just` does.""" def test_executable_bits_and_shebangs(self): for f in ("register-host.py", "list-hosts.py"): p = SCRIPTS / f self.assertTrue(os.access(p, os.X_OK), f) self.assertEqual(p.read_text().splitlines()[0], "#!/usr/bin/env python3") def test_run_from_another_cwd_via_symlink(self): import subprocess link = self.repo / "linked-list-hosts" link.symlink_to(SCRIPTS / "list-hosts.py") r = subprocess.run([str(link), str(self.repo / "flake.nix")], capture_output=True, text=True, cwd="/") self.assertEqual(r.returncode, 0, r.stderr) self.assertEqual(r.stdout.split(), ["alpha", "beta"]) def test_register_via_subprocess_reproduces_just_invocation(self): import subprocess r = subprocess.run( [str(SCRIPTS / "register-host.py"), "--repo-root", str(self.repo), "--", "willow"], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0, r.stderr) self.assertNotIn("Traceback", r.stderr) if __name__ == "__main__": unittest.main(verbosity=2)