aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/test_common.bats
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-04-12 23:48:14 -0400
committerCraig Jennings <c@cjennings.net>2026-04-12 23:48:14 -0400
commit863ceeac8fdb10258a58d35bcee6874097fffc88 (patch)
tree37180f7a5393784beb0c8bbb35870558f0a02fb5 /tests/unit/test_common.bats
parent33ff55507895af2c29a20ec7e7e707d317cb8ef1 (diff)
downloadarchangel-863ceeac8fdb10258a58d35bcee6874097fffc88.tar.gz
archangel-863ceeac8fdb10258a58d35bcee6874097fffc88.zip
test: add bats unit tests for common.sh and config.sh
23 bats tests covering the pure logic in installer/lib/common.sh (command_exists, require_command, info/warn/error, enable_color, require_root, log) and installer/lib/config.sh (parse_args, load_config, validate_config, check_config). Makefile adds a 'bats' target; 'test' now runs lint + bats (VM integration tests remain under test-install).
Diffstat (limited to 'tests/unit/test_common.bats')
-rw-r--r--tests/unit/test_common.bats88
1 files changed, 88 insertions, 0 deletions
diff --git a/tests/unit/test_common.bats b/tests/unit/test_common.bats
new file mode 100644
index 0000000..6b9c18f
--- /dev/null
+++ b/tests/unit/test_common.bats
@@ -0,0 +1,88 @@
+#!/usr/bin/env bats
+# Unit tests for installer/lib/common.sh
+
+setup() {
+ # shellcheck disable=SC1091
+ source "${BATS_TEST_DIRNAME}/../../installer/lib/common.sh"
+}
+
+@test "command_exists returns 0 for an existing command" {
+ run command_exists bash
+ [ "$status" -eq 0 ]
+}
+
+@test "command_exists returns 1 for a missing command" {
+ run command_exists this_does_not_exist_xyz_42
+ [ "$status" -eq 1 ]
+}
+
+@test "require_command succeeds for an existing command" {
+ run require_command bash
+ [ "$status" -eq 0 ]
+}
+
+@test "require_command fails and reports missing command" {
+ run require_command this_does_not_exist_xyz_42
+ [ "$status" -eq 1 ]
+ [[ "$output" == *"Required command not found"* ]]
+ [[ "$output" == *"this_does_not_exist_xyz_42"* ]]
+}
+
+@test "enable_color populates color variables" {
+ [ -z "$RED" ]
+ [ -z "$GREEN" ]
+ [ -z "$NC" ]
+ enable_color
+ [ -n "$RED" ]
+ [ -n "$GREEN" ]
+ [ -n "$YELLOW" ]
+ [ -n "$BLUE" ]
+ [ -n "$BOLD" ]
+ [ -n "$NC" ]
+}
+
+@test "info prints [INFO] prefix and message" {
+ run info "hello world"
+ [ "$status" -eq 0 ]
+ [[ "$output" == *"[INFO]"* ]]
+ [[ "$output" == *"hello world"* ]]
+}
+
+@test "warn prints [WARN] prefix and message" {
+ run warn "heads up"
+ [ "$status" -eq 0 ]
+ [[ "$output" == *"[WARN]"* ]]
+ [[ "$output" == *"heads up"* ]]
+}
+
+@test "error prints [ERROR] and exits with status 1" {
+ run error "something broke"
+ [ "$status" -eq 1 ]
+ [[ "$output" == *"[ERROR]"* ]]
+ [[ "$output" == *"something broke"* ]]
+}
+
+@test "require_root fails for non-root user" {
+ [ "$EUID" -ne 0 ] || skip "running as root"
+ run require_root
+ [ "$status" -eq 1 ]
+ [[ "$output" == *"must be run as root"* ]]
+}
+
+@test "log writes timestamped line when LOG_FILE set" {
+ local tmp
+ tmp=$(mktemp)
+ LOG_FILE="$tmp" log "test entry"
+ run cat "$tmp"
+ [ "$status" -eq 0 ]
+ [[ "$output" == *"test entry"* ]]
+ [[ "$output" =~ \[[0-9]{4}-[0-9]{2}-[0-9]{2} ]]
+ rm -f "$tmp"
+}
+
+@test "log is a no-op when LOG_FILE unset" {
+ unset LOG_FILE
+ run log "should not crash"
+ [ "$status" -eq 0 ]
+ [ -z "$output" ]
+}