-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_tests.sh
executable file
·61 lines (52 loc) · 1.39 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
#!/bin/bash
# Default values for flags
VERBOSE=false
COVERAGE=false
PACKAGE=""
RACE=false
TIMEOUT=""
# Help message
usage() {
echo "Usage: $0 [options]"
echo "Options:"
echo " -v Enable verbose output for each test"
echo " -c Generate a coverage report"
echo " -p <package> Specify package to test (default is all packages)"
echo " -r Enable race condition detection"
echo " -t <time> Set a custom timeout (e.g., 2m, 1h)"
echo " -h Display this help message"
}
# Parse command-line options
while getopts "vcp:rt:h" opt; do
case ${opt} in
v ) VERBOSE=true ;;
c ) COVERAGE=true ;;
p ) PACKAGE=$OPTARG ;;
r ) RACE=true ;;
t ) TIMEOUT=$OPTARG ;;
h ) usage; exit 0 ;;
* ) usage; exit 1 ;;
esac
done
# Base command
CMD="go test"
# Add flags based on options
[ "$VERBOSE" = true ] && CMD+=" -v"
[ "$RACE" = true ] && CMD+=" -race"
[ -n "$TIMEOUT" ] && CMD+=" -timeout $TIMEOUT"
[ -n "$PACKAGE" ] && CMD+=" $PACKAGE" || CMD+=" ./..."
# Coverage option
if [ "$COVERAGE" = true ]; then
COVERAGE_FILE="coverage.out"
CMD+=" -coverprofile=$COVERAGE_FILE"
fi
# Run the command
echo "Running command: $CMD"
$CMD
# Show coverage report if generated
if [ "$COVERAGE" = true ]; then
echo "Coverage report:"
go tool cover -func=$COVERAGE_FILE
# Optional: Display HTML report
# go tool cover -html=$COVERAGE_FILE
fi