From b106d275a784b51ffd2ff03c8ac36d75b74c880f Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Fri, 15 Sep 2017 08:10:14 +0200 Subject: [PATCH] Move enable/disable functionality to global enabled directory --- lib/helpers.bash | 27 ++++++++------- test/lib/helpers.bats | 77 +++++++++++++++++++++++++++++++++---------- 2 files changed, 75 insertions(+), 29 deletions(-) diff --git a/lib/helpers.bash b/lib/helpers.bash index 0184455e..184ee700 100644 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -284,16 +284,21 @@ _disable-thing () fi 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.*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.*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 + 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 @@ -375,13 +380,13 @@ _enable-thing () return fi - mkdir -p "${BASH_IT}/$subdirectory/enabled" + 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} - 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 diff --git a/test/lib/helpers.bats b/test/lib/helpers.bats index 00b13b3a..d9bf1922 100644 --- a/test/lib/helpers.bats +++ b/test/lib/helpers.bats @@ -9,90 +9,107 @@ 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 + cp -r $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" ] + assert [ -L "$BASH_IT/enabled/150---ansible.aliases.bash" ] } @test "bash-it: 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" { 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" { 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" { 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" { 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" ] } @test "bash-it: 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" { 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" { 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" { 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" { 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" { 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" { @@ -103,11 +120,23 @@ function local_setup { @test "bash-it: 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 "bash-it: 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" { @@ -127,16 +156,28 @@ 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 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: enable the nvm plugin twice" { 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" ] run _enable-plugin "nvm" assert_line "0" 'nvm is already enabled.' - 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" {