Files
luk/tests/test_utils.sh
2025-07-15 22:13:46 -04:00

132 lines
2.9 KiB
Bash

#!/bin/bash
# Test utilities for run_himalaya.sh testing
# Colors for test output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Test counters
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0
# Setup function - prepares test environment
setup_test() {
# Clear any existing mock environment variables
unset MOCK_ENVELOPE_LIST
unset MOCK_MESSAGE_CONTENT
unset MOCK_EMPTY_INBOX
unset MOCK_HIMALAYA_FAIL
unset MOCK_INVALID_MESSAGE_ID
# Set up PATH to use mock himalaya
export PATH="$(pwd)/tests:$PATH"
# Create temporary files for testing
export TEST_TEMP_DIR="/tmp/himalaya_test_$$"
mkdir -p "$TEST_TEMP_DIR"
}
# Cleanup function
cleanup_test() {
# Remove temporary files
if [ -n "$TEST_TEMP_DIR" ] && [ -d "$TEST_TEMP_DIR" ]; then
rm -rf "$TEST_TEMP_DIR"
fi
# Reset PATH
export PATH=$(echo "$PATH" | sed "s|$(pwd)/tests:||")
}
# Function to simulate user input
simulate_input() {
local input="$1"
echo "$input"
}
# Function to run a test
run_test() {
local test_name="$1"
local test_function="$2"
echo -e "${YELLOW}Running: $test_name${NC}"
TESTS_RUN=$((TESTS_RUN + 1))
setup_test
if $test_function; then
echo -e "${GREEN}✓ PASS: $test_name${NC}"
TESTS_PASSED=$((TESTS_PASSED + 1))
else
echo -e "${RED}✗ FAIL: $test_name${NC}"
TESTS_FAILED=$((TESTS_FAILED + 1))
fi
cleanup_test
echo
}
# Function to assert equality
assert_equals() {
local expected="$1"
local actual="$2"
local message="$3"
if [ "$expected" = "$actual" ]; then
return 0
else
echo -e "${RED}Assertion failed: $message${NC}"
echo -e "${RED}Expected: '$expected'${NC}"
echo -e "${RED}Actual: '$actual'${NC}"
return 1
fi
}
# Function to assert command success
assert_success() {
local command="$1"
local message="$2"
if eval "$command" >/dev/null 2>&1; then
return 0
else
echo -e "${RED}Command failed: $message${NC}"
echo -e "${RED}Command: $command${NC}"
return 1
fi
}
# Function to assert command failure
assert_failure() {
local command="$1"
local message="$2"
if eval "$command" >/dev/null 2>&1; then
echo -e "${RED}Command unexpectedly succeeded: $message${NC}"
echo -e "${RED}Command: $command${NC}"
return 1
else
return 0
fi
}
# Function to print test summary
print_test_summary() {
echo "===================="
echo "Test Summary"
echo "===================="
echo "Tests run: $TESTS_RUN"
echo -e "Passed: ${GREEN}$TESTS_PASSED${NC}"
echo -e "Failed: ${RED}$TESTS_FAILED${NC}"
if [ $TESTS_FAILED -eq 0 ]; then
echo -e "${GREEN}All tests passed!${NC}"
return 0
else
echo -e "${RED}Some tests failed!${NC}"
return 1
fi
}