Skip to content

Commit 448b1bc

Browse files
committed
ref: improve refute_line to handle output and input streams
Introduce a new helper function `__refute_stream_line` to streamline the logic and reduce code duplication.
1 parent b80a779 commit 448b1bc

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

src/refute_line.bash

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@
2626
# It checks that the unexpected line does not appear in the output (default) or at a specific line number.
2727
# Matching can be literal (default), partial or regular expression.
2828
#
29-
# *__Warning:__
30-
# Due to a [bug in Bats][bats-93], empty lines are discarded from `${lines[@]}`,
31-
# causing line indices to change and preventing testing for empty lines.*
32-
#
33-
# [bats-93]: https://github.com/sstephenson/bats/pull/93
34-
#
3529
# ## Looking for a line in the output
3630
#
3731
# By default, the entire output is searched for the unexpected line.
@@ -131,18 +125,35 @@
131125
# ```
132126
# FIXME(ztombol): Display `${lines[@]}' instead of `$output'!
133127
refute_line() {
128+
__refute_stream_line "$@"
129+
}
130+
131+
__refute_stream_line() {
132+
local -r caller=${FUNCNAME[1]}
134133
local -i is_match_line=0
135134
local -i is_mode_partial=0
136135
local -i is_mode_regexp=0
137-
: "${lines?}"
136+
137+
if [[ "${caller}" == "refute_line" ]]; then
138+
: "${lines?}"
139+
local -ar stream_lines=("${lines[@]}")
140+
local -r stream_type=output
141+
elif [[ "${caller}" == "refute_stderr_line" ]]; then
142+
: "${stderr_lines?}"
143+
local -ar stream_lines=("${stderr_lines[@]}")
144+
local -r stream_type=stderr
145+
else
146+
# Coding error: unknown caller
147+
:
148+
fi
138149

139150
# Handle options.
140151
while (( $# > 0 )); do
141152
case "$1" in
142153
-n|--index)
143154
if (( $# < 2 )) || ! [[ $2 =~ ^-?([0-9]|[1-9][0-9]+)$ ]]; then
144155
echo "\`--index' requires an integer argument: \`$2'" \
145-
| batslib_decorate 'ERROR: refute_line' \
156+
| batslib_decorate "ERROR: ${caller}" \
146157
| fail
147158
return $?
148159
fi
@@ -159,7 +170,7 @@ refute_line() {
159170

160171
if (( is_mode_partial )) && (( is_mode_regexp )); then
161172
echo "\`--partial' and \`--regexp' are mutually exclusive" \
162-
| batslib_decorate 'ERROR: refute_line' \
173+
| batslib_decorate "ERROR: ${caller}" \
163174
| fail
164175
return $?
165176
fi
@@ -169,7 +180,7 @@ refute_line() {
169180

170181
if (( is_mode_regexp == 1 )) && [[ '' =~ $unexpected ]] || (( $? == 2 )); then
171182
echo "Invalid extended regular expression: \`$unexpected'" \
172-
| batslib_decorate 'ERROR: refute_line' \
183+
| batslib_decorate "ERROR: ${caller}" \
173184
| fail
174185
return $?
175186
fi
@@ -178,40 +189,40 @@ refute_line() {
178189
if (( is_match_line )); then
179190
# Specific line.
180191
if (( is_mode_regexp )); then
181-
if [[ ${lines[$idx]} =~ $unexpected ]]; then
192+
if [[ ${stream_lines[$idx]} =~ $unexpected ]]; then
182193
batslib_print_kv_single 6 \
183194
'index' "$idx" \
184195
'regexp' "$unexpected" \
185-
'line' "${lines[$idx]}" \
196+
'line' "${stream_lines[$idx]}" \
186197
| batslib_decorate 'regular expression should not match line' \
187198
| fail
188199
fi
189200
elif (( is_mode_partial )); then
190-
if [[ ${lines[$idx]} == *"$unexpected"* ]]; then
201+
if [[ ${stream_lines[$idx]} == *"$unexpected"* ]]; then
191202
batslib_print_kv_single 9 \
192203
'index' "$idx" \
193204
'substring' "$unexpected" \
194-
'line' "${lines[$idx]}" \
205+
'line' "${stream_lines[$idx]}" \
195206
| batslib_decorate 'line should not contain substring' \
196207
| fail
197208
fi
198209
else
199-
if [[ ${lines[$idx]} == "$unexpected" ]]; then
210+
if [[ ${stream_lines[$idx]} == "$unexpected" ]]; then
200211
batslib_print_kv_single 5 \
201212
'index' "$idx" \
202-
'line' "${lines[$idx]}" \
213+
'line' "${stream_lines[$idx]}" \
203214
| batslib_decorate 'line should differ' \
204215
| fail
205216
fi
206217
fi
207218
else
208-
# Line contained in output.
219+
# Line contained in output/error stream.
209220
if (( is_mode_regexp )); then
210221
local -i idx
211-
for (( idx = 0; idx < ${#lines[@]}; ++idx )); do
212-
if [[ ${lines[$idx]} =~ $unexpected ]]; then
222+
for (( idx = 0; idx < ${#stream_lines[@]}; ++idx )); do
223+
if [[ ${stream_lines[$idx]} =~ $unexpected ]]; then
213224
{ local -ar single=( 'regexp' "$unexpected" 'index' "$idx" )
214-
local -a may_be_multi=( 'output' "$output" )
225+
local -a may_be_multi=( "${stream_type}" "${!stream_type}" )
215226
local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )"
216227
batslib_print_kv_single "$width" "${single[@]}"
217228
if batslib_is_single_line "${may_be_multi[1]}"; then
@@ -228,10 +239,10 @@ refute_line() {
228239
done
229240
elif (( is_mode_partial )); then
230241
local -i idx
231-
for (( idx = 0; idx < ${#lines[@]}; ++idx )); do
232-
if [[ ${lines[$idx]} == *"$unexpected"* ]]; then
242+
for (( idx = 0; idx < ${#stream_lines[@]}; ++idx )); do
243+
if [[ ${stream_lines[$idx]} == *"$unexpected"* ]]; then
233244
{ local -ar single=( 'substring' "$unexpected" 'index' "$idx" )
234-
local -a may_be_multi=( 'output' "$output" )
245+
local -a may_be_multi=( "${stream_type}" "${!stream_type}" )
235246
local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )"
236247
batslib_print_kv_single "$width" "${single[@]}"
237248
if batslib_is_single_line "${may_be_multi[1]}"; then
@@ -248,10 +259,10 @@ refute_line() {
248259
done
249260
else
250261
local -i idx
251-
for (( idx = 0; idx < ${#lines[@]}; ++idx )); do
252-
if [[ ${lines[$idx]} == "$unexpected" ]]; then
262+
for (( idx = 0; idx < ${#stream_lines[@]}; ++idx )); do
263+
if [[ ${stream_lines[$idx]} == "$unexpected" ]]; then
253264
{ local -ar single=( 'line' "$unexpected" 'index' "$idx" )
254-
local -a may_be_multi=( 'output' "$output" )
265+
local -a may_be_multi=( "${stream_type}" "${!stream_type}" )
255266
local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )"
256267
batslib_print_kv_single "$width" "${single[@]}"
257268
if batslib_is_single_line "${may_be_multi[1]}"; then
@@ -261,7 +272,7 @@ refute_line() {
261272
batslib_print_kv_multi "${may_be_multi[@]}"
262273
fi
263274
} \
264-
| batslib_decorate 'line should not be in output' \
275+
| batslib_decorate "line should not be in ${stream_type}" \
265276
| fail
266277
return $?
267278
fi

0 commit comments

Comments
 (0)