#!/usr/bin/env bash set -u # shellcheck source=harness.sh source "$(dirname "$0")/harness.sh" CH="$SCRIPTS/capture-hardware.sh" PLACEHOLDER="# PLACEHOLDER hardware.nix for host \"willow\"." setup() { export PATH="$SHIMS:$PATH" export FAKE_NGC_ARGS="$T/ngc.args" REPO="$T/repo"; mkdir -p "$REPO/hosts/willow"; echo '{ }' > "$REPO/flake.nix" echo "$PLACEHOLDER" > "$REPO/hosts/willow/hardware.nix" cd "$REPO" || exit 1 } hw() { cat "$REPO/hosts/willow/hardware.nix"; } no_debris() { [[ -z "$(ls -A "$REPO/hosts/willow" | grep -v '^hardware.nix$' || true)" ]] || _fail "debris in hosts/willow: $(ls -A "$REPO/hosts/willow")"; } test_replaces_placeholder() { setup run "$CH" willow assert_rc 0 [[ "$(hw)" == *'nixpkgs.hostPlatform'* ]] || _fail "not replaced: $(hw)" assert_mode "$REPO/hosts/willow/hardware.nix" 644 no_debris } test_generator_failure_preserves_existing_file() { setup echo "{ real = hand-edited; }" > hosts/willow/hardware.nix FAKE_NGC_MODE=fail run "$CH" --force willow assert_rc 3; assert_err_has "left untouched" assert_eq "{ real = hand-edited; }" "$(hw)" "existing hardware.nix was damaged" no_debris } test_partial_output_then_failure_preserves_file() { setup FAKE_NGC_MODE=partial-then-fail run "$CH" willow assert_rc 3 assert_eq "$PLACEHOLDER" "$(hw)" no_debris } test_empty_or_garbage_output_rejected() { setup local mode for mode in empty garbage; do FAKE_NGC_MODE=$mode run "$CH" willow assert_rc 3 "mode $mode" assert_eq "$PLACEHOLDER" "$(hw)" done no_debris } test_refuses_to_overwrite_real_file_non_interactively() { setup echo "{ real = 1; }" > hosts/willow/hardware.nix run "$CH" willow assert_rc 1; assert_err_has "refusing to overwrite" assert_eq "{ real = 1; }" "$(hw)" } test_force_overwrites_real_file() { setup echo "{ real = 1; }" > hosts/willow/hardware.nix run "$CH" -f willow assert_rc 0 [[ "$(hw)" == *hostPlatform* ]] || _fail "not overwritten" } test_interactive_confirmation() { setup echo "{ real = 1; }" > hosts/willow/hardware.nix # `read` needs a tty for the prompt branch; emulate one with script(1) if present. command -v script >/dev/null 2>&1 || { echo " (skipped: no script(1))"; return 0; } printf 'no\n' | script -qec "$CH willow" /dev/null >/dev/null 2>&1 assert_eq "{ real = 1; }" "$(hw)" "answering 'no' must keep the file" printf 'yes\n' | script -qec "$CH willow" /dev/null >/dev/null 2>&1 [[ "$(hw)" == *hostPlatform* ]] || _fail "answering 'yes' should overwrite" } test_missing_host_dir_gives_actionable_message() { setup run "$CH" other assert_rc 1; assert_err_has "just register-host other" } test_hostname_traversal_rejected() { setup local bad for bad in '../x' 'a/b' 'Willow' '-x' '' 'a b' '..'; do run "$CH" "$bad" assert_rc 1 "name '$bad'" done assert_no_path "$T/hosts" } test_not_in_a_repo() { setup run "$CH" --repo-root "$T" willow assert_rc 1; assert_err_has "no flake.nix" run "$CH" --repo-root "$T/nope" willow assert_rc 1; assert_err_has "repo root not found" } test_not_on_nixos() { setup local bin="$T/bin"; mkdir -p "$bin" local c; for c in bash dirname readlink env grep mktemp rm mv chmod cat git; do ln -s "$(command -v $c)" "$bin/$c"; done PATH="$bin" run "$CH" willow assert_rc 2; assert_err_has "nixos-generate-config not found" assert_eq "$PLACEHOLDER" "$(hw)" } test_installer_root_detected_only_when_target_has_etc() { setup mkdir -p "$T/mnt/etc" FAKE_MOUNTPOINTS="$T/mnt" run "$CH" --install-root "$T/mnt" willow assert_rc 0; assert_err_has "using --root $T/mnt" [[ "$(<"$FAKE_NGC_ARGS")" == *"--root $T/mnt"* ]] || _fail "args: $(<"$FAKE_NGC_ARGS")" rmdir "$T/mnt/etc" echo "$PLACEHOLDER" > hosts/willow/hardware.nix FAKE_MOUNTPOINTS="$T/mnt" run "$CH" --install-root "$T/mnt" willow assert_rc 0; assert_err_has "doesn't look like a NixOS target" [[ "$(<"$FAKE_NGC_ARGS")" != *"--root"* ]] || _fail "should not pass --root" } test_not_mounted_means_running_system() { setup run "$CH" --install-root "$T/mnt" willow assert_rc 0 [[ "$(<"$FAKE_NGC_ARGS")" != *"--root"* ]] || _fail "should not pass --root" } test_uses_sudo_only_when_not_root() { setup export FAKE_SUDO_LOG="$T/sudo.log" run "$CH" willow; assert_rc 0 if ((EUID == 0)); then [[ ! -e "$FAKE_SUDO_LOG" ]] || _fail "sudo used even though we are root" else grep -q "nixos-generate-config" "$FAKE_SUDO_LOG" || _fail "sudo not used for a non-root user" fi } test_relative_repo_root_and_git_diff() { setup git init -q . && git add -A && git -c user.email=a@b -c user.name=n commit -qm init cd "$T" || exit 1 run "$CH" --repo-root repo willow assert_rc 0; assert_err_has "git diff"; assert_err_has "git-tracked" assert_out_has "hostPlatform" # the diff body } test_help_and_unknown_flags() { setup run "$CH" -h; assert_rc 0; assert_out_has "Usage:" run "$CH" --nope willow; assert_rc 1 run "$CH" willow extra; assert_rc 1 run "$CH" ; assert_rc 1 run "$CH" --repo-root; assert_rc 2 } test_leading_dash_name_via_double_dash_is_validated() { setup run "$CH" -- -rf assert_rc 1; assert_err_has "invalid hostname" } run_all "${1:-}"