Files
samios/tests/test_helper.sh
T
Sami Ahmed 1ff4110b70
SamiOS CI / lint-and-test (push) Successful in 10s
[grade=B] SSOT: VERSION single-source-of-truth across all 7 components
- Add VERSION at repo root (canonical) + airootfs mirror
- Refactor samios CLI, voice-bridge, installer, profiledef,
  desktop-setup, PKGBUILD to derive VERSION from canonical file via
  walk-up or conditional read (no embedded release-version literals)
- Makefile targets: version-set NEW=X (single file change), version-sync
- tests/test_version.sh: 55 assertions locking the invariants

Verified end-to-end:
  make version-set NEW=0.3.0-rc2 → all 7 components see 0.3.0-rc2;
  PKGBUILD transforms hyphen to underscore for Arch.

Codex verdict: B (0 blocking issues, shippable).
2026-08-11 06:57:17 -07:00

148 lines
4.5 KiB
Bash
Executable File

#!/bin/bash
# Common test helpers for SamiOS test suite
# Sourced by individual test scripts
# Note: deliberately no `set -u` here. Tests use variables in single-quoted
# bash -c subshells where shellcheck can see them but bash treats them as
# "set in a different scope". Adding -u causes spurious "unbound variable"
# errors when the subshell hasn't yet assigned a var the outer scope
# references (e.g. `assert "...$VAR..."`). `set -o pipefail` is enough to
# catch pipeline failures.
set -o pipefail
# ---------------------------------------------------------------------------
# Path setup — resolve repo root relative to this file
# ---------------------------------------------------------------------------
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
PROFILE_DIR="$REPO_ROOT/packaging/archiso"
SCRIPTS_DIR="$REPO_ROOT/packaging/scripts"
TESTS_DIR="$REPO_ROOT/tests"
# Export these so bash -c subshells (used by `assert ... bash -c '...'`)
# can reference them without re-derivation. test_version.sh relies on
# this for its git-clone mutation tests.
export REPO_ROOT PROFILE_DIR SCRIPTS_DIR TESTS_DIR
# ---------------------------------------------------------------------------
# Counters
# ---------------------------------------------------------------------------
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0
CURRENT_SUITE=""
# ---------------------------------------------------------------------------
# Output helpers
# ---------------------------------------------------------------------------
suite() {
CURRENT_SUITE="$1"
echo ""
echo "=== $CURRENT_SUITE ==="
}
assert() {
# assert <description> <condition-cmd...>
local desc="$1"; shift
local output
if output="$("$@" 2>&1)"; then
echo "$desc"
((TESTS_PASSED++))
else
echo "$desc"
echo " output: $output"
((TESTS_FAILED++))
fi
((TESTS_RUN++))
}
assert_contains() {
# assert_contains <description> <file> <pattern>
local desc="$1" file="$2" pattern="$3"
if grep -q "$pattern" "$file" 2>/dev/null; then
echo "$desc"
((TESTS_PASSED++))
else
echo "$desc"
echo " '$pattern' not found in $file"
((TESTS_FAILED++))
fi
((TESTS_RUN++))
}
assert_not_contains() {
# assert_not_contains <description> <file> <pattern>
local desc="$1" file="$2" pattern="$3"
if grep -q "$pattern" "$file" 2>/dev/null; then
echo "$desc"
echo " '$pattern' unexpectedly found in $file"
((TESTS_FAILED++))
else
echo "$desc"
((TESTS_PASSED++))
fi
((TESTS_RUN++))
}
assert_file_exists() {
local desc="$1" file="$2"
if [ -f "$file" ]; then
echo "$desc"
((TESTS_PASSED++))
else
echo "$desc"
echo " file not found: $file"
((TESTS_FAILED++))
fi
((TESTS_RUN++))
}
assert_cmd_output_contains() {
# assert_cmd_output_contains <description> <expected-substring> <cmd...>
local desc="$1" expected="$2"; shift 2
local output
output="$("$@" 2>&1)" || true
if echo "$output" | grep -q "$expected"; then
echo "$desc"
((TESTS_PASSED++))
else
echo "$desc"
echo " expected '$expected' in output, got:"
echo " $output"
((TESTS_FAILED++))
fi
((TESTS_RUN++))
}
assert_exit_code() {
# assert_exit_code <description> <expected-code> <cmd...>
local desc="$1" expected="$2"; shift 2
local actual=0
"$@" >/dev/null 2>&1 || actual=$?
if [ "$actual" -eq "$expected" ]; then
echo "$desc"
((TESTS_PASSED++))
else
echo "$desc"
echo " expected exit code $expected, got $actual"
((TESTS_FAILED++))
fi
((TESTS_RUN++))
}
# ---------------------------------------------------------------------------
# Summary — prints results and sets exit code
# ---------------------------------------------------------------------------
print_summary() {
echo ""
echo "────────────────────────────────────────"
echo "Tests run: $TESTS_RUN"
echo "Tests passed: $TESTS_PASSED"
echo "Tests failed: $TESTS_FAILED"
echo "────────────────────────────────────────"
if [ "$TESTS_FAILED" -gt 0 ]; then
return 1
fi
return 0
}