File tree 3 files changed +48
-2
lines changed
3 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -91,7 +91,7 @@ function assert_exactly_one_of {
91
91
# Determine how many arg_vals are non-empty
92
92
for (( i= 0 ; i< $((num_args)) ; i+=2 )); do
93
93
arg_names+=(" ${args[i]} " )
94
- if [[ ! -z " ${args[i+1]} " ]]; then
94
+ if [[ -n " ${args[i+1]} " ]]; then
95
95
num_non_empty=$(( num_non_empty+ 1 ))
96
96
fi
97
97
done
@@ -108,4 +108,12 @@ function assert_uid_is_root_or_sudo {
108
108
log_error " This script should be run using sudo or as the root user"
109
109
exit 1
110
110
fi
111
- }
111
+ }
112
+
113
+ # Assert that the user running this script has permissions to run sudo.
114
+ function assert_user_has_sudo_perms {
115
+ if ! sudo -n true > /dev/null 2>&1 ; then
116
+ log_error " This script should be run using sudo, as the root user, or as a user with sudo permissions."
117
+ exit 1
118
+ fi
119
+ }
Original file line number Diff line number Diff line change @@ -56,3 +56,32 @@ function string_is_empty_or_null {
56
56
local -r response=" $1 "
57
57
[[ -z " $response " || " $response " == " null" ]]
58
58
}
59
+
60
+
61
+ # Given a string $str, return the substring beginning at index $start and ending at index $end.
62
+ #
63
+ # Example:
64
+ #
65
+ # string_substr "hello world" 0 5
66
+ # Returns "hello"
67
+ function string_substr {
68
+ local -r str=" $1 "
69
+ local -r start=" $2 "
70
+ local end=" $3 "
71
+
72
+ if [[ " $start " -lt 0 || " $end " -lt 0 ]]; then
73
+ log_error " In the string_substr bash function, each of \$ start and \$ end must be >= 0."
74
+ exit 1
75
+ fi
76
+
77
+ if [[ " $start " -gt " $end " ]]; then
78
+ log_error " In the string_substr bash function, \$ start must be < \$ end."
79
+ exit 1
80
+ fi
81
+
82
+ if [[ -z " $end " ]]; then
83
+ end=" ${# str} "
84
+ fi
85
+
86
+ echo " ${str: $start : $end } "
87
+ }
Original file line number Diff line number Diff line change @@ -186,4 +186,13 @@ load "test-helper"
186
186
assert_failure
187
187
}
188
188
189
+ @test " string_substr empty string" {
190
+ run string_substr " " 0 0
191
+ assert_success
192
+ }
189
193
194
+ @test " string_substr non empty string" {
195
+ run string_substr " hello world" 0 5
196
+ assert_success
197
+ assert_output " hello"
198
+ }
You can’t perform that action at this time.
0 commit comments