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
#
test/plugins/alias-completion.plugin.bats
test/run
test/test_helper.bash
test/test_helper_libs.bash
# themes
#

View File

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

View File

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

View File

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

View File

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

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

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

View File

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

View File

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

View File

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

View File

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

View File

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

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

@ -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()' {

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

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

View File

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

View File

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

View File

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

View File

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

View File

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

3
test/test_helper.bash 100644 → 100755
View File

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

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
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' {

View File

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

View File

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