From afde183e5a41e47a2e4fce7a8d931f2102f957de Mon Sep 17 00:00:00 2001 From: cornfeedhobo Date: Thu, 28 Jan 2021 20:51:46 -0600 Subject: [PATCH] refactor reloader to pass new tests --- clean_files.txt | 1 + scripts/reloader.bash | 112 +++++++++++++++++++++++++---------------- test/lib/reloader.bats | 38 +++++++++++--- 3 files changed, 101 insertions(+), 50 deletions(-) diff --git a/clean_files.txt b/clean_files.txt index f8212f92..521a0766 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -18,6 +18,7 @@ # docs hooks +scripts # root files # diff --git a/scripts/reloader.bash b/scripts/reloader.bash index e5fb4e39..8ecc3a6f 100644 --- a/scripts/reloader.bash +++ b/scripts/reloader.bash @@ -1,54 +1,80 @@ -#!/bin/bash -BASH_IT_LOG_PREFIX="core: reloader: " -pushd "${BASH_IT}" >/dev/null || exit 1 +# shellcheck shell=bash +# shellcheck disable=SC1090,SC2221,SC2222,SC2145,SC2064 -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_LOG_PREFIX="$extension: $filename: " +BASH_IT_LOG_PREFIX="core: reloader: " +pushd "${BASH_IT}" > /dev/null || exit 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 - _bash_it_config_type="" +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 all enabled components..." ;; - esac + case $1 in + alias | completion | plugin) + _bash_it_config_type=$1 + _log_debug "Loading enabled $1 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 - 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 ;; - esac +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" + # shellcheck disable=SC2046 + _bash-it-load-sources $(sort <(compgen -G "./${2}/enabled/*.bash")) + ;; + esac fi unset _bash_it_config_file unset _bash_it_config_type -popd >/dev/null || exit 1 +popd > /dev/null || exit 1 diff --git a/test/lib/reloader.bats b/test/lib/reloader.bats index 0cb736c8..27175b86 100644 --- a/test/lib/reloader.bats +++ b/test/lib/reloader.bats @@ -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!' }