diff --git a/.gitignore b/.gitignore index b748d384..191a765c 100755 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ plugins/custom.plugins.bash .*.un~ bats .idea +enabled/* diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index d9c0abf6..ba9efe60 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -27,17 +27,19 @@ This order is subject to change. For `aliases`, `plugins` and `completions`, the following rules are applied that influence the load order: -* Each type has its own `enabled` directory, into which the enabled components are linked into. Enabled plugins are symlinked from `$BASH_IT/plugins/available` to `$BASH_IT/plugins/enabled` for example. -* Within each of the `enabled` directories, the files are loaded in alphabetical order. +* There is a global `enabled` directory, which the enabled components are linked into. Enabled plugins are symlinked from `$BASH_IT/plugins/available` to `$BASH_IT/enabled` for example. All component types are linked into the same common `$BASH_IT/enabled` directory. +* Within the common `enabled` directories, the files are loaded in alphabetical order, which is based on the item's load priority (see next item). * When enabling a component, a _load priority_ is assigned to the file. The following default priorities are used: * Aliases: 150 * Plugins: 250 * Completions: 350 -* When symlinking a component into an `enabled` directory, the load priority is used as a prefix for the linked name, separated with three dashes from the name of the component. The `node.plugin.bash` would be symlinked to `250---node.plugin.bash` for example. +* When symlinking a component into the `enabled` directory, the load priority is used as a prefix for the linked name, separated with three dashes from the name of the component. The `node.plugin.bash` would be symlinked to `250---node.plugin.bash` for example. * Each file can override the default load priority by specifying a new value. To do this, the file needs to include a comment in the following form. This example would cause the `node.plugin.bash` (if included in that file) to be linked to `225---node.plugin.bash`: ```bash # BASH_IT_LOAD_PRIORITY: 225 ``` +Having the order based on a numeric priority in a common directory allows for more flexibility. While in general, aliases are loaded first (since their default priority is 150), it's possible to load some aliases after the plugins, or some plugins after completions by setting the items' load priority. This is more flexible than a fixed type-based order or a strict alphabetical order based on name. + These items are subject to change. When making changes to the internal functionality, this page needs to be updated as well. diff --git a/bash_it.sh b/bash_it.sh index 725e62a2..5371fc9c 100755 --- a/bash_it.sh +++ b/bash_it.sh @@ -29,6 +29,7 @@ then fi # Load composure first, so we support function metadata +# shellcheck source=./lib/composure.bash source "${BASH_IT}/lib/composure.bash" # support 'plumbing' metadata @@ -40,10 +41,14 @@ APPEARANCE_LIB="${BASH_IT}/lib/appearance.bash" for config_file in $LIB do if [ $config_file != $APPEARANCE_LIB ]; then + # shellcheck disable=SC1090 source $config_file fi done +# Load the global "enabled" directory +_load_global_bash_it_files + # Load enabled aliases, completion, plugins for file_type in "aliases" "plugins" "completion" do @@ -51,10 +56,13 @@ do done # Load colors first so they can be used in base theme +# shellcheck source=./themes/colors.theme.bash source "${BASH_IT}/themes/colors.theme.bash" +# shellcheck source=./themes/base.theme.bash source "${BASH_IT}/themes/base.theme.bash" # appearance (themes) now, after all dependencies +# shellcheck source=./lib/appearance.bash source $APPEARANCE_LIB # Load custom aliases, completion, plugins @@ -62,6 +70,7 @@ for file_type in "aliases" "completion" "plugins" do if [ -e "${BASH_IT}/${file_type}/custom.${file_type}.bash" ] then + # shellcheck disable=SC1090 source "${BASH_IT}/${file_type}/custom.${file_type}.bash" fi done @@ -71,24 +80,31 @@ CUSTOM="${BASH_IT_CUSTOM:=${BASH_IT}/custom}/*.bash ${BASH_IT_CUSTOM:=${BASH_IT} for config_file in $CUSTOM do if [ -e "${config_file}" ]; then + # shellcheck disable=SC1090 source $config_file fi done unset config_file if [[ $PROMPT ]]; then - export PS1="\["$PROMPT"\]" + export PS1="\[""$PROMPT""\]" fi # Adding Support for other OSes PREVIEW="less" -[ -s /usr/bin/gloobus-preview ] && PREVIEW="gloobus-preview" -[ -s /Applications/Preview.app ] && PREVIEW="/Applications/Preview.app" + +if [ -s /usr/bin/gloobus-preview ]; then + PREVIEW="gloobus-preview" +elif [ -s /Applications/Preview.app ]; then + # shellcheck disable=SC2034 + PREVIEW="/Applications/Preview.app" +fi # Load all the Jekyll stuff if [ -e "$HOME/.jekyllconfig" ] then + # shellcheck disable=SC1090 . "$HOME/.jekyllconfig" fi diff --git a/completion/available/bash-it.completion.bash b/completion/available/bash-it.completion.bash index 5222a484..e59b3fb1 100644 --- a/completion/available/bash-it.completion.bash +++ b/completion/available/bash-it.completion.bash @@ -2,112 +2,119 @@ _bash-it-comp-enable-disable() { - local enable_disable_args="alias plugin completion" - COMPREPLY=( $(compgen -W "${enable_disable_args}" -- ${cur}) ) + local enable_disable_args="alias completion plugin" + COMPREPLY=( $(compgen -W "${enable_disable_args}" -- ${cur}) ) } _bash-it-comp-list-available-not-enabled() { - subdirectory="$1" + subdirectory="$1" - local available_things=$(for f in `ls -1 "${BASH_IT}/$subdirectory/available/"*.bash 2>/dev/null`; - do - if [ ! -e "${BASH_IT}/$subdirectory/enabled/"$(basename $f) ] && [ ! -e "${BASH_IT}/$subdirectory/enabled/"*$BASH_IT_LOAD_PRIORITY_SEPARATOR$(basename $f) ] - then - basename $f | cut -d'.' -f1 - fi - done) + local available_things - COMPREPLY=( $(compgen -W "all ${available_things}" -- ${cur}) ) + available_things=$(for f in `compgen -G "${BASH_IT}/$subdirectory/available/*.bash" | sort -d`; + do + file_entity=$(basename $f) + + typeset enabled_component=$(command ls "${BASH_IT}/$subdirectory/enabled/"{[0-9]*$BASH_IT_LOAD_PRIORITY_SEPARATOR$file_entity,$file_entity} 2>/dev/null | head -1) + typeset enabled_component_global=$(command ls "${BASH_IT}/enabled/"[0-9]*$BASH_IT_LOAD_PRIORITY_SEPARATOR$file_entity 2>/dev/null | head -1) + + if [ -z "$enabled_component" ] && [ -z "$enabled_component_global" ] + then + basename $f | sed -e 's/\(.*\)\..*\.bash/\1/g' + fi + done) + + COMPREPLY=( $(compgen -W "all ${available_things}" -- ${cur}) ) } _bash-it-comp-list-enabled() { - subdirectory="$1" + local subdirectory="$1" + local suffix enabled_things - local enabled_things=$(for f in `ls -1 "${BASH_IT}/$subdirectory/enabled/"*.bash 2>/dev/null`; - do - basename $f | cut -d'.' -f1 | sed -e "s/^[0-9]*---//g" - done) + suffix=$(echo "$subdirectory" | sed -e 's/plugins/plugin/g') - COMPREPLY=( $(compgen -W "all ${enabled_things}" -- ${cur}) ) + enabled_things=$(for f in `sort -d <(compgen -G "${BASH_IT}/$subdirectory/enabled/*.${suffix}.bash") <(compgen -G "${BASH_IT}/enabled/*.${suffix}.bash")`; + do + basename $f | sed -e 's/\(.*\)\..*\.bash/\1/g' | sed -e "s/^[0-9]*---//g" + done) + + COMPREPLY=( $(compgen -W "all ${enabled_things}" -- ${cur}) ) } _bash-it-comp-list-available() { - subdirectory="$1" + subdirectory="$1" - local enabled_things=$(for f in `ls -1 "${BASH_IT}/$subdirectory/available/"*.bash`; - do - basename $f | cut -d'.' -f1 - done) + local enabled_things - COMPREPLY=( $(compgen -W "${enabled_things}" -- ${cur}) ) + enabled_things=$(for f in `compgen -G "${BASH_IT}/$subdirectory/available/*.bash" | sort -d`; + do + basename $f | sed -e 's/\(.*\)\..*\.bash/\1/g' + done) + + COMPREPLY=( $(compgen -W "${enabled_things}" -- ${cur}) ) } _bash-it-comp() { - local cur prev opts prevprev - COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - prev="${COMP_WORDS[COMP_CWORD-1]}" - chose_opt="${COMP_WORDS[1]}" - file_type="${COMP_WORDS[2]}" - opts="help show enable disable update search migrate" - case "${chose_opt}" in - show) - local show_args="plugins aliases completions" - COMPREPLY=( $(compgen -W "${show_args}" -- ${cur}) ) - return 0 - ;; - help) - local help_args="plugins aliases completions migrate update" - COMPREPLY=( $(compgen -W "${help_args}" -- ${cur}) ) - return 0 - ;; + local cur prev opts + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + chose_opt="${COMP_WORDS[1]}" + file_type="${COMP_WORDS[2]}" + opts="disable enable help migrate search show update" + case "${chose_opt}" in + show) + local show_args="aliases completions plugins" + COMPREPLY=( $(compgen -W "${show_args}" -- ${cur}) ) + return 0 + ;; + help) + if [ x"${prev}" == x"aliases" ]; then + _bash-it-comp-list-available aliases + return 0 + else + local help_args="aliases completions migrate plugins update" + COMPREPLY=( $(compgen -W "${help_args}" -- ${cur}) ) + return 0 + fi + ;; update | search | migrate) return 0 ;; - enable | disable) - if [ x"${chose_opt}" == x"enable" ];then - suffix="available-not-enabled" - else - suffix="enabled" - fi - case "${file_type}" in - alias) - _bash-it-comp-list-${suffix} aliases - return 0 - ;; - plugin) - _bash-it-comp-list-${suffix} plugins - return 0 - ;; - completion) - _bash-it-comp-list-${suffix} completion - return 0 - ;; - *) - _bash-it-comp-enable-disable - return 0 - ;; - esac - ;; - aliases) - prevprev="${COMP_WORDS[COMP_CWORD-2]}" + enable | disable) + if [ x"${chose_opt}" == x"enable" ];then + suffix="available-not-enabled" + else + suffix="enabled" + fi + case "${file_type}" in + alias) + _bash-it-comp-list-${suffix} aliases + return 0 + ;; + plugin) + _bash-it-comp-list-${suffix} plugins + return 0 + ;; + completion) + _bash-it-comp-list-${suffix} completion + return 0 + ;; + *) + _bash-it-comp-enable-disable + return 0 + ;; + esac + ;; + esac - case "${prevprev}" in - help) - _bash-it-comp-list-available aliases - return 0 - ;; - esac - ;; - esac + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - - return 0 + return 0 } # Activate completion for bash-it and its common misspellings diff --git a/lib/helpers.bash b/lib/helpers.bash index 0184455e..0405eede 100644 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -1,8 +1,19 @@ +#!/usr/bin/env bash + 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_COMPLETION=${BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION:-350} BASH_IT_LOAD_PRIORITY_SEPARATOR="---" +function _command_exists () +{ + _about 'checks for existence of a command' + _param '1: command to check' + _example '$ _command_exists ls && echo exists' + _group 'lib' + type "$1" &> /dev/null ; +} + # Helper function loading various enable-able files function _load_bash_it_files() { subdirectory="$1" @@ -18,6 +29,20 @@ function _load_bash_it_files() { fi } +function _load_global_bash_it_files() { + # In the new structure + if [ -d "${BASH_IT}/enabled" ] + then + FILES="${BASH_IT}/enabled/*.bash" + for config_file in $FILES + do + if [ -e "${config_file}" ]; then + source $config_file + fi + done + fi +} + # Function for reloading aliases function reload_aliases() { _load_bash_it_files "aliases" @@ -61,7 +86,7 @@ bash-it () help) func=_help-$component;; search) - _bash-it-search $component $* + _bash-it-search $component "$@" return;; update) func=_bash-it_update;; @@ -87,7 +112,7 @@ bash-it () fi fi - if [ x"$verb" == x"enable" -o x"$verb" == x"disable" ];then + if [ x"$verb" == x"enable" ] || [ x"$verb" == x"disable" ]; then # Automatically run a migration if required _bash-it-migrate @@ -96,7 +121,7 @@ bash-it () $func $arg done else - $func $* + $func "$@" fi } @@ -136,12 +161,17 @@ _bash-it_update() { _about 'updates Bash-it' _group 'lib' - cd "${BASH_IT}" + cd "${BASH_IT}" || return + if [ -z $BASH_IT_REMOTE ]; then BASH_IT_REMOTE="origin" fi + git fetch &> /dev/null - local status="$(git rev-list master..${BASH_IT_REMOTE}/master 2> /dev/null)" + + declare status + status="$(git rev-list master..${BASH_IT_REMOTE}/master 2> /dev/null)" + if [[ -n "${status}" ]]; then git pull --rebase &> /dev/null if [[ $? -eq 0 ]]; then @@ -158,37 +188,43 @@ _bash-it_update() { else echo "Bash-it is up to date, nothing to do!" fi - cd - &> /dev/null + cd - &> /dev/null || return } _bash-it-migrate() { _about 'migrates Bash-it configuration from a previous format to the current one' _group 'lib' + declare migrated_something + migrated_something=false + for file_type in "aliases" "plugins" "completion" do - shopt -s nullglob - for f in "${BASH_IT}/$file_type/enabled/"*.bash + for f in `sort <(compgen -G "${BASH_IT}/$file_type/enabled/*.bash")` do typeset ff=$(basename $f) - # Only process the ones that don't use the new structure - if ! [[ $ff =~ ^[0-9]*$BASH_IT_LOAD_PRIORITY_SEPARATOR.*\.bash$ ]] ; then - # Get the type of component from the extension - typeset single_type=$(echo $ff | sed -e 's/.*\.\(.*\)\.bash/\1/g' | sed 's/aliases/alias/g') - typeset component_name=$(echo $ff | cut -d'.' -f1) + # Get the type of component from the extension + typeset single_type=$(echo $ff | sed -e 's/.*\.\(.*\)\.bash/\1/g' | sed 's/aliases/alias/g') + # Cut off the optional "250---" prefix and the suffix + typeset component_name=$(echo $ff | sed -e 's/[0-9]*[-]*\(.*\)\..*\.bash/\1/g') - echo "Migrating $single_type $component_name." + migrated_something=true - disable_func="_disable-$single_type" - enable_func="_enable-$single_type" + echo "Migrating $single_type $component_name." - $disable_func $component_name - $enable_func $component_name - fi + disable_func="_disable-$single_type" + enable_func="_enable-$single_type" + + $disable_func $component_name + $enable_func $component_name done - shopt -u nullglob done + + if [ "$migrated_something" = "true" ]; then + echo "" + echo "If any migration errors were reported, please try the following: reload && bash-it migrate" + fi } _bash-it-describe () @@ -211,7 +247,11 @@ _bash-it-describe () for f in "${BASH_IT}/$subdirectory/available/"*.bash do # Check for both the old format without the load priority, and the extended format with the priority - if [ -e "${BASH_IT}/$subdirectory/enabled/"$(basename $f) ] || [ -e "${BASH_IT}/$subdirectory/enabled/"*$BASH_IT_LOAD_PRIORITY_SEPARATOR$(basename $f) ]; then + declare enabled_files enabled_file + enabled_file=$(basename $f) + 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 enabled='x' else enabled=' ' @@ -271,29 +311,37 @@ _disable-thing () return fi + typeset f suffix + suffix=$(echo "$subdirectory" | sed -e 's/plugins/plugin/g') + if [ "$file_entity" = "all" ]; then - typeset f $file_type - for f in "${BASH_IT}/$subdirectory/available/"*.bash + # Disable everything that's using the old structure + for f in `compgen -G "${BASH_IT}/$subdirectory/enabled/*.${suffix}.bash"` do - plugin=$(basename $f) - if [ -e "${BASH_IT}/$subdirectory/enabled/$plugin" ]; then - rm "${BASH_IT}/$subdirectory/enabled/$(basename $plugin)" - fi - if [ -e "${BASH_IT}/$subdirectory/enabled/"*$BASH_IT_LOAD_PRIORITY_SEPARATOR$plugin ]; then - rm "${BASH_IT}/$subdirectory/enabled/"*$BASH_IT_LOAD_PRIORITY_SEPARATOR$(basename $plugin) - fi + rm "$f" + done + + # Disable everything in the global "enabled" directory + for f in `compgen -G "${BASH_IT}/enabled/*.${suffix}.bash"` + do + rm "$f" done else - # Use a glob to search for both possible patterns - # 250---node.plugin.bash - # node.plugin.bash - # 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.*bash,$file_entity.*bash} 2>/dev/null | head -1) - if [ -z "$plugin" ]; then - printf '%s\n' "sorry, $file_entity does not appear to be an enabled $file_type." - return + typeset 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 + # Use a glob to search for both possible patterns + # 250---node.plugin.bash + # node.plugin.bash + # 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) + if [ -z "$plugin" ]; then + printf '%s\n' "sorry, $file_entity does not appear to be an enabled $file_type." + return + fi + rm "${BASH_IT}/$subdirectory/enabled/$(basename $plugin)" + else + rm "${BASH_IT}/enabled/$(basename $plugin_global)" fi - rm "${BASH_IT}/$subdirectory/enabled/$(basename $plugin)" fi if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then @@ -369,19 +417,26 @@ _enable-thing () to_enable=$(basename $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. - typeset enabled_plugin=$(command ls "${BASH_IT}/$subdirectory/enabled/"{[0-9]*$BASH_IT_LOAD_PRIORITY_SEPARATOR$to_enable,$to_enable} 2>/dev/null | head -1) + 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) if [ ! -z "$enabled_plugin" ] ; then printf '%s\n' "$file_entity is already enabled." return fi - mkdir -p "${BASH_IT}/$subdirectory/enabled" + 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) + if [ ! -z "$enabled_plugin_global" ] ; then + printf '%s\n' "$file_entity is already enabled." + return + fi + + mkdir -p "${BASH_IT}/enabled" # Load the priority from the file if it present there - local local_file_priority=$(grep -E "^# BASH_IT_LOAD_PRIORITY:" "${BASH_IT}/$subdirectory/available/$to_enable" | awk -F': ' '{ print $2 }') - local use_load_priority=${local_file_priority:-$load_priority} + declare local_file_priority use_load_priority + local_file_priority=$(grep -E "^# BASH_IT_LOAD_PRIORITY:" "${BASH_IT}/$subdirectory/available/$to_enable" | awk -F': ' '{ print $2 }') + use_load_priority=${local_file_priority:-$load_priority} - ln -s ../available/$to_enable "${BASH_IT}/$subdirectory/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 if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then @@ -418,18 +473,22 @@ _help-aliases() cat "${BASH_IT}/aliases/$alias_path" | metafor alias | sed "s/$/'/" else typeset f - for f in "${BASH_IT}/aliases/enabled/"* + + for f in `sort <(compgen -G "${BASH_IT}/aliases/enabled/*") <(compgen -G "${BASH_IT}/enabled/*.aliases.bash")` do _help-list-aliases $f done - _help-list-aliases "${BASH_IT}/aliases/custom.aliases.bash" + + if [ -e "${BASH_IT}/aliases/custom.aliases.bash" ]; then + _help-list-aliases "${BASH_IT}/aliases/custom.aliases.bash" + fi fi } _help-list-aliases () { - typeset file=$(basename $1) - printf '\n\n%s:\n' "${file%%.*}" + typeset file=$(basename $1 | sed -e 's/[0-9]*[-]*\(.*\)\.aliases\.bash/\1/g') + printf '\n\n%s:\n' "${file}" # metafor() strips trailing quotes, restore them with sed.. cat $1 | metafor alias | sed "s/$/'/" } diff --git a/plugins/available/battery.plugin.bash b/plugins/available/battery.plugin.bash index dd941372..0391ca10 100644 --- a/plugins/available/battery.plugin.bash +++ b/plugins/available/battery.plugin.bash @@ -2,23 +2,23 @@ cite about-plugin about-plugin 'display info about your battery charge level' ac_adapter_connected(){ - if command_exists upower; + if _command_exists upower; then upower -i $(upower -e | grep BAT) | grep 'state' | grep -q 'charging\|fully-charged' return $? - elif command_exists acpi; + elif _command_exists acpi; then acpi -a | grep -q "on-line" return $? - elif command_exists pmset; + elif _command_exists pmset; then pmset -g batt | grep -q 'AC Power' return $? - elif command_exists ioreg; + elif _command_exists ioreg; then ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = Yes' return $? - elif command_exists WMIC; + elif _command_exists WMIC; then WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=2' return $? @@ -26,23 +26,23 @@ ac_adapter_connected(){ } ac_adapter_disconnected(){ - if command_exists upower; + if _command_exists upower; then upower -i $(upower -e | grep BAT) | grep 'state' | grep -q 'discharging' return $? - elif command_exists acpi; + elif _command_exists acpi; then acpi -a | grep -q "off-line" return $? - elif command_exists pmset; + elif _command_exists pmset; then pmset -g batt | grep -q 'Battery Power' return $? - elif command_exists ioreg; + elif _command_exists ioreg; then ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = No' return $? - elif command_exists WMIC; + elif _command_exists WMIC; then WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=1' return $? @@ -52,12 +52,12 @@ ac_adapter_disconnected(){ battery_percentage(){ about 'displays battery charge as a percentage of full (100%)' group 'battery' - - if command_exists upower; + + if _command_exists upower; then local UPOWER_OUTPUT=$(upower --show-info $(upower --enumerate | grep BAT) | grep percentage | tail --bytes 5) echo ${UPOWER_OUTPUT: : -1} - elif command_exists acpi; + elif _command_exists acpi; then local ACPI_OUTPUT=$(acpi -b) case $ACPI_OUTPUT in @@ -72,7 +72,7 @@ battery_percentage(){ ;; esac ;; - + *" Charging"* | *" Discharging"*) local PERC_OUTPUT=$(echo $ACPI_OUTPUT | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}' ) echo ${PERC_OUTPUT} @@ -84,7 +84,7 @@ battery_percentage(){ echo '-1' ;; esac - elif command_exists pmset; + elif _command_exists pmset; then local PMSET_OUTPUT=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p') case $PMSET_OUTPUT in @@ -95,7 +95,7 @@ battery_percentage(){ echo $PMSET_OUTPUT | head -c 2 ;; esac - elif command_exists ioreg; + elif _command_exists ioreg; then local IOREG_OUTPUT=$(ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%05.2f%%"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}') case $IOREG_OUTPUT in @@ -106,7 +106,7 @@ battery_percentage(){ echo $IOREG_OUTPUT | head -c 2 ;; esac - elif command_exists WMIC; + elif _command_exists WMIC; then local WINPC=$(echo porcent=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List) | grep -o '[0-9]*') case $WINPC in @@ -125,7 +125,7 @@ battery_percentage(){ battery_charge(){ about 'graphical display of your battery charge' group 'battery' - + # Full char local F_C='▸' # Depleted char @@ -136,7 +136,7 @@ battery_charge(){ local DANGER_COLOR="${red}" local BATTERY_OUTPUT="${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${D_C}" local BATTERY_PERC=$(battery_percentage) - + case $BATTERY_PERC in no) echo "" diff --git a/test/README.md b/test/README.md index 281b1b6f..d227e3c1 100644 --- a/test/README.md +++ b/test/README.md @@ -1,4 +1,13 @@ ## Testing with [Bats](https://github.com/sstephenson/bats#installing-bats-from-source) + +To execute the unit tests, please run the `run` script: + +```bash +# If you are in the `test` directory: +./run + +# If you are in the root `.bash_it` directory: +test/run ``` -bats test/{lib,plugins} -``` + +The `run` script will automatically install [Bats](https://github.com/sstephenson/bats#installing-bats-from-source) if it is not already present, and will then run all tests found under the `test` directory, including subdirectories. diff --git a/test/bash_it/bash_it.bats b/test/bash_it/bash_it.bats new file mode 100644 index 00000000..ccd0c9a5 --- /dev/null +++ b/test/bash_it/bash_it.bats @@ -0,0 +1,369 @@ +#!/usr/bin/env bats + +load ../test_helper +load ../../lib/composure + +function local_setup { + mkdir -p "$BASH_IT" + lib_directory="$(cd "$(dirname "$0")" && pwd)" + # Use rsync to copy Bash-it to the temp folder + # rsync is faster than cp, since we can exclude the large ".git" folder + rsync -qavrKL -d --delete-excluded --exclude=.git $lib_directory/../.. "$BASH_IT" + + rm -rf "$BASH_IT"/enabled + rm -rf "$BASH_IT"/aliases/enabled + rm -rf "$BASH_IT"/completion/enabled + rm -rf "$BASH_IT"/plugins/enabled + + # Copy the test fixture to the Bash-it folder + rsync -a "$BASH_IT/test/fixtures/bash_it/" "$BASH_IT/" + + # Don't pollute the user's actual $HOME directory + # Use a test home directory instead + export BASH_IT_TEST_CURRENT_HOME="${HOME}" + export BASH_IT_TEST_HOME="$(cd "${BASH_IT}/.." && pwd)/BASH_IT_TEST_HOME" + mkdir -p "${BASH_IT_TEST_HOME}" + export HOME="${BASH_IT_TEST_HOME}" +} + +function local_teardown { + export HOME="${BASH_IT_TEST_CURRENT_HOME}" + + rm -rf "${BASH_IT_TEST_HOME}" + + assert_equal "${BASH_IT_TEST_CURRENT_HOME}" "${HOME}" +} + +@test "bash-it: verify that the test fixture is available" { + assert [ -e "$BASH_IT/aliases/available/a.aliases.bash" ] + assert [ -e "$BASH_IT/aliases/available/b.aliases.bash" ] +} + +@test "bash-it: load aliases in order" { + mkdir -p $BASH_IT/aliases/enabled + mkdir -p $BASH_IT/plugins/enabled + + ln -s $BASH_IT/plugins/available/base.plugin.bash $BASH_IT/plugins/enabled/250---base.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/250---base.plugin.bash" ] + + ln -s $BASH_IT/aliases/available/a.aliases.bash $BASH_IT/aliases/enabled/150---a.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---a.aliases.bash" ] + ln -s $BASH_IT/aliases/available/b.aliases.bash $BASH_IT/aliases/enabled/150---b.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---b.aliases.bash" ] + + # The `test_alias` alias should not exist + run alias test_alias &> /dev/null + assert_failure + + load "$BASH_IT/bash_it.sh" + + run alias test_alias &> /dev/null + assert_success + assert_line "0" "alias test_alias='b'" +} + +@test "bash-it: load aliases in priority order" { + mkdir -p $BASH_IT/aliases/enabled + mkdir -p $BASH_IT/plugins/enabled + + ln -s $BASH_IT/plugins/available/base.plugin.bash $BASH_IT/plugins/enabled/250---base.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/250---base.plugin.bash" ] + + ln -s $BASH_IT/aliases/available/a.aliases.bash $BASH_IT/aliases/enabled/175---a.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/175---a.aliases.bash" ] + ln -s $BASH_IT/aliases/available/b.aliases.bash $BASH_IT/aliases/enabled/150---b.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---b.aliases.bash" ] + + # The `test_alias` alias should not exist + run alias test_alias &> /dev/null + assert_failure + + load "$BASH_IT/bash_it.sh" + + run alias test_alias &> /dev/null + assert_success + assert_line "0" "alias test_alias='a'" +} + +@test "bash-it: load aliases and plugins in priority order" { + mkdir -p $BASH_IT/aliases/enabled + mkdir -p $BASH_IT/plugins/enabled + + ln -s $BASH_IT/plugins/available/base.plugin.bash $BASH_IT/plugins/enabled/250---base.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/250---base.plugin.bash" ] + + ln -s $BASH_IT/aliases/available/a.aliases.bash $BASH_IT/aliases/enabled/150---a.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---a.aliases.bash" ] + ln -s $BASH_IT/aliases/available/b.aliases.bash $BASH_IT/aliases/enabled/150---b.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---b.aliases.bash" ] + ln -s $BASH_IT/plugins/available/c.plugin.bash $BASH_IT/plugins/enabled/250---c.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/250---c.plugin.bash" ] + + # The `test_alias` alias should not exist + run alias test_alias &> /dev/null + assert_failure + + load "$BASH_IT/bash_it.sh" + + run alias test_alias &> /dev/null + assert_success + assert_line "0" "alias test_alias='c'" +} + +@test "bash-it: load aliases, plugins and completions in priority order" { + mkdir -p $BASH_IT/aliases/enabled + mkdir -p $BASH_IT/plugins/enabled + mkdir -p $BASH_IT/completion/enabled + + ln -s $BASH_IT/plugins/available/base.plugin.bash $BASH_IT/plugins/enabled/250---base.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/250---base.plugin.bash" ] + + ln -s $BASH_IT/aliases/available/a.aliases.bash $BASH_IT/aliases/enabled/150---a.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---a.aliases.bash" ] + ln -s $BASH_IT/aliases/available/b.aliases.bash $BASH_IT/completion/enabled/350---b.completion.bash + assert [ -L "$BASH_IT/completion/enabled/350---b.completion.bash" ] + ln -s $BASH_IT/plugins/available/c.plugin.bash $BASH_IT/plugins/enabled/250---c.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/250---c.plugin.bash" ] + + # The `test_alias` alias should not exist + run alias test_alias &> /dev/null + assert_failure + + load "$BASH_IT/bash_it.sh" + + run alias test_alias &> /dev/null + assert_success + # "b" wins since completions are loaded last in the old directory structure + assert_line "0" "alias test_alias='b'" +} + +@test "bash-it: load aliases, plugins and completions in priority order, even if the priority says otherwise" { + mkdir -p $BASH_IT/aliases/enabled + mkdir -p $BASH_IT/plugins/enabled + mkdir -p $BASH_IT/completion/enabled + + ln -s $BASH_IT/plugins/available/base.plugin.bash $BASH_IT/plugins/enabled/250---base.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/250---base.plugin.bash" ] + + ln -s $BASH_IT/aliases/available/a.aliases.bash $BASH_IT/aliases/enabled/450---a.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/450---a.aliases.bash" ] + ln -s $BASH_IT/aliases/available/b.aliases.bash $BASH_IT/completion/enabled/350---b.completion.bash + assert [ -L "$BASH_IT/completion/enabled/350---b.completion.bash" ] + ln -s $BASH_IT/plugins/available/c.plugin.bash $BASH_IT/plugins/enabled/950---c.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/950---c.plugin.bash" ] + + # The `test_alias` alias should not exist + run alias test_alias &> /dev/null + assert_failure + + load "$BASH_IT/bash_it.sh" + + run alias test_alias &> /dev/null + assert_success + # "b" wins since completions are loaded last in the old directory structure + assert_line "0" "alias test_alias='b'" +} + +@test "bash-it: load aliases and plugins in priority order, with one alias higher than plugins" { + mkdir -p $BASH_IT/aliases/enabled + mkdir -p $BASH_IT/plugins/enabled + + ln -s $BASH_IT/plugins/available/base.plugin.bash $BASH_IT/plugins/enabled/250---base.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/250---base.plugin.bash" ] + + ln -s $BASH_IT/aliases/available/a.aliases.bash $BASH_IT/aliases/enabled/350---a.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/350---a.aliases.bash" ] + ln -s $BASH_IT/aliases/available/b.aliases.bash $BASH_IT/aliases/enabled/150---b.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---b.aliases.bash" ] + ln -s $BASH_IT/plugins/available/c.plugin.bash $BASH_IT/plugins/enabled/250---c.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/250---c.plugin.bash" ] + + # The `test_alias` alias should not exist + run alias test_alias &> /dev/null + assert_failure + + load "$BASH_IT/bash_it.sh" + + run alias test_alias &> /dev/null + assert_success + # This will be c, loaded from the c plugin, since the individual directories + # are loaded one by one. + assert_line "0" "alias test_alias='c'" +} + +@test "bash-it: load global aliases in order" { + mkdir -p $BASH_IT/enabled + + ln -s $BASH_IT/plugins/available/base.plugin.bash $BASH_IT/enabled/250---base.plugin.bash + assert [ -L "$BASH_IT/enabled/250---base.plugin.bash" ] + + ln -s $BASH_IT/aliases/available/a.aliases.bash $BASH_IT/enabled/150---a.aliases.bash + assert [ -L "$BASH_IT/enabled/150---a.aliases.bash" ] + ln -s $BASH_IT/aliases/available/b.aliases.bash $BASH_IT/enabled/150---b.aliases.bash + assert [ -L "$BASH_IT/enabled/150---b.aliases.bash" ] + + # The `test_alias` alias should not exist + run alias test_alias &> /dev/null + assert_failure + + load "$BASH_IT/bash_it.sh" + + run alias test_alias &> /dev/null + assert_success + assert_line "0" "alias test_alias='b'" +} + +@test "bash-it: load global aliases in priority order" { + mkdir -p $BASH_IT/enabled + + ln -s $BASH_IT/plugins/available/base.plugin.bash $BASH_IT/enabled/250---base.plugin.bash + assert [ -L "$BASH_IT/enabled/250---base.plugin.bash" ] + + ln -s $BASH_IT/aliases/available/a.aliases.bash $BASH_IT/enabled/175---a.aliases.bash + assert [ -L "$BASH_IT/enabled/175---a.aliases.bash" ] + ln -s $BASH_IT/aliases/available/b.aliases.bash $BASH_IT/enabled/150---b.aliases.bash + assert [ -L "$BASH_IT/enabled/150---b.aliases.bash" ] + + # The `test_alias` alias should not exist + run alias test_alias &> /dev/null + assert_failure + + load "$BASH_IT/bash_it.sh" + + run alias test_alias &> /dev/null + assert_success + assert_line "0" "alias test_alias='a'" +} + +@test "bash-it: load global aliases and plugins in priority order" { + mkdir -p $BASH_IT/enabled + + ln -s $BASH_IT/plugins/available/base.plugin.bash $BASH_IT/enabled/250---base.plugin.bash + assert [ -L "$BASH_IT/enabled/250---base.plugin.bash" ] + + ln -s $BASH_IT/aliases/available/a.aliases.bash $BASH_IT/enabled/150---a.aliases.bash + assert [ -L "$BASH_IT/enabled/150---a.aliases.bash" ] + ln -s $BASH_IT/aliases/available/b.aliases.bash $BASH_IT/enabled/150---b.aliases.bash + assert [ -L "$BASH_IT/enabled/150---b.aliases.bash" ] + ln -s $BASH_IT/plugins/available/c.plugin.bash $BASH_IT/enabled/250---c.plugin.bash + assert [ -L "$BASH_IT/enabled/250---c.plugin.bash" ] + + # The `test_alias` alias should not exist + run alias test_alias &> /dev/null + assert_failure + + load "$BASH_IT/bash_it.sh" + + run alias test_alias &> /dev/null + assert_success + assert_line "0" "alias test_alias='c'" +} + +@test "bash-it: load global aliases and plugins in priority order, with one alias higher than plugins" { + mkdir -p $BASH_IT/enabled + + ln -s $BASH_IT/plugins/available/base.plugin.bash $BASH_IT/enabled/250---base.plugin.bash + assert [ -L "$BASH_IT/enabled/250---base.plugin.bash" ] + + ln -s $BASH_IT/aliases/available/a.aliases.bash $BASH_IT/enabled/350---a.aliases.bash + assert [ -L "$BASH_IT/enabled/350---a.aliases.bash" ] + ln -s $BASH_IT/aliases/available/b.aliases.bash $BASH_IT/enabled/150---b.aliases.bash + assert [ -L "$BASH_IT/enabled/150---b.aliases.bash" ] + ln -s $BASH_IT/plugins/available/c.plugin.bash $BASH_IT/enabled/250---c.plugin.bash + assert [ -L "$BASH_IT/enabled/250---c.plugin.bash" ] + + # The `test_alias` alias should not exist + run alias test_alias &> /dev/null + assert_failure + + load "$BASH_IT/bash_it.sh" + + run alias test_alias &> /dev/null + assert_success + # This will be a, loaded from the a aliases, since the global directory + # loads all component types at once + assert_line "0" "alias test_alias='a'" +} + +@test "bash-it: load global aliases and plugins in priority order, individual old directories are loaded later" { + mkdir -p $BASH_IT/enabled + mkdir -p $BASH_IT/aliases/enabled + + ln -s $BASH_IT/plugins/available/base.plugin.bash $BASH_IT/enabled/250---base.plugin.bash + assert [ -L "$BASH_IT/enabled/250---base.plugin.bash" ] + + ln -s $BASH_IT/aliases/available/a.aliases.bash $BASH_IT/enabled/350---a.aliases.bash + assert [ -L "$BASH_IT/enabled/350---a.aliases.bash" ] + ln -s $BASH_IT/aliases/available/b.aliases.bash $BASH_IT/enabled/150---b.aliases.bash + assert [ -L "$BASH_IT/enabled/150---b.aliases.bash" ] + ln -s $BASH_IT/plugins/available/c.plugin.bash $BASH_IT/enabled/250---c.plugin.bash + assert [ -L "$BASH_IT/enabled/250---c.plugin.bash" ] + # Add one file in the old directory structure + ln -s $BASH_IT/aliases/available/b.aliases.bash $BASH_IT/aliases/enabled/150---b.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---b.aliases.bash" ] + + # The `test_alias` alias should not exist + run alias test_alias &> /dev/null + assert_failure + + load "$BASH_IT/bash_it.sh" + + run alias test_alias &> /dev/null + assert_success + # This will be "b", loaded from the b aliases in the individual directory, since + # the individual directories are loaded after the global one. + assert_line "0" "alias test_alias='b'" +} + +@test "bash-it: load enabled aliases from new structure, priority-based" { + mkdir -p $BASH_IT/enabled + ln -s $BASH_IT/aliases/available/atom.aliases.bash $BASH_IT/enabled/150---atom.aliases.bash + assert [ -L "$BASH_IT/enabled/150---atom.aliases.bash" ] + ln -s $BASH_IT/plugins/available/base.plugin.bash $BASH_IT/enabled/250---base.plugin.bash + assert [ -L "$BASH_IT/enabled/250---base.plugin.bash" ] + + # The `ah` alias should not exist + run alias ah &> /dev/null + assert_failure + + load "$BASH_IT/bash_it.sh" + + run alias ah &> /dev/null + assert_success +} + +@test "bash-it: load enabled aliases from old structure, priority-based" { + mkdir -p $BASH_IT/aliases/enabled + mkdir -p $BASH_IT/plugins/enabled + ln -s $BASH_IT/aliases/available/atom.aliases.bash $BASH_IT/aliases/enabled/150---atom.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---atom.aliases.bash" ] + ln -s $BASH_IT/plugins/available/base.plugin.bash $BASH_IT/plugins/enabled/250---base.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/250---base.plugin.bash" ] + + # The `ah` alias should not exist + run alias ah &> /dev/null + assert_failure + + load "$BASH_IT/bash_it.sh" + + run alias ah &> /dev/null + assert_success +} + +@test "bash-it: load enabled aliases from old structure, without priorities" { + mkdir -p $BASH_IT/aliases/enabled + mkdir -p $BASH_IT/plugins/enabled + ln -s $BASH_IT/aliases/available/atom.aliases.bash $BASH_IT/aliases/enabled/atom.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/atom.aliases.bash" ] + ln -s $BASH_IT/plugins/available/base.plugin.bash $BASH_IT/plugins/enabled/base.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/base.plugin.bash" ] + + # The `ah` alias should not exist + run alias ah &> /dev/null + assert_failure + + load "$BASH_IT/bash_it.sh" + + run alias ah &> /dev/null + assert_success +} diff --git a/test/completion/bash-it.completion.bats b/test/completion/bash-it.completion.bats new file mode 100644 index 00000000..e2f1f415 --- /dev/null +++ b/test/completion/bash-it.completion.bats @@ -0,0 +1,368 @@ +#!/usr/bin/env bats + +load ../test_helper +load ../../lib/composure +load ../../completion/available/bash-it.completion + +function local_setup { + mkdir -p "$BASH_IT" + lib_directory="$(cd "$(dirname "$0")" && pwd)" + # Use rsync to copy Bash-it to the temp folder + # rsync is faster than cp, since we can exclude the large ".git" folder + rsync -qavrKL -d --delete-excluded --exclude=.git $lib_directory/../.. "$BASH_IT" + + rm -rf "$BASH_IT"/enabled + rm -rf "$BASH_IT"/aliases/enabled + rm -rf "$BASH_IT"/completion/enabled + rm -rf "$BASH_IT"/plugins/enabled + + mkdir -p "$BASH_IT"/enabled + mkdir -p "$BASH_IT"/aliases/enabled + mkdir -p "$BASH_IT"/completion/enabled + mkdir -p "$BASH_IT"/plugins/enabled + + # Don't pollute the user's actual $HOME directory + # Use a test home directory instead + export BASH_IT_TEST_CURRENT_HOME="${HOME}" + export BASH_IT_TEST_HOME="$(cd "${BASH_IT}/.." && pwd)/BASH_IT_TEST_HOME" + mkdir -p "${BASH_IT_TEST_HOME}" + export HOME="${BASH_IT_TEST_HOME}" +} + +function local_teardown { + export HOME="${BASH_IT_TEST_CURRENT_HOME}" + + rm -rf "${BASH_IT_TEST_HOME}" + + assert_equal "${BASH_IT_TEST_CURRENT_HOME}" "${HOME}" +} + +@test "completion bash-it: ensure that the _bash-it-comp function is available" { + type -a _bash-it-comp &> /dev/null + assert_success +} + +function __check_completion () { + # Get the parameters as a single value + COMP_LINE=$* + + # Get the parameters as an array + eval set -- "$@" + COMP_WORDS=("$@") + + # Index of the cursor in the line + COMP_POINT=${#COMP_LINE} + + # Get the last character of the line that was entered + COMP_LAST=$((${COMP_POINT} - 1)) + + # If the last character was a space... + if [[ ${COMP_LINE:$COMP_LAST} = ' ' ]]; then + # ...then add an empty array item + COMP_WORDS+=('') + fi + + # Word index of the last word + COMP_CWORD=$(( ${#COMP_WORDS[@]} - 1 )) + + # Run the Bash-it completion function + _bash-it-comp + + # Return the completion output + echo "${COMPREPLY[@]}" +} + +@test "completion bash-it: help - show options" { + run __check_completion 'bash-it help ' + assert_line "0" "aliases completions migrate plugins update" +} + +@test "completion bash-it: help - aliases v" { + run __check_completion 'bash-it help aliases v' + assert_line "0" "vagrant vault vim" +} + +@test "completion bash-it: update - show no options" { + run __check_completion 'bash-it update ' + assert_line "0" "" +} + +@test "completion bash-it: search - show no options" { + run __check_completion 'bash-it search ' + assert_line "0" "" +} + +@test "completion bash-it: migrate - show no options" { + run __check_completion 'bash-it migrate ' + assert_line "0" "" +} + +@test "completion bash-it: show options" { + run __check_completion 'bash-it ' + assert_line "0" "disable enable help migrate search show update" +} + +@test "completion bash-it: bash-ti - show options" { + run __check_completion 'bash-ti ' + assert_line "0" "disable enable help migrate search show update" +} + +@test "completion bash-it: shit - show options" { + run __check_completion 'shit ' + assert_line "0" "disable enable help migrate search show update" +} + +@test "completion bash-it: bashit - show options" { + run __check_completion 'bashit ' + assert_line "0" "disable enable help migrate search show update" +} + +@test "completion bash-it: batshit - show options" { + run __check_completion 'batshit ' + assert_line "0" "disable enable help migrate search show update" +} + +@test "completion bash-it: bash_it - show options" { + run __check_completion 'bash_it ' + assert_line "0" "disable enable help migrate search show update" +} + +@test "completion bash-it: show - show options" { + run __check_completion 'bash-it show ' + assert_line "0" "aliases completions plugins" +} + +@test "completion bash-it: enable - show options" { + run __check_completion 'bash-it enable ' + assert_line "0" "alias completion plugin" +} + +@test "completion bash-it: enable - show options a" { + run __check_completion 'bash-it enable a' + assert_line "0" "alias" +} + +@test "completion bash-it: disable - show options" { + run __check_completion 'bash-it disable ' + assert_line "0" "alias completion plugin" +} + +@test "completion bash-it: disable - show options a" { + run __check_completion 'bash-it disable a' + assert_line "0" "alias" +} + +@test "completion bash-it: disable - provide nothing when atom is not enabled" { + run __check_completion 'bash-it disable alias ato' + assert_line "0" "" +} + +@test "completion bash-it: disable - provide all when atom is not enabled" { + run __check_completion 'bash-it disable alias a' + assert_line "0" "all" +} + +@test "completion bash-it: disable - provide the a* aliases when atom is enabled with the old location and name" { + ln -s $BASH_IT/aliases/available/atom.aliases.bash $BASH_IT/aliases/enabled/atom.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/atom.aliases.bash" ] + + ln -s $BASH_IT/completion/available/apm.completion.bash $BASH_IT/completion/enabled/apm.completion.bash + assert [ -L "$BASH_IT/completion/enabled/apm.completion.bash" ] + + run __check_completion 'bash-it disable alias a' + assert_line "0" "all atom" +} + +@test "completion bash-it: disable - provide the a* aliases when atom is enabled with the old location and priority-based name" { + ln -s $BASH_IT/aliases/available/atom.aliases.bash $BASH_IT/aliases/enabled/150---atom.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---atom.aliases.bash" ] + + ln -s $BASH_IT/completion/available/apm.completion.bash $BASH_IT/completion/enabled/350---apm.completion.bash + assert [ -L "$BASH_IT/completion/enabled/350---apm.completion.bash" ] + + run __check_completion 'bash-it disable alias a' + assert_line "0" "all atom" +} + +@test "completion bash-it: disable - provide the a* aliases when atom is enabled with the new location and priority-based name" { + ln -s $BASH_IT/aliases/available/atom.aliases.bash $BASH_IT/enabled/150---atom.aliases.bash + assert [ -L "$BASH_IT/enabled/150---atom.aliases.bash" ] + + ln -s $BASH_IT/completion/available/apm.completion.bash $BASH_IT/enabled/350---apm.completion.bash + assert [ -L "$BASH_IT/enabled/350---apm.completion.bash" ] + + run __check_completion 'bash-it disable alias a' + assert_line "0" "all atom" +} + +@test "completion bash-it: disable - provide the docker-machine plugin when docker-machine is enabled with the old location and name" { + ln -s $BASH_IT/aliases/available/docker-compose.aliases.bash $BASH_IT/aliases/enabled/docker-compose.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/docker-compose.aliases.bash" ] + + ln -s $BASH_IT/plugins/available/docker-machine.plugin.bash $BASH_IT/plugins/enabled/docker-machine.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/docker-machine.plugin.bash" ] + + run __check_completion 'bash-it disable plugin docker' + assert_line "0" "docker-machine" +} + +@test "completion bash-it: disable - provide the docker-machine plugin when docker-machine is enabled with the old location and priority-based name" { + ln -s $BASH_IT/aliases/available/docker-compose.aliases.bash $BASH_IT/aliases/enabled/150---docker-compose.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---docker-compose.aliases.bash" ] + + ln -s $BASH_IT/plugins/available/docker-machine.plugin.bash $BASH_IT/plugins/enabled/350---docker-machine.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/350---docker-machine.plugin.bash" ] + + run __check_completion 'bash-it disable plugin docker' + assert_line "0" "docker-machine" +} + +@test "completion bash-it: disable - provide the docker-machine plugin when docker-machine is enabled with the new location and priority-based name" { + ln -s $BASH_IT/aliases/available/docker-compose.aliases.bash $BASH_IT/enabled/150---docker-compose.aliases.bash + assert [ -L "$BASH_IT/enabled/150---docker-compose.aliases.bash" ] + + ln -s $BASH_IT/plugins/available/docker-machine.plugin.bash $BASH_IT/enabled/350---docker-machine.plugin.bash + assert [ -L "$BASH_IT/enabled/350---docker-machine.plugin.bash" ] + + run __check_completion 'bash-it disable plugin docker' + assert_line "0" "docker-machine" +} + +@test "completion bash-it: disable - provide the todo.txt-cli aliases when todo plugin is enabled with the old location and name" { + ln -s $BASH_IT/aliases/available/todo.txt-cli.aliases.bash $BASH_IT/aliases/enabled/todo.txt-cli.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/todo.txt-cli.aliases.bash" ] + + ln -s $BASH_IT/plugins/available/todo.plugin.bash $BASH_IT/plugins/enabled/todo.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/todo.plugin.bash" ] + + run __check_completion 'bash-it disable alias to' + assert_line "0" "todo.txt-cli" +} + +@test "completion bash-it: disable - provide the todo.txt-cli aliases when todo plugin is enabled with the old location and priority-based name" { + ln -s $BASH_IT/aliases/available/todo.txt-cli.aliases.bash $BASH_IT/aliases/enabled/150---todo.txt-cli.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---todo.txt-cli.aliases.bash" ] + + ln -s $BASH_IT/plugins/available/todo.plugin.bash $BASH_IT/plugins/enabled/350---todo.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/350---todo.plugin.bash" ] + + run __check_completion 'bash-it disable alias to' + assert_line "0" "todo.txt-cli" +} + +@test "completion bash-it: disable - provide the todo.txt-cli aliases when todo plugin is enabled with the new location and priority-based name" { + ln -s $BASH_IT/aliases/available/todo.txt-cli.aliases.bash $BASH_IT/enabled/150---todo.txt-cli.aliases.bash + assert [ -L "$BASH_IT/enabled/150---todo.txt-cli.aliases.bash" ] + + ln -s $BASH_IT/plugins/available/todo.plugin.bash $BASH_IT/enabled/350---todo.plugin.bash + assert [ -L "$BASH_IT/enabled/350---todo.plugin.bash" ] + + run __check_completion 'bash-it disable alias to' + assert_line "0" "todo.txt-cli" +} + +@test "completion bash-it: enable - provide the atom aliases when not enabled" { + run __check_completion 'bash-it enable alias ato' + assert_line "0" "atom" +} + +@test "completion bash-it: enable - provide the a* aliases when not enabled" { + run __check_completion 'bash-it enable alias a' + assert_line "0" "all ag ansible apt atom" +} + +@test "completion bash-it: enable - provide the a* aliases when atom is enabled with the old location and name" { + ln -s $BASH_IT/aliases/available/atom.aliases.bash $BASH_IT/aliases/enabled/atom.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/atom.aliases.bash" ] + + run __check_completion 'bash-it enable alias a' + assert_line "0" "all ag ansible apt" +} + +@test "completion bash-it: enable - provide the a* aliases when atom is enabled with the old location and priority-based name" { + ln -s $BASH_IT/aliases/available/atom.aliases.bash $BASH_IT/aliases/enabled/150---atom.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---atom.aliases.bash" ] + + run __check_completion 'bash-it enable alias a' + assert_line "0" "all ag ansible apt" +} + +@test "completion bash-it: enable - provide the a* aliases when atom is enabled with the new location and priority-based name" { + ln -s $BASH_IT/aliases/available/atom.aliases.bash $BASH_IT/enabled/150---atom.aliases.bash + assert [ -L "$BASH_IT/enabled/150---atom.aliases.bash" ] + + run __check_completion 'bash-it enable alias a' + assert_line "0" "all ag ansible apt" +} + +@test "completion bash-it: enable - provide the docker-* plugins when nothing is enabled with the old location and name" { + ln -s $BASH_IT/aliases/available/docker-compose.aliases.bash $BASH_IT/aliases/enabled/docker-compose.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/docker-compose.aliases.bash" ] + + run __check_completion 'bash-it enable plugin docker' + assert_line "0" "docker-compose docker-machine docker" +} + +@test "completion bash-it: enable - provide the docker-* plugins when nothing is enabled with the old location and priority-based name" { + ln -s $BASH_IT/aliases/available/docker-compose.aliases.bash $BASH_IT/aliases/enabled/150---docker-compose.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---docker-compose.aliases.bash" ] + + run __check_completion 'bash-it enable plugin docker' + assert_line "0" "docker-compose docker-machine docker" +} + +@test "completion bash-it: enable - provide the docker-* plugins when nothing is enabled with the new location and priority-based name" { + ln -s $BASH_IT/aliases/available/docker-compose.aliases.bash $BASH_IT/enabled/150---docker-compose.aliases.bash + assert [ -L "$BASH_IT/enabled/150---docker-compose.aliases.bash" ] + + run __check_completion 'bash-it enable plugin docker' + assert_line "0" "docker-compose docker-machine docker" +} + +@test "completion bash-it: enable - provide the docker-* completions when nothing is enabled with the old location and name" { + ln -s $BASH_IT/aliases/available/docker-compose.aliases.bash $BASH_IT/aliases/enabled/docker-compose.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/docker-compose.aliases.bash" ] + + run __check_completion 'bash-it enable completion docker' + assert_line "0" "docker docker-compose docker-machine" +} + +@test "completion bash-it: enable - provide the docker-* completions when nothing is enabled with the old location and priority-based name" { + ln -s $BASH_IT/aliases/available/docker-compose.aliases.bash $BASH_IT/aliases/enabled/150---docker-compose.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---docker-compose.aliases.bash" ] + + run __check_completion 'bash-it enable completion docker' + assert_line "0" "docker docker-compose docker-machine" +} + +@test "completion bash-it: enable - provide the docker-* completions when nothing is enabled with the new location and priority-based name" { + ln -s $BASH_IT/aliases/available/docker-compose.aliases.bash $BASH_IT/enabled/150---docker-compose.aliases.bash + assert [ -L "$BASH_IT/enabled/150---docker-compose.aliases.bash" ] + + run __check_completion 'bash-it enable completion docker' + assert_line "0" "docker docker-compose docker-machine" +} + +@test "completion bash-it: enable - provide the todo.txt-cli aliases when todo plugin is enabled with the old location and name" { + ln -s $BASH_IT/plugins/available/todo.plugin.bash $BASH_IT/plugins/enabled/todo.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/todo.plugin.bash" ] + + run __check_completion 'bash-it enable alias to' + assert_line "0" "todo.txt-cli" +} + +@test "completion bash-it: enable - provide the todo.txt-cli aliases when todo plugin is enabled with the old location and priority-based name" { + ln -s $BASH_IT/plugins/available/todo.plugin.bash $BASH_IT/plugins/enabled/350---todo.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/350---todo.plugin.bash" ] + + run __check_completion 'bash-it enable alias to' + assert_line "0" "todo.txt-cli" +} + +@test "completion bash-it: enable - provide the todo.txt-cli aliases when todo plugin is enabled with the new location and priority-based name" { + ln -s $BASH_IT/plugins/available/todo.plugin.bash $BASH_IT/enabled/350---todo.plugin.bash + assert [ -L "$BASH_IT/enabled/350---todo.plugin.bash" ] + + run __check_completion 'bash-it enable alias to' + assert_line "0" "todo.txt-cli" +} diff --git a/test/fixtures/bash_it/aliases/available/a.aliases.bash b/test/fixtures/bash_it/aliases/available/a.aliases.bash new file mode 100644 index 00000000..9dede3f6 --- /dev/null +++ b/test/fixtures/bash_it/aliases/available/a.aliases.bash @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +alias test_alias="a" diff --git a/test/fixtures/bash_it/aliases/available/b.aliases.bash b/test/fixtures/bash_it/aliases/available/b.aliases.bash new file mode 100644 index 00000000..4f90a7ad --- /dev/null +++ b/test/fixtures/bash_it/aliases/available/b.aliases.bash @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +alias test_alias="b" diff --git a/test/fixtures/bash_it/plugins/available/c.plugin.bash b/test/fixtures/bash_it/plugins/available/c.plugin.bash new file mode 100644 index 00000000..3d17ad7a --- /dev/null +++ b/test/fixtures/bash_it/plugins/available/c.plugin.bash @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +alias test_alias="c" diff --git a/test/install/install.bats b/test/install/install.bats index ded2689a..acb41e0a 100644 --- a/test/install/install.bats +++ b/test/install/install.bats @@ -14,10 +14,16 @@ case $OSTYPE in esac function local_setup { - mkdir -p $BASH_IT + mkdir -p "$BASH_IT" lib_directory="$(cd "$(dirname "$0")" && pwd)" - cp -r $lib_directory/../../* $BASH_IT/ - rm -rf "$BASH_IT/aliases/enabled" "$BASH_IT/completion/enabled" "$BASH_IT/plugins/enabled" + # Use rsync to copy Bash-it to the temp folder + # rsync is faster than cp, since we can exclude the large ".git" folder + rsync -qavrKL -d --delete-excluded --exclude=.git $lib_directory/../.. "$BASH_IT" + + rm -rf "$BASH_IT"/enabled + rm -rf "$BASH_IT"/aliases/enabled + rm -rf "$BASH_IT"/completion/enabled + rm -rf "$BASH_IT"/plugins/enabled # Don't pollute the user's actual $HOME directory # Use a test home directory instead @@ -46,11 +52,11 @@ function local_teardown { assert [ -e "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE" ] - assert [ -L "$BASH_IT/aliases/enabled/150---general.aliases.bash" ] - assert [ -L "$BASH_IT/plugins/enabled/250---base.plugin.bash" ] - assert [ -L "$BASH_IT/plugins/enabled/365---alias-completion.plugin.bash" ] - assert [ -L "$BASH_IT/completion/enabled/350---bash-it.completion.bash" ] - assert [ -L "$BASH_IT/completion/enabled/350---system.completion.bash" ] + assert [ -L "$BASH_IT/enabled/150---general.aliases.bash" ] + assert [ -L "$BASH_IT/enabled/250---base.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/365---alias-completion.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/350---bash-it.completion.bash" ] + assert [ -L "$BASH_IT/enabled/350---system.completion.bash" ] } @test "install: verify that a backup file is created" { diff --git a/test/install/uninstall.bats b/test/install/uninstall.bats index adab216f..6c7ae561 100644 --- a/test/install/uninstall.bats +++ b/test/install/uninstall.bats @@ -14,9 +14,16 @@ case $OSTYPE in esac function local_setup { - mkdir -p $BASH_IT + mkdir -p "$BASH_IT" lib_directory="$(cd "$(dirname "$0")" && pwd)" - cp -r $lib_directory/../../* $BASH_IT/ + # Use rsync to copy Bash-it to the temp folder + # rsync is faster than cp, since we can exclude the large ".git" folder + rsync -qavrKL -d --delete-excluded --exclude=.git $lib_directory/../.. "$BASH_IT" + + rm -rf "$BASH_IT"/enabled + rm -rf "$BASH_IT"/aliases/enabled + rm -rf "$BASH_IT"/completion/enabled + rm -rf "$BASH_IT"/plugins/enabled # Don't pollute the user's actual $HOME directory # Use a test home directory instead diff --git a/test/lib/helpers.bats b/test/lib/helpers.bats index 00b13b3a..7398e08a 100644 --- a/test/lib/helpers.bats +++ b/test/lib/helpers.bats @@ -9,108 +9,228 @@ cite _about _param _example _group _author _version load ../../lib/helpers function local_setup { - mkdir -p $BASH_IT + mkdir -p "$BASH_IT" lib_directory="$(cd "$(dirname "$0")" && pwd)" - cp -r $lib_directory/../.. $BASH_IT - mkdir -p $BASH_IT/aliases/enabled - mkdir -p $BASH_IT/completion/enabled - mkdir -p $BASH_IT/plugins/enabled + # Use rsync to copy Bash-it to the temp folder + # rsync is faster than cp, since we can exclude the large ".git" folder + rsync -qavrKL -d --delete-excluded --exclude=.git $lib_directory/../.. "$BASH_IT" + + rm -rf "$BASH_IT"/enabled + rm -rf "$BASH_IT"/aliases/enabled + rm -rf "$BASH_IT"/completion/enabled + rm -rf "$BASH_IT"/plugins/enabled + + mkdir -p "$BASH_IT"/enabled + mkdir -p "$BASH_IT"/aliases/enabled + mkdir -p "$BASH_IT"/completion/enabled + mkdir -p "$BASH_IT"/plugins/enabled } -@test "bash-it: enable the ansible aliases through the bash-it function" { - run bash-it enable alias "ansible" - assert_line "0" 'ansible enabled with priority 150.' - assert [ -L "$BASH_IT/aliases/enabled/150---ansible.aliases.bash" ] +# TODO Create global __is_enabled function +# TODO Create global __get_base_name function +# TODO Create global __get_enabled_name function + +@test "helpers: _command_exists function exists" { + type -a _command_exists &> /dev/null + assert_success } -@test "bash-it: enable the todo.txt-cli aliases through the bash-it function" { +@test "helpers: _command_exists function positive test ls" { + run _command_exists ls + assert_success +} + +@test "helpers: _command_exists function positive test bash-it" { + run _command_exists bash-it + assert_success +} + +@test "helpers: _command_exists function negative test" { + run _command_exists __addfkds_dfdsjdf + assert_failure +} + +@test "helpers: bash-it help aliases ag" { + run bash-it help aliases "ag" + assert_line "0" "ag='ag --smart-case --pager=\"less -MIRFX'" +} + +@test "helpers: bash-it help aliases without any aliases enabled" { + run bash-it help aliases + assert_line "0" "" +} + +@test "helpers: bash-it help list aliases without any aliases enabled" { + run _help-list-aliases "$BASH_IT/aliases/available/ag.aliases.bash" + assert_line "0" "ag:" +} + +@test "helpers: bash-it help list aliases with ag aliases enabled" { + ln -s $BASH_IT/aliases/available/ag.aliases.bash $BASH_IT/aliases/enabled/150---ag.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---ag.aliases.bash" ] + + run _help-list-aliases "$BASH_IT/aliases/enabled/150---ag.aliases.bash" + assert_line "0" "ag:" +} + +@test "helpers: bash-it help list aliases with todo.txt-cli aliases enabled" { + ln -s $BASH_IT/aliases/available/todo.txt-cli.aliases.bash $BASH_IT/aliases/enabled/150---todo.txt-cli.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---todo.txt-cli.aliases.bash" ] + + run _help-list-aliases "$BASH_IT/aliases/enabled/150---todo.txt-cli.aliases.bash" + assert_line "0" "todo.txt-cli:" +} + +@test "helpers: bash-it help list aliases with docker-compose aliases enabled" { + ln -s $BASH_IT/aliases/available/docker-compose.aliases.bash $BASH_IT/aliases/enabled/150---docker-compose.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---docker-compose.aliases.bash" ] + + run _help-list-aliases "$BASH_IT/aliases/enabled/150---docker-compose.aliases.bash" + assert_line "0" "docker-compose:" +} + +@test "helpers: bash-it help list aliases with ag aliases enabled in global directory" { + ln -s $BASH_IT/aliases/available/ag.aliases.bash $BASH_IT/enabled/150---ag.aliases.bash + assert [ -L "$BASH_IT/enabled/150---ag.aliases.bash" ] + + run _help-list-aliases "$BASH_IT/enabled/150---ag.aliases.bash" + assert_line "0" "ag:" +} + +@test "helpers: bash-it help aliases one alias enabled in the old directory" { + ln -s $BASH_IT/aliases/available/ag.aliases.bash $BASH_IT/aliases/enabled/150---ag.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/150---ag.aliases.bash" ] + + run bash-it help aliases + assert_line "0" "ag:" +} + +@test "helpers: bash-it help aliases one alias enabled in global directory" { + run bash-it enable alias "ag" + assert_line "0" 'ag enabled with priority 150.' + assert [ -L "$BASH_IT/enabled/150---ag.aliases.bash" ] + + run bash-it enable plugin "aws" + assert_line "0" 'aws enabled with priority 250.' + assert [ -L "$BASH_IT/enabled/250---aws.plugin.bash" ] + + run bash-it help aliases + assert_line "0" "ag:" + assert_line "1" "ag='ag --smart-case --pager=\"less -MIRFX'" +} + +@test "helpers: enable the todo.txt-cli aliases through the bash-it function" { run bash-it enable alias "todo.txt-cli" assert_line "0" 'todo.txt-cli enabled with priority 150.' - assert [ -L "$BASH_IT/aliases/enabled/150---todo.txt-cli.aliases.bash" ] + assert [ -L "$BASH_IT/enabled/150---todo.txt-cli.aliases.bash" ] } -@test "bash-it: enable the curl aliases" { +@test "helpers: enable the curl aliases" { run _enable-alias "curl" assert_line "0" 'curl enabled with priority 150.' - assert [ -L "$BASH_IT/aliases/enabled/150---curl.aliases.bash" ] + assert [ -L "$BASH_IT/enabled/150---curl.aliases.bash" ] } -@test "bash-it: enable the apm completion through the bash-it function" { +@test "helpers: enable the apm completion through the bash-it function" { run bash-it enable completion "apm" assert_line "0" 'apm enabled with priority 350.' - assert [ -L "$BASH_IT/completion/enabled/350---apm.completion.bash" ] + assert [ -L "$BASH_IT/enabled/350---apm.completion.bash" ] } -@test "bash-it: enable the brew completion" { +@test "helpers: enable the brew completion" { run _enable-completion "brew" assert_line "0" 'brew enabled with priority 350.' - assert [ -L "$BASH_IT/completion/enabled/350---brew.completion.bash" ] + assert [ -L "$BASH_IT/enabled/350---brew.completion.bash" ] } -@test "bash-it: enable the node plugin" { +@test "helpers: enable the node plugin" { run _enable-plugin "node" assert_line "0" 'node enabled with priority 250.' - assert [ -L "$BASH_IT/plugins/enabled/250---node.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/250---node.plugin.bash" ] + + assert_equal "../plugins/available/node.plugin.bash" "`readlink $BASH_IT/enabled/250---node.plugin.bash`" } -@test "bash-it: enable the node plugin through the bash-it function" { +@test "helpers: enable the node plugin through the bash-it function" { run bash-it enable plugin "node" assert_line "0" 'node enabled with priority 250.' - assert [ -L "$BASH_IT/plugins/enabled/250---node.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/250---node.plugin.bash" ] } -@test "bash-it: enable the node and nvm plugins through the bash-it function" { +@test "helpers: enable the node and nvm plugins through the bash-it function" { run bash-it enable plugin "node" "nvm" assert_line "0" 'node enabled with priority 250.' assert_line "1" 'nvm enabled with priority 225.' - assert [ -L "$BASH_IT/plugins/enabled/250---node.plugin.bash" ] - assert [ -L "$BASH_IT/plugins/enabled/225---nvm.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/250---node.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] } -@test "bash-it: enable the foo-unkown and nvm plugins through the bash-it function" { +@test "helpers: enable the foo-unkown and nvm plugins through the bash-it function" { run bash-it enable plugin "foo-unknown" "nvm" assert_line "0" 'sorry, foo-unknown does not appear to be an available plugin.' assert_line "1" 'nvm enabled with priority 225.' - assert [ -L "$BASH_IT/plugins/enabled/225---nvm.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] } -@test "bash-it: enable the nvm plugin" { +@test "helpers: enable the nvm plugin" { run _enable-plugin "nvm" assert_line "0" 'nvm enabled with priority 225.' - assert [ -L "$BASH_IT/plugins/enabled/225---nvm.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] } -@test "bash-it: enable an unknown plugin" { +@test "helpers: enable an unknown plugin" { run _enable-plugin "unknown-foo" assert_line "0" 'sorry, unknown-foo does not appear to be an available plugin.' + + # Check for both old an new structure assert [ ! -L "$BASH_IT/plugins/enabled/250---unknown-foo.plugin.bash" ] assert [ ! -L "$BASH_IT/plugins/enabled/unknown-foo.plugin.bash" ] + + assert [ ! -L "$BASH_IT/enabled/250---unknown-foo.plugin.bash" ] + assert [ ! -L "$BASH_IT/enabled/unknown-foo.plugin.bash" ] } -@test "bash-it: enable an unknown plugin through the bash-it function" { +@test "helpers: enable an unknown plugin through the bash-it function" { run bash-it enable plugin "unknown-foo" echo "${lines[@]}" assert_line "0" 'sorry, unknown-foo does not appear to be an available plugin.' + + # Check for both old an new structure assert [ ! -L "$BASH_IT/plugins/enabled/250---unknown-foo.plugin.bash" ] assert [ ! -L "$BASH_IT/plugins/enabled/unknown-foo.plugin.bash" ] + + assert [ ! -L "$BASH_IT/enabled/250---unknown-foo.plugin.bash" ] + assert [ ! -L "$BASH_IT/enabled/unknown-foo.plugin.bash" ] } -@test "bash-it: disable a plugin that is not enabled" { +@test "helpers: disable a plugin that is not enabled" { run _disable-plugin "sdkman" assert_line "0" 'sorry, sdkman does not appear to be an enabled plugin.' } -@test "bash-it: enable and disable the nvm plugin" { +@test "helpers: enable and disable the nvm plugin" { run _enable-plugin "nvm" assert_line "0" 'nvm enabled with priority 225.' + assert [ -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] + assert [ ! -L "$BASH_IT/plugins/enabled/225---nvm.plugin.bash" ] + + run _disable-plugin "nvm" + assert_line "0" 'nvm disabled.' + assert [ ! -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] +} + +@test "helpers: disable the nvm plugin if it was enabled with a priority, but in the component-specific directory" { + ln -s $BASH_IT/plugins/available/nvm.plugin.bash $BASH_IT/plugins/enabled/225---nvm.plugin.bash assert [ -L "$BASH_IT/plugins/enabled/225---nvm.plugin.bash" ] + assert [ ! -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] run _disable-plugin "nvm" assert_line "0" 'nvm disabled.' assert [ ! -L "$BASH_IT/plugins/enabled/225---nvm.plugin.bash" ] + assert [ ! -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] } -@test "bash-it: disable the nvm plugin if it was enabled without a priority" { +@test "helpers: disable the nvm plugin if it was enabled without a priority" { ln -s $BASH_IT/plugins/available/nvm.plugin.bash $BASH_IT/plugins/enabled/nvm.plugin.bash assert [ -L "$BASH_IT/plugins/enabled/nvm.plugin.bash" ] @@ -119,7 +239,7 @@ function local_setup { assert [ ! -L "$BASH_IT/plugins/enabled/nvm.plugin.bash" ] } -@test "bash-it: enable the nvm plugin if it was enabled without a priority" { +@test "helpers: enable the nvm plugin if it was enabled without a priority" { ln -s $BASH_IT/plugins/available/nvm.plugin.bash $BASH_IT/plugins/enabled/nvm.plugin.bash assert [ -L "$BASH_IT/plugins/enabled/nvm.plugin.bash" ] @@ -127,19 +247,53 @@ function local_setup { assert_line "0" 'nvm is already enabled.' assert [ -L "$BASH_IT/plugins/enabled/nvm.plugin.bash" ] assert [ ! -L "$BASH_IT/plugins/enabled/225---nvm.plugin.bash" ] + assert [ ! -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] } -@test "bash-it: enable the nvm plugin twice" { - run _enable-plugin "nvm" - assert_line "0" 'nvm enabled with priority 225.' +@test "helpers: enable the nvm plugin if it was enabled with a priority, but in the component-specific directory" { + ln -s $BASH_IT/plugins/available/nvm.plugin.bash $BASH_IT/plugins/enabled/225---nvm.plugin.bash assert [ -L "$BASH_IT/plugins/enabled/225---nvm.plugin.bash" ] run _enable-plugin "nvm" assert_line "0" 'nvm is already enabled.' + assert [ ! -L "$BASH_IT/plugins/enabled/nvm.plugin.bash" ] assert [ -L "$BASH_IT/plugins/enabled/225---nvm.plugin.bash" ] + assert [ ! -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] } -@test "bash-it: migrate enabled plugins that don't use the new priority-based configuration" { +@test "helpers: enable the nvm plugin twice" { + run _enable-plugin "nvm" + assert_line "0" 'nvm enabled with priority 225.' + assert [ -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] + + run _enable-plugin "nvm" + assert_line "0" 'nvm is already enabled.' + assert [ -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] +} + +@test "helpers: migrate plugins and completions that share the same name" { + ln -s $BASH_IT/completion/available/dirs.completion.bash $BASH_IT/completion/enabled/350---dirs.completion.bash + assert [ -L "$BASH_IT/completion/enabled/350---dirs.completion.bash" ] + + ln -s $BASH_IT/plugins/available/dirs.plugin.bash $BASH_IT/plugins/enabled/250---dirs.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/250---dirs.plugin.bash" ] + + run _bash-it-migrate + assert_line "0" 'Migrating plugin dirs.' + assert_line "1" 'dirs disabled.' + assert_line "2" 'dirs enabled with priority 250.' + assert_line "3" 'Migrating completion dirs.' + assert_line "4" 'dirs disabled.' + assert_line "5" 'dirs enabled with priority 350.' + assert_line "6" 'If any migration errors were reported, please try the following: reload && bash-it migrate' + + assert [ -L "$BASH_IT/enabled/350---dirs.completion.bash" ] + assert [ -L "$BASH_IT/enabled/250---dirs.plugin.bash" ] + assert [ ! -L "$BASH_IT/completion/enabled/350----dirs.completion.bash" ] + assert [ ! -L "$BASH_IT/plugins/enabled/250----dirs.plugin.bash" ] +} + +@test "helpers: migrate enabled plugins that don't use the new priority-based configuration" { ln -s $BASH_IT/plugins/available/nvm.plugin.bash $BASH_IT/plugins/enabled/nvm.plugin.bash assert [ -L "$BASH_IT/plugins/enabled/nvm.plugin.bash" ] @@ -150,31 +304,131 @@ function local_setup { assert [ -L "$BASH_IT/aliases/enabled/todo.txt-cli.aliases.bash" ] run _enable-plugin "ssh" - assert [ -L "$BASH_IT/plugins/enabled/250---ssh.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/250---ssh.plugin.bash" ] run _bash-it-migrate - assert [ -L "$BASH_IT/plugins/enabled/225---nvm.plugin.bash" ] - assert [ -L "$BASH_IT/plugins/enabled/250---node.plugin.bash" ] - assert [ -L "$BASH_IT/plugins/enabled/250---ssh.plugin.bash" ] - assert [ -L "$BASH_IT/aliases/enabled/150---todo.txt-cli.aliases.bash" ] + assert_line "0" 'Migrating alias todo.txt-cli.' + assert_line "1" 'todo.txt-cli disabled.' + assert_line "2" 'todo.txt-cli enabled with priority 150.' + + assert [ -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/250---node.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/250---ssh.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/150---todo.txt-cli.aliases.bash" ] assert [ ! -L "$BASH_IT/plugins/enabled/node.plugin.bash" ] assert [ ! -L "$BASH_IT/plugins/enabled/nvm.plugin.bash" ] assert [ ! -L "$BASH_IT/aliases/enabled/todo.txt-cli.aliases.bash" ] } -@test "bash-it: run the migrate command without anything to migrate and nothing enabled" { - run _bash-it-migrate -} +@test "helpers: migrate enabled plugins that use the new priority-based configuration in the individual directories" { + ln -s $BASH_IT/plugins/available/nvm.plugin.bash $BASH_IT/plugins/enabled/225---nvm.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/225---nvm.plugin.bash" ] + + ln -s $BASH_IT/plugins/available/node.plugin.bash $BASH_IT/plugins/enabled/250---node.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/250---node.plugin.bash" ] + + ln -s $BASH_IT/aliases/available/todo.txt-cli.aliases.bash $BASH_IT/aliases/enabled/250---todo.txt-cli.aliases.bash + assert [ -L "$BASH_IT/aliases/enabled/250---todo.txt-cli.aliases.bash" ] -@test "bash-it: run the migrate command without anything to migrate" { run _enable-plugin "ssh" - assert [ -L "$BASH_IT/plugins/enabled/250---ssh.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/250---ssh.plugin.bash" ] run _bash-it-migrate - assert [ -L "$BASH_IT/plugins/enabled/250---ssh.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/250---node.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/250---ssh.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/150---todo.txt-cli.aliases.bash" ] + assert [ ! -L "$BASH_IT/plugins/enabled/225----node.plugin.bash" ] + assert [ ! -L "$BASH_IT/plugins/enabled/250----nvm.plugin.bash" ] + assert [ ! -L "$BASH_IT/aliases/enabled/250----todo.txt-cli.aliases.bash" ] } -@test "bash-it: verify that existing components are automatically migrated when something is enabled" { +@test "helpers: run the migrate command without anything to migrate and nothing enabled" { + run _bash-it-migrate +} + +@test "helpers: run the migrate command without anything to migrate" { + run _enable-plugin "ssh" + assert [ -L "$BASH_IT/enabled/250---ssh.plugin.bash" ] + + run _bash-it-migrate + assert [ -L "$BASH_IT/enabled/250---ssh.plugin.bash" ] +} + +function __migrate_all_components() { + subdirectory="$1" + one_type="$2" + priority="$3" + + for f in "${BASH_IT}/$subdirectory/available/"*.bash + do + to_enable=$(basename $f) + if [ -z "$priority" ]; then + ln -s "../available/$to_enable" "${BASH_IT}/${subdirectory}/enabled/$to_enable" + else + ln -s "../available/$to_enable" "${BASH_IT}/${subdirectory}/enabled/$priority---$to_enable" + fi + done + + ls ${BASH_IT}/${subdirectory}/enabled + + all_available=$(compgen -G "${BASH_IT}/${subdirectory}/available/*.$one_type.bash" | wc -l | xargs) + all_enabled_old=$(compgen -G "${BASH_IT}/${subdirectory}/enabled/*.$one_type.bash" | wc -l | xargs) + + assert_equal "$all_available" "$all_enabled_old" + + run bash-it migrate + + all_enabled_old_after=$(compgen -G "${BASH_IT}/${subdirectory}/enabled/*.$one_type.bash" | wc -l | xargs) + assert_equal "0" "$all_enabled_old_after" + + all_enabled_new_after=$(compgen -G "${BASH_IT}/enabled/*.$one_type.bash" | wc -l | xargs) + assert_equal "$all_enabled_old" "$all_enabled_new_after" +} + +@test "helpers: migrate all plugins" { + subdirectory="plugins" + one_type="plugin" + + __migrate_all_components "$subdirectory" "$one_type" +} + +@test "helpers: migrate all aliases" { + subdirectory="aliases" + one_type="aliases" + + __migrate_all_components "$subdirectory" "$one_type" +} + +@test "helpers: migrate all completions" { + subdirectory="completion" + one_type="completion" + + __migrate_all_components "$subdirectory" "$one_type" +} + +@test "helpers: migrate all plugins with previous priority" { + subdirectory="plugins" + one_type="plugin" + + __migrate_all_components "$subdirectory" "$one_type" "100" +} + +@test "helpers: migrate all aliases with previous priority" { + subdirectory="aliases" + one_type="aliases" + + __migrate_all_components "$subdirectory" "$one_type" "100" +} + +@test "helpers: migrate all completions with previous priority" { + subdirectory="completion" + one_type="completion" + + __migrate_all_components "$subdirectory" "$one_type" "100" +} + +@test "helpers: verify that existing components are automatically migrated when something is enabled" { ln -s $BASH_IT/plugins/available/nvm.plugin.bash $BASH_IT/plugins/enabled/nvm.plugin.bash assert [ -L "$BASH_IT/plugins/enabled/nvm.plugin.bash" ] @@ -182,59 +436,140 @@ function local_setup { assert_line "0" 'Migrating plugin nvm.' assert_line "1" 'nvm disabled.' assert_line "2" 'nvm enabled with priority 225.' - assert_line "3" 'node enabled with priority 250.' + assert_line "3" 'If any migration errors were reported, please try the following: reload && bash-it migrate' + assert_line "4" 'node enabled with priority 250.' assert [ ! -L "$BASH_IT/plugins/enabled/nvm.plugin.bash" ] - assert [ -L "$BASH_IT/plugins/enabled/225---nvm.plugin.bash" ] - assert [ -L "$BASH_IT/plugins/enabled/250---node.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/250---node.plugin.bash" ] } -@test "bash-it: verify that existing components are automatically migrated when something is disabled" { +@test "helpers: verify that existing components are automatically migrated when something is disabled" { ln -s $BASH_IT/plugins/available/nvm.plugin.bash $BASH_IT/plugins/enabled/nvm.plugin.bash assert [ -L "$BASH_IT/plugins/enabled/nvm.plugin.bash" ] ln -s $BASH_IT/plugins/available/node.plugin.bash $BASH_IT/plugins/enabled/250---node.plugin.bash assert [ -L "$BASH_IT/plugins/enabled/250---node.plugin.bash" ] run bash-it disable plugin "node" - assert_line "0" 'Migrating plugin nvm.' - assert_line "1" 'nvm disabled.' - assert_line "2" 'nvm enabled with priority 225.' - assert_line "3" 'node disabled.' + assert_line "0" 'Migrating plugin node.' + assert_line "1" 'node disabled.' + assert_line "2" 'node enabled with priority 250.' + assert_line "3" 'Migrating plugin nvm.' + assert_line "4" 'nvm disabled.' + assert_line "5" 'nvm enabled with priority 225.' + assert_line "6" 'If any migration errors were reported, please try the following: reload && bash-it migrate' + assert_line "7" 'node disabled.' assert [ ! -L "$BASH_IT/plugins/enabled/nvm.plugin.bash" ] - assert [ -L "$BASH_IT/plugins/enabled/225---nvm.plugin.bash" ] + assert [ -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] assert [ ! -L "$BASH_IT/plugins/enabled/250---node.plugin.bash" ] + assert [ ! -L "$BASH_IT/enabled/250---node.plugin.bash" ] } -@test "bash-it: enable all plugins" { +@test "helpers: enable all plugins" { run _enable-plugin "all" local available=$(find $BASH_IT/plugins/available -name *.plugin.bash | wc -l | xargs) - local enabled=$(find $BASH_IT/plugins/enabled -name [0-9]*.plugin.bash | wc -l | xargs) + local enabled=$(find $BASH_IT/enabled -name [0-9]*.plugin.bash | wc -l | xargs) assert_equal "$available" "$enabled" } -@test "bash-it: disable all plugins" { +@test "helpers: disable all plugins" { run _enable-plugin "all" local available=$(find $BASH_IT/plugins/available -name *.plugin.bash | wc -l | xargs) - local enabled=$(find $BASH_IT/plugins/enabled -name [0-9]*.plugin.bash | wc -l | xargs) + local enabled=$(find $BASH_IT/enabled -name [0-9]*.plugin.bash | wc -l | xargs) assert_equal "$available" "$enabled" + run _enable-alias "ag" + assert [ -L "$BASH_IT/enabled/150---ag.aliases.bash" ] + + run _disable-plugin "all" + local enabled2=$(find $BASH_IT/enabled -name [0-9]*.plugin.bash | wc -l | xargs) + assert_equal "0" "$enabled2" + assert [ -L "$BASH_IT/enabled/150---ag.aliases.bash" ] +} + +@test "helpers: disable all plugins in the old directory structure" { + ln -s $BASH_IT/plugins/available/nvm.plugin.bash $BASH_IT/plugins/enabled/nvm.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/nvm.plugin.bash" ] + + ln -s $BASH_IT/plugins/available/node.plugin.bash $BASH_IT/plugins/enabled/node.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/node.plugin.bash" ] + + local enabled=$(find $BASH_IT/plugins/enabled -name *.plugin.bash | wc -l | xargs) + assert_equal "2" "$enabled" + + run _enable-alias "ag" + assert [ -L "$BASH_IT/enabled/150---ag.aliases.bash" ] + run _disable-plugin "all" local enabled2=$(find $BASH_IT/plugins/enabled -name *.plugin.bash | wc -l | xargs) - assert_equal "$enabled2" "0" + assert_equal "0" "$enabled2" + assert [ -L "$BASH_IT/enabled/150---ag.aliases.bash" ] } -@test "bash-it: describe the nvm plugin without enabling it" { +@test "helpers: disable all plugins in the old directory structure with priority" { + ln -s $BASH_IT/plugins/available/nvm.plugin.bash $BASH_IT/plugins/enabled/250---nvm.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/250---nvm.plugin.bash" ] + + ln -s $BASH_IT/plugins/available/node.plugin.bash $BASH_IT/plugins/enabled/250---node.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/250---node.plugin.bash" ] + + local enabled=$(find $BASH_IT/plugins/enabled -name *.plugin.bash | wc -l | xargs) + assert_equal "2" "$enabled" + + run _enable-alias "ag" + assert [ -L "$BASH_IT/enabled/150---ag.aliases.bash" ] + + run _disable-plugin "all" + local enabled2=$(find $BASH_IT/plugins/enabled -name *.plugin.bash | wc -l | xargs) + assert_equal "0" "$enabled2" + assert [ -L "$BASH_IT/enabled/150---ag.aliases.bash" ] +} + +@test "helpers: disable all plugins without anything enabled" { + local enabled=$(find $BASH_IT/enabled -name [0-9]*.plugin.bash | wc -l | xargs) + assert_equal "0" "$enabled" + + run _enable-alias "ag" + assert [ -L "$BASH_IT/enabled/150---ag.aliases.bash" ] + + run _disable-plugin "all" + local enabled2=$(find $BASH_IT/enabled -name [0-9]*.plugin.bash | wc -l | xargs) + assert_equal "0" "$enabled2" + assert [ -L "$BASH_IT/enabled/150---ag.aliases.bash" ] +} + +@test "helpers: enable the ansible aliases through the bash-it function" { + run bash-it enable alias "ansible" + assert_line "0" 'ansible enabled with priority 150.' + assert [ -L "$BASH_IT/enabled/150---ansible.aliases.bash" ] +} + +@test "helpers: describe the nvm plugin without enabling it" { _bash-it-plugins | grep "nvm" | grep "\[ \]" } -@test "bash-it: describe the nvm plugin after enabling it" { +@test "helpers: describe the nvm plugin after enabling it" { run _enable-plugin "nvm" assert_line "0" 'nvm enabled with priority 225.' + assert [ -L "$BASH_IT/enabled/225---nvm.plugin.bash" ] + + _bash-it-plugins | grep "nvm" | grep "\[x\]" +} + +@test "helpers: describe the nvm plugin after enabling it in the old directory" { + ln -s $BASH_IT/plugins/available/nvm.plugin.bash $BASH_IT/plugins/enabled/nvm.plugin.bash + assert [ -L "$BASH_IT/plugins/enabled/nvm.plugin.bash" ] + + _bash-it-plugins | grep "nvm" | grep "\[x\]" +} + +@test "helpers: describe the nvm plugin after enabling it in the old directory with priority" { + ln -s $BASH_IT/plugins/available/nvm.plugin.bash $BASH_IT/plugins/enabled/225---nvm.plugin.bash assert [ -L "$BASH_IT/plugins/enabled/225---nvm.plugin.bash" ] _bash-it-plugins | grep "nvm" | grep "\[x\]" } -@test "bash-it: describe the todo.txt-cli aliases without enabling them" { +@test "helpers: describe the todo.txt-cli aliases without enabling them" { run _bash-it-aliases assert_line "todo.txt-cli [ ] todo.txt-cli abbreviations" } diff --git a/test/lib/search.bats b/test/lib/search.bats index 71a176d4..e00feee5 100644 --- a/test/lib/search.bats +++ b/test/lib/search.bats @@ -13,17 +13,24 @@ load ../../lib/search NO_COLOR=true function local_setup { - mkdir -p $BASH_IT + mkdir -p "$BASH_IT" lib_directory="$(cd "$(dirname "$0")" && pwd)" - cp -r $lib_directory/../../* $BASH_IT/ + # Use rsync to copy Bash-it to the temp folder + # rsync is faster than cp, since we can exclude the large ".git" folder + rsync -qavrKL -d --delete-excluded --exclude=.git $lib_directory/../.. "$BASH_IT" + + rm -rf "$BASH_IT"/enabled + rm -rf "$BASH_IT"/aliases/enabled + rm -rf "$BASH_IT"/completion/enabled + rm -rf "$BASH_IT"/plugins/enabled } -@test "helpers search plugins" { +@test "search: plugin base" { run _bash-it-search-component 'plugins' 'base' [[ "${lines[0]}" =~ 'plugins' && "${lines[0]}" =~ 'base' ]] } -@test "helpers search ruby gem bundle rake rails" { +@test "search: ruby gem bundle rake rails" { # first disable them all, so that the output does not appear with a checkbox # and we can compare the result run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' '--disable' @@ -35,7 +42,7 @@ function local_setup { assert [ "${lines[2]/✓/}" == ' completions => bundler gem rake' ] } -@test "search ruby gem bundle -chruby rake rails" { +@test "search: ruby gem bundle -chruby rake rails" { run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' '--disable' run _bash-it-search 'ruby' 'gem' 'bundle' '-chruby' 'rake' 'rails' assert [ "${lines[0]/✓/}" == ' aliases => bundler rails' ] @@ -43,7 +50,7 @@ function local_setup { assert [ "${lines[2]/✓/}" == ' completions => bundler gem rake' ] } -@test "search (rails enabled) ruby gem bundle rake rails" { +@test "search: (rails enabled) ruby gem bundle rake rails" { run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' '--disable' run _enable-alias 'rails' run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' @@ -52,7 +59,7 @@ function local_setup { assert_line "2" ' completions => bundler gem rake' } -@test "search (all enabled) ruby gem bundle rake rails" { +@test "search: (all enabled) ruby gem bundle rake rails" { run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' '-chruby' 'rails' '--enable' run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' '-chruby' 'rails' assert_line "0" ' aliases => ✓bundler ✓rails' diff --git a/test/plugins/ruby.plugin.bats b/test/plugins/ruby.plugin.bats index c5f13733..a57e45fc 100755 --- a/test/plugins/ruby.plugin.bats +++ b/test/plugins/ruby.plugin.bats @@ -5,6 +5,32 @@ load ../../lib/helpers load ../../lib/composure load ../../plugins/available/ruby.plugin +function local_setup { + mkdir -p "$BASH_IT" + lib_directory="$(cd "$(dirname "$0")" && pwd)" + # Use rsync to copy Bash-it to the temp folder + # rsync is faster than cp, since we can exclude the large ".git" folder + rsync -qavrKL -d --delete-excluded --exclude=.git $lib_directory/../.. "$BASH_IT" + + rm -rf "$BASH_IT"/enabled + rm -rf "$BASH_IT"/aliases/enabled + rm -rf "$BASH_IT"/completion/enabled + rm -rf "$BASH_IT"/plugins/enabled + + mkdir -p "$BASH_IT"/enabled + mkdir -p "$BASH_IT"/aliases/enabled + mkdir -p "$BASH_IT"/completion/enabled + mkdir -p "$BASH_IT"/plugins/enabled + + export OLD_PATH="$PATH" + export PATH="/usr/bin:/bin:/usr/sbin" +} + +function local_teardown { + export PATH="$OLD_PATH" + unset OLD_PATH +} + @test "plugins ruby: remove_gem is defined" { run type remove_gem assert_line 1 "remove_gem () " @@ -15,6 +41,8 @@ load ../../plugins/available/ruby.plugin skip 'ruby not installed' fi - last_path_entry=$(echo $PATH | tr ":" "\n" | tail -1); + load ../../plugins/available/ruby.plugin + + local last_path_entry=$(echo $PATH | tr ":" "\n" | tail -1) [[ "${last_path_entry}" == "${HOME}"/.gem/ruby/*/bin ]] } diff --git a/test/run b/test/run index 737f17e2..bd4b1c61 100755 --- a/test/run +++ b/test/run @@ -9,4 +9,4 @@ if [ -z "${BASH_IT}" ]; then export BASH_IT=$(cd ${test_directory} && dirname $(pwd)) fi -exec $bats_executable ${CI:+--tap} ${test_directory}/{install,lib,plugins,themes} +exec $bats_executable ${CI:+--tap} ${test_directory}/{bash_it,completion,install,lib,plugins,themes} diff --git a/test/themes/base.theme.bats b/test/themes/base.theme.bats index 6bf7efed..ab320c81 100644 --- a/test/themes/base.theme.bats +++ b/test/themes/base.theme.bats @@ -2,7 +2,10 @@ load ../test_helper load ../../lib/composure -load ../../plugins/available/base.plugin + +cite _about _param _example _group _author _version + +load ../../lib/helpers load ../../themes/base.theme @test 'themes base: battery_percentage should not exist' { diff --git a/themes/atomic/atomic.theme.bash b/themes/atomic/atomic.theme.bash index 6dd7ea04..a89aeea3 100644 --- a/themes/atomic/atomic.theme.bash +++ b/themes/atomic/atomic.theme.bash @@ -164,7 +164,7 @@ ___atomic_prompt_clock() { } ___atomic_prompt_battery() { - ! command_exists battery_percentage || + ! _command_exists battery_percentage || [ "${THEME_SHOW_BATTERY}" != "true" ] || [ "$(battery_percentage)" = "no" ] && return diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 58e2d288..66ce7de9 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -474,7 +474,7 @@ function battery_char { fi } -if ! command_exists battery_charge ; then +if ! _command_exists battery_charge ; then # if user has installed battery plugin, skip this... function battery_charge (){ # no op @@ -484,7 +484,7 @@ fi # The battery_char function depends on the presence of the battery_percentage function. # If battery_percentage is not defined, then define battery_char as a no-op. -if ! command_exists battery_percentage ; then +if ! _command_exists battery_percentage ; then function battery_char (){ # no op echo -n diff --git a/themes/brainy/brainy.theme.bash b/themes/brainy/brainy.theme.bash index 54d29f8b..c3aa9b83 100644 --- a/themes/brainy/brainy.theme.bash +++ b/themes/brainy/brainy.theme.bash @@ -152,7 +152,7 @@ ___brainy_prompt_clock() { } ___brainy_prompt_battery() { - ! command_exists battery_percentage || + ! _command_exists battery_percentage || [ "${THEME_SHOW_BATTERY}" != "true" ] || [ "$(battery_percentage)" = "no" ] && return diff --git a/themes/demula/demula.theme.bash b/themes/demula/demula.theme.bash index e698def5..515d096d 100644 --- a/themes/demula/demula.theme.bash +++ b/themes/demula/demula.theme.bash @@ -84,7 +84,7 @@ ${D_BRANCH_COLOR}%b %r ${D_CHANGES_COLOR}%m%u ${D_DEFAULT_COLOR}" # checks if the plugin is installed before calling battery_charge safe_battery_charge() { - if command_exists battery_charge ; + if _command_exists battery_charge ; then battery_charge fi diff --git a/themes/powerline/powerline.base.bash b/themes/powerline/powerline.base.bash index f65e9dd6..6646814a 100644 --- a/themes/powerline/powerline.base.bash +++ b/themes/powerline/powerline.base.bash @@ -43,9 +43,9 @@ function __powerline_user_info_prompt { function __powerline_ruby_prompt { local ruby_version="" - if command_exists rvm; then + if _command_exists rvm; then ruby_version="$(rvm_version_prompt)" - elif command_exists rbenv; then + elif _command_exists rbenv; then ruby_version=$(rbenv_version_prompt) fi diff --git a/themes/rana/rana.theme.bash b/themes/rana/rana.theme.bash index 0aed8348..25530397 100644 --- a/themes/rana/rana.theme.bash +++ b/themes/rana/rana.theme.bash @@ -117,7 +117,7 @@ ${D_BRANCH_COLOR}%b %r ${D_CHANGES_COLOR}%m%u ${D_DEFAULT_COLOR}" # checks if the plugin is installed before calling battery_charge safe_battery_charge() { - if command_exists battery_charge ; + if _command_exists battery_charge ; then battery_charge fi