Skip to content

Commit 524f1d2

Browse files
committed
Fail assert_exactly_one_of if it receives an odd number of arguments
1 parent ce6f0c5 commit 524f1d2

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

modules/bash-commons/src/assert.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ function assert_value_in_list {
8080
function assert_exactly_one_of {
8181
local -ra args=("$@")
8282
local -r num_args="${#args[@]}"
83+
if [[ "$((num_args % 2))" -ne 0 ]]; then
84+
log_error "This script expects an even number of arguments but received $num_args instead."
85+
exit 1
86+
fi
8387

8488
local num_non_empty=0
8589
local -a arg_names=()

test/assert.bats

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ load "test-helper"
184184
assert_failure
185185
}
186186

187+
@test "assert_exactly_one_of list of length 5, with one value set, fails for odd num_args" {
188+
run assert_exactly_one_of "--foo" "foo" "--bar" "" "--baz" "" "--qux"
189+
assert_failure
190+
}
191+
187192
@test "assert_uid_is_root_or_sudo as root" {
188193
run assert_uid_is_root_or_sudo
189194
assert_success

0 commit comments

Comments
 (0)