summaryrefslogtreecommitdiff
path: root/secrets/test/common.sh
diff options
context:
space:
mode:
Diffstat (limited to 'secrets/test/common.sh')
-rwxr-xr-xsecrets/test/common.sh108
1 files changed, 108 insertions, 0 deletions
diff --git a/secrets/test/common.sh b/secrets/test/common.sh
new file mode 100755
index 0000000..33d7d78
--- /dev/null
+++ b/secrets/test/common.sh
@@ -0,0 +1,108 @@
+# Common test utilities for szecret tests
+# Source this file from individual test files
+
+set -euo pipefail
+
+SECRETS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
+
+# Colors for output
+RED='\033[0;31m'
+GREEN='\033[0;32m'
+YELLOW='\033[0;33m'
+NC='\033[0m' # No Color
+
+pass() { echo -e "${GREEN}PASS${NC}: $1"; }
+fail() { echo -e "${RED}FAIL${NC}: $1"; exit 1; }
+skip() { echo -e "${YELLOW}SKIP${NC}: $1"; }
+
+# Create temporary test environment
+setup() {
+ export TEST_DIR="$(mktemp -d)"
+ export SZECRET_STORE="${TEST_DIR}/store"
+ export SZECRET_PUBLIC_KEY="${TEST_DIR}/.id.pub"
+ export SZECRET_PRIVATE_KEY="${TEST_DIR}/.id"
+
+ mkdir -p "${SZECRET_STORE}"
+
+ # Generate test keypair
+ age-keygen -o "${SZECRET_PRIVATE_KEY}" 2>/dev/null
+ age-keygen -y "${SZECRET_PRIVATE_KEY}" > "${SZECRET_PUBLIC_KEY}"
+}
+
+teardown() {
+ if [[ -n "${TEST_DIR:-}" && -d "${TEST_DIR}" ]]; then
+ rm -rf "${TEST_DIR}"
+ fi
+}
+
+# Run a command from the secrets directory
+szecret() {
+ "${SECRETS_DIR}/main.sh" "$@"
+}
+
+# Direct access to individual scripts (useful for testing specific scripts)
+szecret_script() {
+ local script="$1"
+ shift
+ "${SECRETS_DIR}/${script}.sh" "$@"
+}
+
+# Assert that a command succeeds
+assert_success() {
+ if "$@"; then
+ return 0
+ else
+ fail "Command failed: $*"
+ fi
+}
+
+# Assert that a command fails
+assert_failure() {
+ if "$@" 2>/dev/null; then
+ fail "Command should have failed: $*"
+ else
+ return 0
+ fi
+}
+
+# Assert string equality
+assert_equals() {
+ local expected="$1"
+ local actual="$2"
+ local msg="${3:-values}"
+ if [[ "${expected}" == "${actual}" ]]; then
+ return 0
+ else
+ fail "${msg}: expected '${expected}', got '${actual}'"
+ fi
+}
+
+# Assert string contains substring
+assert_contains() {
+ local haystack="$1"
+ local needle="$2"
+ local msg="${3:-string}"
+ if [[ "${haystack}" == *"${needle}"* ]]; then
+ return 0
+ else
+ fail "${msg}: '${haystack}' does not contain '${needle}'"
+ fi
+}
+
+# Assert file exists
+assert_file_exists() {
+ local file="$1"
+ local msg="${2:-file}"
+ if [[ -f "${file}" ]]; then
+ return 0
+ else
+ fail "${msg}: file '${file}' does not exist"
+ fi
+}
+
+# Auto-setup: only run once per process
+if [[ -z "${_SZECRET_TEST_INITIALIZED:-}" ]]; then
+ export _SZECRET_TEST_INITIALIZED=1
+ trap teardown EXIT
+ setup
+fi