Skip to content

Commit ce6f0c5

Browse files
committed
Updates from code-review comments
1 parent 2411239 commit ce6f0c5

File tree

6 files changed

+40
-75
lines changed

6 files changed

+40
-75
lines changed

modules/bash-commons/src/array.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ function array_contains {
2020
#
2121
# Examples:
2222
#
23-
# array_split "a,b,c", ","
23+
# array_split "a,b,c" ","
2424
# Returns: ("a" "b" "c")
2525
#
2626
# Hint:
27-
# When calling this function, use the following construction: ary=( $(array_split "a,b,c", ",") )
27+
# When calling this function, use the following construction: ary=( $(array_split "a,b,c" ",") )
2828
#
2929
# Sources:
3030
# - https://stackoverflow.com/a/15988793/2308858
3131
function array_split {
3232
local -r separator="$1"
3333
local -r str="$2"
34-
local ary=()
34+
local -a ary=()
3535

3636
IFS="$separator" read -a ary <<<"$str"
3737

@@ -48,7 +48,7 @@ function array_split {
4848
function array_join {
4949
local -r separator="$1"
5050
shift
51-
local -ra values=("$@")
51+
local -ar values=("$@")
5252

5353
local out=""
5454
for (( i=0; i<"${#values[@]}"; i++ )); do
@@ -77,7 +77,7 @@ function array_join {
7777
function array_prepend {
7878
local -r prefix="$1"
7979
shift 1
80-
local -r ary=($@)
80+
local -ar ary=($@)
8181

8282
updated_ary=( "${ary[@]/#/$prefix}" )
8383
echo ${updated_ary[*]}

modules/bash-commons/src/assert.sh

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,29 @@ function assert_value_in_list {
7070
fi
7171
}
7272

73+
# Reads in a list of keys and values and asserts that one and only one of the values is set.
74+
# This is useful for command line options that are mutually exclusive.
75+
# Example:
76+
# assert_exactly_one_of "--opt1" "" "--opt2" "val2" "--opt3" "" "--opt4" ""
77+
# Examples that assert failure:
78+
# assert_exactly_one_of "--opt1" "val1" "--opt2" "val2" "--opt3" "" "--opt4" ""
79+
# assert_exactly_one_of "--opt1" "" "--opt2" "" "--opt3" "" "--opt4" ""
7380
function assert_exactly_one_of {
74-
local -r arg_name1="$1"
75-
local -r arg_val1="$2"
76-
local -r arg_name2="$3"
77-
local -r arg_val2="$4"
78-
local -r arg_name3="$5"
79-
local -r arg_val3="$6"
81+
local -ra args=("$@")
82+
local -r num_args="${#args[@]}"
8083

8184
local num_non_empty=0
82-
local arg_names=()
83-
local arg_vals=()
84-
85-
if [[ ! -z "$arg_name1" ]]; then arg_names+=("$arg_name1"); arg_vals+=("$arg_val1"); fi
86-
if [[ ! -z "$arg_name2" ]]; then arg_names+=("$arg_name2"); arg_vals+=("$arg_val2"); fi
87-
if [[ ! -z "$arg_name3" ]]; then arg_names+=("$arg_name3"); arg_vals+=("$arg_val3"); fi
85+
local -a arg_names=()
8886

8987
# Determine how many arg_vals are non-empty
90-
for (( i=0; i<${#arg_vals[@]}; i++ )); do
91-
if [[ ! -z "${arg_vals[i]}" ]]; then
88+
for (( i=0; i<$((num_args+1)); i+=2 )); do
89+
arg_names+=("${args[i]}")
90+
if [[ ! -z "${args[i+1]}" ]]; then
9291
num_non_empty=$((num_non_empty+1))
9392
fi
9493
done
9594

96-
if [[ ${num_non_empty} != 1 ]]; then
95+
if [[ "$num_non_empty" -ne 1 ]]; then
9796
log_error "Exactly one of ${arg_names[*]} must be set."
9897
exit 1
9998
fi

modules/bash-commons/src/aws.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
# (a) it's more convenient to fetch specific info you need, such as an EC2 Instance's private IP and (b) so you can
44
# replace these helpers with mocks to do local testing or unit testing.
55

6+
# shellcheck source=./modules/bash-commons/src/assert.sh
7+
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/assert.sh"
8+
# shellcheck source=./modules/bash-commons/src/log.sh
9+
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/log.sh"
10+
611
# Look up the given path in the EC2 Instance metadata endpoint
712
function aws_lookup_path_in_instance_metadata {
813
local -r path="$1"

modules/bash-commons/src/file.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function file_replace_text_in_files {
5555
local -r original_text_regex="$1"
5656
local -r replacement_text="$2"
5757
shift 2
58-
local -r files=("$@")
58+
local -ar files=("$@")
5959

6060
for file in "${files[@]}"; do
6161
file_replace_text "$original_text_regex" "$replacement_text" "$file"

modules/bash-commons/src/java.sh

Lines changed: 0 additions & 54 deletions
This file was deleted.

test/assert.bats

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,21 @@ load "test-helper"
169169
assert_failure
170170
}
171171

172+
@test "assert_exactly_one_of list of length 4, with value in pos 2" {
173+
run assert_exactly_one_of "--foo" "" "--bar" "bar" "--baz" "" "--qux" ""
174+
assert_success
175+
}
176+
177+
@test "assert_exactly_one_of list of length 4, with value in pos 2 and 4" {
178+
run assert_exactly_one_of "--foo" "" "--bar" "bar" "--baz" "" "--qux" "qux"
179+
assert_failure
180+
}
181+
182+
@test "assert_exactly_one_of list of length 4, with no value set" {
183+
run assert_exactly_one_of "--foo" "" "--bar" "" "--baz" "" "--qux" ""
184+
assert_failure
185+
}
186+
172187
@test "assert_uid_is_root_or_sudo as root" {
173188
run assert_uid_is_root_or_sudo
174189
assert_success

0 commit comments

Comments
 (0)