/
bats
executable file
·158 lines (139 loc) · 3.27 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
#!/usr/bin/env bash
set -e
version() {
printf 'Bats 1.1.0\n'
}
usage() {
version
printf 'Usage: bats [-c] [-r] [-p | -t] <test> [<test> ...]\n'
}
abort() {
printf 'Error: %s\n' "$1" >&2
usage >&2
exit 1
}
help() {
local line
usage
while read -r line; do
printf '%s\n' "$line"
done <<END_OF_HELP_TEXT
<test> is the path to a Bats test file, or the path to a directory
containing Bats test files.
-c, --count Count the number of test cases without running any tests
-h, --help Display this help message
-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_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##*/}"
}
export BATS_CWD="$PWD"
export BATS_TEST_PATTERN="^[[:blank:]]*@test[[:blank:]]+(.*[^[:blank:]])[[:blank:]]+\{(.*)\$"
export PATH="$BATS_ROOT/libexec/bats-core:$PATH"
options=()
arguments=()
for arg in "$@"; do
if [[ "${arg:0:1}" = "-" ]]; then
if [[ "${arg:1:1}" = "-" ]]; then
options[${#options[*]}]="${arg:2}"
else
index=1
while option="${arg:$index:1}"; do
if [[ -z "$option" ]]; then
break
fi
options[${#options[*]}]="$option"
let index+=1
done
fi
else
arguments[${#arguments[*]}]="$arg"
fi
done
unset count_flag pretty recursive
count_flag=''
pretty=''
recursive=''
if [[ -z "${CI:-}" && -t 0 && -t 1 ]]; then
pretty=1
fi
if [[ "${#options[@]}" -ne 0 ]]; then
for option in "${options[@]}"; do
case "$option" in
"h" | "help" )
help
exit 0
;;
"v" | "version" )
version
exit 0
;;
"c" | "count" )
count_flag="-c"
;;
"r" | "recursive" )
recursive=1
;;
"t" | "tap" )
pretty=""
;;
"p" | "pretty" )
pretty=1
;;
* )
abort "Bad command line option '-$option'"
;;
esac
done
fi
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["${#filenames[@]}"]="$file"
done < <(find "$filename" -type f -name "*.bats" -print0 | sort -z)
else
for suite_filename in "$filename"/*.bats; do
filenames["${#filenames[@]}"]="$suite_filename"
done
fi
shopt -u nullglob
else
filenames["${#filenames[@]}"]="$filename"
fi
done
if [[ "${#filenames[@]}" -eq 1 ]]; then
command="bats-exec-test"
else
command="bats-exec-suite"
fi
set -o pipefail execfail
if [[ -z "$pretty" ]]; then
exec "$command" $count_flag "${filenames[@]}"
else
extended_syntax_flag="-x"
formatter="bats-format-tap-stream"
exec "$command" $count_flag $extended_syntax_flag "${filenames[@]}" |
"$formatter"
fi