-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.sh
More file actions
122 lines (108 loc) · 3.16 KB
/
run_tests.sh
File metadata and controls
122 lines (108 loc) · 3.16 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
#!/bin/bash
# Test script for running examples and checking for errors
# Function to run a command and check for errors
run_test() {
local cmd="$1"
echo "Running: $cmd"
case "$output_mode" in
all)
eval "$cmd"
;;
failed)
eval "$cmd" > /dev/null 2>&1
;;
none)
eval "$cmd" > /dev/null 2>&1
;;
*)
echo "Invalid output mode: $output_mode"
exit 1
;;
esac
# Check the exit status of the command
if [ $? -ne 0 ]; then
echo "Test FAILED: $cmd" >> test_results.log
echo "Error encountered while running: $cmd"
failed_commands+=("$cmd") # Add the failed command to the list
if [ "$output_mode" == "failed" ]; then
echo "Output of failed test:"
eval "$cmd"
fi
else
echo "Test PASSED: $cmd" >> test_results.log
fi
}
# Clear the log file
> test_results.log
# Initialize an array to store failed commands
failed_commands=()
# Default output mode
output_mode="all"
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--output-mode)
output_mode="$2"
shift 2
;;
*)
input="$1"
shift
;;
esac
done
# Validate output mode
if [[ "$output_mode" != "all" && "$output_mode" != "failed" && "$output_mode" != "none" ]]; then
echo "Invalid output mode: $output_mode"
echo "Valid modes are: all, failed, none"
exit 1
fi
# Check if input is provided
if [ -z "$input" ]; then
echo "Usage: $0 [--output-mode <all|failed|none>] <commands_file_or_folder>"
exit 1
fi
# Check if the input is a file or a folder
if [ -f "$input" ]; then
# Input is a single file
echo "Running tests from file: $input"
while IFS= read -r cmd; do
# Skip empty lines or comments
[[ -z "$cmd" || "$cmd" =~ ^# ]] && continue
run_test "$cmd"
done < "$input"
elif [ -d "$input" ]; then
# Input is a folder
echo "Running tests from all .txt files in folder: $input"
for file in "$input"/*.txt; do
echo "Processing file: $file"
while IFS= read -r cmd; do
# Skip empty lines or comments
[[ -z "$cmd" || "$cmd" =~ ^# ]] && continue
run_test "$cmd"
done < "$file"
done
# If there are failed commands, write them to a new file
if [ ${#failed_commands[@]} -gt 0 ]; then
failed_tests_file="$input/failed_tests.txt"
echo "Writing failed commands to $failed_tests_file"
> "$failed_tests_file" # Clear or create the file
for failed_cmd in "${failed_commands[@]}"; do
echo "$failed_cmd" >> "$failed_tests_file"
done
echo "You can rerun the failed tests using: bash run_tests.sh $failed_tests_file"
fi
else
echo "Error: '$input' is neither a file nor a folder."
exit 1
fi
# Print summary
if [ ${#failed_commands[@]} -eq 0 ]; then
echo "All tests PASSED!"
else
echo "The following tests FAILED:"
for failed_cmd in "${failed_commands[@]}"; do
echo "$failed_cmd"
done
fi
echo "Test results logged in test_results.log"