forked from driesvints/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun-tests
More file actions
executable file
·102 lines (91 loc) · 3.02 KB
/
Copy pathrun-tests
File metadata and controls
executable file
·102 lines (91 loc) · 3.02 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
#!/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 '')}"
# ---------------------------------------------------------------------------
# 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
# ---------------------------------------------------------------------------
# Separate path args from flags, and detect Ruby spec files
# ---------------------------------------------------------------------------
bats_args=()
ruby_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}")
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_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
# ---------------------------------------------------------------------------
# Exit with combined status
# ---------------------------------------------------------------------------
[[ ${bats_exit} -eq 0 && ${ruby_exit} -eq 0 ]]