Skip to content

Commit 14d2f0e

Browse files
authored
Merge pull request #239 from djpohly/strictmode
Support Bash "strict mode" (and fix one unit test)
2 parents 0c25742 + ec3b627 commit 14d2f0e

28 files changed

Lines changed: 60 additions & 54 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Add `--upgrade` option to `./bashunit`
66
- Remove support to deprecated `setUp`, `tearDown`, `setUpBeforeScript` and `tearDownAfterScript` functions
77
- Optimize test execution time
8+
- Support tests written using Bash's errexit (-e), nounset (-u), and pipefail options ("unofficial strict mode").
89

910
## [0.10.1](https://github.com/TypedDevs/bashunit/compare/0.10.0...0.10.1) - 2023-11-13
1011

bashunit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
set -euo pipefail
23

34
# shellcheck disable=SC2034
45
declare -r BASHUNIT_VERSION="0.10.1"

src/console_results.sh

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ function console_results::render_result() {
1515
echo ""
1616

1717
local total_tests=0
18-
((total_tests+=$(state::get_tests_passed)))
19-
((total_tests+=$(state::get_tests_skipped)))
20-
((total_tests+=$(state::get_tests_incomplete)))
21-
((total_tests+=$(state::get_tests_snapshot)))
22-
((total_tests+=$(state::get_tests_failed)))
18+
((total_tests += $(state::get_tests_passed))) || true
19+
((total_tests += $(state::get_tests_skipped))) || true
20+
((total_tests += $(state::get_tests_incomplete))) || true
21+
((total_tests += $(state::get_tests_snapshot))) || true
22+
((total_tests += $(state::get_tests_failed))) || true
2323

2424
local total_assertions=0
25-
((total_assertions+=$(state::get_assertions_passed)))
26-
((total_assertions+=$(state::get_assertions_skipped)))
27-
((total_assertions+=$(state::get_assertions_incomplete)))
28-
((total_assertions+=$(state::get_assertions_snapshot)))
29-
((total_assertions+=$(state::get_assertions_failed)))
25+
((total_assertions += $(state::get_assertions_passed))) || true
26+
((total_assertions += $(state::get_assertions_skipped))) || true
27+
((total_assertions += $(state::get_assertions_incomplete))) || true
28+
((total_assertions += $(state::get_assertions_snapshot))) || true
29+
((total_assertions += $(state::get_assertions_failed))) || true
3030

3131
printf "%sTests: %s" "$_COLOR_FAINT" "$_COLOR_DEFAULT"
3232
if [[ "$(state::get_tests_passed)" -gt 0 ]] || [[ "$(state::get_assertions_passed)" -gt 0 ]]; then
@@ -111,7 +111,7 @@ function console_results::print_execution_time() {
111111
}
112112

113113
function console_results::print_successful_test() {
114-
((_SUCCESSFUL_TEST_COUNT++))
114+
((_SUCCESSFUL_TEST_COUNT++)) || true
115115

116116
if [[ "$SIMPLE_OUTPUT" == true ]]; then
117117
if (( _SUCCESSFUL_TEST_COUNT % 50 != 0 )); then
@@ -121,7 +121,7 @@ function console_results::print_successful_test() {
121121
fi
122122
else
123123
local test_name=$1
124-
local data=$2
124+
local data=${2-}
125125

126126
if [[ -z "$data" ]]; then
127127
printf "%s✓ Passed%s: %s\n" "$_COLOR_PASSED" "$_COLOR_DEFAULT" "${test_name}"
@@ -163,7 +163,7 @@ function console_results::print_failed_snapshot_test() {
163163

164164
function console_results::print_skipped_test() {
165165
local test_name=$1
166-
local reason=$2
166+
local reason=${2-}
167167

168168
printf "${_COLOR_SKIPPED}↷ Skipped${_COLOR_DEFAULT}: %s\n" "${test_name}"
169169

@@ -174,7 +174,7 @@ function console_results::print_skipped_test() {
174174

175175
function console_results::print_incomplete_test() {
176176
local test_name=$1
177-
local pending=$2
177+
local pending=${2-}
178178

179179
printf "${_COLOR_INCOMPLETE}✒ Incomplete${_COLOR_DEFAULT}: %s\n" "${test_name}"
180180

src/env_configuration.sh

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,10 @@ set -o allexport
55
[[ -f ".env" ]] && source .env set
66
set +o allexport
77

8-
if [[ -z "$PARALLEL_RUN" ]]; then
9-
PARALLEL_RUN=$_DEFAULT_PARALLEL_RUN
10-
fi
11-
12-
if [[ -z "$SHOW_HEADER" ]]; then
13-
SHOW_HEADER=$_DEFAULT_SHOW_HEADER
14-
fi
15-
16-
if [[ -z "$HEADER_ASCII_ART" ]]; then
17-
HEADER_ASCII_ART=$_DEFAULT_HEADER_ASCII_ART
18-
fi
19-
20-
if [[ -z "$SIMPLE_OUTPUT" ]]; then
21-
SIMPLE_OUTPUT=$_DEFAULT_SIMPLE_OUTPUT
22-
fi
23-
24-
if [[ -z "$STOP_ON_FAILURE" ]]; then
25-
STOP_ON_FAILURE=$_DEFAULT_STOP_ON_FAILURE
26-
fi
27-
28-
if [[ -z "$SHOW_EXECUTION_TIME" ]]; then
29-
SHOW_EXECUTION_TIME=$_DEFAULT_SHOW_EXECUTION_TIME
30-
fi
31-
32-
if [[ -z "$DEFAULT_PATH" ]]; then
33-
DEFAULT_PATH=$_DEFAULT_DEFAULT_PATH
34-
fi
8+
: "${PARALLEL_RUN:=$_DEFAULT_PARALLEL_RUN}"
9+
: "${SHOW_HEADER:=$_DEFAULT_SHOW_HEADER}"
10+
: "${HEADER_ASCII_ART:=$_DEFAULT_HEADER_ASCII_ART}"
11+
: "${SIMPLE_OUTPUT:=$_DEFAULT_SIMPLE_OUTPUT}"
12+
: "${STOP_ON_FAILURE:=$_DEFAULT_STOP_ON_FAILURE}"
13+
: "${SHOW_EXECUTION_TIME:=$_DEFAULT_SHOW_EXECUTION_TIME}"
14+
: "${DEFAULT_PATH:=$_DEFAULT_DEFAULT_PATH}"

src/helpers.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ declare -r BASHUNIT_GIT_REPO="https://github.com/TypedDevs/bashunit"
88
# @return string Eg: "Some logic camelCase"
99
#
1010
function helper::normalize_test_function_name() {
11-
local original_function_name="$1"
11+
local original_function_name="${1-}"
1212
local result
1313

1414
# Remove "test_" prefix
@@ -85,7 +85,9 @@ function helper::get_functions_to_run() {
8585
# @param $1 string Eg: "do_something"
8686
#
8787
function helper::execute_function_if_exists() {
88-
"$1" 2>/dev/null
88+
if [[ "$(type -t "$1")" == "function" ]]; then
89+
"$1" 2>/dev/null
90+
fi
8991
}
9092

9193
#
@@ -133,6 +135,7 @@ function helper::get_provider_data() {
133135
grep -B 1 "function $function_name()" "$script" |\
134136
grep "# data_provider " |\
135137
sed -E -e 's/\ *# data_provider (.*)$/\1/g'\
138+
|| true
136139
)
137140

138141
if [[ -n "$data_provider_function" ]]; then

src/runner.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function runner::call_test_functions() {
5050
echo "Running $script"
5151
fi
5252

53-
helper::check_duplicate_functions "$script"
53+
helper::check_duplicate_functions "$script" || true
5454

5555
for function_name in "${functions_to_run[@]}"; do
5656
local provider_data=()
@@ -107,16 +107,16 @@ function runner::parse_execution_result() {
107107
sed -E -e 's/.*##ASSERTIONS_SNAPSHOT=([0-9]*)##.*/\1/g'\
108108
)
109109

110-
_ASSERTIONS_PASSED=$((_ASSERTIONS_PASSED + assertions_passed))
111-
_ASSERTIONS_FAILED=$((_ASSERTIONS_FAILED + assertions_failed))
112-
_ASSERTIONS_SKIPPED=$((_ASSERTIONS_SKIPPED + assertions_skipped))
113-
_ASSERTIONS_INCOMPLETE=$((_ASSERTIONS_INCOMPLETE + assertions_incomplete))
114-
_ASSERTIONS_SNAPSHOT=$((_ASSERTIONS_SNAPSHOT + assertions_snapshot))
110+
((_ASSERTIONS_PASSED += assertions_passed)) || true
111+
((_ASSERTIONS_FAILED += assertions_failed)) || true
112+
((_ASSERTIONS_SKIPPED += assertions_skipped)) || true
113+
((_ASSERTIONS_INCOMPLETE += assertions_incomplete)) || true
114+
((_ASSERTIONS_SNAPSHOT += assertions_snapshot)) || true
115115
}
116116

117117
function runner::run_test() {
118118
local function_name="$1"
119-
local data="$2"
119+
local data="${2-}"
120120
local current_assertions_failed
121121
current_assertions_failed="$(state::get_assertions_failed)"
122122
local current_assertions_snapshot

src/skip_todo.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
function skip() {
4-
local reason=$1
4+
local reason=${1-}
55
local label
66
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
77

@@ -11,7 +11,7 @@ function skip() {
1111
}
1212

1313
function todo() {
14-
local pending=$1
14+
local pending=${1-}
1515
local label
1616
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
1717

tests/acceptance/bashunit.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
set -euo pipefail
23

34
function set_up_before_script() {
45
TEST_ENV_FILE="tests/acceptance/fixtures/.env.default"

tests/acceptance/bashunit_execution_error_test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
set -euo pipefail
23

34
function set_up_before_script() {
45
TEST_ENV_FILE="tests/acceptance/fixtures/.env.default"

tests/acceptance/bashunit_fail_test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
set -euo pipefail
23

34
function set_up_before_script() {
45
TEST_ENV_FILE="tests/acceptance/fixtures/.env.default"

0 commit comments

Comments
 (0)