Skip to content

Commit 65797f3

Browse files
committed
test: update bats from v1.1.0 to v1.8.2 && fix test dockerfile
- bats-assert use [my fork](https://github.com/adoyle-h/bats-assert.git), waiting the [PR](bats-core/bats-assert#52) merged.
1 parent e9ceff4 commit 65797f3

File tree

125 files changed

+95
-290
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+95
-290
lines changed

.gitmodules

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
shallow = true
55
[submodule "tests/fixture/assert"]
66
path = tests/fixture/assert
7-
url = https://github.com/bats-core/bats-assert.git
7+
; TODO: Waiting merged: https://github.com/bats-core/bats-assert/pull/52
8+
url = https://github.com/adoyle-h/bats-assert.git
9+
branch = feat/stderr
810
shallow = true
911
[submodule "tests/fixture/bats-file"]
1012
path = tests/fixture/bats-file

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
.PHONY: test
2+
test:
3+
./test
4+
15
.PHONY: bump-major bump-minor bump-patch
26

37
bump-major:

test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ else
4646
fi
4747

4848
export PATH="$LOBASH_TEST_DIR/fixture/bats/bin:${PATH}"
49+
bats --version
4950

50-
exec bats ${CI:+--tap} "${test_dirs[@]}"
51+
exec bats --print-output-on-failure ${CI:+--tap} "${test_dirs[@]}"

tests/fixture/setup.bash

Lines changed: 67 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
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-
151
load_src() {
162
local path=$1;
173
shift
@@ -26,6 +12,8 @@ load_fixtrue() {
2612

2713
if [[ $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
}
4939
else
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
}
5450
fi
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>
7153
check_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+
}

tests/modules/array_include.bats

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/usr/bin/env bats
22

33
setup_fixture
4-
test_prepare array_include
5-
load_module array_include
64

75
@test "l.array_include (he ll o world) hello" {
86
local arr=(he ll o world)

tests/modules/array_include.s.bats

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/usr/bin/env bats
22

33
setup_fixture
4-
test_prepare array_include.s
5-
load_module array_include.s
64

75
@test "l.array_include.s (he ll o world) hello" {
86
local arr=(he ll o world)

tests/modules/ask.bats

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/usr/bin/env bats
22

33
setup_fixture
4-
test_prepare ask
5-
load_module ask
64

75
# It uses echo pipe just for test.
86
# You should invoke `l.ask <message>` for normal usage.

tests/modules/ask_input.bats

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/usr/bin/env bats
22

33
setup_fixture
4-
test_prepare ask_input
5-
load_module ask_input
64

75
# It uses echo pipe just for test.
86
# You should invoke `l.ask_input <message>` for normal usage.

tests/modules/ask_with_cancel.bats

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/usr/bin/env bats
22

33
setup_fixture
4-
test_prepare ask_with_cancel
5-
load_module ask_with_cancel
64

75
# It uses echo pipe just for test.
86
# You should invoke `l.ask_with_cancel <message>` for normal usage.

0 commit comments

Comments
 (0)