beets/test/test_completion.sh
Thomas Scholtes e8e0682aae Add completion support for bash 3.2
Bash 3.2 does not have associative arrays, so we hack around that by using
generic varibale names like `opts__$cmd`. We also don't support the "?" alias
anymore.
2014-02-22 17:59:23 +01:00

156 lines
2.4 KiB
Bash

#!/bin/bash
initcli() {
COMP_WORDS=( "beet" "$@" )
let COMP_CWORD=${#COMP_WORDS[@]}-1
_beet
}
completes() {
for word in "$@"; do
[[ " ${COMPREPLY[@]} " == *[[:space:]]$word[[:space:]]* ]] || return 1
done
}
COMMANDS='fields import list update remove
stats version modify move write
help'
HELP_OPTS='-h --help'
test_commands() {
initcli '' &&
completes $COMMANDS &&
initcli -v '' &&
completes $COMMANDS &&
initcli -l help '' &&
completes $COMMANDS &&
initcli -d list '' &&
completes $COMMANDS &&
initcli -h '' &&
completes $COMMANDS &&
true
}
test_command_aliases() {
initcli ls &&
completes list &&
initcli l &&
!( completes ls; ) &&
initcli im &&
completes import &&
true
}
test_global_opts() {
initcli - &&
completes \
-l --library \
-d --directory \
-h --help \
-c --config \
-v --verbose &&
true
}
test_global_file_opts() {
initcli --library '' &&
completes $(compgen -f) &&
initcli -l '' &&
completes $(compgen -f) &&
initcli --config '' &&
completes $(compgen -f) &&
initcli -c '' &&
completes $(compgen -f) &&
true
}
test_global_dir_opts() {
initcli --directory '' &&
completes $(compgen -d) &&
initcli -d '' &&
completes $(compgen -d) &&
true
}
test_fields_command() {
initcli fields - &&
completes -h --help &&
initcli fields '' &&
completes $(compgen -f) &&
true
}
test_import_files() {
initcli import '' &&
completes $(compgen -f) &&
initcli import --copy -P '' &&
completes $(compgen -f) &&
initcli import --log '' &&
completes $(compgen -f) &&
true
}
test_import_options() {
initcli imp -
completes \
-h --help \
-c --copy -C --nocopy \
-w --write -W --nowrite \
-a --autotag -A --noautotag \
-p --resume -P --noresume \
-l --log --flat
}
test_list_options() {
initcli list -
completes \
-h --help \
-a --album \
-p --path
}
test_help_command() {
initcli help '' &&
completes $COMMANDS &&
true
}
run_tests() {
local tests=$(set | \
grep --extended-regexp --only-matching '^test_[a-zA-Z_]* \(\) $' |\
grep --extended-regexp --only-matching '[a-zA-Z_]*'
)
local fail=0
if [[ -n $@ ]]; then
tests="$@"
fi
for t in $tests; do
$t || { fail=1 && echo "$t failed" >&2; }
done
return $fail
}
run_tests "$@" && echo "completion tests passed"