summaryrefslogtreecommitdiff
path: root/apps/test-framework/run-tests.sh
blob: ce624a95a2c27dee03e6a0b41f23f1df55f30413 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env bash

# AzerothCore Universal Test Runner
# This script provides a unified way to run BATS tests across all modules

# Get the script directory and project root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"

# Count cores for parallel execution
if [[ -z "$ACORE_TEST_CORES" ]]; then
    if command -v nproc >/dev/null 2>&1; then
        ACORE_TEST_CORES=$(nproc)
    elif command -v sysctl >/dev/null 2>&1; then
        ACORE_TEST_CORES=$(sysctl -n hw.ncpu)
    else
        ACORE_TEST_CORES=1  # Fallback to single core if detection fails
    fi
    export ACORE_TEST_CORES
fi

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

show_help() {
    echo -e "${BLUE}AzerothCore Universal Test Runner${NC}"
    echo ""
    echo "Usage: $0 [OPTIONS] [TEST_MODULES...]"
    echo ""
    echo "Options:"
    echo "  -h, --help      Show this help message"
    echo "  -v, --verbose   Enable verbose output"
    echo "  -t, --tap       Use TAP output format (for CI/CD)"
    echo "  -p, --pretty    Use pretty output format (default)"
    echo "  -f, --filter    Run only tests matching pattern"
    echo "  -c, --count     Show test count only"
    echo "  -d, --debug     Enable debug mode (shows output on failure)"
    echo "  -l, --list      List available test modules"
    echo "  -j, --jobs <num>  Set number of parallel jobs (default: $ACORE_TEST_CORES)"
    echo "  --dir <path>    Run tests in specific directory"
    echo "  --all           Run all tests in all modules"
    echo ""
    echo "Test Modules:"
    echo "  startup-scripts - Startup script tests"
    echo "  compiler        - Compiler script tests"
    echo "  docker          - Docker-related tests"
    echo "  installer       - Installer script tests"
    echo ""
    echo "Examples:"
    echo "  $0                              # Run tests in current directory"
    echo "  $0 --all                        # Run all tests in all modules"
    echo "  $0 startup-scripts              # Run startup-scripts tests only"
    echo "  $0 --dir apps/docker            # Run tests in specific directory"
    echo "  $0 --verbose startup-scripts    # Run with verbose output"
    echo "  $0 --filter starter             # Run only tests matching 'starter'"
    echo "  $0 --tap                         # Output in TAP format for CI"
}

# Parse command line arguments
VERBOSE=false
TAP=false
PRETTY=true
FILTER=""
COUNT_ONLY=false
DEBUG=false
LIST_MODULES=false
RUN_ALL=false
TEST_DIRS=()
TEST_MODULES=()

while [[ $# -gt 0 ]]; do
    case $1 in
        -h|--help)
            show_help
            exit 0
            ;;
        -v|--verbose)
            VERBOSE=true
            shift
            ;;
        -t|--tap)
            TAP=true
            PRETTY=false
            shift
            ;;
        -p|--pretty)
            PRETTY=true
            TAP=false
            shift
            ;;
        -f|--filter)
            FILTER="$2"
            shift 2
            ;;
        -c|--count)
            COUNT_ONLY=true
            shift
            ;;
        -d|--debug)
            DEBUG=true
            shift
            ;;
        -l|--list)
            LIST_MODULES=true
            shift
            ;;
        --dir)
            TEST_DIRS+=("$2")
            shift 2
            ;;
        --all)
            RUN_ALL=true
            shift
            ;;
        -j|--jobs)
            if [[ "$2" =~ ^[0-9]+$ ]]; then
                ACORE_TEST_CORES="$2"
                export ACORE_TEST_CORES
                shift 2
            else
                echo -e "${RED}Error: Invalid number of jobs specified: $2${NC}"
                echo "Please provide a valid number."
                exit 1
            fi
            ;;
        *.bats)
            # Individual test files
            TEST_FILES+=("$1")
            shift
            ;;
        *)
            # Assume it's a module name
            TEST_MODULES+=("$1")
            shift
            ;;
    esac
done

# Check if BATS is installed
if ! command -v bats >/dev/null 2>&1; then
    echo -e "${RED}Error: BATS is not installed${NC}"
    echo "Please install BATS first:"
    echo "  sudo apt install bats    # On Ubuntu/Debian"
    echo "  brew install bats-core   # On macOS"
    echo "Or run: make install-bats"
    exit 1
fi

# Function to find test directories
find_test_directories() {
    local search_paths=()
    
    if [[ "$RUN_ALL" == true ]]; then
        # Find all test directories
        mapfile -t search_paths < <(find "$PROJECT_ROOT/apps" -type d -name "test" 2>/dev/null)
    elif [[ ${#TEST_DIRS[@]} -gt 0 ]]; then
        # Use specified directories
        for dir in "${TEST_DIRS[@]}"; do
            if [[ -d "$PROJECT_ROOT/$dir/test" ]]; then
                search_paths+=("$PROJECT_ROOT/$dir/test")
            elif [[ -d "$dir/test" ]]; then
                search_paths+=("$dir/test")
            elif [[ -d "$dir" ]]; then
                search_paths+=("$dir")
            else
                echo -e "${YELLOW}Warning: Test directory not found: $dir${NC}"
            fi
        done
    elif [[ ${#TEST_MODULES[@]} -gt 0 ]]; then
        # Use specified modules
        for module in "${TEST_MODULES[@]}"; do
            if [[ -d "$PROJECT_ROOT/apps/$module/test" ]]; then
                search_paths+=("$PROJECT_ROOT/apps/$module/test")
            else
                echo -e "${YELLOW}Warning: Module test directory not found: $module${NC}"
            fi
        done
    else
        # Default: use current directory or startup-scripts if run from test-framework
        if [[ "$(basename "$PWD")" == "test-framework" ]]; then
            search_paths=("$PROJECT_ROOT/apps/startup-scripts/test")
        elif [[ -d "./test" ]]; then
            search_paths=("./test")
        else
            echo -e "${YELLOW}No test directory found. Use --all or specify a module.${NC}"
            exit 0
        fi
    fi
    
    echo "${search_paths[@]}"
}

# Function to list available modules
list_modules() {
    echo -e "${BLUE}Available test modules:${NC}"
    find "$PROJECT_ROOT/apps" -type d -name "test" 2>/dev/null | while read -r test_dir; do
        module_name=$(basename "$(dirname "$test_dir")")
        test_count=$(find "$test_dir" -name "*.bats" | wc -l)
        echo -e "  ${GREEN}$module_name${NC} ($test_count test files)"
    done
}

# Show available modules if requested
if [[ "$LIST_MODULES" == true ]]; then
    list_modules
    exit 0
fi

# Find test directories
TEST_SEARCH_PATHS=($(find_test_directories))

if [[ ${#TEST_SEARCH_PATHS[@]} -eq 0 ]]; then
    echo -e "${YELLOW}No test directories found.${NC}"
    echo "Use --list to see available modules."
    exit 0
fi

# Collect all test files
TEST_FILES=()
for test_dir in "${TEST_SEARCH_PATHS[@]}"; do
    if [[ -d "$test_dir" ]]; then
        if [[ -n "$FILTER" ]]; then
            # Find test files matching filter
            mapfile -t filtered_files < <(find "$test_dir" -name "*.bats" -exec grep -l "$FILTER" {} \; 2>/dev/null)
            TEST_FILES+=("${filtered_files[@]}")
        else
            # Use all test files in directory
            mapfile -t dir_files < <(find "$test_dir" -name "*.bats" 2>/dev/null)
            TEST_FILES+=("${dir_files[@]}")
        fi
    fi
done

if [[ ${#TEST_FILES[@]} -eq 0 ]]; then
    if [[ -n "$FILTER" ]]; then
        echo -e "${YELLOW}No test files found matching filter: $FILTER${NC}"
    else
        echo -e "${YELLOW}No test files found in specified directories.${NC}"
    fi
    exit 0
fi

# Show test count only
if [[ "$COUNT_ONLY" == true ]]; then
    total_tests=0
    for file in "${TEST_FILES[@]}"; do
        count=$(grep -c "^@test" "$file" 2>/dev/null || echo 0)
        total_tests=$((total_tests + count))
    done
    echo "Total tests: $total_tests"
    echo "Test files: ${#TEST_FILES[@]}"
    echo "Test directories: ${#TEST_SEARCH_PATHS[@]}"
    exit 0
fi

# Build BATS command
BATS_CMD="bats --jobs $ACORE_TEST_CORES"

# Set output format
if [[ "$TAP" == true ]]; then
    BATS_CMD+=" --formatter tap"
elif [[ "$PRETTY" == true ]]; then
    BATS_CMD+=" --formatter pretty"
fi

# Enable verbose output
if [[ "$VERBOSE" == true ]]; then
    BATS_CMD+=" --verbose-run"
fi

# Add filter if specified
if [[ -n "$FILTER" ]]; then
    BATS_CMD+=" --filter '$FILTER'"
fi

# Add test files
BATS_CMD+=" ${TEST_FILES[*]}"

echo -e "${BLUE}Running AzerothCore Tests with ${ACORE_TEST_CORES} jobs${NC}"
echo -e "${YELLOW}Test directories: ${TEST_SEARCH_PATHS[*]}${NC}"
echo -e "${YELLOW}Test files: ${#TEST_FILES[@]}${NC}"
if [[ -n "$FILTER" ]]; then
    echo -e "${YELLOW}Filter: $FILTER${NC}"
fi
echo ""

# Run tests
if [[ "$DEBUG" == true ]]; then
    echo -e "${YELLOW}Command: $BATS_CMD${NC}"
    echo ""
fi

# Execute BATS
if eval "$BATS_CMD"; then
    echo ""
    echo -e "${GREEN}✅ All tests passed!${NC}"
    exit 0
else
    exit_code=$?
    echo ""
    echo -e "${RED}❌ Some tests failed!${NC}"
    
    if [[ "$DEBUG" == true ]]; then
        echo -e "${YELLOW}Tip: Check the output above for detailed error information${NC}"
        echo -e "${YELLOW}You can also run individual tests for more detailed debugging:${NC}"
        echo -e "${YELLOW}  $0 --verbose --filter <test_name>${NC}"
    fi
    
    exit $exit_code
fi