-
Notifications
You must be signed in to change notification settings - Fork 4
/
run_tests.sh
executable file
·169 lines (154 loc) · 5.49 KB
/
run_tests.sh
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
#!/bin/bash
########################################################################
# run_tests.sh: Run all Python and Ruby tests in the test directory
#
# Description:
# This script checks for the presence of Python and Ruby, and executes all
# Python and Ruby test files located in the 'test' subdirectory. It displays
# the paths and versions of Python and Ruby being used, and checks if each
# test passes or fails.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: LGPLv3 (Details: https://www.gnu.org/licenses/lgpl-3.0.html)
# Contact: [email protected]
#
# Version History:
# v1.5 2024-03-15
# Added functionality to display total number of test scripts
# and test cases executed for each language.
# v1.4 2024-03-06
# Added checks to ensure specified Python and RSpec paths are not only
# non-empty but also point to executable files. This enhancement
# prevents the execution of tests with invalid paths.
# v1.3 2024-01-14
# Added the ability to specify custom Python and RSpec paths
# as command-line arguments.
# v1.2 2023-12-23
# Refactored for POSIX compliance. Replaced Bash-specific syntax
# with POSIX standard commands and structures. Enhanced portability
# and compatibility across different UNIX-like systems.
# v1.1 2023-12-20
# Added environment variable to prevent the creation of __pycache__
# directories during Python tests.
# Replaced 'which' with 'command -v' for command existence check.
# v1.0 2023-12-15
# First release of the test script.
#
# Usage:
# Run this script from the command line to execute all tests:
# ./run_tests.sh [Python path] [RSpec path]
# If no paths are specified, it will use the default system paths.
#
########################################################################
export PYTHONDONTWRITEBYTECODE=1
# Check if SCRIPTS variable is set
if [ -z "$SCRIPTS" ]; then
echo "Error: SCRIPTS environment variable is not set."
echo "Please set the SCRIPTS variable to the path of your script collection."
exit 1
fi
cd "$SCRIPTS" || exit
python_path="$1"
rspec_path="$2"
# Initialize failure counters and total test counters
python_failures=0
ruby_failures=0
python_tests=0
ruby_tests=0
python_scripts=0
ruby_scripts=0
total_tests=0
total_scripts=0
# Function to extract test count from Python test result
extract_python_test_count() {
local result="$1"
local regex='Ran ([0-9]+) tests?'
if [[ $result =~ $regex ]]; then
python_tests="${BASH_REMATCH[1]}"
else
python_tests=0
fi
}
# Function to extract test count from Ruby test result
extract_ruby_test_count() {
local result="$1"
local regex='([0-9]+) examples?, [0-9]+ failures?'
if [[ $result =~ $regex ]]; then
ruby_tests="${BASH_REMATCH[1]}"
else
ruby_tests=0
fi
}
# Check if Python is installed
if [ -z "$python_path" ]; then
python_path=$(command -v python)
fi
if [ -z "$python_path" ]; then
echo "Python is not installed. Skipping Python tests."
else
if [ ! -x "$python_path" ]; then
echo "Error: Specified Python path is either invalid or not executable."
exit 1
fi
# Display Python path and version
echo "Python path: $python_path"
"$python_path" --version
# Execute Python tests
for file in test/*_test.py; do
echo "Running Python test: $file"
output="$("$python_path" "$file" 2>&1)" # Capture both stdout and stderr
echo "$output"
if ! echo "$output" | grep -qE "OK|SKIPPED|OK \(skipped=[0-9]+\)" ; then
echo "Failure in Python test: $file"
python_failures=$((python_failures + 1))
fi
extract_python_test_count "$output"
python_scripts=$((python_scripts + 1))
total_tests=$((total_tests + python_tests))
done
total_scripts=$((total_scripts + python_scripts))
echo "All Python tests completed. Total Python test scripts: $total_scripts, Total Python test cases: $total_tests"
fi
# Check if RSpec is installed
if [ -z "$rspec_path" ]; then
rspec_path=$(command -v rspec)
fi
if [ -z "$rspec_path" ]; then
echo "RSpec is not installed. Skipping Ruby tests."
else
if [ ! -x "$rspec_path" ]; then
echo "Error: Specified RSpec path is either invalid or not executable."
exit 1
fi
# Display RSpec path and version
echo "RSpec path: $rspec_path"
ruby_dir="$(dirname "$rspec_path")"
ruby_command="$ruby_dir/ruby"
"$ruby_command" --version
"$rspec_path" --version
# Execute Ruby tests
for file in test/*_test.rb; do
echo "Running Ruby test: $file"
output="$("$rspec_path" "$file" 2>&1)" # Capture both stdout and stderr
echo "$output"
if ! echo "$output" | grep -q "0 failures"; then
echo "Failure in Ruby test: $file"
ruby_failures=$((ruby_failures + 1))
fi
extract_ruby_test_count "$output"
ruby_scripts=$((ruby_scripts + 1))
total_tests=$((total_tests + ruby_tests))
done
total_scripts=$((total_scripts + ruby_scripts))
echo "All Ruby tests completed. Total Ruby test scripts: $ruby_scripts, Total Ruby test cases: $ruby_tests"
fi
# Final report
total_failures=$((python_failures + ruby_failures))
if [ "$total_failures" -ne 0 ]; then
echo "Some tests failed. Total failures: $total_failures."
exit 1
else
echo "All tests passed successfully. Total test scripts: $total_scripts, Total test cases: $total_tests"
exit 0
fi