| 1 | #!/bin/bash |
| 2 | |
| 3 | # Exit on any error |
| 4 | set -euo pipefail |
| 5 | |
| 6 | # Configurations |
| 7 | export DESKTOP_FORCE_MOBILE="Y" |
| 8 | MAX_INACTIVITY=${MAX_INACTIVITY:-180} |
| 9 | MAX_PARALLEL_TESTS=${MAX_PARALLEL_TESTS:-2} |
| 10 | TEST_TIMEOUT=${TEST_TIMEOUT:-600} |
| 11 | RETRY_COUNT=${RETRY_COUNT:-1} |
| 12 | |
| 13 | # Data directories to clean |
| 14 | DATA_DIRS=( |
| 15 | "$HOME/.local/share/com.example.cake_wallet" |
| 16 | "$HOME/Documents/cake_wallet" |
| 17 | "$HOME/.config/cake_wallet" |
| 18 | ) |
| 19 | |
| 20 | # Global state |
| 21 | declare -a targets |
| 22 | declare -a passed_tests |
| 23 | declare -a failed_tests |
| 24 | declare -a test_durations |
| 25 | declare -a test_start_times |
| 26 | |
| 27 | # Signal handling |
| 28 | cleanup() { |
| 29 | echo "🛑 Received interrupt signal, cleaning up..." |
| 30 | exit 1 |
| 31 | } |
| 32 | |
| 33 | trap cleanup SIGINT SIGTERM |
| 34 | |
| 35 | # Utility functions |
| 36 | log() { |
| 37 | echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" |
| 38 | } |
| 39 | |
| 40 | error() { |
| 41 | echo "[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $1" >&2 |
| 42 | } |
| 43 | |
| 44 | # Format seconds into hours, minutes, seconds |
| 45 | format_duration() { |
| 46 | local seconds=$1 |
| 47 | local hours=$((seconds / 3600)) |
| 48 | local minutes=$(((seconds % 3600) / 60)) |
| 49 | local secs=$((seconds % 60)) |
| 50 | |
| 51 | if (( hours > 0 )); then |
| 52 | echo "${hours}h ${minutes}m ${secs}s" |
| 53 | elif (( minutes > 0 )); then |
| 54 | echo "${minutes}m ${secs}s" |
| 55 | else |
| 56 | echo "${secs}s" |
| 57 | fi |
| 58 | } |
| 59 | |
| 60 | clean_data_directories() { |
| 61 | if [[ "${REMOVE_DATA_DIRECTORY:-}" == "Y" ]]; then |
| 62 | log "Cleaning data directories..." |
| 63 | for dir in "${DATA_DIRS[@]}"; do |
| 64 | if [[ -d "$dir" ]]; then |
| 65 | rm -rf "$dir" || error "Failed to remove $dir" |
| 66 | fi |
| 67 | done |
| 68 | fi |
| 69 | } |
| 70 | |
| 71 | # Run a single test with retry logic |
| 72 | run_test() { |
| 73 | local test_file="$1" |
| 74 | local test_name=$(basename "$test_file" .dart) |
| 75 | local retry_count=0 |
| 76 | |
| 77 | while (( retry_count <= RETRY_COUNT )); do |
| 78 | log "Running test: $test_name (attempt $((retry_count + 1)))" |
| 79 | |
| 80 | # Clean data directories before each attempt |
| 81 | clean_data_directories |
| 82 | |
| 83 | # Record start time |
| 84 | local start_time=$(date +%s) |
| 85 | test_start_times+=("$start_time") |
| 86 | |
| 87 | # Run the test in foreground |
| 88 | if flutter drive \ |
| 89 | --driver=test_driver/integration_test.dart \ |
| 90 | --target="$test_file" \ |
| 91 | --dart-define=CI_BUILD=true; then |
| 92 | # Calculate duration |
| 93 | local end_time=$(date +%s) |
| 94 | local duration=$((end_time - start_time)) |
| 95 | test_durations+=("$duration") |
| 96 | |
| 97 | log "✅ Test passed: $test_name ($(format_duration $duration))" |
| 98 | passed_tests+=("$test_name") |
| 99 | return 0 |
| 100 | else |
| 101 | # Calculate duration even for failed tests |
| 102 | local end_time=$(date +%s) |
| 103 | local duration=$((end_time - start_time)) |
| 104 | test_durations+=("$duration") |
| 105 | |
| 106 | log "❌ Test failed: $test_name ($(format_duration $duration))" |
| 107 | |
| 108 | if (( retry_count < RETRY_COUNT )); then |
| 109 | log "Retrying test: $test_name" |
| 110 | retry_count=$((retry_count + 1)) |
| 111 | sleep 5 # Brief pause before retry |
| 112 | else |
| 113 | failed_tests+=("$test_name") |
| 114 | return 1 |
| 115 | fi |
| 116 | fi |
| 117 | done |
| 118 | } |
| 119 | |
| 120 | # Main execution flow |
| 121 | main() { |
| 122 | log "Starting integration test runner" |
| 123 | log "Configuration: RETRY_COUNT=${RETRY_COUNT}" |
| 124 | |
| 125 | # Collect all Dart test files from test_suites directory |
| 126 | while IFS= read -r -d $'\0' file; do |
| 127 | targets+=("$file") |
| 128 | done < <(find integration_test/test_suites -name "*.dart" -type f -print0) |
| 129 | |
| 130 | if [[ $? -ne 0 ]]; then |
| 131 | error "Failed to find test files" |
| 132 | exit 1 |
| 133 | fi |
| 134 | |
| 135 | if (( ${#targets[@]} == 0 )); then |
| 136 | error "No test files found in integration_test/test_suites directory" |
| 137 | exit 1 |
| 138 | fi |
| 139 | |
| 140 | log "Found ${#targets[@]} test files" |
| 141 | |
| 142 | # Record overall start time |
| 143 | local overall_start_time=$(date +%s) |
| 144 | |
| 145 | # Run tests sequentially |
| 146 | for target in "${targets[@]}"; do |
| 147 | run_test "$target" |
| 148 | done |
| 149 | |
| 150 | # Calculate total duration |
| 151 | local overall_end_time=$(date +%s) |
| 152 | local total_duration=$((overall_end_time - overall_start_time)) |
| 153 | |
| 154 | # Generate summary |
| 155 | echo -e "\n===== Test Summary =====" |
| 156 | echo "Total tests: ${#targets[@]}" |
| 157 | echo "Passed: ${#passed_tests[@]}" |
| 158 | echo "Failed: ${#failed_tests[@]}" |
| 159 | echo "Total duration: $(format_duration $total_duration)" |
| 160 | |
| 161 | if (( ${#passed_tests[@]} > 0 )); then |
| 162 | echo -e "\n✅ Passed Tests:" |
| 163 | for i in $(seq 0 $((${#passed_tests[@]} - 1))); do |
| 164 | local test_name="${passed_tests[$i]}" |
| 165 | local duration="${test_durations[$i]}" |
| 166 | echo " - $test_name ($(format_duration $duration))" |
| 167 | done |
| 168 | fi |
| 169 | |
| 170 | if (( ${#failed_tests[@]} > 0 )); then |
| 171 | echo -e "\n❌ Failed Tests:" |
| 172 | for i in $(seq 0 $((${#failed_tests[@]} - 1))); do |
| 173 | local test_name="${failed_tests[$i]}" |
| 174 | local duration="${test_durations[$i]}" |
| 175 | echo " - $test_name ($(format_duration $duration))" |
| 176 | done |
| 177 | exit 1 |
| 178 | else |
| 179 | echo -e "\n🎉 All tests passed successfully!" |
| 180 | fi |
| 181 | } |
| 182 | |
| 183 | # Run main function |
| 184 | main "$@" |