Skip to content

Commit a3803a6

Browse files
qec-pconnermartin-schulze-vireso
authored andcommitted
* Adding support for negative positions via new arg --returned-status.
* Supports positive and negative values. * Negative values count from the end in reverse, similar to (linux's) bash and python. * Can be used as either `--return-status N` or `--return-status=N`. * Adding tests to validate `--return-status` functionality.
1 parent 2547ad4 commit a3803a6

File tree

2 files changed

+775
-51
lines changed

2 files changed

+775
-51
lines changed

lib/bats-core/test_functions.bash

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ bats_pipe() { # [-N] [--] command0 [ \| command1 [ \| command2 [...]]]
191191
# chain of piped commands (similar to `set -o pipefail`).
192192
# Supplying -N (e.g. -0) will instead always use the exit code of the command
193193
# at that position in the chain.
194+
# --returned-status=N could be used as an alternative to -N. This also allows
195+
# for negative values (which count from the end in reverse order).
194196

195197
local pipestatus_position=
196198

@@ -200,6 +202,17 @@ bats_pipe() { # [-N] [--] command0 [ \| command1 [ \| command2 [...]]]
200202
-[0-9]*)
201203
pipestatus_position="${1#-}"
202204
;;
205+
--returned-status*)
206+
if [ "$1" = "--returned-status" ]; then
207+
pipestatus_position="$2"
208+
shift
209+
elif [[ "$1" =~ ^--returned-status= ]]; then
210+
pipestatus_position="${1#--returned-status=}"
211+
else
212+
printf "Usage error: unknown flag '%s'" "$1" >&2
213+
return 1
214+
fi
215+
;;
203216
--)
204217
shift # eat the -- before breaking away
205218
break
@@ -255,9 +268,9 @@ bats_pipe() { # [-N] [--] command0 [ \| command1 [ \| command2 [...]]]
255268
fi
256269

257270
# there will be pipe_count + 1 entries in PIPE_STATUS (pipe_count number of \|'s between each entry).
258-
# valid indices are [0; pipe_count]
259-
if [ -n "$pipestatus_position" ] && (( pipestatus_position > pipe_count )); then
260-
printf "Usage error: Too large of -N argument given. Argument value: '%s'.\n" "$pipestatus_position" >&2
271+
# valid indices are [-(pipe_count + 1), pipe_count]
272+
if [ -n "$pipestatus_position" ] && (( (pipestatus_position > pipe_count) || (-pipestatus_position > (pipe_count + 1)) )); then
273+
printf "Usage error: Too large of -N argument (or --returned-status) given. Argument value: '%s'.\n" "$pipestatus_position" >&2
261274
return 1
262275
fi
263276

@@ -279,7 +292,10 @@ bats_pipe() { # [-N] [--] command0 [ \| command1 [ \| command2 [...]]]
279292
break;
280293
fi
281294
done
295+
elif (( pipestatus_position >= 0 )); then
296+
result_status="${__bats_pipe_eval_pipe_status[$pipestatus_position]}"
282297
else
298+
local backward_iter_index="$((${#__bats_pipe_eval_pipe_status[@]} + pipestatus_position))"
283299
result_status="${__bats_pipe_eval_pipe_status[$pipestatus_position]}"
284300
fi
285301

0 commit comments

Comments
 (0)