Skip to content

Commit b642371

Browse files
committed
FIX bash_unit should work with set -u
set -u bash option treats unset variables and parameters as an arror when performing parameter expansion. One should be able to use this option in its own code or unit test. Before this commit, bash_unit where not able to run with set -u activated.
1 parent 1199e1e commit b642371

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

bash_unit

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ YELLOW="${ESCAPE}[93m"
2626
BLUE="${ESCAPE}[94m"
2727

2828
fail() {
29-
local message=$1
30-
local stdout=$2
31-
local stderr=$3
29+
local message=${1:-}
30+
local stdout=${2:-}
31+
local stderr=${3:-}
3232

3333
notify_test_failed "$__bash_unit_current_test__" "$message"
3434
[[ ! -z $stdout ]] && [ -s "$stdout" ] && notify_stdout < "$stdout"
@@ -40,7 +40,7 @@ fail() {
4040

4141
assert() {
4242
local assertion=$1
43-
local message=$2
43+
local message=${2:-}
4444

4545
_assert_expression \
4646
"$assertion" \
@@ -50,7 +50,7 @@ assert() {
5050

5151
assert_fails() {
5252
local assertion=$1
53-
local message=$2
53+
local message=${2:-}
5454

5555
_assert_expression \
5656
"$assertion" \
@@ -66,7 +66,7 @@ assert_fail() {
6666
assert_status_code() {
6767
local expected_status=$1
6868
local assertion="$2"
69-
local message="$3"
69+
local message="${3:-}"
7070

7171
_assert_expression \
7272
"$assertion" \
@@ -95,7 +95,7 @@ _assert_expression() {
9595
assert_equals() {
9696
local expected=$1
9797
local actual=$2
98-
local message=$3
98+
local message=${3:-}
9999
[[ -z $message ]] || message="$message\n"
100100

101101
if [ "$expected" != "$actual" ]
@@ -107,7 +107,7 @@ assert_equals() {
107107
assert_not_equals() {
108108
local unexpected=$1
109109
local actual=$2
110-
local message=$3
110+
local message=${3:-}
111111
[[ -z $message ]] || message="$message\n"
112112

113113
[ "$unexpected" != "$actual" ] || \
@@ -128,7 +128,7 @@ fake() {
128128

129129
stacktrace() {
130130
local i=1
131-
while ! [ -z "${BASH_SOURCE[$i]}" ]
131+
while ! [ -z "${BASH_SOURCE[$i]:-}" ]
132132
do
133133
echo ${BASH_SOURCE[$i]}:${BASH_LINENO[$((i-1))]}:${FUNCNAME[$i]}\(\)
134134
i=$((i + 1))
@@ -184,15 +184,15 @@ usage() {
184184
# Formating
185185

186186
pretty_success() {
187-
pretty_format "$GREEN" "\u2713" "$1"
187+
pretty_format "$GREEN" "\u2713" "${1:-}"
188188
}
189189

190190
pretty_warning() {
191191
pretty_format "$YELLOW" "\u2717" "$1"
192192
}
193193

194194
pretty_failure() {
195-
pretty_format "$RED" "\u2717" "$1"
195+
pretty_format "$RED" "\u2717" "${1:-}"
196196
}
197197

198198
pretty_format() {

tests/test_core.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ then
151151
"bash_unit should change current working directory to match the directory of the currenlty running test before sourcing test file"
152152
fi
153153

154+
setup() {
155+
# enforce bash variable controls during core tests
156+
# this way we know that people using this enforcement
157+
# in their own code can still rely on bash_unit
158+
set -u
159+
}
160+
154161

155162
line() {
156163
line_nb=$1

0 commit comments

Comments
 (0)