From 056c392a54345db701ee9ebdafdd5fd32dd67aa3 Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Wed, 26 Jan 2022 18:05:34 +0200 Subject: [PATCH 1/3] lib: preexec: Properly return if there was a conflict in check_*_conflict It goes the other way around! --- lib/preexec.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/preexec.bash b/lib/preexec.bash index d0d5b6da74..17cabdf974 100644 --- a/lib/preexec.bash +++ b/lib/preexec.bash @@ -29,13 +29,13 @@ __bp_install_after_session_init function __check_precmd_conflict() { local f __bp_trim_whitespace f "${1?}" - ! _bash-it-array-contains-element "${f}" "${precmd_functions[@]}" + _bash-it-array-contains-element "${f}" "${precmd_functions[@]}" } function __check_preexec_conflict() { local f __bp_trim_whitespace f "${1?}" - ! _bash-it-array-contains-element "${f}" "${preexec_functions[@]}" + _bash-it-array-contains-element "${f}" "${preexec_functions[@]}" } function safe_append_prompt_command { From d1d7cd43832c6239f405cf9eb0b33ee7dd37b3c2 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Thu, 27 Jan 2022 11:30:21 -0800 Subject: [PATCH 2/3] tests: add *.bats to `.editorconfig` --- .editorconfig | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 8a181ef5e3..f67aa2df5a 100755 --- a/.editorconfig +++ b/.editorconfig @@ -16,7 +16,7 @@ trim_trailing_whitespace = false indent_size = tab indent_style = tab -[{**.*sh,test/run}] +[{**.*sh,test/run,**.bats}] indent_size = tab indent_style = tab @@ -29,3 +29,8 @@ end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true + +[**.bats] +indent_size = tab +indent_style = tab +shell_variant = bats From 2343e2dd3589622b020d7780e7025e76b30ec5cf Mon Sep 17 00:00:00 2001 From: John D Pell Date: Thu, 27 Jan 2022 10:54:58 -0800 Subject: [PATCH 3/3] lib/preexec: tests! --- test/lib/preexec.bats | 164 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 test/lib/preexec.bats diff --git a/test/lib/preexec.bats b/test/lib/preexec.bats new file mode 100644 index 0000000000..d85f952c28 --- /dev/null +++ b/test/lib/preexec.bats @@ -0,0 +1,164 @@ +# shellcheck shell=bats + +load ../test_helper + +function local_setup { + setup_test_fixture + export __bp_enable_subshells=yas +} + +@test "vendor preexec: __bp_install_after_session_init() without existing" { + test_prompt_string="" + export PROMPT_COMMAND="$test_prompt_string" + + load ../../vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh + assert_success + + assert_equal "${PROMPT_COMMAND}" $'__bp_trap_string="$(trap -p DEBUG)"\ntrap - DEBUG\n__bp_install' +} + +@test "vendor preexec: __bp_install_after_session_init() with existing" { + test_prompt_string="nah" + export PROMPT_COMMAND="$test_prompt_string" + + load ../../vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh + assert_success + + assert_equal "${PROMPT_COMMAND}" "$test_prompt_string"$'\n__bp_trap_string="$(trap -p DEBUG)"\ntrap - DEBUG\n__bp_install' +} + +@test "vendor preexec: __bp_install_after_session_init() delayed" { + test_prompt_string="nah" + export PROMPT_COMMAND="$test_prompt_string" + export __bp_delay_install="blarg" + + load ../../vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh + assert_success + + assert_equal "${PROMPT_COMMAND}" "$test_prompt_string" + + run __bp_install_after_session_init + assert_success + + __bp_install_after_session_init + assert_equal "${PROMPT_COMMAND}" "$test_prompt_string"$'\n__bp_trap_string="$(trap -p DEBUG)"\ntrap - DEBUG\n__bp_install' +} + +@test "vendor preexec: __bp_install() without existing" { + test_prompt_string="" + export PROMPT_COMMAND="$test_prompt_string" + + load ../../vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh + assert_success + + run __bp_install + assert_success + + __bp_install + assert_equal "${PROMPT_COMMAND}" $'__bp_precmd_invoke_cmd\n__bp_interactive_mode' +} + +@test "vendor preexec: __bp_install() with existing" { + test_prompt_string="nah" + export PROMPT_COMMAND="$test_prompt_string" + + load ../../vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh + assert_success + + run __bp_install + assert_success + + __bp_install + assert_equal "${PROMPT_COMMAND}" $'__bp_precmd_invoke_cmd\n'"$test_prompt_string"$'\n__bp_interactive_mode' +} + +@test "lib preexec: __bp_require_not_readonly()" { + run type -t __bp_require_not_readonly + assert_failure + + run load ../../lib/preexec.bash + assert_success + load ../../lib/preexec.bash + + run type -t __bp_require_not_readonly + assert_success + + export HISTCONTROL=blah:blah PROMPT_COMMAND="silly;rabbit" + readonly HISTCONTROL PROMPT_COMMAND + + run __bp_require_not_readonly + assert_success +} + +@test "lib preexec: __bp_adjust_histcontrol()" { + run type -t __bp_adjust_histcontrol + assert_failure + + run load ../../lib/preexec.bash + assert_success + load ../../lib/preexec.bash + + run type -t __bp_adjust_histcontrol + assert_success + + test_history_control_string="ignoreall:ignoredups:ignorespace:erasedups" + export HISTCONTROL="${test_history_control_string}" + + run __bp_adjust_histcontrol + assert_success + assert_equal "${HISTCONTROL}" "${test_history_control_string}" +} + +@test "lib preexec: __check_precmd_conflict()" { + test_precmd_function_name="test" + load ../test_helper_libs + + run __check_precmd_conflict "$test_precmd_function_name" + assert_failure + + export precmd_functions=("$test_precmd_function_name") + + run __check_precmd_conflict "$test_precmd_function_name" + assert_success +} + +@test "lib preexec: __check_preexec_conflict()" { + test_preexec_function_name="test" + load ../test_helper_libs + + run __check_preexec_conflict "$test_preexec_function_name" + assert_failure + + export preexec_functions=("$test_preexec_function_name") + + run __check_preexec_conflict "$test_preexec_function_name" + assert_success +} + +@test "lib preexec: safe_append_prompt_command()" { + test_precmd_function_name="test" + load ../test_helper_libs + + export precmd_functions=() + assert_equal "${precmd_functions[*]}" "" + + run safe_append_prompt_command "$test_precmd_function_name" + assert_success + + safe_append_prompt_command "$test_precmd_function_name" + assert_equal "${precmd_functions[*]}" "$test_precmd_function_name" +} + +@test "lib preexec: safe_append_preexec()" { + test_preexec_function_name="test" + load ../test_helper_libs + + export preexec_functions=() + assert_equal "${preexec_functions[*]}" "" + + run safe_append_preexec "$test_preexec_function_name" + assert_success + + safe_append_preexec "$test_preexec_function_name" + assert_equal "${preexec_functions[*]}" "$test_preexec_function_name" +}