-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun-tests
More file actions
executable file
·137 lines (123 loc) · 4.26 KB
/
Copy pathrun-tests
File metadata and controls
executable file
·137 lines (123 loc) · 4.26 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
#!/usr/bin/env bash
set -euo pipefail
BATS_BIN="${BATS_BIN:-/opt/homebrew/bin/bats}"
RUBY_BIN="${RUBY_BIN:-$(command -v ruby 2>/dev/null || echo '')}"
PYTHON_BIN="${PYTHON_BIN:-$(command -v python3 2>/dev/null || echo '')}"
# ---------------------------------------------------------------------------
# Prerequisite checks
# ---------------------------------------------------------------------------
if ! command -v bats &>/dev/null && [[ ! -x "${BATS_BIN}" ]]; then
echo "Error: bats is not installed or not found." >&2
echo "" >&2
echo "Install it with:" >&2
echo " brew install bats-core" >&2
exit 1
fi
BATS="$(command -v bats 2>/dev/null || echo "${BATS_BIN}")"
if [[ -z "${RUBY_BIN}" ]]; then
echo "Warning: ruby not found — skipping Ruby specs." >&2
fi
if [[ -z "${PYTHON_BIN}" ]]; then
echo "Warning: python3 not found — skipping Python tests." >&2
fi
# ---------------------------------------------------------------------------
# Separate path args from flags, and detect Ruby spec files
# ---------------------------------------------------------------------------
bats_args=()
ruby_files=()
python_files=()
filter_pattern=""
has_path_arg=false
i=0
args=("$@")
while [[ $i -lt ${#args[@]} ]]; do
arg="${args[$i]}"
case "${arg}" in
--filter)
i=$((i + 1))
filter_pattern="${args[$i]:-}"
bats_args+=("--filter" "${filter_pattern}")
;;
--filter=*)
filter_pattern="${arg#*=}"
bats_args+=("${arg}")
;;
--*)
bats_args+=("${arg}")
;;
*)
has_path_arg=true
if [[ "${arg}" == *_spec.rb ]]; then
ruby_files+=("${arg}")
elif [[ "${arg}" == *_test.py ]]; then
python_files+=("${arg}")
else
bats_args+=("${arg}")
fi
;;
esac
i=$((i + 1))
done
# ---------------------------------------------------------------------------
# Run BATS tests
# ---------------------------------------------------------------------------
bats_exit=0
if [[ "${has_path_arg}" == false ]]; then
"${BATS}" --recursive tests/ "${bats_args[@]+"${bats_args[@]}"}" || bats_exit=$?
elif [[ ${#bats_args[@]} -gt 0 ]]; then
"${BATS}" "${bats_args[@]}" || bats_exit=$?
fi
# ---------------------------------------------------------------------------
# Run Ruby specs
# ---------------------------------------------------------------------------
ruby_exit=0
if [[ -n "${RUBY_BIN}" ]]; then
if [[ ${#ruby_files[@]} -gt 0 ]]; then
# Explicit spec files passed on command line
for spec in "${ruby_files[@]}"; do
if [[ -n "${filter_pattern}" ]]; then
"${RUBY_BIN}" "${spec}" --name "/${filter_pattern}/" || ruby_exit=$?
else
"${RUBY_BIN}" "${spec}" || ruby_exit=$?
fi
done
elif [[ "${has_path_arg}" == false ]]; then
# Discovery mode: run all *_spec.rb under tests/
while IFS= read -r -d '' spec; do
if [[ -n "${filter_pattern}" ]]; then
"${RUBY_BIN}" "${spec}" --name "/${filter_pattern}/" || ruby_exit=$?
else
"${RUBY_BIN}" "${spec}" || ruby_exit=$?
fi
done < <(find tests/ -name '*_spec.rb' -print0 | sort -z)
fi
fi
# ---------------------------------------------------------------------------
# Run Python tests
# ---------------------------------------------------------------------------
python_exit=0
if [[ -n "${PYTHON_BIN}" ]]; then
if [[ ${#python_files[@]} -gt 0 ]]; then
# Explicit test files passed on command line
for spec in "${python_files[@]}"; do
if [[ -n "${filter_pattern}" ]]; then
"${PYTHON_BIN}" "${spec}" -k "${filter_pattern}" || python_exit=$?
else
"${PYTHON_BIN}" "${spec}" || python_exit=$?
fi
done
elif [[ "${has_path_arg}" == false ]]; then
# Discovery mode: run all *_test.py under tests/
while IFS= read -r -d '' spec; do
if [[ -n "${filter_pattern}" ]]; then
"${PYTHON_BIN}" "${spec}" -k "${filter_pattern}" || python_exit=$?
else
"${PYTHON_BIN}" "${spec}" || python_exit=$?
fi
done < <(find tests/ -name '*_test.py' -print0 | sort -z)
fi
fi
# ---------------------------------------------------------------------------
# Exit with combined status
# ---------------------------------------------------------------------------
[[ ${bats_exit} -eq 0 && ${ruby_exit} -eq 0 && ${python_exit} -eq 0 ]]