-
Notifications
You must be signed in to change notification settings - Fork 409
/
bats
executable file
·171 lines (148 loc) · 3.43 KB
/
bats
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env bash
set -e
export BATS_VERSION='1.1.0'
version() {
printf 'Bats %s\n' "$BATS_VERSION"
}
abort() {
printf 'Error: %s\n' "$1" >&2
usage >&2
exit 1
}
usage() {
local cmd="${0##*/}"
local line
while IFS= read -r line; do
printf '%s\n' "$line"
done <<END_OF_HELP_TEXT
Usage: $cmd [-cr] [-f <regex>] [-j <jobs>] [-p | -t] <test>...
$cmd [-h | -v]
<test> is the path to a Bats test file, or the path to a directory
containing Bats test files (ending with ".bats").
-c, --count Count the number of test cases without running any tests
-f, --filter Filter test cases by names matching the regular expression
-h, --help Display this help message
-j, --jobs Number of parallel jobs to run (requires GNU parallel)
-p, --pretty Show results in pretty format (default for terminals)
-r, --recursive Include tests in subdirectories
-t, --tap Show results in TAP format
-v, --version Display the version number
For more information, see https://github.com/bats-core/bats-core
END_OF_HELP_TEXT
}
expand_link() {
readlink="$(type -p greadlink readlink | head -1)"
"$readlink" -f "$1"
}
expand_path() {
local path="${1%/}"
local dirname="${path%/*}"
local result="$2"
if [[ "$dirname" == "$path" ]]; then
dirname="$PWD"
else
cd "$dirname"
dirname="$PWD"
cd "$OLDPWD"
fi
printf -v "$result" '%s/%s' "$dirname" "${path##*/}"
}
BATS_LIBEXEC="$(dirname "$(expand_link "$BASH_SOURCE")")"
export BATS_CWD="$PWD"
export BATS_TEST_PATTERN="^[[:blank:]]*@test[[:blank:]]+(.*[^[:blank:]])[[:blank:]]+\{(.*)\$"
export BATS_TEST_FILTER=
export PATH="$BATS_LIBEXEC:$PATH"
arguments=()
# Unpack single-character options bundled together, e.g. -cr, -pr.
for arg in "$@"; do
if [[ "$arg" =~ ^-[^-]. ]]; then
index=1
while option="${arg:$((index++)):1}"; do
if [[ -z "$option" ]]; then
break
fi
arguments+=("-$option")
done
else
arguments+=("$arg")
fi
shift
done
set -- "${arguments[@]}"
arguments=()
unset flags pretty recursive
flags=()
pretty=
recursive=
if [[ -z "${CI:-}" && -t 0 && -t 1 ]] && command -v tput >/dev/null; then
pretty=1
fi
while [[ "$#" -ne 0 ]]; do
case "$1" in
-h|--help)
version
usage
exit 0
;;
-v|--version)
version
exit 0
;;
-c|--count)
flags+=('-c')
;;
-f|--filter)
shift
flags+=('-f' "$1")
;;
-j|--jobs)
shift
flags+=('-j' "$1")
;;
-r|--recursive)
recursive=1
;;
-t|--tap)
pretty=
;;
-p|--pretty)
pretty=1
;;
-*)
abort "Bad command line option '$1'"
;;
*)
arguments+=("$1")
;;
esac
shift
done
if [[ "${#arguments[@]}" -eq 0 ]]; then
abort 'Must specify at least one <test>'
fi
filenames=()
for filename in "${arguments[@]}"; do
expand_path "$filename" 'filename'
if [[ -d "$filename" ]]; then
shopt -s nullglob
if [[ "$recursive" -eq 1 ]]; then
while IFS= read -r -d $'\0' file; do
filenames+=("$file")
done < <(find "$filename" -type f -name '*.bats' -print0 | sort -z)
else
for suite_filename in "$filename"/*.bats; do
filenames+=("$suite_filename")
done
fi
shopt -u nullglob
else
filenames+=("$filename")
fi
done
formatter="cat"
if [[ -n "$pretty" ]]; then
flags+=("-x")
formatter="bats-format-tap-stream"
fi
set -o pipefail execfail
exec bats-exec-suite "${flags[@]}" "${filenames[@]}" | "$formatter"