@bats-hardened/bats 1.14.1-alpha02

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of @bats-hardened/bats might be problematic. Click here for more details.

@@ -0,0 +1,360 @@
1
+ #!/usr/bin/env bash
2
+ set -e
3
+
4
+ # shellcheck source=lib/bats-core/formatter.bash
5
+ source "$BATS_ROOT/$BATS_LIBDIR/bats-core/formatter.bash"
6
+
7
+ BASE_PATH=.
8
+ BATS_ENABLE_TIMING=
9
+ BATS_PRETTY_FORMAT_INTERACTIVE=
10
+
11
+ while [[ "$#" -ne 0 ]]; do
12
+ case "$1" in
13
+ -T)
14
+ BATS_ENABLE_TIMING="-T"
15
+ ;;
16
+ --base-path)
17
+ shift
18
+ normalize_base_path BASE_PATH "$1"
19
+ ;;
20
+ --interactive)
21
+ BATS_PRETTY_FORMAT_INTERACTIVE=1
22
+ ;;
23
+ esac
24
+ shift
25
+ done
26
+
27
+ update_count_column_width() {
28
+ count_column_width=$((${#count} * 2 + 2))
29
+ if [[ -n "$BATS_ENABLE_TIMING" ]]; then
30
+ # additional space for ' in %s sec'
31
+ count_column_width=$((count_column_width + ${#SECONDS} + 8))
32
+ fi
33
+ # also update dependent value
34
+ update_count_column_left
35
+ }
36
+
37
+ update_screen_width() {
38
+ screen_width="$(tput cols)"
39
+ # also update dependent value
40
+ update_count_column_left
41
+ }
42
+
43
+ update_count_column_left() {
44
+ count_column_left=$((screen_width - count_column_width))
45
+ }
46
+
47
+ # avoid unset variables
48
+ count=0
49
+ screen_width=80
50
+ update_count_column_width
51
+ update_screen_width
52
+ test_result=
53
+
54
+ trap update_screen_width WINCH
55
+
56
+ begin() {
57
+ if [[ -z "$BATS_PRETTY_FORMAT_INTERACTIVE" ]]; then
58
+ # don't print on begin in non-interactive mode
59
+ return
60
+ fi
61
+ test_result= # reset to avoid carrying over result state from previous test
62
+ line_backoff_count=0
63
+ go_to_column 0
64
+ update_count_column_width
65
+ buffer_with_truncation $((count_column_left - 1)) ' %s' "$name"
66
+ clear_to_end_of_line
67
+ go_to_column $count_column_left
68
+ if [[ -n "$BATS_ENABLE_TIMING" ]]; then
69
+ buffer "%${#count}s/${count} in %s sec" "$index" "$SECONDS"
70
+ else
71
+ buffer "%${#count}s/${count}" "$index"
72
+ fi
73
+ go_to_column 1
74
+ }
75
+
76
+ finish_test() {
77
+ if [[ -n "${BATS_PRETTY_FORMAT_INTERACTIVE-}" ]]; then
78
+ move_up $line_backoff_count
79
+ go_to_column 0
80
+ fi
81
+ buffer "$@"
82
+ if [[ -n "${TIMEOUT-}" ]]; then
83
+ set_color 2
84
+ if [[ -n "$BATS_ENABLE_TIMING" ]]; then
85
+ buffer ' [%s (timeout: %s)]' "$TIMING" "$TIMEOUT"
86
+ else
87
+ buffer ' [timeout: %s]' "$TIMEOUT"
88
+ fi
89
+ else
90
+ if [[ -n "$BATS_ENABLE_TIMING" ]]; then
91
+ set_color 2
92
+ buffer ' [%s]' "$TIMING"
93
+ fi
94
+ fi
95
+ advance
96
+ if [[ -n "${BATS_PRETTY_FORMAT_INTERACTIVE-}" ]]; then
97
+ move_down $((line_backoff_count - 1))
98
+ fi
99
+ }
100
+
101
+ pass() {
102
+ local TIMING="${1:-}"
103
+ finish_test ' ✓ %s' "$name"
104
+ test_result=pass
105
+ }
106
+
107
+ skip() {
108
+ local reason="$1" TIMING="${2:-}"
109
+ if [[ -n "$reason" ]]; then
110
+ reason=": $reason"
111
+ fi
112
+ finish_test ' - %s (skipped%s)' "$name" "$reason"
113
+ test_result=skip
114
+ }
115
+
116
+ fail() {
117
+ local TIMING="${1:-}"
118
+ set_color 1 bold
119
+ finish_test ' ✗ %s' "$name"
120
+ test_result=fail
121
+ }
122
+
123
+ timeout() {
124
+ local TIMING="${1:-}"
125
+ set_color 3 bold
126
+ TIMEOUT="${2:-}" finish_test ' ✗ %s' "$name"
127
+ test_result=timeout
128
+ }
129
+
130
+ log() {
131
+ case ${test_result} in
132
+ pass)
133
+ clear_color
134
+ ;;
135
+ fail)
136
+ set_color 1
137
+ ;;
138
+ timeout)
139
+ set_color 3
140
+ ;;
141
+ esac
142
+ buffer ' %s\n' "$1"
143
+ clear_color
144
+ }
145
+
146
+ summary() {
147
+ if [ "$failures" -eq 0 ]; then
148
+ set_color 2 bold
149
+ else
150
+ set_color 1 bold
151
+ fi
152
+
153
+ buffer '\n%d test' "$count"
154
+ if [[ "$count" -ne 1 ]]; then
155
+ buffer 's'
156
+ fi
157
+
158
+ buffer ', %d failure' "$failures"
159
+ if [[ "$failures" -ne 1 ]]; then
160
+ buffer 's'
161
+ fi
162
+
163
+ if [[ "$skipped" -gt 0 ]]; then
164
+ buffer ', %d skipped' "$skipped"
165
+ fi
166
+
167
+ if ((timed_out > 0)); then
168
+ buffer ', %d timed out' "$timed_out"
169
+ fi
170
+
171
+ not_run=$((count - passed - failures - skipped - timed_out))
172
+ if [[ "$not_run" -gt 0 ]]; then
173
+ buffer ', %d not run' "$not_run"
174
+ fi
175
+
176
+ if [[ -n "$BATS_ENABLE_TIMING" ]]; then
177
+ buffer " in $SECONDS seconds"
178
+ fi
179
+
180
+ buffer '\n'
181
+ clear_color
182
+ }
183
+
184
+ buffer_with_truncation() {
185
+ local width="$1"
186
+ shift
187
+ local string
188
+
189
+ # shellcheck disable=SC2059
190
+ printf -v 'string' -- "$@"
191
+
192
+ if [[ "${#string}" -gt "$width" ]]; then
193
+ buffer '%s...' "${string:0:$((width - 4))}"
194
+ else
195
+ buffer '%s' "$string"
196
+ fi
197
+ }
198
+
199
+ move_up() {
200
+ if [[ $1 -gt 0 ]]; then # avoid moving if we got 0
201
+ buffer '\x1B[%dA' "$1"
202
+ fi
203
+ }
204
+
205
+ move_down() {
206
+ if [[ $1 -gt 0 ]]; then # avoid moving if we got 0
207
+ buffer '\x1B[%dB' "$1"
208
+ fi
209
+ }
210
+
211
+ go_to_column() {
212
+ local column="$1"
213
+ buffer '\x1B[%dG' $((column + 1))
214
+ }
215
+
216
+ clear_to_end_of_line() {
217
+ buffer '\x1B[K'
218
+ }
219
+
220
+ advance() {
221
+ clear_to_end_of_line
222
+ buffer '\n'
223
+ clear_color
224
+ }
225
+
226
+ set_color() {
227
+ local color="$1"
228
+ local weight=22
229
+
230
+ if [[ "${2:-}" == 'bold' ]]; then
231
+ weight=1
232
+ fi
233
+ buffer '\x1B[%d;%dm' "$((30 + color))" "$weight"
234
+ }
235
+
236
+ clear_color() {
237
+ buffer '\x1B[0m'
238
+ }
239
+
240
+ _buffer=
241
+
242
+ buffer() {
243
+ local content
244
+ # shellcheck disable=SC2059
245
+ printf -v content -- "$@"
246
+ _buffer+="$content"
247
+ }
248
+
249
+ prefix_buffer_with() {
250
+ local old_buffer="$_buffer"
251
+ _buffer=''
252
+ "$@"
253
+ _buffer="$_buffer$old_buffer"
254
+ }
255
+
256
+ flush() {
257
+ printf '%s' "$_buffer"
258
+ _buffer=
259
+ }
260
+
261
+ finish() {
262
+ flush
263
+ printf '\n'
264
+ }
265
+
266
+ trap finish EXIT
267
+ trap '' INT
268
+
269
+ bats_tap_stream_plan() {
270
+ count="$1"
271
+ index=0
272
+ passed=0
273
+ failures=0
274
+ skipped=0
275
+ timed_out=0
276
+ name=
277
+ update_count_column_width
278
+ }
279
+
280
+ bats_tap_stream_begin() {
281
+ index="$1"
282
+ name="$2"
283
+ begin
284
+ flush
285
+ }
286
+
287
+ bats_tap_stream_ok() {
288
+ index="$1"
289
+ name="$2"
290
+ ((++passed))
291
+
292
+ pass "${BATS_FORMATTER_TEST_DURATION:-}"
293
+ }
294
+
295
+ bats_tap_stream_skipped() {
296
+ index="$1"
297
+ name="$2"
298
+ ((++skipped))
299
+ skip "$3" "${BATS_FORMATTER_TEST_DURATION:-}"
300
+ }
301
+
302
+ bats_tap_stream_not_ok() {
303
+ index="$1"
304
+ name="$2"
305
+
306
+ if [[ ${BATS_FORMATTER_TEST_TIMEOUT-x} != x ]]; then
307
+ timeout "${BATS_FORMATTER_TEST_DURATION:-}" "${BATS_FORMATTER_TEST_TIMEOUT}s"
308
+ ((++timed_out))
309
+ else
310
+ fail "${BATS_FORMATTER_TEST_DURATION:-}"
311
+ ((++failures))
312
+ fi
313
+
314
+ }
315
+
316
+ bats_tap_stream_comment() { # <comment> <scope>
317
+ local scope=$2
318
+ # count the lines we printed after the begin text,
319
+ if [[ $line_backoff_count -eq 0 && $scope == begin ]]; then
320
+ # if this is the first line after begin, go down one line
321
+ buffer "\n"
322
+ ((++line_backoff_count)) # prefix-increment to avoid "error" due to returning 0
323
+ fi
324
+
325
+ ((++line_backoff_count))
326
+ ((line_backoff_count += ${#1} / screen_width)) # account for linebreaks due to length
327
+ log "$1"
328
+ }
329
+
330
+ bats_tap_stream_suite() {
331
+ #test_file="$1"
332
+ line_backoff_count=0
333
+ index=
334
+ # indicate filename for failures
335
+ local file_name="${1#"$BASE_PATH"}"
336
+ name="File $file_name"
337
+ set_color 4 bold
338
+ buffer "%s\n" "$file_name"
339
+ clear_color
340
+ }
341
+
342
+ line_backoff_count=0
343
+ bats_tap_stream_unknown() { # <full line> <scope>
344
+ local scope=$2
345
+ # count the lines we printed after the begin text, (or after suite, in case of syntax errors)
346
+ if [[ $line_backoff_count -eq 0 && ($scope == begin || $scope == suite) ]]; then
347
+ # if this is the first line after begin, go down one line
348
+ buffer "\n"
349
+ ((++line_backoff_count)) # prefix-increment to avoid "error" due to returning 0
350
+ fi
351
+
352
+ ((++line_backoff_count))
353
+ ((line_backoff_count += ${#1} / screen_width)) # account for linebreaks due to length
354
+ buffer "%s\n" "$1"
355
+ flush
356
+ }
357
+
358
+ bats_parse_internal_extended_tap
359
+
360
+ summary
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env bash
2
+ set -e
3
+ trap '' INT
4
+
5
+ # shellcheck source=lib/bats-core/formatter.bash
6
+ source "$BATS_ROOT/$BATS_LIBDIR/bats-core/formatter.bash"
7
+
8
+ bats_tap_stream_plan() {
9
+ printf "1..%d\n" "$1"
10
+ }
11
+
12
+ bats_tap_stream_begin() { #<test index> <test name>
13
+ :
14
+ }
15
+
16
+ bats_tap_stream_ok() { # [<test index> <test name>
17
+ printf "ok %d %s" "$1" "$2"
18
+ if [[ "${BATS_FORMATTER_TEST_DURATION-x}" != x ]]; then
19
+ printf " # in %d ms" "$BATS_FORMATTER_TEST_DURATION"
20
+ fi
21
+ printf "\n"
22
+ }
23
+
24
+ bats_tap_stream_not_ok() { # <test index> <test name>
25
+ printf "not ok %d %s" "$1" "$2"
26
+ if [[ "${BATS_FORMATTER_TEST_DURATION-x}" != x ]]; then
27
+ printf " # in %d ms" "$BATS_FORMATTER_TEST_DURATION"
28
+ fi
29
+ if [[ "${BATS_FORMATTER_TEST_TIMEOUT-x}" != x ]]; then
30
+ printf " # timeout after %d s" "${BATS_FORMATTER_TEST_TIMEOUT}"
31
+ fi
32
+ printf "\n"
33
+ }
34
+
35
+ bats_tap_stream_skipped() { # <test index> <test name> <reason>
36
+ if [[ $# -eq 3 ]]; then
37
+ printf "ok %d %s # skip %s\n" "$1" "$2" "$3"
38
+ else
39
+ printf "ok %d %s # skip\n" "$1" "$2"
40
+ fi
41
+ }
42
+
43
+ bats_tap_stream_comment() { # <comment text without leading '# '>
44
+ printf "# %s\n" "$1"
45
+ }
46
+
47
+ bats_tap_stream_suite() { # <file name>
48
+ :
49
+ }
50
+
51
+ bats_tap_stream_unknown() { # <full line>
52
+ printf "%s\n" "$1"
53
+ }
54
+
55
+ bats_parse_internal_extended_tap
@@ -0,0 +1,89 @@
1
+ #!/usr/bin/env bash
2
+ set -e
3
+
4
+ yaml_block_open=''
5
+ add_yaml_entry() {
6
+ if [[ -z "$yaml_block_open" ]]; then
7
+ printf " ---\n"
8
+ fi
9
+ printf " %s: %s\n" "$1" "$2"
10
+ yaml_block_open=1
11
+ }
12
+
13
+ close_previous_yaml_block() {
14
+ if [[ -n "$yaml_block_open" ]]; then
15
+ printf " ...\n"
16
+ yaml_block_open=''
17
+ fi
18
+ }
19
+
20
+ trap '' INT
21
+
22
+ number_of_printed_log_lines_for_this_test_so_far=0
23
+
24
+ # shellcheck source=lib/bats-core/formatter.bash
25
+ source "$BATS_ROOT/$BATS_LIBDIR/bats-core/formatter.bash"
26
+
27
+ bats_tap_stream_plan() {
28
+ printf "TAP version 13\n"
29
+ printf "1..%d\n" "$1"
30
+ }
31
+
32
+ bats_tap_stream_begin() { #<test index> <test name>
33
+ :
34
+ }
35
+
36
+ bats_tap_stream_ok() { # <test index> <test name>
37
+ close_previous_yaml_block
38
+ number_of_printed_log_lines_for_this_test_so_far=0
39
+ printf "ok %d %s\n" "$1" "$2"
40
+ if [[ "${BATS_FORMATTER_TEST_DURATION-x}" != x ]]; then
41
+ add_yaml_entry "duration_ms" "${BATS_FORMATTER_TEST_DURATION}"
42
+ fi
43
+ }
44
+
45
+ pass_on_optional_data() {
46
+ if [[ "${BATS_FORMATTER_TEST_DURATION-x}" != x ]]; then
47
+ add_yaml_entry "duration_ms" "${BATS_FORMATTER_TEST_DURATION}"
48
+ fi
49
+ if [[ "${BATS_FORMATTER_TEST_TIMEOUT-x}" != x ]]; then
50
+ add_yaml_entry "timeout_sec" "${BATS_FORMATTER_TEST_TIMEOUT}"
51
+ fi
52
+ }
53
+
54
+ bats_tap_stream_not_ok() { # <test index> <test name>
55
+ close_previous_yaml_block
56
+ number_of_printed_log_lines_for_this_test_so_far=0
57
+
58
+ printf "not ok %d %s\n" "$1" "$2"
59
+ pass_on_optional_data
60
+ }
61
+
62
+ bats_tap_stream_skipped() { # <test index> <test name> <reason>
63
+ close_previous_yaml_block
64
+ number_of_printed_log_lines_for_this_test_so_far=0
65
+
66
+ printf "not ok %d %s # SKIP %s\n" "$1" "$2" "$3"
67
+ pass_on_optional_data
68
+ }
69
+
70
+ bats_tap_stream_comment() { # <comment text without leading '# '>
71
+ if [[ $number_of_printed_log_lines_for_this_test_so_far -eq 0 ]]; then
72
+ add_yaml_entry "message" "|" # use a multiline string for this entry
73
+ fi
74
+ ((++number_of_printed_log_lines_for_this_test_so_far))
75
+ printf " %s\n" "$1"
76
+ }
77
+
78
+ bats_tap_stream_suite() { # <file name>
79
+ :
80
+ }
81
+
82
+ bats_tap_stream_unknown() { # <full line>
83
+ :
84
+ }
85
+
86
+ bats_parse_internal_extended_tap
87
+
88
+ # close the final block if there was one
89
+ close_previous_yaml_block