beets/test/test_completion.sh
Thomas Scholtes 14ece207c9 Add completion command
The command prints a shell script that provides completion for the `beet`
command. To test it run `eval "$(beet completion)"` in your shell.

I also included some crude testing for this. The `test/test_completion.sh`
script runs tests in a shell and exit with a non-zero status code if the tests
fail. It assumes that the completion script is already loaded in the executing
shell.

As of now the completion only works for bash 4.1 and newer.
2014-02-20 01:15:03 +01:00

159 lines
2.4 KiB
Bash

#!/bin/bash
initcli() {
COMP_WORDS=( "beet" "$@" )
let COMP_CWORD=${#COMP_WORDS[@]}-1
_beet
}
completes() {
for word in "$@"; do
[[ ${COMPREPLY[@]} =~ "$word" ]] || 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 &&
initcli '?' '' &&
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"