File: bats-format-pretty

package info (click to toggle)
bats 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,016 kB
  • sloc: sh: 4,351; makefile: 33; python: 28; xml: 3
file content (348 lines) | stat: -rwxr-xr-x 6,507 bytes parent folder | download | duplicates (2)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env bash
set -e

# shellcheck source=lib/bats-core/formatter.bash
source "$BATS_ROOT/$BATS_LIBDIR/bats-core/formatter.bash"

BASE_PATH=.
BATS_ENABLE_TIMING=

while [[ "$#" -ne 0 ]]; do
  case "$1" in
  -T)
    BATS_ENABLE_TIMING="-T"
    ;;
  --base-path)
    shift
    normalize_base_path BASE_PATH "$1"
    ;;
  esac
  shift
done

update_count_column_width() {
  count_column_width=$((${#count} * 2 + 2))
  if [[ -n "$BATS_ENABLE_TIMING" ]]; then
    # additional space for ' in %s sec'
    count_column_width=$((count_column_width + ${#SECONDS} + 8))
  fi
  # also update dependent value
  update_count_column_left
}

update_screen_width() {
  screen_width="$(tput cols)"
  # also update dependent value
  update_count_column_left
}

update_count_column_left() {
  count_column_left=$((screen_width - count_column_width))
}

# avoid unset variables
count=0
screen_width=80
update_count_column_width
update_screen_width
test_result=

trap update_screen_width WINCH

begin() {
  test_result= # reset to avoid carrying over result state from previous test
  line_backoff_count=0
  go_to_column 0
  update_count_column_width
  buffer_with_truncation $((count_column_left - 1)) '   %s' "$name"
  clear_to_end_of_line
  go_to_column $count_column_left
  if [[ -n "$BATS_ENABLE_TIMING" ]]; then
    buffer "%${#count}s/${count} in %s sec" "$index" "$SECONDS"
  else
    buffer "%${#count}s/${count}" "$index"
  fi
  go_to_column 1
}

finish_test() {
  move_up $line_backoff_count
  go_to_column 0
  buffer "$@"
  if [[ -n "${TIMEOUT-}" ]]; then
    set_color 2
    if [[ -n "$BATS_ENABLE_TIMING" ]]; then
      buffer ' [%s (timeout: %s)]' "$TIMING" "$TIMEOUT"
    else
      buffer ' [timeout: %s]' "$TIMEOUT"
    fi
  else
    if [[ -n "$BATS_ENABLE_TIMING" ]]; then
      set_color 2
      buffer ' [%s]' "$TIMING"
    fi
  fi
  advance
  move_down $((line_backoff_count - 1))
}

pass() {
  local TIMING="${1:-}"
  finish_test ' ✓ %s' "$name"
  test_result=pass
}

skip() {
  local reason="$1" TIMING="${2:-}"
  if [[ -n "$reason" ]]; then
    reason=": $reason"
  fi
  finish_test ' - %s (skipped%s)' "$name" "$reason"
  test_result=skip
}

fail() {
  local TIMING="${1:-}"
  set_color 1 bold
  finish_test ' ✗ %s' "$name"
  test_result=fail
}

timeout() {
  local TIMING="${1:-}"
  set_color 3 bold
  TIMEOUT="${2:-}" finish_test ' ✗ %s' "$name"
  test_result=timeout
}

log() {
  case ${test_result} in
  pass)
    clear_color
    ;;
  fail)
    set_color 1
    ;;
  timeout)
    set_color 3
    ;;
  esac
  buffer '   %s\n' "$1"
  clear_color
}

summary() {
  if [ "$failures" -eq 0 ]; then
    set_color 2 bold
  else
    set_color 1 bold
  fi

  buffer '\n%d test' "$count"
  if [[ "$count" -ne 1 ]]; then
    buffer 's'
  fi

  buffer ', %d failure' "$failures"
  if [[ "$failures" -ne 1 ]]; then
    buffer 's'
  fi

  if [[ "$skipped" -gt 0 ]]; then
    buffer ', %d skipped' "$skipped"
  fi

  if ((timed_out > 0)); then
    buffer ', %d timed out' "$timed_out"
  fi

  not_run=$((count - passed - failures - skipped - timed_out))
  if [[ "$not_run" -gt 0 ]]; then
    buffer ', %d not run' "$not_run"
  fi

  if [[ -n "$BATS_ENABLE_TIMING" ]]; then
    buffer " in $SECONDS seconds"
  fi

  buffer '\n'
  clear_color
}

buffer_with_truncation() {
  local width="$1"
  shift
  local string

  # shellcheck disable=SC2059
  printf -v 'string' -- "$@"

  if [[ "${#string}" -gt "$width" ]]; then
    buffer '%s...' "${string:0:$((width - 4))}"
  else
    buffer '%s' "$string"
  fi
}

move_up() {
  if [[ $1 -gt 0 ]]; then # avoid moving if we got 0
    buffer '\x1B[%dA' "$1"
  fi
}

move_down() {
  if [[ $1 -gt 0 ]]; then # avoid moving if we got 0
    buffer '\x1B[%dB' "$1"
  fi
}

go_to_column() {
  local column="$1"
  buffer '\x1B[%dG' $((column + 1))
}

clear_to_end_of_line() {
  buffer '\x1B[K'
}

advance() {
  clear_to_end_of_line
  buffer '\n'
  clear_color
}

set_color() {
  local color="$1"
  local weight=22

  if [[ "${2:-}" == 'bold' ]]; then
    weight=1
  fi
  buffer '\x1B[%d;%dm' "$((30 + color))" "$weight"
}

clear_color() {
  buffer '\x1B[0m'
}

_buffer=

buffer() {
  local content
  # shellcheck disable=SC2059
  printf -v content -- "$@"
  _buffer+="$content"
}

prefix_buffer_with() {
  local old_buffer="$_buffer"
  _buffer=''
  "$@"
  _buffer="$_buffer$old_buffer"
}

flush() {
  printf '%s' "$_buffer"
  _buffer=
}

finish() {
  flush
  printf '\n'
}

trap finish EXIT
trap '' INT

bats_tap_stream_plan() {
  count="$1"
  index=0
  passed=0
  failures=0
  skipped=0
  timed_out=0
  name=
  update_count_column_width
}

bats_tap_stream_begin() {
  index="$1"
  name="$2"
  begin
  flush
}

bats_tap_stream_ok() {
  index="$1"
  name="$2"
  ((++passed))

  pass "${BATS_FORMATTER_TEST_DURATION:-}"
}

bats_tap_stream_skipped() {
  index="$1"
  name="$2"
  ((++skipped))
  skip "$3" "${BATS_FORMATTER_TEST_DURATION:-}"
}

bats_tap_stream_not_ok() {
  index="$1"
  name="$2"

  if [[ ${BATS_FORMATTER_TEST_TIMEOUT-x} != x ]]; then
    timeout "${BATS_FORMATTER_TEST_DURATION:-}" "${BATS_FORMATTER_TEST_TIMEOUT}s"
    ((++timed_out))
  else
    fail "${BATS_FORMATTER_TEST_DURATION:-}"
    ((++failures))
  fi

}

bats_tap_stream_comment() { # <comment> <scope>
  local scope=$2
  # count the lines we printed after the begin text,
  if [[ $line_backoff_count -eq 0 && $scope == begin ]]; then
    # if this is the first line after begin, go down one line
    buffer "\n"
    ((++line_backoff_count)) # prefix-increment to avoid "error" due to returning 0
  fi

  ((++line_backoff_count))
  ((line_backoff_count += ${#1} / screen_width)) # account for linebreaks due to length
  log "$1"
}

bats_tap_stream_suite() {
  #test_file="$1"
  line_backoff_count=0
  index=
  # indicate filename for failures
  local file_name="${1#"$BASE_PATH"}"
  name="File $file_name"
  set_color 4 bold
  buffer "%s\n" "$file_name"
  clear_color
}

line_backoff_count=0
bats_tap_stream_unknown() { # <full line> <scope>
  local scope=$2
  # count the lines we printed after the begin text, (or after suite, in case of syntax errors)
  if [[ $line_backoff_count -eq 0 && ($scope == begin || $scope == suite) ]]; then
    # if this is the first line after begin, go down one line
    buffer "\n"
    ((++line_backoff_count)) # prefix-increment to avoid "error" due to returning 0
  fi

  ((++line_backoff_count))
  ((line_backoff_count += ${#1} / screen_width)) # account for linebreaks due to length
  buffer "%s\n" "$1"
  flush
}

bats_parse_internal_extended_tap

summary