#!/bin/bash # Don't use set -e because we want to continue even if tests fail set -uo pipefail PROJECT="dehydrate" SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" DEHYDRATE="${SCRIPT_DIR}/output/${PROJECT}" TEST_DIR="${SCRIPT_DIR}/test_temp" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Test counters TESTS_PASSED=0 TESTS_FAILED=0 # Function to print test results print_test_result() { local test_name="$1" local result="$2" if [ "$result" -eq 0 ]; then echo -e "${GREEN}✓${NC} $test_name" ((TESTS_PASSED++)) else echo -e "${RED}✗${NC} $test_name" ((TESTS_FAILED++)) fi } # Function to cleanup test artifacts cleanup() { echo -e "\n${YELLOW}Cleaning up test artifacts...${NC}" rm -rf "$TEST_DIR" } # Set up trap to ensure cleanup runs trap cleanup EXIT # Create test directory mkdir -p "$TEST_DIR" echo -e "${YELLOW}Running dehydrate tests...${NC}\n" # Check if dehydrate binary exists if [ ! -f "$DEHYDRATE" ]; then echo -e "${RED}Error: dehydrate binary not found at $DEHYDRATE${NC}" echo "Please run ./build.sh first to build dehydrate" exit 1 fi if [ ! -x "$DEHYDRATE" ]; then echo -e "${RED}Error: dehydrate binary is not executable${NC}" exit 1 fi echo "Using dehydrate binary: $DEHYDRATE" # Test 1: Version command (dehydrate shows version in help output) echo "Test 1: Version command" VERSION_OUTPUT=$("$DEHYDRATE" 2>&1 || true) # Extract version from the beginning of help output VERSION=$(echo "$VERSION_OUTPUT" | head -n 1 | sed 's/Dehydrate version //') if [[ "$VERSION" =~ ^[0-9]{4}\.[0-9]{4}\.[0-9]{4}$ ]]; then print_test_result "Version format (YYYY.MMDD.HHMM)" 0 else print_test_result "Version format (YYYY.MMDD.HHMM)" 1 fi # Test 2: Help command (shows help when no args provided) echo -e "\nTest 2: Help command" HELP_OUTPUT=$("$DEHYDRATE" 2>&1 || true) if [[ "$HELP_OUTPUT" =~ "Usage: dehydrate" ]] && [[ "$HELP_OUTPUT" =~ "Converts existing files" ]]; then print_test_result "Help command output" 0 else print_test_result "Help command output" 1 fi # Test 3: Basic dehydration test echo -e "\nTest 3: Basic dehydration test" # Create a test source directory TEST_SRC_DIR="${TEST_DIR}/test_src" mkdir -p "$TEST_SRC_DIR" echo "int main() { return 0; }" > "$TEST_SRC_DIR/main.cpp" echo "#include " > "$TEST_SRC_DIR/header.hpp" # Run dehydrate on the test source DEHYDRATE_OUTPUT=$("$DEHYDRATE" "$TEST_SRC_DIR" "$TEST_DIR" 2>&1 || true) # Dehydrate creates files with pattern _.{cpp,hpp} if [ -f "$TEST_DIR/_test_src.hpp" ] && [ -f "$TEST_DIR/_test_src.cpp" ]; then print_test_result "Basic dehydration creates output files" 0 else print_test_result "Basic dehydration creates output files" 1 fi # Test 4: Test the generated files have valid syntax echo -e "\nTest 4: Test generated files syntax" if [ -f "$TEST_DIR/_test_src.hpp" ] && [ -f "$TEST_DIR/_test_src.cpp" ]; then # Check that the header file has the expected namespace declaration if grep -q "namespace recreate_test_src" "$TEST_DIR/_test_src.hpp"; then # Check that the cpp file includes the header if grep -q "#include \"_test_src.hpp\"" "$TEST_DIR/_test_src.cpp"; then # Check that the function is declared if grep -q "recreate_tree" "$TEST_DIR/_test_src.hpp"; then print_test_result "Generated files have correct structure" 0 else print_test_result "Generated files have correct structure" 1 fi else print_test_result "Generated files have correct structure" 1 fi else print_test_result "Generated files have correct structure" 1 fi else print_test_result "Generated files have correct structure" 1 fi cleanup # Print summary for basic tests echo -e "\n${YELLOW}Basic Test Summary:${NC}" echo -e "Tests passed: ${GREEN}${TESTS_PASSED}${NC}" echo -e "Tests failed: ${RED}${TESTS_FAILED}${NC}" # Run comprehensive tests if basic tests passed if [ "$TESTS_FAILED" -eq 0 ]; then echo -e "\n${YELLOW}Running comprehensive tests...${NC}" if [ -f "$SCRIPT_DIR/test/test.sh" ]; then cd "$SCRIPT_DIR/test" ./test.sh COMPREHENSIVE_RESULT=$? cd "$SCRIPT_DIR" if [ "$COMPREHENSIVE_RESULT" -eq 0 ]; then echo -e "\n${GREEN}All tests passed!${NC}" exit 0 else echo -e "\n${RED}Comprehensive tests failed!${NC}" exit 1 fi else echo -e "${YELLOW}Warning: Comprehensive test suite not found${NC}" echo -e "\n${GREEN}Basic tests passed!${NC}" exit 0 fi else echo -e "\n${RED}Basic tests failed! Skipping comprehensive tests.${NC}" exit 1 fi