lib/helpers: first `shellcheck` pass

Quote things, SC2268, SC2143, SC2181, SC2162, SC2016, SC2013, &c.

Rewrite globbing per `shellcheck`’s SC2013, and alsö s/typeset/local/g. Eliminate `compgen` where possible.

Alsö: use the existing utility functions `_bash-it-get-component-type-from-path` and `_bash-it-get-component-name-from-path`, which just use parameter substitution anyway. Why was `sed` here?

Alsö, don't add not-existing directories to `$PATH` in `pathmunge()`.

Finally, merge PR #1865 from NoahGorny...and clean it a bit...
pull/1934/head
John D Pell 2022-01-03 15:57:18 -08:00 committed by John D Pell
parent 9b51dc0b5f
commit 5eab3bd288
6 changed files with 401 additions and 366 deletions

404
lib/helpers.bash 100755 → 100644
View File

@ -1,4 +1,7 @@
#!/usr/bin/env bash # shellcheck shell=bash
# shellcheck disable=SC2016
#
# A collection of reusable functions.
BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS=${BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS:-150} BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS=${BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS:-150}
BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN=${BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN:-250} BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN=${BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN:-250}
@ -77,7 +80,7 @@ function _bash_it_homebrew_check()
} }
function _make_reload_alias() { function _make_reload_alias() {
echo "source \${BASH_IT}/scripts/reloader.bash ${1} ${2}" echo "source '${BASH_IT}/scripts/reloader.bash' '${1}' '${2}'"
} }
# Alias for reloading aliases # Alias for reloading aliases
@ -110,50 +113,50 @@ bash-it ()
example '$ bash-it restart' example '$ bash-it restart'
example '$ bash-it profile list|save|load|rm [profile_name]' example '$ bash-it profile list|save|load|rm [profile_name]'
example '$ bash-it doctor errors|warnings|all' example '$ bash-it doctor errors|warnings|all'
typeset verb=${1:-} local verb=${1:-}
shift shift
typeset component=${1:-} local component=${1:-}
shift shift
typeset func local func
case $verb in case $verb in
show) show)
func=_bash-it-$component;; func="_bash-it-$component";;
enable) enable)
func=_enable-$component;; func="_enable-$component";;
disable) disable)
func=_disable-$component;; func="_disable-$component";;
help) help)
func=_help-$component;; func="_help-$component";;
doctor) doctor)
func=_bash-it-doctor-$component;; func="_bash-it-doctor-$component";;
profile) profile)
func=_bash-it-profile-$component;; func=_bash-it-profile-$component;;
search) search)
_bash-it-search $component "$@" _bash-it-search "$component" "$@"
return;; return;;
update) update)
func=_bash-it-update-$component;; func="_bash-it-update-$component";;
migrate) migrate)
func=_bash-it-migrate;; func="_bash-it-migrate";;
version) version)
func=_bash-it-version;; func="_bash-it-version";;
restart) restart)
func=_bash-it-restart;; func="_bash-it-restart";;
reload) reload)
func=_bash-it-reload;; func="_bash-it-reload";;
*) *)
reference bash-it reference "bash-it"
return;; return;;
esac esac
# pluralize component if necessary # pluralize component if necessary
if ! _is_function $func; then if ! _is_function "$func"; then
if _is_function ${func}s; then if _is_function "${func}s"; then
func=${func}s func="${func}s"
else else
if _is_function ${func}es; then if _is_function "${func}es"; then
func=${func}es func="${func}es"
else else
echo "oops! $component is not a valid option!" echo "oops! $component is not a valid option!"
reference bash-it reference bash-it
@ -162,20 +165,21 @@ bash-it ()
fi fi
fi fi
if [ x"$verb" == x"enable" ] || [ x"$verb" == x"disable" ]; then if [[ "$verb" == "enable" || "$verb" == "disable" ]]
then
# Automatically run a migration if required # Automatically run a migration if required
_bash-it-migrate _bash-it-migrate
for arg in "$@" for arg in "$@"
do do
$func $arg "$func" "$arg"
done done
if [ -n "${BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE:-}" ]; then if [[ -n "${BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE:-}" ]]; then
_bash-it-reload _bash-it-reload
fi fi
else else
$func "$@" "$func" "$@"
fi fi
} }
@ -237,8 +241,8 @@ _bash-it_update_migrate_and_restart() {
_about 'Checks out the wanted version, pops directory and restart. Does not return (because of the restart!)' _about 'Checks out the wanted version, pops directory and restart. Does not return (because of the restart!)'
_param '1: Which branch to checkout to' _param '1: Which branch to checkout to'
_param '2: Which type of version we are using' _param '2: Which type of version we are using'
git checkout "$1" &> /dev/null if git checkout "$1" &> /dev/null
if [[ $? -eq 0 ]]; then then
echo "Bash-it successfully updated." echo "Bash-it successfully updated."
echo "" echo ""
echo "Migrating your installation to the latest $2 version now..." echo "Migrating your installation to the latest $2 version now..."
@ -246,6 +250,7 @@ _bash-it_update_migrate_and_restart() {
echo "" echo ""
echo "All done, enjoy!" echo "All done, enjoy!"
# Don't forget to restore the original pwd! # Don't forget to restore the original pwd!
# shellcheck disable=SC2164
popd &> /dev/null popd &> /dev/null
_bash-it-restart _bash-it-restart
else else
@ -259,96 +264,101 @@ _bash-it-update-() {
_group 'lib' _group 'lib'
declare silent declare silent
for word in $@; do for word in "$@"; do
if [[ ${word} == "--silent" || ${word} == "-s" ]]; then if [[ "${word}" == "--silent" || "${word}" == "-s" ]]; then
silent=true silent=true
fi fi
done done
pushd "${BASH_IT}" &> /dev/null || return pushd "${BASH_IT?}" >/dev/null || return
DIFF=$(git diff --name-status) DIFF=$(git diff --name-status)
[ -n "$DIFF" ] && echo -e "Local changes detected in bash-it directory. Clean '$BASH_IT' directory to proceed.\n$DIFF" && return 1 if [[ -n "$DIFF" ]]; then
echo -e "Local changes detected in bash-it directory. Clean '$BASH_IT' directory to proceed.\n$DIFF"
return 1
fi
if [ -z "$BASH_IT_REMOTE" ]; then if [[ -z "$BASH_IT_REMOTE" ]]; then
BASH_IT_REMOTE="origin" BASH_IT_REMOTE="origin"
fi fi
git fetch $BASH_IT_REMOTE --tags &> /dev/null git fetch "$BASH_IT_REMOTE" --tags &> /dev/null
if [ -z "$BASH_IT_DEVELOPMENT_BRANCH" ]; then if [[ -z "$BASH_IT_DEVELOPMENT_BRANCH" ]]; then
BASH_IT_DEVELOPMENT_BRANCH="master" BASH_IT_DEVELOPMENT_BRANCH="master"
fi fi
# Defaults to stable update # Defaults to stable update
if [ -z "$1" ] || [ "$1" == "stable" ]; then if [[ -z "$1" || "$1" == "stable" ]]; then
version="stable" version="stable"
TARGET=$(git describe --tags "$(git rev-list --tags --max-count=1)" 2> /dev/null) TARGET=$(git describe --tags "$(git rev-list --tags --max-count=1)" 2> /dev/null)
if [[ -z "$TARGET" ]]; then if [[ -z "$TARGET" ]]; then
echo "Can not find tags, so can not update to latest stable version..." echo "Can not find tags, so can not update to latest stable version..."
# shellcheck disable=SC2164
popd &> /dev/null popd &> /dev/null
return return
fi fi
else else
version="dev" version="dev"
TARGET=${BASH_IT_REMOTE}/${BASH_IT_DEVELOPMENT_BRANCH} TARGET="${BASH_IT_REMOTE}/${BASH_IT_DEVELOPMENT_BRANCH}"
fi fi
declare revision declare revision
revision="HEAD..${TARGET}" revision="HEAD..${TARGET}"
declare status declare status
status="$(git rev-list ${revision} 2> /dev/null)" status="$(git rev-list "${revision}" 2> /dev/null)"
declare revert declare revert
if [[ -z "${status}" && ${version} == "stable" ]]; then if [[ -z "${status}" && "${version}" == "stable" ]]; then
revision="${TARGET}..HEAD" revision="${TARGET}..HEAD"
status="$(git rev-list ${revision} 2> /dev/null)" status="$(git rev-list "${revision}" 2> /dev/null)"
revert=true revert=true
fi fi
if [[ -n "${status}" ]]; then if [[ -n "${status}" ]]; then
if [[ $revert ]]; then if [[ -n "${revert}" ]]; then
echo "Your version is a more recent development version ($(git log -1 --format=%h HEAD))" echo "Your version is a more recent development version ($(git log -1 --format=%h HEAD))"
echo "You can continue in order to revert and update to the latest stable version" echo "You can continue in order to revert and update to the latest stable version"
echo "" echo ""
log_color="%Cred" log_color="%Cred"
fi fi
for i in $(git rev-list --merges --first-parent ${revision}); do for i in $(git rev-list --merges --first-parent "${revision}"); do
num_of_lines=$(git log -1 --format=%B $i | awk 'NF' | wc -l) num_of_lines=$(git log -1 --format=%B "$i" | awk 'NF' | wc -l)
if [ $num_of_lines -eq 1 ]; then if [[ "$num_of_lines" -eq 1 ]]; then
description="%s" description="%s"
else else
description="%b" description="%b"
fi fi
git log --format="${log_color}%h: $description (%an)" -1 $i git log --format="${log_color}%h: $description (%an)" -1 "$i"
done done
echo "" echo ""
if [[ $silent ]]; then if [[ -n "${silent}" ]]; then
echo "Updating to ${TARGET}($(git log -1 --format=%h "${TARGET}"))..." echo "Updating to ${TARGET}($(git log -1 --format=%h "${TARGET}"))..."
_bash-it_update_migrate_and_restart $TARGET $version _bash-it_update_migrate_and_restart "$TARGET" "$version"
else else
read -e -n 1 -p "Would you like to update to ${TARGET}($(git log -1 --format=%h "${TARGET}"))? [Y/n] " RESP read -r -e -n 1 -p "Would you like to update to ${TARGET}($(git log -1 --format=%h "${TARGET}"))? [Y/n] " RESP
case $RESP in case "$RESP" in
[yY]|"") [yY]|"")
_bash-it_update_migrate_and_restart $TARGET $version _bash-it_update_migrate_and_restart "$TARGET" "$version"
;; ;;
[nN]) [nN])
echo "Not updating…" echo "Not updating…"
;; ;;
*) *)
echo -e "\033[91mPlease choose y or n.\033[m" echo -e "${echo_orange?}Please choose y or n.${echo_reset_color?}"
;; ;;
esac esac
fi fi
else else
if [[ ${version} == "stable" ]]; then if [[ "${version}" == "stable" ]]; then
echo "You're on the latest stable version. If you want to check out the latest 'dev' version, please run \"bash-it update dev\"" echo "You're on the latest stable version. If you want to check out the latest 'dev' version, please run \"bash-it update dev\""
else else
echo "Bash-it is up to date, nothing to do!" echo "Bash-it is up to date, nothing to do!"
fi fi
fi fi
# shellcheck disable=SC2164
popd &> /dev/null popd &> /dev/null
} }
@ -361,32 +371,34 @@ _bash-it-migrate() {
for file_type in "aliases" "plugins" "completion" for file_type in "aliases" "plugins" "completion"
do do
for f in `sort <(compgen -G "${BASH_IT}/$file_type/enabled/*.bash")` for _bash_it_config_file in "${BASH_IT}/$file_type/enabled"/*.bash
do do
typeset ff="${f##*/}" [[ -f "$_bash_it_config_file" ]] || continue
# Get the type of component from the extension # Get the type of component from the extension
typeset single_type=$(echo $ff | sed -e 's/.*\.\(.*\)\.bash/\1/g' | sed 's/aliases/alias/g') local component_type="$(_bash-it-get-component-type-from-path "$_bash_it_config_file")"
# Cut off the optional "250---" prefix and the suffix # Cut off the optional "250---" prefix and the suffix
typeset component_name=$(echo $ff | sed -e 's/[0-9]*[-]*\(.*\)\..*\.bash/\1/g') local component_name="$(_bash-it-get-component-name-from-path "$_bash_it_config_file")"
migrated_something=true migrated_something=true
echo "Migrating $single_type $component_name." local single_type="${component_type/aliases/aliass}"
echo "Migrating ${single_type%s} $component_name."
disable_func="_disable-$single_type" disable_func="_disable-${single_type%s}"
enable_func="_enable-$single_type" enable_func="_enable-${single_type%s}"
$disable_func "$component_name" "$disable_func" "$component_name"
$enable_func "$component_name" "$enable_func" "$component_name"
done done
unset _bash_it_config_file
done done
if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then if [[ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]]; then
_bash-it-reload _bash-it-reload
fi fi
if [ "$migrated_something" = "true" ]; then if [[ "$migrated_something" == "true" ]]; then
echo "" echo ""
echo "If any migration errors were reported, please try the following: reload && bash-it migrate" echo "If any migration errors were reported, please try the following: reload && bash-it migrate"
fi fi
@ -398,28 +410,28 @@ _bash-it-version() {
cd "${BASH_IT}" || return cd "${BASH_IT}" || return
if [ -z "${BASH_IT_REMOTE:-}" ]; then if [[ -z "${BASH_IT_REMOTE:-}" ]]; then
BASH_IT_REMOTE="origin" BASH_IT_REMOTE="origin"
fi fi
BASH_IT_GIT_REMOTE=$(git remote get-url $BASH_IT_REMOTE) BASH_IT_GIT_REMOTE="$(git remote get-url "$BASH_IT_REMOTE")"
BASH_IT_GIT_URL=${BASH_IT_GIT_REMOTE%.git} BASH_IT_GIT_URL="${BASH_IT_GIT_REMOTE%.git}"
if [[ "$BASH_IT_GIT_URL" == *"git@"* ]]; then if [[ "$BASH_IT_GIT_URL" == *"git@"* ]]; then
# Fix URL in case it is ssh based URL # Fix URL in case it is ssh based URL
BASH_IT_GIT_URL=${BASH_IT_GIT_URL/://} BASH_IT_GIT_URL="${BASH_IT_GIT_URL/://}"
BASH_IT_GIT_URL=${BASH_IT_GIT_URL/git@/https://} BASH_IT_GIT_URL="${BASH_IT_GIT_URL/git@/https://}"
fi fi
current_tag=$(git describe --exact-match --tags 2> /dev/null) current_tag="$(git describe --exact-match --tags 2> /dev/null)"
if [[ -z $current_tag ]]; then if [[ -z "$current_tag" ]]; then
BASH_IT_GIT_VERSION_INFO="$(git log --pretty=format:'%h on %aI' -n 1)" BASH_IT_GIT_VERSION_INFO="$(git log --pretty=format:'%h on %aI' -n 1)"
TARGET=${BASH_IT_GIT_VERSION_INFO%% *} TARGET="${BASH_IT_GIT_VERSION_INFO%% *}"
echo "Version type: dev" echo "Version type: dev"
echo "Current git SHA: $BASH_IT_GIT_VERSION_INFO" echo "Current git SHA: $BASH_IT_GIT_VERSION_INFO"
echo "Commit info: $BASH_IT_GIT_URL/commit/$TARGET" echo "Commit info: $BASH_IT_GIT_URL/commit/$TARGET"
else else
TARGET=$current_tag TARGET="$current_tag"
echo "Version type: stable" echo "Version type: stable"
echo "Current tag: $current_tag" echo "Current tag: $current_tag"
echo "Tag information: $BASH_IT_GIT_URL/releases/tag/$current_tag" echo "Tag information: $BASH_IT_GIT_URL/releases/tag/$current_tag"
@ -444,21 +456,21 @@ _bash-it-doctor-all() {
_about 'reloads a profile file with error, warning and debug logs' _about 'reloads a profile file with error, warning and debug logs'
_group 'lib' _group 'lib'
_bash-it-doctor $BASH_IT_LOG_LEVEL_ALL _bash-it-doctor "$BASH_IT_LOG_LEVEL_ALL"
} }
_bash-it-doctor-warnings() { _bash-it-doctor-warnings() {
_about 'reloads a profile file with error and warning logs' _about 'reloads a profile file with error and warning logs'
_group 'lib' _group 'lib'
_bash-it-doctor $BASH_IT_LOG_LEVEL_WARNING _bash-it-doctor "$BASH_IT_LOG_LEVEL_WARNING"
} }
_bash-it-doctor-errors() { _bash-it-doctor-errors() {
_about 'reloads a profile file with error logs' _about 'reloads a profile file with error logs'
_group 'lib' _group 'lib'
_bash-it-doctor $BASH_IT_LOG_LEVEL_ERROR _bash-it-doctor "$BASH_IT_LOG_LEVEL_ERROR"
} }
_bash-it-doctor-() { _bash-it-doctor-() {
@ -472,12 +484,12 @@ _bash-it-profile-save() {
_about 'saves the current configuration to the "profile" directory' _about 'saves the current configuration to the "profile" directory'
_group 'lib' _group 'lib'
local name=$1 local name="${1:-}"
while [ -z "$1" ]; do while [[ -z "$name" ]]; do
read -r -e -p "Please enter the name of the profile to save: " name read -r -e -p "Please enter the name of the profile to save: " name
case $name in case $name in
"") "")
echo -e "\033[91mPlease choose a name.\033[m" echo -e "${echo_orange?}Please choose a name.${echo_reset_color?}"
;; ;;
*) *)
break break
@ -485,37 +497,37 @@ _bash-it-profile-save() {
esac esac
done done
local profile_path="${BASH_IT}/profiles/${name}.bash_it" local profile_path="${BASH_IT}/profiles/${name}.bash_it" RESP
if [ -f "$profile_path" ]; then if [[ -s "$profile_path" ]]; then
echo -e "\033[0;33mProfile \"$name\" already exists.\033[m" echo -e "${echo_yellow?}Profile \"$name\" already exists.${echo_reset_color?}"
while true; do while true; do
read -r -e -n 1 -p "Would you like to overwrite existing profile? [y/N] " RESP read -r -e -n 1 -p "Would you like to overwrite existing profile? [y/N] " RESP
case $RESP in case $RESP in
[yY]) [yY])
echo -e "\033[0;32mOverwriting profile \"$name\"...\033[m" echo -e "${echo_green?}Overwriting profile \"$name\"...${echo_reset_color?}"
rm "$profile_path" rm "$profile_path"
break break
;; ;;
[nN] | "") [nN] | "")
echo -e "\033[91mAborting profile save...\033[m" echo -e "${echo_orange?}Aborting profile save...${echo_reset_color?}"
return 1 return 1
;; ;;
*) *)
echo -e "\033[91mPlease choose y or n.\033[m" echo -e "${echo_orange?}Please choose y or n.${echo_reset_color?}"
;; ;;
esac esac
done done
fi fi
local something_exists local something_exists subdirectory
echo "# This file is auto generated by Bash-it. Do not edit manually!" > "$profile_path" echo "# This file is auto generated by Bash-it. Do not edit manually!" > "$profile_path"
for subdirectory in "plugins" "completion" "aliases"; do for subdirectory in "plugins" "completion" "aliases"; do
local component_exists="" local component_exists="" f
echo "Saving $subdirectory configuration..." echo "Saving $subdirectory configuration..."
for f in "${BASH_IT}/$subdirectory/available/"*.bash; do for f in "${BASH_IT}/$subdirectory/available/"*.bash; do
_bash-it-determine-component-status-from-path "$f" _bash-it-determine-component-status-from-path "$f"
if [ "$enabled" == "x" ]; then if [[ "$enabled" == "x" ]]; then
if [ -z "$component_exists" ]; then if [[ -z "$component_exists" ]]; then
# This is the first component of this type, print the header # This is the first component of this type, print the header
component_exists="yes" component_exists="yes"
something_exists="yes" something_exists="yes"
@ -526,7 +538,7 @@ _bash-it-profile-save() {
fi fi
done done
done done
if [ -z "$something_exists" ]; then if [[ -z "$something_exists" ]]; then
echo "It seems like no configuration was enabled.." echo "It seems like no configuration was enabled.."
echo "Make sure to double check that this is the wanted behavior." echo "Make sure to double check that this is the wanted behavior."
fi fi
@ -535,27 +547,27 @@ _bash-it-profile-save() {
echo "" echo ""
echo "Profile location: $profile_path" echo "Profile location: $profile_path"
echo "Load the profile by invoking \"bash-it profile load $name\"" echo "Load the profile by invoking \"bash-it profile load $name\""
} }
_bash-it-profile-load-parse-profile() { _bash-it-profile-load-parse-profile() {
_about 'Internal function used to parse the profile file' _about 'Internal function used to parse the profile file'
_param '1: path to the profile file' _param '1: path to the profile file'
_param '2: dry run- only check integrity of the profile file' _param '2: dry run- only check integrity of the profile file'
_example '$ _bash-it-profile-load-parse-profile "profile.bash_it" "dry"' _example '$ _bash-it-profile-load-parse-profile "profile.bash_it" "dry"'
local num=0 local -i num=0
while read -r -a line; do while read -r -a line; do
num=$((num + 1)) ((++num))
# Ignore comments and empty lines # Ignore comments and empty lines
[[ -z "${line[*]}" || "${line[*]}" =~ ^#.* ]] && continue [[ -z "${line[*]}" || "${line[*]}" =~ ^#.* ]] && continue
local enable_func="_enable-${line[0]}" local enable_func="_enable-${line[0]}"
local subdirectory=${line[0]} local subdirectory=${line[0]}
local component=${line[1]} local component=${line[1]}
typeset to_enable=$(command ls "${BASH_IT}/$subdirectory/available/$component".*bash 2>/dev/null | head -1) local to_enable=$(command ls "${BASH_IT}/$subdirectory/available/$component".*bash 2>/dev/null | head -1)
# Ignore botched lines # Ignore botched lines
if [[ -z "$to_enable" ]]; then if [[ -z "${to_enable}" ]]; then
echo -e "\033[91mBad line(#$num) in profile, aborting load...\033[m" echo -e "${echo_orange?}Bad line(#$num) in profile, aborting load...${echo_reset_color?}"
local bad="bad line" local bad="bad line"
break break
fi fi
@ -567,57 +579,59 @@ _bash-it-profile-load-parse-profile() {
# Make sure to propagate the error # Make sure to propagate the error
[[ -z $bad ]] [[ -z $bad ]]
} }
_bash-it-profile-list() { _bash-it-profile-list() {
about 'lists all profiles from the "profiles" directory' about 'lists all profiles from the "profiles" directory'
_group 'lib' _group 'lib'
local profile
echo "Available profiles:" echo "Available profiles:"
for profile in "${BASH_IT}/profiles"/*.bash_it; do for profile in "${BASH_IT}/profiles"/*.bash_it; do
profile="${profile##*/}" profile="${profile##*/}"
echo "${profile/.bash_it/}" echo "${profile/.bash_it/}"
done done
} }
_bash-it-profile-rm() { _bash-it-profile-rm() {
about 'Removes a profile from the "profiles" directory' about 'Removes a profile from the "profiles" directory'
_group 'lib' _group 'lib'
local name="$1" local name="$1"
if [[ -z $name ]]; then if [[ -z $name ]]; then
echo -e "\033[91mPlease specify profile name to remove...\033[m" echo -e "${echo_orange?}Please specify profile name to remove...${echo_reset_color?}"
return 1 return 1
fi fi
# Users should not be allowed to delete the default profile # Users should not be allowed to delete the default profile
if [[ $name == "default" ]]; then if [[ $name == "default" ]]; then
echo -e "\033[91mCan not remove the default profile...\033[m" echo -e "${echo_orange?}Can not remove the default profile...${echo_reset_color?}"
return 1 return 1
fi fi
local profile_path="${BASH_IT}/profiles/$name.bash_it" local profile_path="${BASH_IT}/profiles/$name.bash_it"
if [[ ! -f "$profile_path" ]]; then if [[ ! -f "$profile_path" ]]; then
echo -e "\033[91mCould not find profile \"$name\"...\033[m" echo -e "${echo_orange?}Could not find profile \"$name\"...${echo_reset_color?}"
return 1 return 1
fi fi
command rm "$profile_path" command rm "$profile_path"
echo "Removed profile \"$name\" successfully!" echo "Removed profile \"$name\" successfully!"
} }
_bash-it-profile-load() { _bash-it-profile-load() {
_about 'loads a configuration from the "profiles" directory' _about 'loads a configuration from the "profiles" directory'
_group 'lib' _group 'lib'
local name="$1" local name="$1"
if [[ -z $name ]]; then if [[ -z $name ]]; then
echo -e "\033[91mPlease specify profile name to load, not changing configuration...\033[m" echo -e "${echo_orange?}Please specify profile name to load, not changing configuration...${echo_reset_color?}"
return 1 return 1
fi fi
local profile_path="${BASH_IT}/profiles/$name.bash_it" local profile_path="${BASH_IT}/profiles/$name.bash_it"
if [[ ! -f "$profile_path" ]]; then if [[ ! -f "$profile_path" ]]; then
echo -e "\033[91mCould not find profile \"$name\", not changing configuration...\033[m" echo -e "${echo_orange?}Could not find profile \"$name\", not changing configuration...${echo_reset_color?}"
return 1 return 1
fi fi
@ -632,7 +646,7 @@ _bash-it-profile-load() {
echo "" echo ""
echo "Profile \"$name\" enabled!" echo "Profile \"$name\" enabled!"
fi fi
} }
_bash-it-restart() { _bash-it-restart() {
_about 'restarts the shell in order to fully reload it' _about 'restarts the shell in order to fully reload it'
@ -655,38 +669,41 @@ _bash-it-reload() {
_about 'reloads a profile file' _about 'reloads a profile file'
_group 'lib' _group 'lib'
pushd "${BASH_IT}" &> /dev/null || return pushd "${BASH_IT?}" >/dev/null || return
case $OSTYPE in case $OSTYPE in
darwin*) darwin*)
# shellcheck disable=SC1090
source ~/.bash_profile source ~/.bash_profile
;; ;;
*) *)
# shellcheck disable=SC1090
source ~/.bashrc source ~/.bashrc
;; ;;
esac esac
popd &> /dev/null || return # shellcheck disable=SC2164
popd
} }
_bash-it-determine-component-status-from-path () _bash-it-determine-component-status-from-path ()
{ {
_about 'internal function used to process component name and check if its enabled' _about 'internal function used to process component name and check if its enabled'
_param '1: full path to available component file' _param '1: full path to available component file'
_example '$ _bash-it-determine-component-status-from-path "${BASH_IT}/plugins/available/git.plugin.bash' _example '$ _bash-it-determine-component-status-from-path "${BASH_IT}/plugins/available/git.plugin.bash'
# Check for both the old format without the load priority, and the extended format with the priority # Check for both the old format without the load priority, and the extended format with the priority
declare enabled_files enabled_file local enabled_files enabled_file
enabled_file="${f##*/}" enabled_file="${f##*/}"
enabled_file_clean=$(echo "$enabled_file" | sed -e 's/\(.*\)\..*\.bash/\1/g') enabled_file_clean=$(echo "$enabled_file" | sed -e 's/\(.*\)\..*\.bash/\1/g')
enabled_files=$(sort <(compgen -G "${BASH_IT}/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") | wc -l) enabled_files=$(sort <(compgen -G "${BASH_IT}/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") | wc -l)
if [ "$enabled_files" -gt 0 ]; then if [[ "$enabled_files" -gt 0 ]]; then
enabled='x' enabled='x'
else else
enabled=' ' enabled=' '
fi fi
} }
_bash-it-describe () _bash-it-describe ()
{ {
@ -702,13 +719,13 @@ _bash-it-describe ()
file_type="$3" file_type="$3"
column_header="$4" column_header="$4"
typeset f local f
typeset enabled local enabled
printf "%-20s%-10s%s\n" "$column_header" 'Enabled?' 'Description' printf "%-20s%-10s%s\n" "$column_header" 'Enabled?' 'Description'
for f in "${BASH_IT}/$subdirectory/available/"*.bash for f in "${BASH_IT}/$subdirectory/available"/*.bash
do do
_bash-it-determine-component-status-from-path "$f" _bash-it-determine-component-status-from-path "$f"
printf "%-20s%-10s%s\n" "$enabled_file_clean" " [$enabled]" "$(cat $f | metafor about-$file_type)" printf "%-20s%-10s%s\n" "$enabled_file_clean" " [$enabled]" "$(metafor "about-$file_type" < "$f")"
done done
printf '\n%s\n' "to enable $preposition $file_type, do:" printf '\n%s\n' "to enable $preposition $file_type, do:"
printf '%s\n' "$ bash-it enable $file_type <$file_type name> [$file_type name]... -or- $ bash-it enable $file_type all" printf '%s\n' "$ bash-it enable $file_type <$file_type name> [$file_type name]... -or- $ bash-it enable $file_type all"
@ -723,12 +740,12 @@ _on-disable-callback()
_example '$ _on-disable-callback gitstatus' _example '$ _on-disable-callback gitstatus'
_group 'lib' _group 'lib'
callback=$1_on_disable callback="$1_on_disable"
_command_exists $callback && $callback _command_exists "$callback" && "$callback"
} }
_disable-all () _disable-all ()
{ {
_about 'disables all bash_it components' _about 'disables all bash_it components'
_example '$ _disable-all' _example '$ _disable-all'
_group 'lib' _group 'lib'
@ -736,7 +753,7 @@ _disable-all ()
_disable-plugin "all" _disable-plugin "all"
_disable-alias "all" _disable-alias "all"
_disable-completion "all" _disable-completion "all"
} }
_disable-plugin () _disable-plugin ()
{ {
@ -745,8 +762,8 @@ _disable-plugin ()
_example '$ disable-plugin rvm' _example '$ disable-plugin rvm'
_group 'lib' _group 'lib'
_disable-thing "plugins" "plugin" $1 _disable-thing "plugins" "plugin" "$1"
_on-disable-callback $1 _on-disable-callback "$1"
} }
_disable-alias () _disable-alias ()
@ -756,7 +773,7 @@ _disable-alias ()
_example '$ disable-alias git' _example '$ disable-alias git'
_group 'lib' _group 'lib'
_disable-thing "aliases" "alias" $1 _disable-thing "aliases" "alias" "$1"
} }
_disable-completion () _disable-completion ()
@ -766,7 +783,7 @@ _disable-completion ()
_example '$ disable-completion git' _example '$ disable-completion git'
_group 'lib' _group 'lib'
_disable-thing "completion" "completion" $1 _disable-thing "completion" "completion" "$1"
} }
_disable-thing () _disable-thing ()
@ -781,35 +798,34 @@ _disable-thing ()
file_type="$2" file_type="$2"
file_entity="$3" file_entity="$3"
if [ -z "$file_entity" ]; then if [[ -z "$file_entity" ]]; then
reference "disable-$file_type" reference "disable-$file_type"
return return
fi fi
typeset f suffix local f suffix _bash_it_config_file
suffix=$(echo "$subdirectory" | sed -e 's/plugins/plugin/g') suffix="${subdirectory/plugins/plugin}"
if [ "$file_entity" = "all" ]; then if [[ "$file_entity" = "all" ]]; then
# Disable everything that's using the old structure # Disable everything that's using the old structure
for f in `compgen -G "${BASH_IT}/$subdirectory/enabled/*.${suffix}.bash"`
do for _bash_it_config_file in "${BASH_IT}/$subdirectory/enabled"/*."${suffix}.bash"; do
rm "$f" rm -f "$_bash_it_config_file"
done done
for _bash_it_config_file in "${BASH_IT}/enabled"/*".${suffix}.bash"; do
# Disable everything in the global "enabled" directory # Disable everything in the global "enabled" directory
for f in `compgen -G "${BASH_IT}/enabled/*.${suffix}.bash"` rm -f "$_bash_it_config_file"
do
rm "$f"
done done
else else
typeset plugin_global=$(command ls $ "${BASH_IT}/enabled/"[0-9]*$BASH_IT_LOAD_PRIORITY_SEPARATOR$file_entity.$suffix.bash 2>/dev/null | head -1) local plugin_global="$(command ls $ "${BASH_IT}/enabled"/[0-9]*"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${file_entity}.${suffix}.bash" 2>/dev/null | head -1)"
if [ -z "$plugin_global" ]; then if [[ -z "$plugin_global" ]]; then
# Use a glob to search for both possible patterns # Use a glob to search for both possible patterns
# 250---node.plugin.bash # 250---node.plugin.bash
# node.plugin.bash # node.plugin.bash
# Either one will be matched by this glob # Either one will be matched by this glob
typeset plugin=$(command ls $ "${BASH_IT}/$subdirectory/enabled/"{[0-9]*$BASH_IT_LOAD_PRIORITY_SEPARATOR$file_entity.$suffix.bash,$file_entity.$suffix.bash} 2>/dev/null | head -1) local plugin="$(command ls $ "${BASH_IT}/$subdirectory/enabled/"{[0-9]*"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${file_entity}.${suffix}.bash","${file_entity}.${suffix}.bash"} 2>/dev/null | head -1)"
if [ -z "$plugin" ]; then if [[ -z "$plugin" ]]; then
printf '%s\n' "sorry, $file_entity does not appear to be an enabled $file_type." printf '%s\n' "sorry, $file_entity does not appear to be an enabled $file_type."
return return
fi fi
@ -835,14 +851,14 @@ _enable-plugin ()
_example '$ enable-plugin rvm' _example '$ enable-plugin rvm'
_group 'lib' _group 'lib'
_enable-thing "plugins" "plugin" $1 $BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN _enable-thing "plugins" "plugin" "$1" "$BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN"
} }
_enable-plugins () _enable-plugins ()
{ {
_about 'alias of _enable-plugin' _about 'alias of _enable-plugin'
_enable-plugin "$@" _enable-plugin "$@"
} }
_enable-alias () _enable-alias ()
{ {
@ -851,14 +867,14 @@ _enable-alias ()
_example '$ enable-alias git' _example '$ enable-alias git'
_group 'lib' _group 'lib'
_enable-thing "aliases" "alias" $1 $BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS _enable-thing "aliases" "alias" "$1" "$BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS"
} }
_enable-aliases () _enable-aliases ()
{ {
_about 'alias of _enable-alias' _about 'alias of _enable-alias'
_enable-alias "$@" _enable-alias "$@"
} }
_enable-completion () _enable-completion ()
{ {
@ -867,7 +883,7 @@ _enable-completion ()
_example '$ enable-completion git' _example '$ enable-completion git'
_group 'lib' _group 'lib'
_enable-thing "completion" "completion" $1 $BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION _enable-thing "completion" "completion" "$1" "$BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION"
} }
_enable-thing () _enable-thing ()
@ -885,38 +901,37 @@ _enable-thing ()
file_entity="$3" file_entity="$3"
load_priority="$4" load_priority="$4"
if [ -z "$file_entity" ]; then if [[ -z "$file_entity" ]]; then
reference "enable-$file_type" reference "enable-$file_type"
return return
fi fi
if [ "$file_entity" = "all" ]; then if [[ "$file_entity" == "all" ]]; then
typeset f $file_type local _bash_it_config_file
for f in "${BASH_IT}/$subdirectory/available/"*.bash for _bash_it_config_file in "${BASH_IT}/$subdirectory/available"/*.bash; do
do to_enable="$(basename "$_bash_it_config_file" ".$file_type.bash")"
to_enable=$(basename $f .$file_type.bash) if [[ "$file_type" == "alias" ]]; then
if [ "$file_type" = "alias" ]; then to_enable="$(basename "$_bash_it_config_file" ".aliases.bash")"
to_enable=$(basename $f ".aliases.bash")
fi fi
_enable-thing $subdirectory $file_type $to_enable $load_priority _enable-thing "$subdirectory" "$file_type" "$to_enable" "$load_priority"
done done
else else
typeset to_enable=$(command ls "${BASH_IT}/$subdirectory/available/"$file_entity.*bash 2>/dev/null | head -1) local to_enable="$(command ls "${BASH_IT}/$subdirectory/available/$file_entity".*.bash 2>/dev/null | head -1)"
if [ -z "$to_enable" ]; then if [[ -z "$to_enable" ]]; then
printf '%s\n' "sorry, $file_entity does not appear to be an available $file_type." printf '%s\n' "sorry, $file_entity does not appear to be an available $file_type."
return return
fi fi
to_enable="${to_enable##*/}" to_enable="${to_enable##*/}"
# Check for existence of the file using a wildcard, since we don't know which priority might have been used when enabling it. # Check for existence of the file using a wildcard, since we don't know which priority might have been used when enabling it.
typeset enabled_plugin=$(command ls "${BASH_IT}/$subdirectory/enabled/"{[0-9][0-9][0-9]$BASH_IT_LOAD_PRIORITY_SEPARATOR$to_enable,$to_enable} 2>/dev/null | head -1) local enabled_plugin="$(command ls "${BASH_IT}/$subdirectory/enabled"/{[0-9][0-9][0-9]"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}","${to_enable}"} 2>/dev/null | head -1)"
if [ ! -z "$enabled_plugin" ] ; then if [[ -n "$enabled_plugin" ]]; then
printf '%s\n' "$file_entity is already enabled." printf '%s\n' "$file_entity is already enabled."
return return
fi fi
typeset enabled_plugin_global=$(command compgen -G "${BASH_IT}/enabled/[0-9][0-9][0-9]$BASH_IT_LOAD_PRIORITY_SEPARATOR$to_enable" 2>/dev/null | head -1) local enabled_plugin_global="$(command compgen -G "${BASH_IT}/enabled/[0-9][0-9][0-9]${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}" 2>/dev/null | head -1)"
if [ ! -z "$enabled_plugin_global" ] ; then if [[ -n "$enabled_plugin_global" ]]; then
printf '%s\n' "$file_entity is already enabled." printf '%s\n' "$file_entity is already enabled."
return return
fi fi
@ -928,7 +943,7 @@ _enable-thing ()
local_file_priority="$(_bash-it-egrep "^# BASH_IT_LOAD_PRIORITY:" "${BASH_IT}/$subdirectory/available/$to_enable" | awk -F': ' '{ print $2 }')" local_file_priority="$(_bash-it-egrep "^# BASH_IT_LOAD_PRIORITY:" "${BASH_IT}/$subdirectory/available/$to_enable" | awk -F': ' '{ print $2 }')"
use_load_priority="${local_file_priority:-$load_priority}" use_load_priority="${local_file_priority:-$load_priority}"
ln -s ../$subdirectory/available/$to_enable "${BASH_IT}/enabled/${use_load_priority}${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}" ln -s "../$subdirectory/available/$to_enable" "${BASH_IT}/enabled/${use_load_priority}${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}"
fi fi
_bash-it-clean-component-cache "${file_type}" _bash-it-clean-component-cache "${file_type}"
@ -951,7 +966,7 @@ _help-aliases()
_example '$ alias-help' _example '$ alias-help'
_example '$ alias-help git' _example '$ alias-help git'
if [ -n "$1" ]; then if [[ -n "$1" ]]; then
case $1 in case $1 in
custom) custom)
alias_path='custom.aliases.bash' alias_path='custom.aliases.bash'
@ -960,16 +975,16 @@ _help-aliases()
alias_path="available/$1.aliases.bash" alias_path="available/$1.aliases.bash"
;; ;;
esac esac
cat "${BASH_IT}/aliases/$alias_path" | metafor alias | sed "s/$/'/" metafor alias < "${BASH_IT}/aliases/$alias_path" | sed "s/$/'/"
else else
typeset f local f
for f in `sort <(compgen -G "${BASH_IT}/aliases/enabled/*") <(compgen -G "${BASH_IT}/enabled/*.aliases.bash")` for f in "${BASH_IT}/aliases/enabled"/* "${BASH_IT}/enabled"/*."aliases.bash"; do
do [[ -f "$f" ]] || continue
_help-list-aliases $f _help-list-aliases "$f"
done done
if [ -e "${BASH_IT}/aliases/custom.aliases.bash" ]; then if [[ -e "${BASH_IT}/aliases/custom.aliases.bash" ]]; then
_help-list-aliases "${BASH_IT}/aliases/custom.aliases.bash" _help-list-aliases "${BASH_IT}/aliases/custom.aliases.bash"
fi fi
fi fi
@ -977,10 +992,10 @@ _help-aliases()
_help-list-aliases () _help-list-aliases ()
{ {
typeset file=$(basename $1 | sed -e 's/[0-9]*[-]*\(.*\)\.aliases\.bash/\1/g') local file="$(basename "$1" | sed -e 's/[0-9]*[-]*\(.*\)\.aliases\.bash/\1/g')"
printf '\n\n%s:\n' "${file}" printf '\n\n%s:\n' "${file}"
# metafor() strips trailing quotes, restore them with sed.. # metafor() strips trailing quotes, restore them with sed..
cat $1 | metafor alias | sed "s/$/'/" metafor alias < "$1" | sed "s/$/'/"
} }
_help-plugins() _help-plugins()
@ -990,30 +1005,28 @@ _help-plugins()
# display a brief progress message... # display a brief progress message...
printf '%s' 'please wait, building help...' printf '%s' 'please wait, building help...'
typeset grouplist=$(mktemp -t grouplist.XXXXXX) local grouplist=$(mktemp -t grouplist.XXXXXX)
typeset func local func
for func in $(_typeset_functions) for func in $(_typeset_functions); do
do local group="$(declare -f "$func" | metafor group)"
typeset group="$(typeset -f $func | metafor group)" if [[ -z "$group" ]]; then
if [ -z "$group" ]; then
group='misc' group='misc'
fi fi
typeset about="$(typeset -f $func | metafor about)" local about="$(declare -f "$func" | metafor about)"
_letterpress "$about" $func >> $grouplist.$group _letterpress "$about" "$func" >> "$grouplist.$group"
echo $grouplist.$group >> $grouplist echo "$grouplist.$group" >> "$grouplist"
done done
# clear progress message # clear progress message
printf '\r%s\n' ' ' printf '\r%s\n' ' '
typeset group local group
typeset gfile local gfile
for gfile in $(cat $grouplist | sort | uniq) while IFS= read -r gfile; do
do
printf '%s\n' "${gfile##*.}:" printf '%s\n' "${gfile##*.}:"
cat $gfile cat "$gfile"
printf '\n' printf '\n'
rm $gfile 2> /dev/null rm "$gfile" 2> /dev/null
done | less done < <(sort -u "$grouplist") | less
rm $grouplist 2> /dev/null rm "$grouplist" 2> /dev/null
} }
_help-profile () { _help-profile () {
@ -1025,7 +1038,7 @@ _help-profile () {
echo "Use 'bash-it profile save foo' to save the current configuration into a profile named 'foo'." echo "Use 'bash-it profile save foo' to save the current configuration into a profile named 'foo'."
echo "Use 'bash-it profile load foo' to load an existing profile named 'foo'." echo "Use 'bash-it profile load foo' to load an existing profile named 'foo'."
echo "Use 'bash-it profile rm foo' to remove an existing profile named 'foo'." echo "Use 'bash-it profile rm foo' to remove an existing profile named 'foo'."
} }
_help-update () { _help-update () {
_about 'help message for update command' _about 'help message for update command'
@ -1050,22 +1063,21 @@ all_groups ()
declare -f | metafor group | sort -u declare -f | metafor group | sort -u
} }
if ! type pathmunge > /dev/null 2>&1 if ! _command_exists pathmunge
then then function pathmunge () {
function pathmunge () {
about 'prevent duplicate directories in you PATH variable' about 'prevent duplicate directories in you PATH variable'
group 'helpers' group 'helpers'
example 'pathmunge /path/to/dir is equivalent to PATH=/path/to/dir:$PATH' example 'pathmunge /path/to/dir is equivalent to PATH=/path/to/dir:$PATH'
example 'pathmunge /path/to/dir after is equivalent to PATH=$PATH:/path/to/dir' example 'pathmunge /path/to/dir after is equivalent to PATH=$PATH:/path/to/dir'
if ! [[ $PATH =~ (^|:)$1($|:) ]] ; then if [[ -d "${1:-}" && ! $PATH =~ (^|:)$1($|:) ]]; then
if [ "$2" = "after" ] ; then if [[ "${2:-}" == "after" ]]; then
export PATH=$PATH:$1 export PATH=$PATH:$1
else else
export PATH=$1:$PATH export PATH=$1:$PATH
fi fi
fi fi
} }
fi fi
# `_bash-it-find-in-ancestor` uses the shell's ability to run a function in # `_bash-it-find-in-ancestor` uses the shell's ability to run a function in

View File

View File

View File

View File

@ -3,6 +3,22 @@
load ../test_helper load ../test_helper
load ../test_helper_libs load ../test_helper_libs
function local_setup()
{
setup_test_fixture
}
function setup_go_path()
{
local go_path="$1"
# Make sure that the requested GO folder is available
assert_dir_exist "$go_path/bin"
# Make sure that the requested GO folder is on the path
export GOPATH="$go_path:${GOPATH:-}"
}
# We test `go version` in each test to account for users with goenv and no system go. # We test `go version` in each test to account for users with goenv and no system go.
@test 'ensure _bash-it-gopath-pathmunge is defined' { @test 'ensure _bash-it-gopath-pathmunge is defined' {
@ -14,42 +30,47 @@ load ../test_helper_libs
@test 'plugins go: single entry in GOPATH' { @test 'plugins go: single entry in GOPATH' {
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found' { _command_exists go && go version &>/dev/null; } || skip 'golang not found'
export GOPATH="/foo" setup_go_path "$BASH_IT/test/fixtures/go/gopath"
load ../../plugins/available/go.plugin load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1 <<<$PATH)" "/foo/bin" assert_equal "$(cut -d':' -f1 <<<$PATH)" "$BASH_IT/test/fixtures/go/gopath/bin"
} }
@test 'plugins go: single entry in GOPATH, with space' { @test 'plugins go: single entry in GOPATH, with space' {
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found' { _command_exists go && go version &>/dev/null; } || skip 'golang not found'
export GOPATH="/foo bar" setup_go_path "$BASH_IT/test/fixtures/go/go path"
load ../../plugins/available/go.plugin load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1 <<<$PATH)" "/foo bar/bin" assert_equal "$(cut -d':' -f1 <<<$PATH)" "$BASH_IT/test/fixtures/go/go path/bin"
} }
@test 'plugins go: single entry in GOPATH, with escaped space' { @test 'plugins go: single entry in GOPATH, with escaped space' {
skip 'huh?'
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found' { _command_exists go && go version &>/dev/null; } || skip 'golang not found'
export GOPATH="/foo\ bar" setup_go_path "$BASH_IT/test/fixtures/go/go\ path"
load ../../plugins/available/go.plugin load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1 <<<$PATH)" "/foo\ bar/bin" assert_equal "$(cut -d':' -f1 <<<$PATH)" "$BASH_IT/test/fixtures/go/go\ path/bin"
} }
@test 'plugins go: multiple entries in GOPATH' { @test 'plugins go: multiple entries in GOPATH' {
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found' { _command_exists go && go version &>/dev/null; } || skip 'golang not found'
export GOPATH="/foo:/bar" setup_go_path "$BASH_IT/test/fixtures/go/gopath"
setup_go_path "$BASH_IT/test/fixtures/go/gopath2"
load ../../plugins/available/go.plugin load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "/foo/bin:/bar/bin" assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/gopath2/bin:$BASH_IT/test/fixtures/go/gopath/bin"
} }
@test 'plugins go: multiple entries in GOPATH, with space' { @test 'plugins go: multiple entries in GOPATH, with space' {
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found' { _command_exists go && go version &>/dev/null; } || skip 'golang not found'
export GOPATH="/foo:/foo bar" setup_go_path "$BASH_IT/test/fixtures/go/gopath"
setup_go_path "$BASH_IT/test/fixtures/go/go path"
load ../../plugins/available/go.plugin load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "/foo/bin:/foo bar/bin" assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/go path/bin:$BASH_IT/test/fixtures/go/gopath/bin"
} }
@test 'plugins go: multiple entries in GOPATH, with escaped space' { @test 'plugins go: multiple entries in GOPATH, with escaped space' {
skip 'huh?'
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found' { _command_exists go && go version &>/dev/null; } || skip 'golang not found'
export GOPATH="/foo:/foo\ bar" setup_go_path "$BASH_IT/test/fixtures/go/gopath"
setup_go_path "$BASH_IT/test/fixtures/go/go path"
load ../../plugins/available/go.plugin load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "/foo/bin:/foo\ bar/bin" assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/go\ path/bin:$BASH_IT/test/fixtures/go/gopath/bin"
} }

View File

@ -6,6 +6,8 @@ load ../test_helper_libs
function local_setup { function local_setup {
setup_test_fixture setup_test_fixture
_command_exists "ruby" && mkdir -p "$(ruby -e 'print Gem.user_dir')/bin"
export OLD_PATH="$PATH" export OLD_PATH="$PATH"
export PATH="/usr/bin:/bin:/usr/sbin" export PATH="/usr/bin:/bin:/usr/sbin"
} }