Skip to content

Commit

Permalink
test: update bats from v1.1.0 to v1.8.2 && fix test dockerfile
Browse files Browse the repository at this point in the history
- bats-assert use [my fork](https://github.com/adoyle-h/bats-assert.git), waiting the [PR](bats-core/bats-assert#52) merged.
  • Loading branch information
adoyle-h committed Dec 11, 2022
1 parent e9ceff4 commit 65797f3
Show file tree
Hide file tree
Showing 125 changed files with 95 additions and 290 deletions.
4 changes: 3 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
shallow = true
[submodule "tests/fixture/assert"]
path = tests/fixture/assert
url = https://github.com/bats-core/bats-assert.git
; TODO: Waiting merged: https://github.com/bats-core/bats-assert/pull/52
url = https://github.com/adoyle-h/bats-assert.git
branch = feat/stderr
shallow = true
[submodule "tests/fixture/bats-file"]
path = tests/fixture/bats-file
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.PHONY: test
test:
./test

.PHONY: bump-major bump-minor bump-patch

bump-major:
Expand Down
3 changes: 2 additions & 1 deletion test
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ else
fi

export PATH="$LOBASH_TEST_DIR/fixture/bats/bin:${PATH}"
bats --version

exec bats ${CI:+--tap} "${test_dirs[@]}"
exec bats --print-output-on-failure ${CI:+--tap} "${test_dirs[@]}"
2 changes: 1 addition & 1 deletion tests/fixture/assert
104 changes: 67 additions & 37 deletions tests/fixture/setup.bash
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# bats not open errexit, nounset and pipefail by default
set -o errexit
# set -o nounset
set -o pipefail
(shopt -p inherit_errexit &>/dev/null) && shopt -s inherit_errexit

if [[ -n ${DOCKER:-} ]]; then
load /test/support/load.bash
load /test/assert/load.bash
else
load "$LOBASH_TEST_DIR"/fixture/support/load.bash
load "$LOBASH_TEST_DIR"/fixture/assert/load.bash
fi

load_src() {
local path=$1;
shift
Expand All @@ -26,6 +12,8 @@ load_fixtrue() {

if [[ $LOBASH_USE_DIST == true ]]; then
load_module() {
declare -p "_loaded_module_lobash_dist" &>/dev/null && return

# shellcheck source=./dist/lobash.bash
source "$LOBASH_ROOT_DIR/dist/lobash.bash"

Expand All @@ -45,28 +33,22 @@ if [[ $LOBASH_USE_DIST == true ]]; then
eval "_LOBASH_${uniq_key}_PUBLIC_DEPTH=2"
fi
declare -g "_loaded_module_lobash_dist"=true
}
else
load_module() {
[[ $# != 1 ]] && echo "load_module must have one argument at least." >&2 && return 3
declare -p "_loaded_module_${1//./_}" &>/dev/null && return
if ! declare -f _lobash.import >/dev/null; then
_lobash.import_internal imports
fi
_lobash.imports "$1"
declare -g "_loaded_module_${1//./_}"=true
}
fi
# Fix: bats-core reset "set -e"
# https://github.com/bats-core/bats-core/blob/master/libexec/bats-core/bats-exec-test#L60
run() {
local origFlags="$-"
local origIFS="$IFS"
set +eET
# bats has bug, /lobash/tests/fixture/bats/libexec/bats-core/bats-exec-test: line 7: BASH_SOURCE: unbound variable
output="$(set -o nounset; set -e; "$@" 2>&1)"
status="$?"
IFS=$'\n' lines=($output)
IFS="$origIFS"
set "-$origFlags"
}
# check_bash <module_name>
check_bash() {
local module_name=$1
Expand All @@ -77,18 +59,66 @@ check_bash() {
if (( compare > 0 )); then
echo "[Skip Test] '$module_name' support Bash $bashver+, while current BASH_VERSION=$BASH_VERSION"
exit 0
exit 1
fi
}
test_prepare() {
local module_name=$1
# This line is important. Set cache map variable
declare -A _LOBASH_MOD_META_CACHE
_lobash.import_internals module_meta
# Do not define functions and variables in setup_file,
# because it runs in child process different from test and setup.
setup_file() {
if [[ $BATS_TEST_FILENAME =~ "/modules/$module_name.bats"$ ]]; then
# This line is important. Set cache map variable
declare -A _LOBASH_MOD_META_CACHE
_lobash.import_internals module_meta
if ! check_bash "$module_name"; then
skip
return
fi
fi
}
# https://bats-core.readthedocs.io/en/stable/tutorial.html#avoiding-costly-repeated-setups
setup() {
local module_name
module_name=$(basename "$BATS_TEST_FILENAME" .bats)
# If import has bug, all test cases will failed
load_src load_internals
if [[ "$BATS_TEST_FILENAME" =~ "/modules/$module_name.bats"$ ]]; then
# Auto load module for /modules/*.bats testing
load_module "$module_name"
fi
check_bash "$module_name"
# Note: Use setup_test instead of setup function in .bats
if declare -f setup_test >/dev/null; then setup_test; fi
}
# If import has bug, all test cases will failed
load_src load_internals
{
# Hack in run() at tests/fixture/bats/lib/bats-core/test_functions.bash
# set -eETu in run() command
# shellcheck disable=2016
eval "$(declare -f run | \
sed 's/$("$pre_command"/$(set -eETu; "$pre_command"/' | \
sed 's/ && status=0 || status=$?;/; status=$?;/')" # this line fix errexit ignored in test condition
# For debug:
# declare -f run | sed 's/$("$pre_command"/$(set -eETu; "$pre_command"/' | sed 's/ && status=0 || status=$?;/; status=$?;/' > /dev/tty
}
{
# To load assert helpers
if [[ -n ${DOCKER:-} ]]; then
load /test/support/load.bash
load /test/assert/load.bash
load /test/bats-file/load.bash
else
load "$LOBASH_TEST_DIR"/fixture/support/load.bash
load "$LOBASH_TEST_DIR"/fixture/assert/load.bash
load "$LOBASH_TEST_DIR"/fixture/bats-file/load.bash
fi
# To fix run --separate-stderr
bats_require_minimum_version 1.5.0
}
2 changes: 0 additions & 2 deletions tests/modules/array_include.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare array_include
load_module array_include

@test "l.array_include (he ll o world) hello" {
local arr=(he ll o world)
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/array_include.s.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare array_include.s
load_module array_include.s

@test "l.array_include.s (he ll o world) hello" {
local arr=(he ll o world)
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/ask.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare ask
load_module ask

# It uses echo pipe just for test.
# You should invoke `l.ask <message>` for normal usage.
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/ask_input.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare ask_input
load_module ask_input

# It uses echo pipe just for test.
# You should invoke `l.ask_input <message>` for normal usage.
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/ask_with_cancel.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare ask_with_cancel
load_module ask_with_cancel

# It uses echo pipe just for test.
# You should invoke `l.ask_with_cancel <message>` for normal usage.
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/basename.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare basename
load_module basename

@test "l.basename hello.world" {
run l.basename hello.world
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/basename.p.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare basename.p
load_module basename.p

@test "echo hello.world | l.basename.p" {
t() {
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/benchmark.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare benchmark
load_module benchmark

@test "l.benchmark" {
foo() {
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/choose.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare choose
load_module choose

@test "l.choose a b. Enter 2" {
test() {
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/compose.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare compose
load_module compose

@test "l.compose foo bar baz" {
foo() {
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/count_file_lines.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare count_file_lines
load_module count_file_lines

@test "l.count_file_lines" {
local lines=10
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/count_files.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare count_files
load_module count_files

@test "l.count_files /temp_dir with no file" {
local temp_dir
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/count_lines.bats.bak
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare count_lines
load_module count_lines

@test "l.count_lines ''" {
run l.count_lines ''
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/count_lines.p.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare count_lines.p
load_module count_lines.p

@test "printf '' | l.count_lines.p" {
test() {
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/cur_function_name.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare cur_function_name
load_module cur_function_name

@test "l.cur_function_name" {
foo() {
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/date.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare date
load_module date

@test "l.date '%s'" {
run l.date '%s'
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/detect_os.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare detect_os
load_module detect_os

@test "l.detect_os" {
skip "how to test it?"
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/dirname.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare dirname
load_module dirname

@test "l.dirname /" {
run l.dirname /
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/dirname.p.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare dirname.p
load_module dirname.p

@test "echo '/' | l.dirname.p" {
t() {
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/echo.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare echo
load_module echo

@test "l.echo hello" {
run l.echo hello
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/echo_array.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare echo_array
load_module echo_array

@test "l.echo_array (a b c)" {
local arr=(a b c)
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/echo_screen.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare echo_screen
load_module echo_screen

@test "l.echo_screen hello" {
run l.echo_screen hello
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/end_with.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare end_with
load_module end_with

@test "l.end_with hello o" {
run l.end_with "hello" "o"
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/end_with.s.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare end_with.s
load_module end_with.s

@test "l.end_with.s hello o" {
run l.end_with.s "hello" "o"
Expand Down
2 changes: 0 additions & 2 deletions tests/modules/extname.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bats

setup_fixture
test_prepare extname
load_module extname

@test "l.extname file.name" {
run l.extname file.name
Expand Down
Loading

0 comments on commit 65797f3

Please sign in to comment.