| 1 | #!/usr/bin/env bash |
| 2 | set -e |
| 3 | |
| 4 | # Just stream the TAP output (sans extended syntax) if tput is missing |
| 5 | command -v tput >/dev/null || exec grep -v "^begin " |
| 6 | |
| 7 | header_pattern='[0-9]+\.\.[0-9]+' |
| 8 | IFS= read -r header |
| 9 | |
| 10 | if [[ "$header" =~ $header_pattern ]]; then |
| 11 | count="${header:3}" |
| 12 | index=0 |
| 13 | failures=0 |
| 14 | skipped=0 |
| 15 | name="" |
| 16 | count_column_width=$(( ${#count} * 2 + 2 )) |
| 17 | else |
| 18 | # If the first line isn't a TAP plan, print it and pass the rest through |
| 19 | printf "%s\n" "$header" |
| 20 | exec cat |
| 21 | fi |
| 22 | |
| 23 | update_screen_width() { |
| 24 | screen_width="$(tput cols)" |
| 25 | count_column_left=$(( $screen_width - $count_column_width )) |
| 26 | } |
| 27 | |
| 28 | trap update_screen_width WINCH |
| 29 | update_screen_width |
| 30 | |
| 31 | begin() { |
| 32 | go_to_column 0 |
| 33 | printf_with_truncation $(( $count_column_left - 1 )) " %s" "$name" |
| 34 | clear_to_end_of_line |
| 35 | go_to_column $count_column_left |
| 36 | printf "%${#count}s/${count}" "$index" |
| 37 | go_to_column 1 |
| 38 | } |
| 39 | |
| 40 | pass() { |
| 41 | go_to_column 0 |
| 42 | printf " ✓ %s" "$name" |
| 43 | advance |
| 44 | } |
| 45 | |
| 46 | skip() { |
| 47 | local reason="$1" |
| 48 | [ -z "$reason" ] || reason=": $reason" |
| 49 | go_to_column 0 |
| 50 | printf " - %s (skipped%s)" "$name" "$reason" |
| 51 | advance |
| 52 | } |
| 53 | |
| 54 | fail() { |
| 55 | go_to_column 0 |
| 56 | set_color 1 bold |
| 57 | printf " ✗ %s" "$name" |
| 58 | advance |
| 59 | } |
| 60 | |
| 61 | log() { |
| 62 | set_color 1 |
| 63 | printf " %s\n" "$1" |
| 64 | clear_color |
| 65 | } |
| 66 | |
| 67 | summary() { |
| 68 | printf "\n%d test%s" "$count" "$(plural "$count")" |
| 69 | |
| 70 | printf ", %d failure%s" "$failures" "$(plural "$failures")" |
| 71 | |
| 72 | if [ "$skipped" -gt 0 ]; then |
| 73 | printf ", %d skipped" "$skipped" |
| 74 | fi |
| 75 | |
| 76 | printf "\n" |
| 77 | } |
| 78 | |
| 79 | printf_with_truncation() { |
| 80 | local width="$1" |
| 81 | shift |
| 82 | local string="$(printf "$@")" |
| 83 | |
| 84 | if [ "${#string}" -gt "$width" ]; then |
| 85 | printf "%s..." "${string:0:$(( $width - 4 ))}" |
| 86 | else |
| 87 | printf "%s" "$string" |
| 88 | fi |
| 89 | } |
| 90 | |
| 91 | go_to_column() { |
| 92 | local column="$1" |
| 93 | printf "\x1B[%dG" $(( $column + 1 )) |
| 94 | } |
| 95 | |
| 96 | clear_to_end_of_line() { |
| 97 | printf "\x1B[K" |
| 98 | } |
| 99 | |
| 100 | advance() { |
| 101 | clear_to_end_of_line |
| 102 | echo |
| 103 | clear_color |
| 104 | } |
| 105 | |
| 106 | set_color() { |
| 107 | local color="$1" |
| 108 | local weight="$2" |
| 109 | printf "\x1B[%d;%dm" $(( 30 + $color )) "$( [ "$weight" = "bold" ] && echo 1 || echo 22 )" |
| 110 | } |
| 111 | |
| 112 | clear_color() { |
| 113 | printf "\x1B[0m" |
| 114 | } |
| 115 | |
| 116 | plural() { |
| 117 | [ "$1" -eq 1 ] || echo "s" |
| 118 | } |
| 119 | |
| 120 | _buffer="" |
| 121 | |
| 122 | buffer() { |
| 123 | _buffer="${_buffer}$("$@")" |
| 124 | } |
| 125 | |
| 126 | flush() { |
| 127 | printf "%s" "$_buffer" |
| 128 | _buffer="" |
| 129 | } |
| 130 | |
| 131 | finish() { |
| 132 | flush |
| 133 | printf "\n" |
| 134 | } |
| 135 | |
| 136 | trap finish EXIT |
| 137 | |
| 138 | while IFS= read -r line; do |
| 139 | case "$line" in |
| 140 | "begin "* ) |
| 141 | let index+=1 |
| 142 | name="${line#* $index }" |
| 143 | buffer begin |
| 144 | flush |
| 145 | ;; |
| 146 | "ok "* ) |
| 147 | skip_expr="ok $index # skip (\(([^)]*)\))?" |
| 148 | if [[ "$line" =~ $skip_expr ]]; then |
| 149 | let skipped+=1 |
| 150 | buffer skip "${BASH_REMATCH[2]}" |
| 151 | else |
| 152 | buffer pass |
| 153 | fi |
| 154 | ;; |
| 155 | "not ok "* ) |
| 156 | let failures+=1 |
| 157 | buffer fail |
| 158 | ;; |
| 159 | "# "* ) |
| 160 | buffer log "${line:2}" |
| 161 | ;; |
| 162 | esac |
| 163 | done |
| 164 | |
| 165 | buffer summary |