-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-tests.sh
More file actions
executable file
·127 lines (109 loc) · 3.72 KB
/
run-tests.sh
File metadata and controls
executable file
·127 lines (109 loc) · 3.72 KB
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
#!/bin/bash
# Bash script to run Java tests and return structured results
# This script uses Gradle to run JUnit 5 tests and parses the output
TEST_CLASS=${1:-"com.example.testing.ProjectSetupTest"}
BRANCH_NAME=${2:-"6-2-solution"}
echo "Running tests for branch: $BRANCH_NAME"
echo "Test class: $TEST_CLASS"
echo "=================================================="
# Function to create JSON-like output
create_test_result() {
local all_tests_passed=$1
local total_tests=$2
local passed_tests=$3
local failed_tests=$4
local failed_test_names=$5
local error_message=$6
echo "{"
echo " \"BranchName\": \"$BRANCH_NAME\","
echo " \"AllTestsPassed\": $all_tests_passed,"
echo " \"TotalTests\": $total_tests,"
echo " \"PassedTests\": $passed_tests,"
echo " \"FailedTests\": $failed_tests,"
echo " \"FailedTestNames\": [$failed_test_names],"
echo " \"ErrorMessage\": \"$error_message\","
echo " \"Timestamp\": \"$(date '+%Y-%m-%d %H:%M:%S')\""
echo "}"
}
# Check if Gradle wrapper exists
if [ ! -f "gradlew" ]; then
create_test_result false 0 0 0 "[]" "Gradle wrapper not found"
exit 1
fi
# Check if test files exist
if [ ! -d "src/test/java" ] || [ -z "$(find src/test/java -name '*.java' -type f)" ]; then
create_test_result false 0 0 0 "[]" "No test files found"
exit 1
fi
# Run Gradle tests
echo "Running Gradle tests..."
if [ "$TEST_CLASS" = "all" ]; then
# Run all tests
TEST_OUTPUT=$(./gradlew test 2>&1)
else
# Run specific test class
TEST_OUTPUT=$(./gradlew test --tests "$TEST_CLASS" 2>&1)
fi
# Parse the test output
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0
FAILED_TEST_NAMES=""
# Extract numbers from JUnit test summary using grep -o
TOTAL_TESTS=$(echo "$TEST_OUTPUT" | grep -E "(\d+) tests completed|Tests run: (\d+)" | grep -o '[0-9]\+' | head -1)
PASSED_TESTS=$(echo "$TEST_OUTPUT" | grep "(\d+) successful" | grep -o '[0-9]\+' | head -1)
FAILED_TESTS=$(echo "$TEST_OUTPUT" | grep -E "(\d+) failed|Failures: (\d+)" | grep -o '[0-9]\+' | head -1)
# Handle cases where parsing might fail
if [ -z "$TOTAL_TESTS" ] || ! [[ "$TOTAL_TESTS" =~ ^[0-9]+$ ]]; then
TOTAL_TESTS=0
fi
if [ -z "$PASSED_TESTS" ] || ! [[ "$PASSED_TESTS" =~ ^[0-9]+$ ]]; then
PASSED_TESTS=0
fi
if [ -z "$FAILED_TESTS" ] || ! [[ "$FAILED_TESTS" =~ ^[0-9]+$ ]]; then
FAILED_TESTS=0
fi
# Check for build success/failure
if echo "$TEST_OUTPUT" | grep -q "BUILD SUCCESSFUL"; then
if [ "$TOTAL_TESTS" -eq 0 ]; then
TOTAL_TESTS=1
PASSED_TESTS=1
FAILED_TESTS=0
fi
elif echo "$TEST_OUTPUT" | grep -q "BUILD FAILED"; then
if [ "$TOTAL_TESTS" -eq 0 ]; then
TOTAL_TESTS=1
PASSED_TESTS=0
FAILED_TESTS=1
fi
fi
# Extract failed test names
FAILED_TEST_NAMES=$(echo "$TEST_OUTPUT" | grep "FAILED.*(" | sed 's/.*FAILED.*(\(.*\))/\1/' | sed 's/^/"/' | sed 's/$/"/' | tr '\n' ',' | sed 's/,$//')
if [ -z "$FAILED_TEST_NAMES" ]; then
FAILED_TEST_NAMES=""
else
FAILED_TEST_NAMES="[$FAILED_TEST_NAMES]"
fi
# Determine if all tests passed
if [ "$FAILED_TESTS" -eq 0 ] && [ "$TOTAL_TESTS" -gt 0 ]; then
ALL_TESTS_PASSED=true
else
ALL_TESTS_PASSED=false
fi
# Display results
echo ""
echo "Test Results:"
echo "Total Tests: $TOTAL_TESTS"
echo "Passed: $PASSED_TESTS"
echo "Failed: $FAILED_TESTS"
if [ "$ALL_TESTS_PASSED" = true ]; then
echo "🎉 ALL TESTS PASSED!"
else
echo "❌ Some tests failed"
if [ -n "$FAILED_TEST_NAMES" ] && [ "$FAILED_TEST_NAMES" != "[]" ]; then
echo "Failed tests:"
echo "$TEST_OUTPUT" | grep "FAILED.*(" | sed 's/.*FAILED.*(/ - /' | sed 's/).*//'
fi
fi
# Return the result
create_test_result "$ALL_TESTS_PASSED" "$TOTAL_TESTS" "$PASSED_TESTS" "$FAILED_TESTS" "$FAILED_TEST_NAMES" ""