#!/bin/bash # Framework Functions Unit Tests # Tests core framework functionality set -euo pipefail PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" # Source framework functions from the vendored KNELShellFramework FRAMEWORK_INCLUDES="$PROJECT_ROOT/vendor/git@git.knownelement.com/29418/KNEL/KNELShellFramework/Framework-Includes" source "$FRAMEWORK_INCLUDES/Logging.sh" 2>/dev/null || echo "Warning: Logging.sh not found" source "$FRAMEWORK_INCLUDES/PrettyPrint.sh" 2>/dev/null || echo "Warning: PrettyPrint.sh not found" source "$FRAMEWORK_INCLUDES/ErrorHandling.sh" 2>/dev/null || echo "Warning: ErrorHandling.sh not found" function test_logging_variables() { echo "๐Ÿ” Testing logging variables..." if [[ -n "${CURRENT_TIMESTAMP:-}" ]]; then echo "โœ… CURRENT_TIMESTAMP is set" else echo "โŒ CURRENT_TIMESTAMP is not set" return 1 fi if [[ -n "${LOGFILENAME:-}" ]]; then echo "โœ… LOGFILENAME is set" else echo "โŒ LOGFILENAME is not set" return 1 fi return 0 } function test_pretty_print_functions() { echo "๐Ÿ” Testing pretty print functions..." # Test if pretty print functions exist if command -v print_info >/dev/null 2>&1; then print_info "Test info message" >/dev/null 2>&1 || true echo "โœ… print_info function exists" else echo "โŒ print_info function missing" return 1 fi if command -v print_error >/dev/null 2>&1; then print_error "Test error message" >/dev/null 2>&1 || true echo "โœ… print_error function exists" else echo "โŒ print_error function missing" return 1 fi return 0 } function test_error_handling() { echo "๐Ÿ” Testing error handling..." # Test if error handling functions exist if command -v error_out >/dev/null 2>&1; then echo "โœ… error_out function exists" else echo "โŒ error_out function missing" return 1 fi if command -v handle_failure >/dev/null 2>&1; then echo "โœ… handle_failure function exists" else echo "โŒ handle_failure function missing" return 1 fi # Test bash strict mode is set if [[ "$-" == *e* ]]; then echo "โœ… Bash strict mode (set -e) is enabled" else echo "โŒ Bash strict mode (set -e) not enabled" return 1 fi if [[ "$-" == *u* ]]; then echo "โœ… Bash unset variable checking (set -u) is enabled" else echo "โŒ Bash unset variable checking (set -u) not enabled" return 1 fi return 0 } function test_framework_includes_exist() { echo "๐Ÿ” Testing framework includes exist..." local required_includes=( "Logging.sh" "PrettyPrint.sh" "ErrorHandling.sh" "PreflightCheck.sh" ) local missing_files=0 for include_file in "${required_includes[@]}"; do if [[ -f "$FRAMEWORK_INCLUDES/$include_file" ]]; then echo "โœ… Framework include exists: $include_file" else echo "โŒ Framework include missing: $include_file" ((++missing_files)) fi done return $missing_files } function test_syntax_validation() { echo "๐Ÿ” Testing script syntax validation..." local syntax_errors=0 local script_dirs=( "$FRAMEWORK_INCLUDES" "$PROJECT_ROOT/Project-Includes" "$PROJECT_ROOT/ProjectCode" ) for dir in "${script_dirs[@]}"; do if [[ -d "$dir" ]]; then while IFS= read -r -d '' file; do # Skip files that aren't bash scripts despite a .sh extension (e.g. PHP agents) local shebang shebang="$(head -c 32 "$file" 2>/dev/null)" case "$shebang" in *php*|*python*|*perl*) continue ;; esac if bash -n "$file" 2>/dev/null; then echo "โœ… Syntax valid: $(basename "$file")" else echo "โŒ Syntax error in: $(basename "$file")" ((++syntax_errors)) fi done < <(find "$dir" -name "*.sh" -type f -print0) fi done return $syntax_errors } # Main test execution function main() { echo "๐Ÿงช Running Framework Functions Unit Tests" echo "========================================" local total_failures=0 # Run all unit tests test_framework_includes_exist || ((total_failures++)) test_logging_variables || ((total_failures++)) test_pretty_print_functions || ((total_failures++)) test_error_handling || ((total_failures++)) test_syntax_validation || ((total_failures++)) echo "========================================" if [[ $total_failures -eq 0 ]]; then echo "โœ… All framework function unit tests passed" exit 0 else echo "โŒ $total_failures framework function unit tests failed" exit 1 fi } # Run main if executed directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi