-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.sh
More file actions
155 lines (125 loc) · 4.44 KB
/
tests.sh
File metadata and controls
155 lines (125 loc) · 4.44 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
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
#!/usr/bin/env bash
set -euo pipefail
# Directory where test binaries live
TEST_BIN_DIR="./build/tests"
DEFAULT_SLEEP=2
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Extra args to pass to all sub-tests (for Moteus transport/controller)
# NOTE: This can now include a space-separated list of IDs in a single
# quoted argument, e.g. --moteus-id "1 2 3"
MOTEUS_ARGS=()
# Format: "Test Name:binary_name arguments"
tests=(
"Read:read_test --duration-ms 2000"
"Velocity Control:write_test --position nan --velocity 2.0 --duration-ms 5000 --kp-scale 0.1 --kd-scale 0.1"
"Constant Acceleration Trajectory:write_test --position 1.0 --velocity 0.0 --accel-limit 2.0 --velocity-limit 0.5 --duration-ms 5000 --kp-scale 0.1 --kd-scale 0.1"
"Torque Control:write_test --velocity 0.0 --kp-scale 0.0 --kd-scale 0.0 --ilimit-scale 0.0 --feedforward-torque 0.1 --duration-ms 1000"
)
usage() {
cat <<EOF
Usage: $0 [moteus-options...] [pattern]
Run all test binaries from ${TEST_BIN_DIR}.
Moteus options (forwarded to all sub-tests):
--moteus-id "<id1> [id2 ...]" Controller ID(s); space-separated, MUST be quoted
--socketcan-iface <iface> SocketCAN interface (e.g. can0)
--socketcan-ignore-errors <n> Ignore SocketCAN errors flag
--can-disable-brs <n> Disable CAN BRS flag
The --moteus-id value is passed verbatim to the C++ programs, which
will instantiate one controller per ID and run the commands on all of them.
Optional:
pattern Only run tests whose name or binary matches this substring.
Examples:
$0
# run all tests on default moteus-id (from the binaries, typically 1)
$0 torque
# run only torque-related tests on default moteus-id
$0 --moteus-id "1 2 3" --socketcan-iface can0
# all tests on IDs 1,2,3 via can0 (each C++ program will control 3 controllers)
$0 --moteus-id "3 4" torque
# torque tests only, on moteus IDs 3 and 4
$0 --moteus-id "1 2" --socketcan-iface can1 read
# only "Read" test, on IDs 1 and 2 via can1
EOF
}
run_test() {
local name="$1"
local raw_cmd="$2"
# Split the raw command string into an array (e.g., "write" "--velocity" "2.0")
read -r -a cmd_parts <<< "$raw_cmd"
# Extract the binary name (first element) and arguments (the rest)
local bin_name="${cmd_parts[0]}"
local bin_path="${TEST_BIN_DIR}/${bin_name}"
echo
echo -e "${YELLOW}==== TEST START: ${name} (${raw_cmd}) ====${NC}"
# Check if the binary file exists
if [[ ! -x "$bin_path" ]]; then
echo -e "${RED}MISSING/NOT EXECUTABLE:${NC} ${bin_path}"
return 1
fi
# Run the binary path with its arguments + global MOTEUS_ARGS
# NOTE: MOTEUS_ARGS may contain something like: --moteus-id "1 2 3"
# which is seen by the C++ program as a single argument "1 2 3".
if "$bin_path" "${cmd_parts[@]:1}" "${MOTEUS_ARGS[@]}"; then
echo -e "${GREEN}==== EXECUTED : ${name} ====${NC}"
else
echo -e "${RED}==== FAIL : ${name} ====${NC}"
return 1
fi
sleep "${DEFAULT_SLEEP}"
}
main() {
local pattern=""
MOTEUS_ARGS=()
# Parse CLI args: extract moteus options, plus optional pattern
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
--moteus-id|--socketcan-iface|--socketcan-ignore-errors|--can-disable-brs)
if [[ $# -lt 2 ]]; then
echo -e "${RED}Missing value for option:${NC} $1"
exit 1
fi
# Push option name and its value verbatim into MOTEUS_ARGS.
# For --moteus-id, the value may be a quoted string with spaces.
MOTEUS_ARGS+=("$1" "$2")
shift 2
;;
*)
# First non-option is treated as pattern (if provided)
if [[ -z "${pattern}" ]]; then
pattern="$1"
shift
else
echo -e "${RED}Unexpected extra argument:${NC} $1"
echo "Only a single pattern is supported."
exit 1
fi
;;
esac
done
local any_run=0
for entry in "${tests[@]}"; do
# Split the entry by the first colon only
IFS=":" read -r name full_cmd <<< "$entry"
# Pattern filter (optional)
if [[ -n "$pattern" ]] && \
[[ "$name" != *"$pattern"* && "$full_cmd" != *"$pattern"* ]]; then
continue
fi
any_run=1
run_test "$name" "$full_cmd"
done
if [[ "$any_run" -eq 0 ]]; then
echo -e "${RED}No tests matched pattern:${NC} '${pattern}'"
exit 1
fi
echo
echo -e "${GREEN}All selected tests completed.${NC}"
}
main "$@"