summaryrefslogtreecommitdiff
path: root/apps/compiler/test/test_compiler.bats
blob: ff217e638901add938e58f1d6bef8e104f8b6722 (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
#!/usr/bin/env bats

# Require minimum BATS version to avoid warnings
bats_require_minimum_version 1.5.0

# AzerothCore Compiler Scripts Test Suite
# Tests the functionality of the compiler scripts using the unified test framework

# Load the AzerothCore test framework
load '../../test-framework/bats_libs/acore-support'
load '../../test-framework/bats_libs/acore-assert'

# Setup that runs before each test
setup() {
    compiler_setup
    export SCRIPT_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")/.." && pwd)"
    export COMPILER_SCRIPT="$SCRIPT_DIR/compiler.sh"
}

# Cleanup that runs after each test
teardown() {
    acore_test_teardown
}

# ===== COMPILER SCRIPT TESTS =====

@test "compiler: should show help with --help argument" {
    run bash -c "echo '' | timeout 5s $COMPILER_SCRIPT --help"
    [ "$status" -eq 0 ]
    [[ "$output" =~ "Available commands:" ]]
}

@test "compiler: should show help with empty input" {
    run bash -c "echo '' | timeout 5s $COMPILER_SCRIPT 2>&1 || true"
    # The script might exit with timeout (124) or success (0), both are acceptable for this test
    [[ "$status" -eq 0 ]] || [[ "$status" -eq 124 ]]
    # Check if output contains expected content - looking for menu options
    [[ "$output" =~ "build:" ]] || [[ "$output" =~ "clean:" ]] || [[ "$output" =~ "Please enter your choice" ]] || [[ -z "$output" ]]
}

@test "compiler: should accept option numbers" {
    # Test option 7 (ccacheShowStats) which should be safe to run
    run bash -c "echo '7' | timeout 10s $COMPILER_SCRIPT 2>/dev/null || true"
    # The script might exit with timeout (124) or success (0), both are acceptable
    [[ "$status" -eq 0 ]] || [[ "$status" -eq 124 ]]
}

@test "compiler: should accept option by name" {
    run timeout 10s "$COMPILER_SCRIPT" ccacheShowStats
    [ "$status" -eq 0 ]
}

@test "compiler: should handle invalid option gracefully" {
    run timeout 5s "$COMPILER_SCRIPT" invalidOption
    [ "$status" -eq 0 ]
    [[ "$output" =~ "invalid option" ]]
}

@test "compiler: should handle invalid number gracefully" {
    run bash -c "echo '999' | timeout 5s $COMPILER_SCRIPT 2>/dev/null || true"
    # The script might exit with timeout (124) or success (0), both are acceptable
    [[ "$status" -eq 0 ]] || [[ "$status" -eq 124 ]]
    # Check if output contains expected content, or if there's no output due to timeout, that's also acceptable
    [[ "$output" =~ "invalid option" ]] || [[ "$output" =~ "Please enter your choice" ]] || [[ -z "$output" ]]
}

@test "compiler: should quit with quit option" {
    run timeout 5s "$COMPILER_SCRIPT" quit
    [ "$status" -eq 0 ]
}

# ===== FUNCTION TESTS =====

@test "functions: comp_clean should handle non-existent build directory" {
    # Source the functions with a non-existent build path
    run bash -c "
        export BUILDPATH='/tmp/non_existent_build_dir_$RANDOM'
        source '$SCRIPT_DIR/includes/functions.sh'
        comp_clean
    "
    # Accept either success or failure - the important thing is the function runs
    [[ "$status" -eq 0 ]] || [[ "$status" -eq 1 ]]
    [[ "$output" =~ "Cleaning build files" ]]
}

@test "functions: comp_clean should remove build files when directory exists" {
    # Create a temporary build directory with test files
    local test_build_dir="/tmp/test_build_$RANDOM"
    mkdir -p "$test_build_dir/subdir"
    touch "$test_build_dir/test_file.txt"
    touch "$test_build_dir/subdir/nested_file.txt"
    
    # Run the clean function
    run bash -c "
        export BUILDPATH='$test_build_dir'
        source '$SCRIPT_DIR/includes/functions.sh'
        comp_clean
    "
    
    [ "$status" -eq 0 ]
    [[ "$output" =~ "Cleaning build files" ]]
    # Directory should still exist but be empty
    [ -d "$test_build_dir" ]
    [ ! -f "$test_build_dir/test_file.txt" ]
    [ ! -f "$test_build_dir/subdir/nested_file.txt" ]
    
    # Cleanup
    rm -rf "$test_build_dir"
}

@test "functions: comp_ccacheShowStats should run without errors when ccache enabled" {
    run bash -c "
        export AC_CCACHE=true
        source '$SCRIPT_DIR/includes/functions.sh'
        comp_ccacheShowStats
    "
    [ "$status" -eq 0 ]
}

@test "functions: comp_ccacheShowStats should do nothing when ccache disabled" {
    run bash -c "
        export AC_CCACHE=false
        source '$SCRIPT_DIR/includes/functions.sh'
        comp_ccacheShowStats
    "
    [ "$status" -eq 0 ]
    # Should produce no output when ccache is disabled
}

@test "functions: comp_ccacheClean should handle disabled ccache" {
    run bash -c "
        export AC_CCACHE=false
        source '$SCRIPT_DIR/includes/functions.sh'
        comp_ccacheClean
    "
    [ "$status" -eq 0 ]
    [[ "$output" =~ "ccache is disabled" ]]
}

@test "functions: comp_ccacheClean should run when ccache enabled" {
    # Only run if ccache is actually available
    if command -v ccache >/dev/null 2>&1; then
        run bash -c "
            export AC_CCACHE=true
            source '$SCRIPT_DIR/includes/functions.sh'
            comp_ccacheClean
        "
        [ "$status" -eq 0 ]
        [[ "$output" =~ "Cleaning ccache" ]]
    else
        skip "ccache not available on system"
    fi
}

@test "functions: comp_ccacheEnable should set environment variables" {
    # Call the function in a subshell to capture environment changes
    run bash -c "
        export AC_CCACHE=true
        source '$SCRIPT_DIR/includes/functions.sh'
        comp_ccacheEnable
        env | grep CCACHE | head -5
    "
    
    [ "$status" -eq 0 ]
    [[ "$output" =~ "CCACHE_MAXSIZE" ]] || [[ "$output" =~ "CCACHE_COMPRESS" ]]
}

@test "functions: comp_ccacheEnable should not set variables when ccache disabled" {
    # Call the function and verify it returns early when ccache is disabled
    run bash -c "
        export AC_CCACHE=false
        source '$SCRIPT_DIR/includes/functions.sh'
        comp_ccacheEnable
        # The function should return early, so we check if it completed successfully
        echo 'Function completed without setting CCACHE vars'
    "
    
    [ "$status" -eq 0 ]
    [[ "$output" =~ "Function completed" ]]
}

# Mock tests for build functions (these would normally require a full setup)
@test "functions: comp_configure should detect platform" {
    # Mock cmake command to avoid actual configuration
    run -127 bash -c "
        function cmake() {
            echo 'CMAKE called with args: $*'
            return 0
        }
        export -f cmake
        
        # Set required variables
        export BUILDPATH='/tmp'
        export SRCPATH='/tmp'
        export BINPATH='/tmp'
        export CTYPE='Release'
        
        # Source the functions
        source '$SCRIPT_DIR/includes/functions.sh'
        
        # Run configure in the /tmp directory
        cd /tmp && comp_configure
    "
    
    # Accept command not found as this might indicate missing dependencies
    [[ "$status" -eq 0 ]] || [[ "$status" -eq 127 ]]
    # If successful, check for expected output
    if [ "$status" -eq 0 ]; then
        [[ "$output" =~ "Platform:" ]] || [[ "$output" =~ "CMAKE called with args:" ]]
    fi
}

@test "functions: comp_compile should detect thread count" {
    # Mock cmake command to avoid actual compilation
    run -127 bash -c "
        function cmake() {
            echo 'CMAKE called with args: $*'
            return 0
        }
        export -f cmake
        
        # Mock other commands
        function pushd() { echo 'pushd $*'; }
        function popd() { echo 'popd $*'; }
        function time() { shift; \"\$@\"; }
        export -f pushd popd time
        
        # Set required variables
        export BUILDPATH='/tmp'
        export MTHREADS=0
        export CTYPE='Release'
        export AC_BINPATH_FULL='/tmp'
        
        # Source the functions
        source '$SCRIPT_DIR/includes/functions.sh'
        
        # Run compile in the /tmp directory
        cd /tmp && comp_compile
    "
    
    # Accept command not found as this might indicate missing dependencies
    [[ "$status" -eq 0 ]] || [[ "$status" -eq 127 ]]
    # If successful, check for expected output
    if [ "$status" -eq 0 ]; then
        [[ "$output" =~ "pushd" ]] || [[ "$output" =~ "CMAKE called with args:" ]]
    fi
}

@test "functions: comp_build should call configure and compile" {
    # Mock the comp_configure and comp_compile functions
    run -127 bash -c "
        function comp_configure() {
            echo 'comp_configure called'
            return 0
        }
        
        function comp_compile() {
            echo 'comp_compile called'
            return 0
        }
        
        export -f comp_configure comp_compile
        
        # Source the functions
        source '$SCRIPT_DIR/includes/functions.sh'
        
        # Run build
        comp_build
    "
    
    # Accept command not found as this might indicate missing dependencies
    [[ "$status" -eq 0 ]] || [[ "$status" -eq 127 ]]
    # If successful, check for expected output
    if [ "$status" -eq 0 ]; then
        [[ "$output" =~ "comp_configure called" ]] && [[ "$output" =~ "comp_compile called" ]]
    fi
}

@test "functions: comp_all should call clean and build" {
    # Mock the comp_clean and comp_build functions
    run -127 bash -c "
        function comp_clean() {
            echo 'comp_clean called'
            return 0
        }
        
        function comp_build() {
            echo 'comp_build called'
            return 0
        }
        
        export -f comp_clean comp_build
        
        # Source the functions
        source '$SCRIPT_DIR/includes/functions.sh'
        
        # Run all
        comp_all
    "
    
    # Accept command not found as this might indicate missing dependencies
    [[ "$status" -eq 0 ]] || [[ "$status" -eq 127 ]]
    # If successful, check for expected output
    if [ "$status" -eq 0 ]; then
        [[ "$output" =~ "comp_clean called" ]] && [[ "$output" =~ "comp_build called" ]]
    fi
}