refactor reloader to pass new tests

pull/1761/head
cornfeedhobo 2021-01-28 20:51:46 -06:00
parent 83f4c70e81
commit afde183e5a
No known key found for this signature in database
GPG Key ID: 724357093F994B26
3 changed files with 101 additions and 50 deletions

View File

@ -18,6 +18,7 @@
#
docs
hooks
scripts
# root files
#

View File

@ -1,51 +1,77 @@
#!/bin/bash
# shellcheck shell=bash
# shellcheck disable=SC1090,SC2221,SC2222,SC2145,SC2064
BASH_IT_LOG_PREFIX="core: reloader: "
pushd "${BASH_IT}" > /dev/null || exit 1
function _set-prefix-based-on-path()
{
filename=$(_bash-it-get-component-name-from-path "$1")
extension=$(_bash-it-get-component-type-from-path "$1")
_bash-it-load-sources() {
if [[ -z "$1" ]]; then
return 0
fi
local scripts current output retval
scripts=("$@")
current="${scripts[0]}"
scripts=("${scripts[@]:1}")
filename=$(_bash-it-get-component-name-from-path "$current")
extension=$(_bash-it-get-component-type-from-path "$current")
BASH_IT_LOG_PREFIX="$extension: $filename: "
if [[ -e $current ]]; then
_log_debug "Loading component..."
output=$(
set -e
(source "$current") 2>&1
)
retval=$?
set +e
trap "_bash-it-load-sources ${scripts[@]}" EXIT
if [[ $retval -gt 0 ]]; then
_log_error 'Loading component failed...'
if [[ -n "$output" ]]; then
while read -r line; do _log_error "$line"; done <<< "$output"
fi
else
_log_debug 'Loading component successful!'
source "$current"
fi
trap - EXIT
else
_log_error "Unable to read $current"
fi
_bash-it-load-sources "${scripts[@]}"
}
if [ "$1" != "skip" ] && [ -d "./enabled" ]; then
if [[ "$1" != "skip" ]] && [[ -d "./enabled" ]]; then
_bash_it_config_type=""
case $1 in
alias | completion | plugin)
_bash_it_config_type=$1
_log_debug "Loading enabled $1 components..." ;;
_log_debug "Loading enabled $1 components..."
;;
* | '')
_log_debug "Loading all enabled components..." ;;
_log_debug "Loading all enabled components..."
;;
esac
for _bash_it_config_file in $(sort <(compgen -G "./enabled/*${_bash_it_config_type}.bash")); do
if [ -e "${_bash_it_config_file}" ]; then
_set-prefix-based-on-path "${_bash_it_config_file}"
_log_debug "Loading component..."
# shellcheck source=/dev/null
source $_bash_it_config_file
else
echo "Unable to read ${_bash_it_config_file}" > /dev/stderr
fi
done
# shellcheck disable=SC2046
_bash-it-load-sources $(sort <(compgen -G "./enabled/*${_bash_it_config_type}.bash"))
fi
if [ -n "${2}" ] && [ -d "${2}/enabled" ]; then
if [[ -n "${2}" ]] && [[ -d "${2}/enabled" ]]; then
case $2 in
aliases | completion | plugins)
_log_warning "Using legacy enabling for $2, please update your bash-it version and migrate"
for _bash_it_config_file in $(sort <(compgen -G "./${2}/enabled/*.bash")); do
if [ -e "$_bash_it_config_file" ]; then
_set-prefix-based-on-path "${_bash_it_config_file}"
_log_debug "Loading component..."
# shellcheck source=/dev/null
source "$_bash_it_config_file"
else
echo "Unable to locate ${_bash_it_config_file}" > /dev/stderr
fi
done ;;
# shellcheck disable=SC2046
_bash-it-load-sources $(sort <(compgen -G "./${2}/enabled/*.bash"))
;;
esac
fi

View File

@ -23,6 +23,17 @@ function local_setup {
run source "${BASH_IT}/scripts/reloader.bash"
assert_success
# test that loading proceeds properly
assert_line 'DEBUG: plugin: zz: Loading component...'
assert_line 'DEBUG: plugin: zz: Loading component successful!'
assert_line 'You found me!'
# test that the expected plugin is acting as expected
assert_line 'DEBUG: bash: exit-nonzero.bash: Loading component...'
assert_line 'ERROR: bash: exit-nonzero.bash: Loading component failed...'
assert_line 'ERROR: bash: exit-nonzero.bash: exit-nonzero: stdout message'
assert_line 'ERROR: bash: exit-nonzero.bash: exit-nonzero: stderr message'
}
@test "reloader: exit-zero" {
@ -37,6 +48,15 @@ function local_setup {
run source "${BASH_IT}/scripts/reloader.bash"
assert_success
assert_line 'DEBUG: plugin: zz: Loading component...'
assert_line 'DEBUG: plugin: zz: Loading component successful!'
assert_line 'You found me!'
assert_line 'DEBUG: bash: exit-zero.bash: Loading component...'
assert_line 'DEBUG: bash: exit-zero.bash: Loading component successful!'
assert_line 'exit-zero: stdout message'
assert_line 'exit-zero: stderr message'
}
@test "reloader: return-nonzero" {
@ -52,12 +72,14 @@ function local_setup {
run source "${BASH_IT}/scripts/reloader.bash"
assert_success
assert_line 'DEBUG: bash: return-nonzero.bash: Loading component...'
assert_line 'return-nonzero: stdout message'
assert_line 'return-nonzero: stderr message'
assert_line 'DEBUG: plugin: zz: Loading component...'
assert_line 'DEBUG: plugin: zz: Loading component successful!'
assert_line 'You found me!'
assert_line 'DEBUG: bash: return-nonzero.bash: Loading component...'
assert_line 'ERROR: bash: return-nonzero.bash: Loading component failed...'
assert_line 'ERROR: bash: return-nonzero.bash: return-nonzero: stdout message'
assert_line 'ERROR: bash: return-nonzero.bash: return-nonzero: stderr message'
}
@test "reloader: return-zero" {
@ -73,10 +95,12 @@ function local_setup {
run source "${BASH_IT}/scripts/reloader.bash"
assert_success
assert_line 'DEBUG: plugin: zz: Loading component...'
assert_line 'DEBUG: plugin: zz: Loading component successful!'
assert_line 'You found me!'
assert_line 'DEBUG: bash: return-zero.bash: Loading component...'
assert_line 'DEBUG: bash: return-zero.bash: Loading component successful!'
assert_line 'return-zero: stdout message'
assert_line 'return-zero: stderr message'
assert_line 'DEBUG: plugin: zz: Loading component...'
assert_line 'You found me!'
}