Merge pull request #2002 from gaelicWizard/bats

tests: path quoting and boilerplate
pull/1958/head
Noah Gorny 2022-01-09 01:04:12 +02:00 committed by GitHub
commit 4ec1b5f3e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 107 additions and 152 deletions

View File

@ -122,7 +122,9 @@ plugins/available/zoxide.plugin.bash
# tests # tests
# #
test/plugins/alias-completion.plugin.bats test/plugins/alias-completion.plugin.bats
test/run
test/test_helper.bash test/test_helper.bash
test/test_helper_libs.bash
# themes # themes
# #

View File

@ -1,7 +1,6 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
function local_setup { function local_setup {
setup_test_fixture setup_test_fixture

View File

@ -1,7 +1,6 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
load ../../completion/available/bash-it.completion load ../../completion/available/bash-it.completion
function local_setup { function local_setup {

View File

@ -1,7 +1,6 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
# Determine which config file to use based on OS. # Determine which config file to use based on OS.
case $OSTYPE in case $OSTYPE in

View File

@ -1,7 +1,6 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
# Determine which config file to use based on OS. # Determine which config file to use based on OS.
case $OSTYPE in case $OSTYPE in

1
test/lib/composure.bats 100755 → 100644
View File

@ -1,7 +1,6 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
@test "lib composure: _composure_keywords()" { @test "lib composure: _composure_keywords()" {
run _composure_keywords run _composure_keywords

View File

@ -1,14 +1,7 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load ../test_helper_libs
load ../../lib/log
load ../../lib/utilities
load ../../lib/search
cite _about _param _example _group _author _version
load ../../lib/helpers
load ../../plugins/available/base.plugin load ../../plugins/available/base.plugin
function local_setup { function local_setup {

View File

@ -1,10 +1,8 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load ../../themes/colors.theme
load ../../lib/appearance
cite _about _param _example _group _author _version
load ../../lib/log load ../../lib/log
load ../../lib/helpers load ../../lib/helpers
load ../../plugins/available/base.plugin load ../../plugins/available/base.plugin

View File

@ -1,13 +1,7 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load ../test_helper_libs
load ../../lib/log
load ../../lib/helpers
load ../../lib/utilities
load ../../lib/search
cite _about _param _example _group _author _version
load ../../plugins/available/base.plugin load ../../plugins/available/base.plugin
load ../../aliases/available/git.aliases load ../../aliases/available/git.aliases

View File

@ -1,93 +1,84 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load ../test_helper_libs
load ../../lib/helpers
load ../../lib/utilities
load ../../lib/search
cite _about _param _example _group _author _version
function local_setup { function local_setup {
setup_test_fixture 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" { @test "_bash-it-component-item-is-enabled() - for a disabled item" {
run item_enabled aliases svn run _bash-it-component-item-is-enabled aliases svn
assert_line -n 0 '' assert_failure
} }
@test "_bash-it-component-item-is-enabled() - for an enabled/disabled item" { @test "_bash-it-component-item-is-enabled() - for an enabled/disabled item" {
run bash-it enable alias svn run bash-it enable alias svn
assert_line -n 0 'svn enabled with priority 150.' assert_line -n 0 'svn enabled with priority 150.'
run item_enabled alias svn run _bash-it-component-item-is-enabled alias svn
assert_line -n 0 'alias svn is enabled' assert_success
run _bash-it-component-item-is-disabled alias svn
assert_failure
run bash-it disable alias svn run bash-it disable alias svn
assert_line -n 0 'svn disabled.' assert_line -n 0 'svn disabled.'
run item_enabled alias svn run _bash-it-component-item-is-enabled alias svn
assert_line -n 0 '' assert_failure
run _bash-it-component-item-is-disabled alias svn
assert_success
} }
@test "_bash-it-component-item-is-disabled() - for a disabled item" { @test "_bash-it-component-item-is-disabled() - for a disabled item" {
run item_disabled alias svn run _bash-it-component-item-is-disabled alias svn
assert_line -n 0 'alias svn is disabled' assert_success
} }
@test "_bash-it-component-item-is-disabled() - for an enabled/disabled item" { @test "_bash-it-component-item-is-disabled() - for an enabled/disabled item" {
run bash-it enable alias svn run bash-it enable alias svn
assert_line -n 0 'svn enabled with priority 150.' assert_line -n 0 'svn enabled with priority 150.'
run item_disabled alias svn run _bash-it-component-item-is-disabled alias svn
assert_line -n 0 '' assert_failure
run _bash-it-component-item-is-enabled alias svn
assert_success
run bash-it disable alias svn run bash-it disable alias svn
assert_line -n 0 'svn disabled.' assert_line -n 0 'svn disabled.'
run item_disabled alias svn run _bash-it-component-item-is-disabled alias svn
assert_line -n 0 'alias svn is disabled' 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" { @test "_bash-it-array-contains-element() - when match is found, and is the first" {
declare -a fruits=(apple pear orange mandarin) declare -a fruits=(apple pear orange mandarin)
run has_match apple "${fruits[@]}" run _bash-it-array-contains-element apple "${fruits[@]}"
assert_line -n 0 'has apple' assert_success
} }
@test "_bash-it-array-contains-element() - when match is found, and is the last" { @test "_bash-it-array-contains-element() - when match is found, and is the last" {
declare -a fruits=(apple pear orange mandarin) declare -a fruits=(apple pear orange mandarin)
run has_match mandarin "${fruits[@]}" run _bash-it-array-contains-element mandarin "${fruits[@]}"
assert_line -n 0 'has mandarin' assert_success
} }
@test "_bash-it-array-contains-element() - when match is found, and is in the middle" { @test "_bash-it-array-contains-element() - when match is found, and is in the middle" {
declare -a fruits=(apple pear orange mandarin) declare -a fruits=(apple pear orange mandarin)
run has_match pear "${fruits[@]}" run _bash-it-array-contains-element pear "${fruits[@]}"
assert_line -n 0 'has pear' assert_success
} }
@test "_bash-it-array-contains-element() - when match is found, and it has spaces" { @test "_bash-it-array-contains-element() - when match is found, and it has spaces" {
declare -a fruits=(apple pear orange mandarin "yellow watermelon") declare -a fruits=(apple pear orange mandarin "yellow watermelon")
run has_match "yellow watermelon" "${fruits[@]}" run _bash-it-array-contains-element "yellow watermelon" "${fruits[@]}"
assert_line -n 0 'has yellow watermelon' assert_success
} }
@test "_bash-it-array-contains-element() - when match is not found" { @test "_bash-it-array-contains-element() - when match is not found" {
declare -a fruits=(apple pear orange mandarin) declare -a fruits=(apple pear orange mandarin)
run has_match xyz "${fruits[@]}" run _bash-it-array-contains-element xyz "${fruits[@]}"
assert_line -n 0 '' assert_failure
} }

View File

@ -1,11 +1,7 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load ../test_helper_libs
load ../../lib/log
load ../../lib/helpers
cite _about _param _example _group _author _version
load ../../completion/available/capistrano.completion load ../../completion/available/capistrano.completion

4
test/plugins/base.plugin.bats 100755 → 100644
View File

@ -1,9 +1,7 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load ../test_helper_libs
load ../../lib/log
load ../../lib/helpers
load ../../plugins/available/base.plugin load ../../plugins/available/base.plugin
@test 'plugins base: ips()' { @test 'plugins base: ips()' {

6
test/plugins/battery.plugin.bats 100755 → 100644
View File

@ -1,11 +1,7 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load ../test_helper_libs
load "${BASH_IT}/lib/log.bash"
load "${BASH_IT}/lib/helpers.bash"
cite _about _param _example _group _author _version
load ../../plugins/available/battery.plugin load ../../plugins/available/battery.plugin

View File

@ -1,8 +1,8 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load ../../lib/helpers load ../test_helper_libs
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load ../../vendor/init.d/preexec
load ../../plugins/available/cmd-returned-notify.plugin load ../../plugins/available/cmd-returned-notify.plugin

View File

@ -1,8 +1,7 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load ../../lib/helpers load ../test_helper_libs
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
# We test `go version` in each test to account for users with goenv and no system go. # We test `go version` in each test to account for users with goenv and no system go.

View File

@ -1,9 +1,7 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load ../../lib/helpers load ../test_helper_libs
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
load ../../plugins/available/ruby.plugin
function local_setup { function local_setup {
setup_test_fixture setup_test_fixture
@ -18,17 +16,21 @@ function local_teardown {
} }
@test "plugins ruby: remove_gem is defined" { @test "plugins ruby: remove_gem is defined" {
load ../../plugins/available/ruby.plugin
run type remove_gem run type remove_gem
assert_line -n 1 "remove_gem () " assert_line -n 1 "remove_gem () "
} }
@test "plugins ruby: PATH includes ~/.gem/ruby/bin" { @test "plugins ruby: PATH includes ~/.gem/ruby/bin" {
if ! which ruby >/dev/null; then if ! type ruby >/dev/null; then
skip 'ruby not installed' skip 'ruby not installed'
fi fi
mkdir -p "$(ruby -e 'print Gem.user_dir')/bin"
load ../../plugins/available/ruby.plugin load ../../plugins/available/ruby.plugin
local last_path_entry=$(echo $PATH | tr ":" "\n" | tail -1) local last_path_entry="$(tail -1 <<<"${PATH//:/$'\n'}")"
[[ "${last_path_entry}" == "${HOME}"/.gem/ruby/*/bin ]] [[ "${last_path_entry}" == "$(ruby -e 'print Gem.user_dir')/bin" ]]
} }

View File

@ -1,8 +1,7 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load ../../lib/helpers load ../test_helper_libs
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
load ../../plugins/available/xterm.plugin load ../../plugins/available/xterm.plugin
@ -21,30 +20,30 @@ function local_setup {
@test "plugins xterm: shorten command output" { @test "plugins xterm: shorten command output" {
export SHORT_TERM_LINE=true 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_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" { @test "plugins xterm: full command output" {
export SHORT_TERM_LINE=false 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_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" { @test "plugins xterm: shorten dirname output" {
export SHORT_TERM_LINE=true export SHORT_TERM_LINE=true
run _short-dirname run _short-dirname
assert_success assert_success
assert_output "$(basename $PWD)" assert_output "$(basename "${PWD}")"
} }
@test "plugins xterm: full dirname output" { @test "plugins xterm: full dirname output" {
export SHORT_TERM_LINE=false export SHORT_TERM_LINE=false
run _short-dirname run _short-dirname
assert_success assert_success
assert_output $PWD assert_output "${PWD}"
} }
@test "plugins xterm: set xterm title" { @test "plugins xterm: set xterm title" {

View File

@ -4,13 +4,12 @@ bats_executable="${test_directory}/../test_lib/bats-core/bin/bats"
git submodule init && git submodule update git submodule init && git submodule update
if [ -z "${BASH_IT}" ]; then if [[ -z "${BASH_IT}" ]]; then
declare BASH_IT
BASH_IT="$(cd "${test_directory}" && dirname "${PWD}")" BASH_IT="$(cd "${test_directory}" && dirname "${PWD}")"
export BASH_IT export BASH_IT
fi fi
if [ -z "$1" ]; then if [[ -z "$1" ]]; then
test_dirs=("${test_directory}"/{bash_it,completion,install,lib,plugins,themes}) test_dirs=("${test_directory}"/{bash_it,completion,install,lib,plugins,themes})
else else
test_dirs=("$1") test_dirs=("$1")
@ -22,25 +21,23 @@ fi
# so we can run `bats` in parallel processing mode, which is a lot faster. # so we can run `bats` in parallel processing mode, which is a lot faster.
if command -v parallel &> /dev/null \ if command -v parallel &> /dev/null \
&& parallel -V &> /dev/null \ && parallel -V &> /dev/null \
&& { parallel -V 2> /dev/null | grep -q '^GNU\>'; } && { parallel -V 2> /dev/null | grep -q '^GNU\>'; }; then
then
# Expect to run at least on a dual-core CPU; slightly degraded performance # Expect to run at least on a dual-core CPU; slightly degraded performance
# shouldn't matter otherwise. # shouldn't matter otherwise.
declare -i -r test_jobs_default=2 declare -i -r test_jobs_default=2
# shellcheck disable=SC2155
declare -i -r test_jobs_effective="$( declare -i -r test_jobs_effective="$(
if [ "${TEST_JOBS:-detect}" = "detect" ] \ if [[ "${TEST_JOBS:-detect}" = "detect" ]] \
&& command -v nproc &> /dev/null && command -v nproc &> /dev/null; then
then
nproc nproc
elif [ -n "${TEST_JOBS}" ] \ elif [[ -n "${TEST_JOBS}" ]] \
&& [ "${TEST_JOBS}" != "detect" ] && [[ "${TEST_JOBS}" != "detect" ]]; then
then
echo "${TEST_JOBS}" echo "${TEST_JOBS}"
else else
echo ${test_jobs_default} echo "${test_jobs_default}"
fi fi
)" )"
exec "$bats_executable" ${CI:+--tap} --jobs ${test_jobs_effective} \ exec "$bats_executable" "${CI:+--tap}" --jobs "${test_jobs_effective}" \
"${test_dirs[@]}" "${test_dirs[@]}"
else else
# Run `bats` in single-threaded mode. # Run `bats` in single-threaded mode.

3
test/test_helper.bash 100644 → 100755
View File

@ -21,6 +21,7 @@ load "${TEST_DEPS_DIR}/bats-file/load.bash"
# support 'plumbing' metadata # support 'plumbing' metadata
cite _about _param _example _group _author _version cite _about _param _example _group _author _version
cite about-alias about-plugin about-completion
local_setup() { local_setup() {
true true
@ -41,7 +42,7 @@ setup_test_fixture() {
if command -v rsync &> /dev/null; then if command -v rsync &> /dev/null; then
# Use rsync to copy Bash-it to the temp folder # 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 else
rm -rf "$BASH_IT" rm -rf "$BASH_IT"
mkdir -p "$BASH_IT" mkdir -p "$BASH_IT"

View File

@ -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"

View File

@ -1,12 +1,8 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load ../test_helper_libs
load ../../lib/log load ../../themes/colors.theme
cite _about _param _example _group _author _version
load ../../lib/helpers
load ../../themes/base.theme load ../../themes/base.theme
@test 'themes base: battery_percentage should not exist' { @test 'themes base: battery_percentage should not exist' {
@ -22,30 +18,32 @@ load ../../themes/base.theme
} }
@test 'themes base: battery_char should exist' { @test 'themes base: battery_char should exist' {
run type -a battery_char &> /dev/null run type -t battery_char
assert_success assert_success
assert_line "function"
run battery_char run battery_char
assert_success
assert_line -n 0 "" assert_line -n 0 ""
run type -a battery_char
assert_line " echo -n"
} }
@test 'themes base: battery_char should exist if battery plugin loaded' { @test 'themes base: battery_char should exist if battery plugin loaded' {
unset -f battery_char 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_success
assert_line "function"
load ../../themes/base.theme
run type -t battery_char
assert_success
assert_line "function"
run battery_char run battery_char
assert_success assert_success
run type -a battery_char 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' { @test 'themes base: battery_charge should exist' {

View File

@ -1,12 +1,7 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load ../test_helper_libs
load ../../lib/log
cite _about _param _example _group _author _version
load ../../lib/helpers
load ../../themes/githelpers.theme load ../../themes/githelpers.theme
load ../../themes/base.theme load ../../themes/base.theme

View File

@ -1,12 +1,7 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load ../test_helper_libs
load ../../lib/log
cite _about _param _example _group _author _version
load ../../lib/helpers
function local_setup { function local_setup {
setup_test_fixture setup_test_fixture