refactor reloader to pass new tests
parent
83f4c70e81
commit
afde183e5a
|
|
@ -18,6 +18,7 @@
|
||||||
#
|
#
|
||||||
docs
|
docs
|
||||||
hooks
|
hooks
|
||||||
|
scripts
|
||||||
|
|
||||||
# root files
|
# root files
|
||||||
#
|
#
|
||||||
|
|
|
||||||
|
|
@ -1,54 +1,80 @@
|
||||||
#!/bin/bash
|
# shellcheck shell=bash
|
||||||
BASH_IT_LOG_PREFIX="core: reloader: "
|
# shellcheck disable=SC1090,SC2221,SC2222,SC2145,SC2064
|
||||||
pushd "${BASH_IT}" >/dev/null || exit 1
|
|
||||||
|
|
||||||
function _set-prefix-based-on-path()
|
BASH_IT_LOG_PREFIX="core: reloader: "
|
||||||
{
|
pushd "${BASH_IT}" > /dev/null || exit 1
|
||||||
filename=$(_bash-it-get-component-name-from-path "$1")
|
|
||||||
extension=$(_bash-it-get-component-type-from-path "$1")
|
_bash-it-load-sources() {
|
||||||
BASH_IT_LOG_PREFIX="$extension: $filename: "
|
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=""
|
_bash_it_config_type=""
|
||||||
|
|
||||||
case $1 in
|
case $1 in
|
||||||
alias|completion|plugin)
|
alias | completion | plugin)
|
||||||
_bash_it_config_type=$1
|
_bash_it_config_type=$1
|
||||||
_log_debug "Loading enabled $1 components..." ;;
|
_log_debug "Loading enabled $1 components..."
|
||||||
*|'')
|
;;
|
||||||
_log_debug "Loading all enabled components..." ;;
|
* | '')
|
||||||
esac
|
_log_debug "Loading all enabled components..."
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
for _bash_it_config_file in $(sort <(compgen -G "./enabled/*${_bash_it_config_type}.bash")); do
|
# shellcheck disable=SC2046
|
||||||
if [ -e "${_bash_it_config_file}" ]; then
|
_bash-it-load-sources $(sort <(compgen -G "./enabled/*${_bash_it_config_type}.bash"))
|
||||||
_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
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -n "${2}" ] && [ -d "${2}/enabled" ]; then
|
if [[ -n "${2}" ]] && [[ -d "${2}/enabled" ]]; then
|
||||||
case $2 in
|
case $2 in
|
||||||
aliases|completion|plugins)
|
aliases | completion | plugins)
|
||||||
_log_warning "Using legacy enabling for $2, please update your bash-it version and migrate"
|
_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
|
# shellcheck disable=SC2046
|
||||||
if [ -e "$_bash_it_config_file" ]; then
|
_bash-it-load-sources $(sort <(compgen -G "./${2}/enabled/*.bash"))
|
||||||
_set-prefix-based-on-path "${_bash_it_config_file}"
|
;;
|
||||||
_log_debug "Loading component..."
|
esac
|
||||||
# shellcheck source=/dev/null
|
|
||||||
source "$_bash_it_config_file"
|
|
||||||
else
|
|
||||||
echo "Unable to locate ${_bash_it_config_file}" > /dev/stderr
|
|
||||||
fi
|
|
||||||
done ;;
|
|
||||||
esac
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
unset _bash_it_config_file
|
unset _bash_it_config_file
|
||||||
unset _bash_it_config_type
|
unset _bash_it_config_type
|
||||||
popd >/dev/null || exit 1
|
popd > /dev/null || exit 1
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,17 @@ function local_setup {
|
||||||
|
|
||||||
run source "${BASH_IT}/scripts/reloader.bash"
|
run source "${BASH_IT}/scripts/reloader.bash"
|
||||||
assert_success
|
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" {
|
@test "reloader: exit-zero" {
|
||||||
|
|
@ -37,6 +48,15 @@ function local_setup {
|
||||||
|
|
||||||
run source "${BASH_IT}/scripts/reloader.bash"
|
run source "${BASH_IT}/scripts/reloader.bash"
|
||||||
assert_success
|
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" {
|
@test "reloader: return-nonzero" {
|
||||||
|
|
@ -52,12 +72,14 @@ function local_setup {
|
||||||
run source "${BASH_IT}/scripts/reloader.bash"
|
run source "${BASH_IT}/scripts/reloader.bash"
|
||||||
assert_success
|
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...'
|
||||||
|
assert_line 'DEBUG: plugin: zz: Loading component successful!'
|
||||||
assert_line 'You found me!'
|
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" {
|
@test "reloader: return-zero" {
|
||||||
|
|
@ -73,10 +95,12 @@ function local_setup {
|
||||||
run source "${BASH_IT}/scripts/reloader.bash"
|
run source "${BASH_IT}/scripts/reloader.bash"
|
||||||
assert_success
|
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...'
|
||||||
|
assert_line 'DEBUG: bash: return-zero.bash: Loading component successful!'
|
||||||
assert_line 'return-zero: stdout message'
|
assert_line 'return-zero: stdout message'
|
||||||
assert_line 'return-zero: stderr message'
|
assert_line 'return-zero: stderr message'
|
||||||
|
|
||||||
assert_line 'DEBUG: plugin: zz: Loading component...'
|
|
||||||
assert_line 'You found me!'
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue