Skip to content

Commit

Permalink
Merge unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaschke committed Aug 28, 2023
2 parents 157ea80 + 098cb63 commit 61f8019
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 26 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name: CI
on:
push:

env:
TERM: xterm

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
Expand All @@ -12,6 +15,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: Install shellcheck
run: sudo apt-get install shellcheck
- uses: pre-commit/[email protected]
Expand All @@ -28,7 +33,7 @@ jobs:
- uses: actions/checkout@v3
with:
submodules: recursive
- run: ./test/bats/bin/bats test/*.bats
- run: ./test/bats/bin/bats --formatter pretty test/*.bats

- name: Check that *.js scripts are all identical to main.js
run: |
Expand Down
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ repos:
language: system
files: \.(sh|bats)$
args: ["-x"]
- id: unittests
name: unittests
entry: ./test/bats/bin/bats
language: system
files: \.bats$
args: ["--formatter", "pretty"]
34 changes: 22 additions & 12 deletions src/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -211,33 +211,40 @@ function ici_timed {
}

function ici_teardown {
# don't run teardown code within subshells, but only at top level
if [ "$BASH_SUBSHELL" -le "$__ici_top_level" ]; then
local exit_code=$1
trap - EXIT # Reset signal handler since the shell is about to exit.
local called_from_signal_handler; called_from_signal_handler=${2:-false}

# Reset signal handler since the shell is about to exit.
[ "$called_from_signal_handler" == true ] && trap - EXIT

local cleanup=()
# shellcheck disable=SC2016
IFS=: command eval 'cleanup=(${_CLEANUP})'
for c in "${cleanup[@]}"; do
ici_warn Cleaning up "${c/#\~/$HOME}"
rm -rf "${c/#\~/$HOME}"
done

local location=$ICI_FOLD_NAME
# end fold/timing if needed
if [ -n "$ICI_FOLD_NAME" ]; then
local color_wrap=${ANSI_GREEN}
if [ "$exit_code" -ne "0" ]; then color_wrap=${ANSI_RED}; fi # Red color for errors
gha_error "Failure in $ICI_FOLD_NAME with code: $exit_code"
if [ -n "$ICI_START_TIME" ]; then
ici_time_end "$color_wrap" "$exit_code"
else
ici_end_fold "$ICI_FOLD_NAME"
fi
else
gha_error "Failure with code: $exit_code"
fi
ici_error "Failure $(test -n "$location" && echo "in $location")"

exec {__ici_log_fd}>&-
exec {__ici_err_fd}>&-
if [ "$called_from_signal_handler" = true ]; then
# These will fail if ici_setup was not called
exec {__ici_log_fd}>&-
exec {__ici_err_fd}>&-
fi
fi
}

Expand All @@ -247,7 +254,7 @@ function ici_trap_exit {
ici_warn "terminated unexpectedly with exit code '$exit_code'"
TRACE=true ici_backtrace "$@"
exit_code=143
ici_teardown "$exit_code"
ici_teardown "$exit_code" true
exit "$exit_code"
}

Expand All @@ -265,7 +272,6 @@ function ici_trap_exit {
function ici_exit {
local exit_code=${1:-$?}
ici_backtrace "$@"

ici_teardown "$exit_code"

if [ "$exit_code" == "${EXPECT_EXIT_CODE:-0}" ] ; then
Expand Down Expand Up @@ -580,17 +586,21 @@ function gha_warning {

function ici_start_fold() {
if [ -n "$ICI_FOLD_NAME" ]; then
local old_name=$ICI_FOLD_NAME
# report error _within_ the previous fold
ici_warn "ici_start_fold: nested folds are not supported (still open: '$ICI_FOLD_NAME')"
ici_end_fold
ici_warn "ici_start_fold: nested folds are not supported (still open: '$old_name')"
fi
ICI_FOLD_NAME=$1
gha_cmd group "$ICI_FOLD_NAME"
}

function ici_end_fold() {
gha_cmd endgroup
ICI_FOLD_NAME=
if [ -z "$ICI_FOLD_NAME" ]; then
ici_warn "spurious call to ici_end_fold"
else
gha_cmd endgroup
ICI_FOLD_NAME=
fi
}

function gha_report_result() {
Expand Down
116 changes: 103 additions & 13 deletions test/util.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
function setup {
load 'test_helper/bats-support/load'
load 'test_helper/bats-assert/load'
bats_require_minimum_version 1.10.0

# Get the containing directory of this file ($BATS_TEST_FILENAME)
# Use that instead of ${BASH_SOURCE[0]} as the latter points to the bats executable!
Expand All @@ -16,6 +17,19 @@ function setup {
source "${SRC_PATH}/env.sh"
}

# wrapper around bats' run() to increment __ici_top_level beforehand
# This is needed for all functions that eventually call ici_teardown,
# i.e. ici_step, ici_hook, ici_exit, etc.
function ici_run {
__ici_top_level=$((__ici_top_level+1))
run "$@"
__ici_top_level=$((__ici_top_level-1))
}

function sub_shell {
eval "$*"
}

@test "ici_append" {
local VAR=""

Expand All @@ -27,8 +41,7 @@ function setup {
ici_append VAR "$TWO_LINE_VAR"
run echo "$VAR"

local expected
expected=$(cat <<-EOF
local expected; expected=$(cat <<-EOF
first line
second line
third line
Expand All @@ -47,10 +60,9 @@ EOF
local HOOK="echo 1st line"
ici_append HOOK "echo 2nd line"
ici_append HOOK "echo 3rd line"
run ici_hook HOOK
ici_run ici_hook HOOK

local expected
expected=$(cat <<-EOF
local expected; expected=$(cat <<-EOF
1st line
2nd line
3rd line
Expand Down Expand Up @@ -79,14 +91,10 @@ EOF
ici_append expected "${!var}"
done

function sub_shell {
eval "$*"
}

run "$@" sub_shell "echo \"$all\"; echo \"stderr\" 1>&2; return $exit_code"
# shellcheck disable=SC2016
local cmd='echo "$all"; echo "stderr" 1>&2; return $exit_code'
run "-$exit_code" "$@" sub_shell "$cmd"
assert_output "$expected"
# shellcheck disable=SC2031
[ "$status" -eq "$exit_code" ]
}
@test "ici_quiet_true" {
test_filtering_helper 0 "" ici_quiet
Expand All @@ -99,6 +107,88 @@ EOF
test_filtering_helper 0 "passed" ici_filter "good"
}
@test "ici_filter_false" {
# order of stdout and stderr is changed due to extra filter step
# order of stdout and stderr might change due to extra filter step
test_filtering_helper 1 "all error" ici_filter "good" || \
test_filtering_helper 1 "error all" ici_filter "good"
}

# bats test_tags=folding
@test "folding_success" {
# shellcheck disable=SC2034
local HOOK="echo successful"
local expected; expected=$(cat <<EOF
::group::HOOK
$ ( echo successful; )
successful
'HOOK' returned with code '0' after 0 min 0 sec
::endgroup::
EOF
)
ici_run ici_hook HOOK
assert_output "$expected"
}

# bats test_tags=folding
@test "folding_failure" {
local HOOK="echo failure; false;"
local expected; expected=$(cat <<EOF
::group::HOOK
$ ( echo failure; false;; )
failure
::error::Failure in HOOK with code: 1
'HOOK' returned with code '1' after 0 min 0 sec
::endgroup::
EOF
)
ici_run -1 ici_hook HOOK
assert_output "$expected"

# exit yields same result as false
local sed_cmd='s/false/exit 1; echo "never reached"/'
HOOK=$(echo "$HOOK" | sed "$sed_cmd")
expected=$(echo "$expected" | sed "$sed_cmd")

ici_run -1 ici_hook HOOK
assert_output "$expected"
}

# bats test_tags=folding
@test "folding_cleanup" {
local tmps=()
for i in 1 2 3; do
local tmp; tmp=$(mktemp)
[ -f "$tmp" ] # file should exist
ici_cleanup_later "$tmp" # register file for removal during ici_teardown
tmps+=("$tmp")
done

local HOOK="false"
ici_run -1 ici_hook HOOK

for tmp in "${tmps[@]}"; do
[ ! -f "$tmp" ] # file should be deleted by now
done
}

# bats test_tags=folding
@test folding_double_start_fold {
local HOOK; HOOK="ici_start_fold test; ici_start_fold test; ici_end_fold"
local expected; expected=$(cat <<EOF
::group::test
ici_start_fold: nested folds are not supported (still open: 'test')
::endgroup::
::group::test
::endgroup::
EOF
)
run eval "$HOOK"
assert_output "$expected"
}
# bats test_tags=folding
@test folding_double_end_fold {
local HOOK; HOOK="ici_start_fold test; ici_end_fold; ici_end_fold"
run eval "$HOOK"
echo "$output" | grep -q "spurious call to ici_end_fold"
}

0 comments on commit 61f8019

Please sign in to comment.