1- # bats not open errexit, nounset and pipefail by default
2- set -o errexit
3- # set -o nounset
4- set -o pipefail
5- (shopt -p inherit_errexit & > /dev/null) && shopt -s inherit_errexit
6-
7- if [[ -n ${DOCKER:- } ]]; then
8- load /test/support/load.bash
9- load /test/assert/load.bash
10- else
11- load " $LOBASH_TEST_DIR " /fixture/support/load.bash
12- load " $LOBASH_TEST_DIR " /fixture/assert/load.bash
13- fi
14-
151load_src () {
162 local path=$1 ;
173 shift
@@ -26,6 +12,8 @@ load_fixtrue() {
2612
2713if [[ $LOBASH_USE_DIST == true ]]; then
2814 load_module () {
15+ declare -p " _loaded_module_lobash_dist" & > /dev/null && return
16+
2917 # shellcheck source=./dist/lobash.bash
3018 source " $LOBASH_ROOT_DIR /dist/lobash.bash"
3119
@@ -45,28 +33,22 @@ if [[ $LOBASH_USE_DIST == true ]]; then
4533
4634 eval " _LOBASH_${uniq_key} _PUBLIC_DEPTH=2"
4735 fi
36+
37+ declare -g " _loaded_module_lobash_dist" =true
4838 }
4939else
5040 load_module () {
5141 [[ $# != 1 ]] && echo " load_module must have one argument at least." >&2 && return 3
42+
43+ declare -p " _loaded_module_${1// ./ _} " & > /dev/null && return
44+ if ! declare -f _lobash.import > /dev/null; then
45+ _lobash.import_internal imports
46+ fi
5247 _lobash.imports " $1 "
48+ declare -g " _loaded_module_${1// ./ _} " =true
5349 }
5450fi
5551
56- # Fix: bats-core reset "set -e"
57- # https://github.com/bats-core/bats-core/blob/master/libexec/bats-core/bats-exec-test#L60
58- run () {
59- local origFlags=" $- "
60- local origIFS=" $IFS "
61- set +eET
62- # bats has bug, /lobash/tests/fixture/bats/libexec/bats-core/bats-exec-test: line 7: BASH_SOURCE: unbound variable
63- output=" $( set -o nounset; set -e; " $@ " 2>&1 ) "
64- status=" $? "
65- IFS=$' \n ' lines=($output )
66- IFS=" $origIFS "
67- set " -$origFlags "
68- }
69-
7052# check_bash <module_name>
7153check_bash () {
7254 local module_name=$1
@@ -77,18 +59,66 @@ check_bash() {
7759
7860 if (( compare > 0 )) ; then
7961 echo " [Skip Test] '$module_name ' support Bash $bashver +, while current BASH_VERSION=$BASH_VERSION "
80- exit 0
62+ exit 1
8163 fi
8264}
8365
84- test_prepare () {
85- local module_name=$1
86- # This line is important. Set cache map variable
87- declare -A _LOBASH_MOD_META_CACHE
88- _lobash.import_internals module_meta
66+ # Do not define functions and variables in setup_file,
67+ # because it runs in child process different from test and setup.
68+ setup_file () {
69+ if [[ $BATS_TEST_FILENAME =~ " /modules/$module_name .bats" $ ]]; then
70+ # This line is important. Set cache map variable
71+ declare -A _LOBASH_MOD_META_CACHE
72+ _lobash.import_internals module_meta
73+
74+ if ! check_bash " $module_name " ; then
75+ skip
76+ return
77+ fi
78+ fi
79+ }
80+
81+ # https://bats-core.readthedocs.io/en/stable/tutorial.html#avoiding-costly-repeated-setups
82+ setup () {
83+ local module_name
84+ module_name=$( basename " $BATS_TEST_FILENAME " .bats)
85+
86+ # If import has bug, all test cases will failed
87+ load_src load_internals
88+
89+ if [[ " $BATS_TEST_FILENAME " =~ " /modules/$module_name .bats" $ ]]; then
90+ # Auto load module for /modules/*.bats testing
91+ load_module " $module_name "
92+ fi
8993
90- check_bash " $module_name "
94+ # Note: Use setup_test instead of setup function in .bats
95+ if declare -f setup_test > /dev/null; then setup_test; fi
9196}
9297
93- # If import has bug, all test cases will failed
94- load_src load_internals
98+ {
99+ # Hack in run() at tests/fixture/bats/lib/bats-core/test_functions.bash
100+ # set -eETu in run() command
101+ # shellcheck disable=2016
102+ eval " $( declare -f run | \
103+ sed ' s/$("$pre_command"/$(set -eETu; "$pre_command"/' | \
104+ sed ' s/ && status=0 || status=$?;/; status=$?;/' ) " # this line fix errexit ignored in test condition
105+
106+ # For debug:
107+ # declare -f run | sed 's/$("$pre_command"/$(set -eETu; "$pre_command"/' | sed 's/ && status=0 || status=$?;/; status=$?;/' > /dev/tty
108+ }
109+
110+ {
111+ # To load assert helpers
112+ if [[ -n ${DOCKER:- } ]]; then
113+ load /test/support/load.bash
114+ load /test/assert/load.bash
115+ load /test/bats-file/load.bash
116+ else
117+ load " $LOBASH_TEST_DIR " /fixture/support/load.bash
118+ load " $LOBASH_TEST_DIR " /fixture/assert/load.bash
119+ load " $LOBASH_TEST_DIR " /fixture/bats-file/load.bash
120+ fi
121+
122+ # To fix run --separate-stderr
123+ bats_require_minimum_version 1.5.0
124+ }
0 commit comments