mirror of
https://github.com/sstephenson/bats.git
synced 2024-12-27 06:59:45 +01:00
bfa4ebcd0f
The `((x++))` syntax is shorthand for `let x++`. According to `help let`: If the last ARG evaluates to 0, let returns 1; 0 is returned otherwise. Thus the exit status of the expression `x=0; let x++` is 1, since the post-increment `++` operator evaluates to the value of the variable before incrementing. In Bash 4, this non-zero exit status properly triggers `set -e`'s error trap, but in Bash 3 it does not. That's why the tests were passing on OS X (Bash 3) but not Linux (Bash 4). We can work around the problem by choosing an incrementation expression that never evaluates to 0, such as `+=` or the pre-increment `++` operator. For consistency and clarity, I've changed to `x+=1` everywhere. Ref. #25, #27
158 lines
2.5 KiB
Bash
Executable File
158 lines
2.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -e
|
|
|
|
# Just stream the TAP output (sans extended syntax) if tput is missing
|
|
command -v tput >/dev/null || exec grep -v "^begin "
|
|
|
|
header_pattern='[0-9]+\.\.[0-9]+'
|
|
IFS= read -r header
|
|
|
|
if [[ "$header" =~ $header_pattern ]]; then
|
|
count="${header:3}"
|
|
index=0
|
|
failures=0
|
|
name=""
|
|
count_column_width=$(( ${#count} * 2 + 2 ))
|
|
else
|
|
# If the first line isn't a TAP plan, print it and pass the rest through
|
|
printf "%s\n" "$header"
|
|
exec cat
|
|
fi
|
|
|
|
update_screen_width() {
|
|
screen_width="$(tput cols)"
|
|
count_column_left=$(( $screen_width - $count_column_width ))
|
|
}
|
|
|
|
trap update_screen_width WINCH
|
|
update_screen_width
|
|
|
|
begin() {
|
|
go_to_column 0
|
|
printf_with_truncation $(( $count_column_left - 1 )) " %s" "$name"
|
|
clear_to_end_of_line
|
|
go_to_column $count_column_left
|
|
printf "%${#count}s/${count}" "$index"
|
|
go_to_column 1
|
|
}
|
|
|
|
pass() {
|
|
go_to_column 0
|
|
printf " ✓ %s" "$name"
|
|
advance
|
|
}
|
|
|
|
skip() {
|
|
local reason="$1"
|
|
[ -z "$reason" ] || reason=": $reason"
|
|
go_to_column 0
|
|
printf " - %s (skipped%s)" "$name" "$reason"
|
|
advance
|
|
}
|
|
|
|
fail() {
|
|
go_to_column 0
|
|
set_color 1 bold
|
|
printf " ✗ %s" "$name"
|
|
advance
|
|
}
|
|
|
|
log() {
|
|
set_color 1
|
|
printf " %s\n" "$1"
|
|
clear_color
|
|
}
|
|
|
|
summary() {
|
|
printf "\n%d test%s, %d failure%s\n" \
|
|
"$count" "$(plural "$count")" \
|
|
"$failures" "$(plural "$failures")"
|
|
}
|
|
|
|
printf_with_truncation() {
|
|
local width="$1"
|
|
shift
|
|
local string="$(printf "$@")"
|
|
|
|
if [ "${#string}" -gt "$width" ]; then
|
|
printf "%s..." "${string:0:$(( $width - 4 ))}"
|
|
else
|
|
printf "%s" "$string"
|
|
fi
|
|
}
|
|
|
|
go_to_column() {
|
|
local column="$1"
|
|
printf "\x1B[%dG" $(( $column + 1 ))
|
|
}
|
|
|
|
clear_to_end_of_line() {
|
|
printf "\x1B[K"
|
|
}
|
|
|
|
advance() {
|
|
clear_to_end_of_line
|
|
echo
|
|
clear_color
|
|
}
|
|
|
|
set_color() {
|
|
local color="$1"
|
|
local weight="$2"
|
|
printf "\x1B[%d;%dm" $(( 30 + $color )) "$( [ "$weight" = "bold" ] && echo 1 || echo 22 )"
|
|
}
|
|
|
|
clear_color() {
|
|
printf "\x1B[0m"
|
|
}
|
|
|
|
plural() {
|
|
[ "$1" -eq 1 ] || echo "s"
|
|
}
|
|
|
|
_buffer=""
|
|
|
|
buffer() {
|
|
_buffer="${_buffer}$("$@")"
|
|
}
|
|
|
|
flush() {
|
|
printf "%s" "$_buffer"
|
|
_buffer=""
|
|
}
|
|
|
|
finish() {
|
|
flush
|
|
printf "\n"
|
|
}
|
|
|
|
trap finish EXIT
|
|
|
|
while IFS= read -r line; do
|
|
case "$line" in
|
|
"begin "* )
|
|
let index+=1
|
|
name="${line#* $index }"
|
|
buffer begin
|
|
flush
|
|
;;
|
|
"ok "* )
|
|
skip_expr="ok $index # skip (\(([^)]*)\))?"
|
|
if [[ "$line" =~ $skip_expr ]]; then
|
|
buffer skip "${BASH_REMATCH[2]}"
|
|
else
|
|
buffer pass
|
|
fi
|
|
;;
|
|
"not ok "* )
|
|
let failures+=1
|
|
buffer fail
|
|
;;
|
|
"# "* )
|
|
buffer log "${line:2}"
|
|
;;
|
|
esac
|
|
done
|
|
|
|
buffer summary
|