113 lines
3.8 KiB
Bash
Executable File
113 lines
3.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -u
|
|
# shellcheck source=harness.sh
|
|
source "$(dirname "$0")/harness.sh"
|
|
RC_="$SCRIPTS/render-config.sh"
|
|
|
|
setup() {
|
|
export PATH="$SHIMS:$PATH" FAKE_NIX_STORE="$T/store" FAKE_NIX_LOG="$T/nix.log"
|
|
REPO="$T/repo"; mkdir -p "$REPO"; cd "$REPO" || exit 1
|
|
cat > flake.nix <<'EOF'
|
|
{ outputs = { nixpkgs, ... }: { nixosConfigurations = {
|
|
whitegrizzly = nixpkgs.lib.nixosSystem { };
|
|
willow = nixpkgs.lib.nixosSystem { };
|
|
}; }; }
|
|
EOF
|
|
}
|
|
|
|
test_renders_plain_writable_files() {
|
|
setup
|
|
run "$RC_" --user alex
|
|
assert_rc 0; assert_err_has "Rendered 2 file(s)"
|
|
assert_file rendered/.config/helix/config.toml
|
|
[[ ! -L rendered/.config/helix/config.toml ]] || _fail "still a store symlink"
|
|
assert_file_eq rendered/.config/helix/config.toml 'theme = "base16"'
|
|
[[ -w rendered/.gitconfig ]] || _fail "not writable"
|
|
assert_mode rendered/.gitconfig 600
|
|
}
|
|
|
|
test_default_host_is_first_in_flake_and_attr_is_correct() {
|
|
setup
|
|
run "$RC_" --user alex; assert_rc 0
|
|
assert_eq ".#nixosConfigurations.whitegrizzly.config.home-manager.users.alex.home.activationPackage" "$(<"$T/store/abc-home-manager-generation/attr")"
|
|
run "$RC_" --host willow --user bob --out o2; assert_rc 0
|
|
assert_eq ".#nixosConfigurations.willow.config.home-manager.users.bob.home.activationPackage" "$(<"$T/store/abc-home-manager-generation/attr")"
|
|
}
|
|
|
|
test_attr_override() {
|
|
setup
|
|
run "$RC_" --attr homeConfigurations.alex.activationPackage; assert_rc 0
|
|
assert_eq ".#homeConfigurations.alex.activationPackage" "$(<"$T/store/abc-home-manager-generation/attr")"
|
|
}
|
|
|
|
test_list_prints_relative_sorted_paths() {
|
|
setup
|
|
run "$RC_" --user alex --list; assert_rc 0
|
|
assert_eq ".config/helix/config.toml
|
|
.gitconfig" "$OUT"
|
|
}
|
|
|
|
test_rerun_replaces_stale_files() {
|
|
setup
|
|
run "$RC_" --user alex; assert_rc 0
|
|
echo stale > rendered/.stale; echo local-edit > rendered/.gitconfig
|
|
run "$RC_" --user alex; assert_rc 0
|
|
assert_no_path rendered/.stale
|
|
assert_file_eq rendered/.gitconfig "$(printf '[user]\nname = alex')"
|
|
}
|
|
|
|
test_refuses_to_delete_unrelated_directory() {
|
|
setup
|
|
mkdir -p precious; echo data > precious/file
|
|
run "$RC_" --user alex --out precious
|
|
assert_rc 1; assert_err_has "refusing to delete"
|
|
assert_file_eq precious/file data
|
|
mkdir empty; run "$RC_" --user alex --out empty; assert_rc 0 # empty dir is fine
|
|
echo x > afile; run "$RC_" --user alex --out afile; assert_rc 1
|
|
}
|
|
|
|
test_build_failure_leaves_previous_render_intact() {
|
|
setup
|
|
run "$RC_" --user alex; assert_rc 0
|
|
FAKE_NIX_FAIL=1 run "$RC_" --user alex
|
|
assert_rc 1; assert_err_has "git add"
|
|
assert_file rendered/.gitconfig
|
|
}
|
|
|
|
test_output_without_home_files_is_an_error() {
|
|
setup
|
|
FAKE_NIX_NOHOMEFILES=1 run "$RC_" --user alex
|
|
assert_rc 1; assert_err_has "no home-files"
|
|
assert_no_path rendered
|
|
}
|
|
|
|
test_input_validation() {
|
|
setup
|
|
local a
|
|
for a in "--host Bad" "--host ../x" "--user a;b" "--user 1a" "--attr a;b" "--attr \$(x)" "--bogus"; do
|
|
# shellcheck disable=SC2086
|
|
run "$RC_" $a; [[ $RC == 2 ]] || _fail "'$a' -> exit $RC"
|
|
done
|
|
run "$RC_" --out ""; assert_rc 2
|
|
[[ ! -e "$FAKE_NIX_LOG" ]] || _fail "nix ran on invalid input"
|
|
}
|
|
|
|
test_environment_errors() {
|
|
setup
|
|
local bin="$T/bin"; mkdir -p "$bin"
|
|
local c; for c in bash dirname readlink env cat; do ln -s "$(command -v $c)" "$bin/$c"; done
|
|
PATH="$bin" run "$RC_"; assert_rc 3
|
|
rm flake.nix; run "$RC_" --user alex; assert_rc 1; assert_err_has "no flake.nix"
|
|
run "$RC_" --repo-root /nonexistent; assert_rc 1
|
|
}
|
|
|
|
test_warns_when_not_gitignored() {
|
|
setup
|
|
git init -q .; run "$RC_" --user alex; assert_err_has "not git-ignored"
|
|
echo "/rendered/" > .gitignore; run "$RC_" --user alex; assert_err_lacks "not git-ignored"
|
|
}
|
|
|
|
test_help() { setup; run "$RC_" -h; assert_rc 0; assert_out_has "Usage:"; }
|
|
|
|
run_all "${1:-}"
|