|
| 1 | +#!/bin/bash |
| 2 | +# Unit Tests for Test Runner Functions |
| 3 | +# Tests the test runner functionality and integration |
| 4 | + |
| 5 | +# Source the test runner |
| 6 | +if [ -f "$(dirname "$0")/../lib/runner.sh" ]; then |
| 7 | + source "$(dirname "$0")/../lib/runner.sh" |
| 8 | +else |
| 9 | + echo "ERROR: runner.sh not found" |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +# Test test result initialization |
| 14 | +test_init_test_results() { |
| 15 | + echo "Testing test result initialization..." |
| 16 | + |
| 17 | + # Initialize test results |
| 18 | + init_test_results |
| 19 | + |
| 20 | + # Verify all counters are zero |
| 21 | + test_assert_equals "TESTS_RUN initialized" "0" "$TEST_RESULTS_TESTS_RUN" |
| 22 | + test_assert_equals "TESTS_PASSED initialized" "0" "$TEST_RESULTS_TESTS_PASSED" |
| 23 | + test_assert_equals "TESTS_FAILED initialized" "0" "$TEST_RESULTS_TESTS_FAILED" |
| 24 | + test_assert_equals "SUITES_RUN initialized" "0" "$TEST_RESULTS_SUITES_RUN" |
| 25 | + test_assert_equals "SUITES_PASSED initialized" "0" "$TEST_RESULTS_SUITES_PASSED" |
| 26 | + test_assert_equals "SUITES_FAILED initialized" "0" "$TEST_RESULTS_SUITES_FAILED" |
| 27 | +} |
| 28 | + |
| 29 | +# Test test result aggregation |
| 30 | +test_test_result_aggregation() { |
| 31 | + echo "Testing test result aggregation..." |
| 32 | + |
| 33 | + # Initialize test results |
| 34 | + init_test_results |
| 35 | + |
| 36 | + # Simulate some test results |
| 37 | + TEST_RESULTS_TESTS_RUN=10 |
| 38 | + TEST_RESULTS_TESTS_PASSED=8 |
| 39 | + TEST_RESULTS_TESTS_FAILED=2 |
| 40 | + TEST_RESULTS_SUITES_RUN=3 |
| 41 | + TEST_RESULTS_SUITES_PASSED=2 |
| 42 | + TEST_RESULTS_SUITES_FAILED=1 |
| 43 | + |
| 44 | + # Test get_test_results function |
| 45 | + local results |
| 46 | + results=$(get_test_results) |
| 47 | + test_assert_equals "get_test_results format" "10 8 2 3 2 1" "$results" |
| 48 | +} |
| 49 | + |
| 50 | +# Test JSON result generation |
| 51 | +test_json_result_generation() { |
| 52 | + echo "Testing JSON result generation..." |
| 53 | + |
| 54 | + # Initialize test results |
| 55 | + init_test_results |
| 56 | + |
| 57 | + # Set some test results |
| 58 | + TEST_RESULTS_TESTS_RUN=5 |
| 59 | + TEST_RESULTS_TESTS_PASSED=4 |
| 60 | + TEST_RESULTS_TESTS_FAILED=1 |
| 61 | + TEST_RESULTS_SUITES_RUN=2 |
| 62 | + TEST_RESULTS_SUITES_PASSED=1 |
| 63 | + TEST_RESULTS_SUITES_FAILED=1 |
| 64 | + |
| 65 | + # Test JSON generation |
| 66 | + local json_result |
| 67 | + json_result=$(get_test_results_json) |
| 68 | + test_assert_contains "JSON contains tests_run" "$json_result" '"tests_run":5' |
| 69 | + test_assert_contains "JSON contains tests_passed" "$json_result" '"tests_passed":4' |
| 70 | + test_assert_contains "JSON contains tests_failed" "$json_result" '"tests_failed":1' |
| 71 | + test_assert_contains "JSON contains suites_run" "$json_result" '"suites_run":2' |
| 72 | + test_assert_contains "JSON contains suites_passed" "$json_result" '"suites_passed":1' |
| 73 | + test_assert_contains "JSON contains suites_failed" "$json_result" '"suites_failed":1' |
| 74 | +} |
| 75 | + |
| 76 | +# Test all tests passed check |
| 77 | +test_all_tests_passed() { |
| 78 | + echo "Testing all tests passed check..." |
| 79 | + |
| 80 | + # Initialize test results |
| 81 | + init_test_results |
| 82 | + |
| 83 | + # Test case 1: All tests passed |
| 84 | + TEST_RESULTS_TESTS_FAILED=0 |
| 85 | + TEST_RESULTS_SUITES_FAILED=0 |
| 86 | + |
| 87 | + if all_tests_passed; then |
| 88 | + test_assert_equals "all_tests_passed: all passed" "0" "$?" |
| 89 | + else |
| 90 | + test_assert_equals "all_tests_passed: all passed" "1" "$?" |
| 91 | + fi |
| 92 | + |
| 93 | + # Test case 2: Some tests failed |
| 94 | + TEST_RESULTS_TESTS_FAILED=2 |
| 95 | + TEST_RESULTS_SUITES_FAILED=1 |
| 96 | + |
| 97 | + if all_tests_passed; then |
| 98 | + test_assert_equals "all_tests_passed: some failed" "0" "$?" |
| 99 | + else |
| 100 | + test_assert_equals "all_tests_passed: some failed" "1" "$?" |
| 101 | + fi |
| 102 | +} |
| 103 | + |
| 104 | +# Test exit code generation |
| 105 | +test_exit_code_generation() { |
| 106 | + echo "Testing exit code generation..." |
| 107 | + |
| 108 | + # Initialize test results |
| 109 | + init_test_results |
| 110 | + |
| 111 | + # Test case 1: All tests passed |
| 112 | + TEST_RESULTS_TESTS_FAILED=0 |
| 113 | + TEST_RESULTS_SUITES_FAILED=0 |
| 114 | + |
| 115 | + local exit_code |
| 116 | + exit_code=$(get_exit_code) |
| 117 | + test_assert_equals "get_exit_code: all passed" "0" "$exit_code" |
| 118 | + |
| 119 | + # Test case 2: Some tests failed |
| 120 | + TEST_RESULTS_TESTS_FAILED=1 |
| 121 | + TEST_RESULTS_SUITES_FAILED=0 |
| 122 | + |
| 123 | + exit_code=$(get_exit_code) |
| 124 | + test_assert_equals "get_exit_code: some failed" "1" "$exit_code" |
| 125 | +} |
| 126 | + |
| 127 | +# Test test file execution |
| 128 | +test_test_file_execution() { |
| 129 | + echo "Testing test file execution..." |
| 130 | + |
| 131 | + # Create a temporary test file |
| 132 | + local temp_test_file=$(mktemp) |
| 133 | + cat > "$temp_test_file" << 'EOF' |
| 134 | +#!/bin/bash |
| 135 | +# Temporary test file |
| 136 | +echo "PASS:Test 1" |
| 137 | +echo "PASS:Test 2" |
| 138 | +echo "FAIL:Test 3" |
| 139 | +EOF |
| 140 | + chmod +x "$temp_test_file" |
| 141 | + |
| 142 | + # Test file execution |
| 143 | + if run_test_file "$temp_test_file" "temp_test" false; then |
| 144 | + test_assert_equals "run_test_file execution" "0" "$?" |
| 145 | + else |
| 146 | + test_assert_equals "run_test_file execution" "1" "$?" |
| 147 | + fi |
| 148 | + |
| 149 | + # Clean up |
| 150 | + rm -f "$temp_test_file" |
| 151 | +} |
| 152 | + |
| 153 | +# Test test suite execution |
| 154 | +test_test_suite_execution() { |
| 155 | + echo "Testing test suite execution..." |
| 156 | + |
| 157 | + # Create temporary test files |
| 158 | + local temp_test1=$(mktemp) |
| 159 | + local temp_test2=$(mktemp) |
| 160 | + |
| 161 | + cat > "$temp_test1" << 'EOF' |
| 162 | +#!/bin/bash |
| 163 | +echo "PASS:Suite Test 1" |
| 164 | +EOF |
| 165 | + |
| 166 | + cat > "$temp_test2" << 'EOF' |
| 167 | +#!/bin/bash |
| 168 | +echo "PASS:Suite Test 2" |
| 169 | +EOF |
| 170 | + |
| 171 | + chmod +x "$temp_test1" "$temp_test2" |
| 172 | + |
| 173 | + # Test suite execution |
| 174 | + if run_test_suite "temp_suite" "$temp_test1" "$temp_test2"; then |
| 175 | + test_assert_equals "run_test_suite execution" "0" "$?" |
| 176 | + else |
| 177 | + test_assert_equals "run_test_suite execution" "1" "$?" |
| 178 | + fi |
| 179 | + |
| 180 | + # Clean up |
| 181 | + rm -f "$temp_test1" "$temp_test2" |
| 182 | +} |
| 183 | + |
| 184 | +# Test error handling in test execution |
| 185 | +test_error_handling() { |
| 186 | + echo "Testing error handling in test execution..." |
| 187 | + |
| 188 | + # Test with non-existent file |
| 189 | + if run_test_file "/non/existent/file" "error_test" false; then |
| 190 | + test_assert_equals "run_test_file: non-existent file" "0" "$?" |
| 191 | + else |
| 192 | + test_assert_equals "run_test_file: non-existent file" "1" "$?" |
| 193 | + fi |
| 194 | + |
| 195 | + # Test with invalid test file |
| 196 | + local temp_bad_file=$(mktemp) |
| 197 | + cat > "$temp_bad_file" << 'EOF' |
| 198 | +#!/bin/bash |
| 199 | +# This will cause an error |
| 200 | +invalid_command_that_does_not_exist |
| 201 | +EOF |
| 202 | + chmod +x "$temp_bad_file" |
| 203 | + |
| 204 | + if run_test_file "$temp_bad_file" "error_test" false; then |
| 205 | + test_assert_equals "run_test_file: invalid command" "1" "$?" |
| 206 | + else |
| 207 | + test_assert_equals "run_test_file: invalid command" "0" "$?" |
| 208 | + fi |
| 209 | + |
| 210 | + # Clean up |
| 211 | + rm -f "$temp_bad_file" |
| 212 | +} |
| 213 | + |
| 214 | +# Test summary display |
| 215 | +test_summary_display() { |
| 216 | + echo "Testing summary display..." |
| 217 | + |
| 218 | + # Initialize test results |
| 219 | + init_test_results |
| 220 | + |
| 221 | + # Set some test results |
| 222 | + TEST_RESULTS_TESTS_RUN=10 |
| 223 | + TEST_RESULTS_TESTS_PASSED=8 |
| 224 | + TEST_RESULTS_TESTS_FAILED=2 |
| 225 | + TEST_RESULTS_SUITES_RUN=3 |
| 226 | + TEST_RESULTS_SUITES_PASSED=2 |
| 227 | + TEST_RESULTS_SUITES_FAILED=1 |
| 228 | + TEST_RESULTS_START_TIME=$(($(date +%s) - 5)) |
| 229 | + |
| 230 | + # Test summary display (capture output) |
| 231 | + local summary_output |
| 232 | + summary_output=$(print_test_summary 2>&1) |
| 233 | + |
| 234 | + test_assert_contains "Summary contains total tests" "$summary_output" "Total Tests: 10" |
| 235 | + test_assert_contains "Summary contains passed tests" "$summary_output" "Passed: 8" |
| 236 | + test_assert_contains "Summary contains failed tests" "$summary_output" "Failed: 2" |
| 237 | + test_assert_contains "Summary contains test suites" "$summary_output" "Test Suites: 3" |
| 238 | +} |
| 239 | + |
| 240 | +# Test runner for test runner tests |
| 241 | +run_test_runner_tests() { |
| 242 | + echo "🧪 Testing Test Runner Functions..." |
| 243 | + |
| 244 | + test_init_test_results |
| 245 | + test_test_result_aggregation |
| 246 | + test_json_result_generation |
| 247 | + test_all_tests_passed |
| 248 | + test_exit_code_generation |
| 249 | + test_test_file_execution |
| 250 | + test_test_suite_execution |
| 251 | + test_error_handling |
| 252 | + test_summary_display |
| 253 | +} |
0 commit comments