diff --git a/clean_files.txt b/clean_files.txt index 4ecda787..ad9a364a 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -122,7 +122,9 @@ plugins/available/zoxide.plugin.bash # tests # test/plugins/alias-completion.plugin.bats +test/run test/test_helper.bash +test/test_helper_libs.bash # themes # diff --git a/test/bash_it/bash_it.bats b/test/bash_it/bash_it.bats index a692909e..13c84238 100644 --- a/test/bash_it/bash_it.bats +++ b/test/bash_it/bash_it.bats @@ -1,7 +1,6 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" function local_setup { setup_test_fixture diff --git a/test/completion/bash-it.completion.bats b/test/completion/bash-it.completion.bats index fbf0a3fa..cb1761bf 100644 --- a/test/completion/bash-it.completion.bats +++ b/test/completion/bash-it.completion.bats @@ -1,7 +1,6 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load ../../completion/available/bash-it.completion function local_setup { diff --git a/test/install/install.bats b/test/install/install.bats index 40f3162d..4a88e23f 100644 --- a/test/install/install.bats +++ b/test/install/install.bats @@ -1,7 +1,6 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" # Determine which config file to use based on OS. case $OSTYPE in diff --git a/test/install/uninstall.bats b/test/install/uninstall.bats index 038125b3..16bb7f7b 100644 --- a/test/install/uninstall.bats +++ b/test/install/uninstall.bats @@ -1,7 +1,6 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" # Determine which config file to use based on OS. case $OSTYPE in diff --git a/test/lib/composure.bats b/test/lib/composure.bats old mode 100755 new mode 100644 index 02da83eb..8198936c --- a/test/lib/composure.bats +++ b/test/lib/composure.bats @@ -1,7 +1,6 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" @test "lib composure: _composure_keywords()" { run _composure_keywords diff --git a/test/lib/helpers.bats b/test/lib/helpers.bats index d876d882..4df36277 100644 --- a/test/lib/helpers.bats +++ b/test/lib/helpers.bats @@ -1,14 +1,7 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" -load ../../lib/log -load ../../lib/utilities -load ../../lib/search - -cite _about _param _example _group _author _version - -load ../../lib/helpers +load ../test_helper_libs load ../../plugins/available/base.plugin function local_setup { diff --git a/test/lib/log.bats b/test/lib/log.bats index 329386bd..00efbc2d 100644 --- a/test/lib/log.bats +++ b/test/lib/log.bats @@ -1,10 +1,8 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" -load ../../lib/appearance +load ../../themes/colors.theme -cite _about _param _example _group _author _version load ../../lib/log load ../../lib/helpers load ../../plugins/available/base.plugin diff --git a/test/lib/search.bats b/test/lib/search.bats index 2081abab..c8a95027 100644 --- a/test/lib/search.bats +++ b/test/lib/search.bats @@ -1,13 +1,7 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" -load ../../lib/log -load ../../lib/helpers -load ../../lib/utilities -load ../../lib/search - -cite _about _param _example _group _author _version +load ../test_helper_libs load ../../plugins/available/base.plugin load ../../aliases/available/git.aliases diff --git a/test/lib/utilities.bats b/test/lib/utilities.bats index 4907a763..a0968fce 100644 --- a/test/lib/utilities.bats +++ b/test/lib/utilities.bats @@ -1,93 +1,84 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" -load ../../lib/helpers -load ../../lib/utilities -load ../../lib/search - -cite _about _param _example _group _author _version +load ../test_helper_libs function local_setup { setup_test_fixture } -function has_match() { - $(_bash-it-array-contains-element ${@}) && echo "has" "$1" -} - -function item_enabled() { - $(_bash-it-component-item-is-enabled ${@}) && echo "$1" "$2" "is enabled" -} - -function item_disabled() { - $(_bash-it-component-item-is-disabled ${@}) && echo "$1" "$2" "is disabled" -} - @test "_bash-it-component-item-is-enabled() - for a disabled item" { - run item_enabled aliases svn - assert_line -n 0 '' + run _bash-it-component-item-is-enabled aliases svn + assert_failure } @test "_bash-it-component-item-is-enabled() - for an enabled/disabled item" { run bash-it enable alias svn assert_line -n 0 'svn enabled with priority 150.' - run item_enabled alias svn - assert_line -n 0 'alias svn is enabled' + run _bash-it-component-item-is-enabled alias svn + assert_success + run _bash-it-component-item-is-disabled alias svn + assert_failure run bash-it disable alias svn assert_line -n 0 'svn disabled.' - run item_enabled alias svn - assert_line -n 0 '' + run _bash-it-component-item-is-enabled alias svn + assert_failure + run _bash-it-component-item-is-disabled alias svn + assert_success } @test "_bash-it-component-item-is-disabled() - for a disabled item" { - run item_disabled alias svn - assert_line -n 0 'alias svn is disabled' + run _bash-it-component-item-is-disabled alias svn + assert_success } @test "_bash-it-component-item-is-disabled() - for an enabled/disabled item" { run bash-it enable alias svn assert_line -n 0 'svn enabled with priority 150.' - run item_disabled alias svn - assert_line -n 0 '' + run _bash-it-component-item-is-disabled alias svn + assert_failure + run _bash-it-component-item-is-enabled alias svn + assert_success run bash-it disable alias svn assert_line -n 0 'svn disabled.' - run item_disabled alias svn - assert_line -n 0 'alias svn is disabled' + run _bash-it-component-item-is-disabled alias svn + assert_success + run _bash-it-component-item-is-enabled alias svn + assert_failure } @test "_bash-it-array-contains-element() - when match is found, and is the first" { declare -a fruits=(apple pear orange mandarin) - run has_match apple "${fruits[@]}" - assert_line -n 0 'has apple' + run _bash-it-array-contains-element apple "${fruits[@]}" + assert_success } @test "_bash-it-array-contains-element() - when match is found, and is the last" { declare -a fruits=(apple pear orange mandarin) - run has_match mandarin "${fruits[@]}" - assert_line -n 0 'has mandarin' + run _bash-it-array-contains-element mandarin "${fruits[@]}" + assert_success } @test "_bash-it-array-contains-element() - when match is found, and is in the middle" { declare -a fruits=(apple pear orange mandarin) - run has_match pear "${fruits[@]}" - assert_line -n 0 'has pear' + run _bash-it-array-contains-element pear "${fruits[@]}" + assert_success } @test "_bash-it-array-contains-element() - when match is found, and it has spaces" { declare -a fruits=(apple pear orange mandarin "yellow watermelon") - run has_match "yellow watermelon" "${fruits[@]}" - assert_line -n 0 'has yellow watermelon' + run _bash-it-array-contains-element "yellow watermelon" "${fruits[@]}" + assert_success } @test "_bash-it-array-contains-element() - when match is not found" { declare -a fruits=(apple pear orange mandarin) - run has_match xyz "${fruits[@]}" - assert_line -n 0 '' + run _bash-it-array-contains-element xyz "${fruits[@]}" + assert_failure } diff --git a/test/plugins/alias-completion.plugin.bats b/test/plugins/alias-completion.plugin.bats index 497dded9..20d13cf2 100644 --- a/test/plugins/alias-completion.plugin.bats +++ b/test/plugins/alias-completion.plugin.bats @@ -1,11 +1,7 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" -load ../../lib/log -load ../../lib/helpers - -cite _about _param _example _group _author _version +load ../test_helper_libs load ../../completion/available/capistrano.completion diff --git a/test/plugins/base.plugin.bats b/test/plugins/base.plugin.bats old mode 100755 new mode 100644 index 6f1099cc..05081a8a --- a/test/plugins/base.plugin.bats +++ b/test/plugins/base.plugin.bats @@ -1,9 +1,7 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" -load ../../lib/log -load ../../lib/helpers +load ../test_helper_libs load ../../plugins/available/base.plugin @test 'plugins base: ips()' { diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats old mode 100755 new mode 100644 index fda52b02..eac6d021 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -1,11 +1,7 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" -load "${BASH_IT}/lib/log.bash" -load "${BASH_IT}/lib/helpers.bash" - -cite _about _param _example _group _author _version +load ../test_helper_libs load ../../plugins/available/battery.plugin diff --git a/test/plugins/cmd-returned-notify.plugin.bats b/test/plugins/cmd-returned-notify.plugin.bats index f201e465..daf58330 100644 --- a/test/plugins/cmd-returned-notify.plugin.bats +++ b/test/plugins/cmd-returned-notify.plugin.bats @@ -1,8 +1,8 @@ #!/usr/bin/env bats load ../test_helper -load ../../lib/helpers -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" +load ../test_helper_libs +load ../../vendor/init.d/preexec load ../../plugins/available/cmd-returned-notify.plugin diff --git a/test/plugins/go.plugin.bats b/test/plugins/go.plugin.bats index 110699e8..4021e643 100644 --- a/test/plugins/go.plugin.bats +++ b/test/plugins/go.plugin.bats @@ -1,8 +1,7 @@ #!/usr/bin/env bats load ../test_helper -load ../../lib/helpers -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" +load ../test_helper_libs # We test `go version` in each test to account for users with goenv and no system go. diff --git a/test/plugins/ruby.plugin.bats b/test/plugins/ruby.plugin.bats index 7a719020..e40dfeae 100755 --- a/test/plugins/ruby.plugin.bats +++ b/test/plugins/ruby.plugin.bats @@ -1,9 +1,7 @@ #!/usr/bin/env bats load ../test_helper -load ../../lib/helpers -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" -load ../../plugins/available/ruby.plugin +load ../test_helper_libs function local_setup { setup_test_fixture @@ -18,17 +16,21 @@ function local_teardown { } @test "plugins ruby: remove_gem is defined" { + load ../../plugins/available/ruby.plugin + run type remove_gem assert_line -n 1 "remove_gem () " } @test "plugins ruby: PATH includes ~/.gem/ruby/bin" { - if ! which ruby >/dev/null; then + if ! type ruby >/dev/null; then skip 'ruby not installed' fi + mkdir -p "$(ruby -e 'print Gem.user_dir')/bin" + load ../../plugins/available/ruby.plugin - local last_path_entry=$(echo $PATH | tr ":" "\n" | tail -1) - [[ "${last_path_entry}" == "${HOME}"/.gem/ruby/*/bin ]] + local last_path_entry="$(tail -1 <<<"${PATH//:/$'\n'}")" + [[ "${last_path_entry}" == "$(ruby -e 'print Gem.user_dir')/bin" ]] } diff --git a/test/plugins/xterm.plugin.bats b/test/plugins/xterm.plugin.bats index 123a91f0..c175d854 100644 --- a/test/plugins/xterm.plugin.bats +++ b/test/plugins/xterm.plugin.bats @@ -1,8 +1,7 @@ #!/usr/bin/env bats load ../test_helper -load ../../lib/helpers -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" +load ../test_helper_libs load ../../plugins/available/xterm.plugin @@ -21,30 +20,30 @@ function local_setup { @test "plugins xterm: shorten command output" { export SHORT_TERM_LINE=true - run _short-command ${BASH_IT}/test/fixtures/plugin/xterm/files/* + run _short-command "${BASH_IT}/test/fixtures/plugin/xterm/files"/* assert_success - assert_output ${BASH_IT}/test/fixtures/plugin/xterm/files/arg0 + assert_output "${BASH_IT}/test/fixtures/plugin/xterm/files/arg0" } @test "plugins xterm: full command output" { export SHORT_TERM_LINE=false - run _short-command ${BASH_IT}/test/fixtures/plugin/xterm/files/* + run _short-command "${BASH_IT}/test/fixtures/plugin/xterm/files"/* assert_success - assert_output "$(echo ${BASH_IT}/test/fixtures/plugin/xterm/files/*)" + assert_output "$(echo "${BASH_IT}/test/fixtures/plugin/xterm/files"/*)" } @test "plugins xterm: shorten dirname output" { export SHORT_TERM_LINE=true run _short-dirname assert_success - assert_output "$(basename $PWD)" + assert_output "$(basename "${PWD}")" } @test "plugins xterm: full dirname output" { export SHORT_TERM_LINE=false run _short-dirname assert_success - assert_output $PWD + assert_output "${PWD}" } @test "plugins xterm: set xterm title" { diff --git a/test/run b/test/run index 7999d4fb..14d83950 100755 --- a/test/run +++ b/test/run @@ -4,16 +4,15 @@ bats_executable="${test_directory}/../test_lib/bats-core/bin/bats" git submodule init && git submodule update -if [ -z "${BASH_IT}" ]; then - declare BASH_IT - BASH_IT="$(cd "${test_directory}" && dirname "${PWD}")" - export BASH_IT +if [[ -z "${BASH_IT}" ]]; then + BASH_IT="$(cd "${test_directory}" && dirname "${PWD}")" + export BASH_IT fi -if [ -z "$1" ]; then - test_dirs=( "${test_directory}"/{bash_it,completion,install,lib,plugins,themes} ) +if [[ -z "$1" ]]; then + test_dirs=("${test_directory}"/{bash_it,completion,install,lib,plugins,themes}) else - test_dirs=( "$1" ) + test_dirs=("$1") fi # Make sure that the `parallel` command is installed, @@ -21,28 +20,26 @@ fi # If that is the case, try to guess the number of CPU cores, # so we can run `bats` in parallel processing mode, which is a lot faster. if command -v parallel &> /dev/null \ - && parallel -V &> /dev/null \ - && { parallel -V 2> /dev/null | grep -q '^GNU\>'; } -then - # Expect to run at least on a dual-core CPU; slightly degraded performance - # shouldn't matter otherwise. - declare -i -r test_jobs_default=2 - declare -i -r test_jobs_effective="$( - if [ "${TEST_JOBS:-detect}" = "detect" ] \ - && command -v nproc &> /dev/null - then - nproc - elif [ -n "${TEST_JOBS}" ] \ - && [ "${TEST_JOBS}" != "detect" ] - then - echo "${TEST_JOBS}" - else - echo ${test_jobs_default} - fi - )" - exec "$bats_executable" ${CI:+--tap} --jobs ${test_jobs_effective} \ - "${test_dirs[@]}" + && parallel -V &> /dev/null \ + && { parallel -V 2> /dev/null | grep -q '^GNU\>'; }; then + # Expect to run at least on a dual-core CPU; slightly degraded performance + # shouldn't matter otherwise. + declare -i -r test_jobs_default=2 + # shellcheck disable=SC2155 + declare -i -r test_jobs_effective="$( + if [[ "${TEST_JOBS:-detect}" = "detect" ]] \ + && command -v nproc &> /dev/null; then + nproc + elif [[ -n "${TEST_JOBS}" ]] \ + && [[ "${TEST_JOBS}" != "detect" ]]; then + echo "${TEST_JOBS}" + else + echo "${test_jobs_default}" + fi + )" + exec "$bats_executable" "${CI:+--tap}" --jobs "${test_jobs_effective}" \ + "${test_dirs[@]}" else - # Run `bats` in single-threaded mode. - exec "$bats_executable" ${CI:+--tap} "${test_dirs[@]}" + # Run `bats` in single-threaded mode. + exec "$bats_executable" ${CI:+--tap} "${test_dirs[@]}" fi diff --git a/test/test_helper.bash b/test/test_helper.bash old mode 100644 new mode 100755 index 8d92e89c..06660979 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -21,6 +21,7 @@ load "${TEST_DEPS_DIR}/bats-file/load.bash" # support 'plumbing' metadata cite _about _param _example _group _author _version +cite about-alias about-plugin about-completion local_setup() { true @@ -41,7 +42,7 @@ setup_test_fixture() { if command -v rsync &> /dev/null; then # Use rsync to copy Bash-it to the temp folder - rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=enabled "$src_topdir" "$BASH_IT" + rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=helper.bash --exclude=enabled "$src_topdir" "$BASH_IT" else rm -rf "$BASH_IT" mkdir -p "$BASH_IT" diff --git a/test/test_helper_libs.bash b/test/test_helper_libs.bash new file mode 100644 index 00000000..57115e7e --- /dev/null +++ b/test/test_helper_libs.bash @@ -0,0 +1,6 @@ +# shellcheck shell=bash + +load "${BASH_IT}/lib/log.bash" +load "${BASH_IT}/lib/utilities.bash" +load "${BASH_IT}/lib/helpers.bash" +load "${BASH_IT}/lib/search.bash" diff --git a/test/themes/base.theme.bats b/test/themes/base.theme.bats index dc8213d3..50098c1d 100644 --- a/test/themes/base.theme.bats +++ b/test/themes/base.theme.bats @@ -1,12 +1,8 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" -load ../../lib/log - -cite _about _param _example _group _author _version - -load ../../lib/helpers +load ../test_helper_libs +load ../../themes/colors.theme load ../../themes/base.theme @test 'themes base: battery_percentage should not exist' { @@ -22,30 +18,32 @@ load ../../themes/base.theme } @test 'themes base: battery_char should exist' { - run type -a battery_char &> /dev/null + run type -t battery_char assert_success + assert_line "function" run battery_char - assert_success assert_line -n 0 "" - - run type -a battery_char - assert_line " echo -n" } @test 'themes base: battery_char should exist if battery plugin loaded' { unset -f battery_char - load ../../plugins/available/battery.plugin - load ../../themes/base.theme - run type -a battery_char &> /dev/null + load ../../plugins/available/battery.plugin + run type -t battery_percentage assert_success + assert_line "function" + + load ../../themes/base.theme + run type -t battery_char + assert_success + assert_line "function" run battery_char assert_success run type -a battery_char - assert_line ' if [[ "${THEME_BATTERY_PERCENTAGE_CHECK}" = true ]]; then' + assert_output --partial 'THEME_BATTERY_PERCENTAGE_CHECK' } @test 'themes base: battery_charge should exist' { diff --git a/test/themes/base.theme.git.bats b/test/themes/base.theme.git.bats index f2c4b9c2..ad2c5a8d 100644 --- a/test/themes/base.theme.git.bats +++ b/test/themes/base.theme.git.bats @@ -1,12 +1,7 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" -load ../../lib/log - -cite _about _param _example _group _author _version - -load ../../lib/helpers +load ../test_helper_libs load ../../themes/githelpers.theme load ../../themes/base.theme diff --git a/test/themes/base.theme.svn.bats b/test/themes/base.theme.svn.bats index d1c2c311..789d85e5 100644 --- a/test/themes/base.theme.svn.bats +++ b/test/themes/base.theme.svn.bats @@ -1,12 +1,7 @@ #!/usr/bin/env bats load ../test_helper -load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" -load ../../lib/log - -cite _about _param _example _group _author _version - -load ../../lib/helpers +load ../test_helper_libs function local_setup { setup_test_fixture