Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle unbound parameters #1903

Merged
merged 12 commits into from
Aug 31, 2021
12 changes: 6 additions & 6 deletions bash_it.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
BASH_IT_LOG_PREFIX="core: main: "

# Only set $BASH_IT if it's not already set
if [ -z "$BASH_IT" ]; then
if [ -z "${BASH_IT:-}" ]; then
# Setting $BASH to maintain backwards compatibility
export BASH_IT=$BASH
BASH="$(bash -c 'echo $BASH')"
Expand All @@ -20,12 +20,12 @@ source "${BASH_IT}"/vendor/github.com/erichs/composure/composure.sh
source "${BASH_IT}/lib/log.bash"

# We can only log it now
[ -z "$BASH_IT_OLD_BASH_SETUP" ] || _log_warning "BASH_IT variable not initialized, please upgrade your bash-it version and reinstall it!"
[ -z "${BASH_IT_OLD_BASH_SETUP:-}" ] || _log_warning "BASH_IT variable not initialized, please upgrade your bash-it version and reinstall it!"

# For backwards compatibility, look in old BASH_THEME location
if [ -z "$BASH_IT_THEME" ]; then
if [ -z "${BASH_IT_THEME:-}" ]; then
_log_warning "BASH_IT_THEME variable not initialized, please upgrade your bash-it version and reinstall it!"
export BASH_IT_THEME="$BASH_THEME"
export BASH_IT_THEME="${BASH_THEME:-}"
unset BASH_THEME
fi

Expand Down Expand Up @@ -122,7 +122,7 @@ for _bash_it_config_file in $CUSTOM; do
done

unset _bash_it_config_file
if [[ $PROMPT ]]; then
if [[ "${PROMPT:-}" ]]; then
export PS1="\[""$PROMPT""\]"
fi

Expand All @@ -144,7 +144,7 @@ if [ -e "$HOME/.jekyllconfig" ]; then
fi

# BASH_IT_RELOAD_LEGACY is set.
if ! command -v reload &> /dev/null && [ -n "$BASH_IT_RELOAD_LEGACY" ]; then
if ! command -v reload &> /dev/null && [ -n "${BASH_IT_RELOAD_LEGACY:-}" ]; then
case $OSTYPE in
darwin*)
alias reload='source ~/.bash_profile'
Expand Down
14 changes: 14 additions & 0 deletions completion/available/system.completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
# Loads the system's Bash completion modules.
# If Homebrew is installed (OS X), it's Bash completion modules are loaded.

if shopt -qo nounset
then # Bash-completion is too large and complex to expect to handle unbound variables throughout the whole codebase.
BASH_IT_RESTORE_NOUNSET=true
shopt -uo nounset
else
BASH_IT_RESTORE_NOUNSET=false
fi

if [[ -r "${BASH_COMPLETION:-}" ]] ; then
source "${BASH_COMPLETION}"
elif [[ -r /etc/bash_completion ]] ; then
Expand All @@ -22,3 +30,9 @@ then
source "$BASH_IT_HOMEBREW_PREFIX"/etc/profile.d/bash_completion.sh
fi
fi

if $BASH_IT_RESTORE_NOUNSET
then
shopt -so nounset
fi
unset BASH_IT_RESTORE_NOUNSET
8 changes: 4 additions & 4 deletions lib/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ bash-it ()
$func $arg
done

if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then
if [ -n "${BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE:-}" ]; then
_bash-it-reload
fi
else
Expand Down Expand Up @@ -354,8 +354,8 @@ _bash-it-migrate() {
disable_func="_disable-$single_type"
enable_func="_enable-$single_type"

$disable_func $component_name
$enable_func $component_name
$disable_func "$component_name"
$enable_func "$component_name"
done
done

Expand All @@ -375,7 +375,7 @@ _bash-it-version() {

cd "${BASH_IT}" || return

if [ -z $BASH_IT_REMOTE ]; then
if [ -z "${BASH_IT_REMOTE:-}" ]; then
BASH_IT_REMOTE="origin"
fi

Expand Down
16 changes: 8 additions & 8 deletions lib/log.bash
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function _log_general()
param '3: message to log'
group 'log'

message=$2${BASH_IT_LOG_PREFIX}$3
_has_colors && echo -e "$1${message}${echo_normal}" || echo -e "${message}"
message=$2${BASH_IT_LOG_PREFIX:-default: }$3
_has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}"
}

function _log_debug()
Expand All @@ -33,8 +33,8 @@ function _log_debug()
example '$ _log_debug "Loading plugin git..."'
group 'log'

[[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0
_log_general "${echo_green}" "DEBUG: " "$1"
[[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0
_log_general "${echo_green:-}" "DEBUG: " "$1"
}

function _log_warning()
Expand All @@ -44,8 +44,8 @@ function _log_warning()
example '$ _log_warning "git binary not found, disabling git plugin..."'
group 'log'

[[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0
_log_general "${echo_yellow}" " WARN: " "$1"
[[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0
_log_general "${echo_yellow:-}" " WARN: " "$1"
}

function _log_error()
Expand All @@ -55,6 +55,6 @@ function _log_error()
example '$ _log_error "Failed to load git plugin..."'
group 'log'

[[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0
_log_general "${echo_red}" "ERROR: " "$1"
[[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0
_log_general "${echo_red:-}" "ERROR: " "$1"
}
2 changes: 1 addition & 1 deletion lib/preview.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if [[ $BASH_PREVIEW ]];
if [[ "${BASH_PREVIEW:-}" ]];
then
unset BASH_PREVIEW #Prevent infinite looping
echo "
Expand Down
2 changes: 1 addition & 1 deletion lib/utilities.bash
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ _bash-it-array-dedup() {

# Outputs a full path of the grep found on the filesystem
_bash-it-grep() {
if [[ -z "${BASH_IT_GREP}" ]] ; then
if [[ -z "${BASH_IT_GREP:-}" ]] ; then
export BASH_IT_GREP="$(which egrep || which grep || '/usr/bin/grep')"
fi
printf "%s " "${BASH_IT_GREP}"
Expand Down
2 changes: 1 addition & 1 deletion plugins/available/base.plugin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function passgen ()

# Create alias pass to passgen when pass isn't installed or
# BASH_IT_LEGACY_PASS is true.
if ! command -v pass &>/dev/null || [[ "$BASH_IT_LEGACY_PASS" = true ]]
if ! command -v pass &>/dev/null || [[ "${BASH_IT_LEGACY_PASS:-}" = true ]]
then
alias pass=passgen
fi
Expand Down
12 changes: 6 additions & 6 deletions test/lib/log.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ load ../../lib/log
@test "lib log: basic debug logging with BASH_IT_LOG_LEVEL_ALL" {
BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL
run _log_debug "test test test"
assert_output "DEBUG: test test test"
assert_output "DEBUG: default: test test test"
}

@test "lib log: basic warning logging with BASH_IT_LOG_LEVEL_ALL" {
BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL
run _log_warning "test test test"
assert_output " WARN: test test test"
assert_output " WARN: default: test test test"
}

@test "lib log: basic error logging with BASH_IT_LOG_LEVEL_ALL" {
BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL
run _log_error "test test test"
assert_output "ERROR: test test test"
assert_output "ERROR: default: test test test"
}

@test "lib log: basic debug logging with BASH_IT_LOG_LEVEL_WARNING" {
Expand All @@ -35,13 +35,13 @@ load ../../lib/log
@test "lib log: basic warning logging with BASH_IT_LOG_LEVEL_WARNING" {
BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_WARNING
run _log_warning "test test test"
assert_output " WARN: test test test"
assert_output " WARN: default: test test test"
}

@test "lib log: basic error logging with BASH_IT_LOG_LEVEL_WARNING" {
BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_WARNING
run _log_error "test test test"
assert_output "ERROR: test test test"
assert_output "ERROR: default: test test test"
}


Expand All @@ -60,7 +60,7 @@ load ../../lib/log
@test "lib log: basic error logging with BASH_IT_LOG_LEVEL_ERROR" {
BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ERROR
run _log_error "test test test"
assert_output "ERROR: test test test"
assert_output "ERROR: default: test test test"
}

@test "lib log: basic debug silent logging" {
Expand Down