#!/usr/bin/env bash
# tests/opentofu-validate/run-tests.sh -- offline harness for
# scripts/opentofu-validate.sh. Only tests the guard clauses this environment
# can actually exercise (no tofu binary is available anywhere this repo has
# been worked in so far -- see opentofu/README.md). The fmt/init/validate path
# itself is UNTESTED here; that is real, logged residual risk, not an
# oversight -- run the script for real on a machine with the tofu binary
# before trusting opentofu/.
# Exit: 0 all pass | 1 any case failed. ASCII + LF.
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT="$HERE/../../scripts/opentofu-validate.sh"
PASS=0; FAIL=0
run() { # run <want_rc> <regex> <label> -- args...
local want="$1" rx="$2" label="$3"; shift 3
local out rc
out="$(bash "$SCRIPT" "$@" 2>&1)"; rc=$?
if [[ "$rc" == "$want" ]] && grep -qE "$rx" <<<"$out"; then
echo " PASS $label"; PASS=$((PASS+1))
else
echo " FAIL $label (rc=$rc want=$want)"; echo "$out" | sed 's/^/ /'; FAIL=$((FAIL+1))
fi
}
run 1 'no such directory' "T1 nonexistent tofu-dir FAILS (rc 1)" "$HERE/does-not-exist"
if command -v tofu >/dev/null 2>&1; then
echo " SKIP T2 tofu-binary-missing case (a tofu binary IS present in this environment -- can't exercise the missing-binary guard here)"
else
TMP="$(mktemp -d)"; trap 'rm -rf "$TMP"' EXIT
run 2 'tofu binary not found' "T2 tofu binary missing FAILS (rc 2)" "$TMP"
fi
echo; echo "RESULT: PASS=$PASS FAIL=$FAIL"
[[ "$FAIL" -eq 0 ]] && { echo "ALL PASS"; exit 0; } || exit 1