navi/tests/run
2020-09-15 16:38:28 -03:00

127 lines
2.6 KiB
Bash
Executable file

#!/usr/bin/env bash
export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)"
source "${NAVI_HOME}/tests/core.bash"
TEST_CHEAT_PATH="${NAVI_HOME}/tests/no_prompt_cheats"
NAVI_EXE="${NAVI_HOME}/target/debug/navi"
_navi() {
stty sane || true
RUST_BACKTRACE=1 NAVI_PATH="${NAVI_TEST_PATH:-$TEST_CHEAT_PATH}" "$NAVI_EXE" "$@"
}
_navi_cases() {
local filter="$*"
filter="${filter::-2}"
_navi --query "$filter" --best-match
}
_navi_cases_test() {
_navi_cases "$1" \
| test::equals "$2"
}
_get_all_tests() {
cat "${TEST_CHEAT_PATH}/cases.cheat" \
| grep '^#' \
| grep ' ->' \
| sed 's/\\n/ /g' \
| sed -E 's/# (.*) -> "(.*)"/\1|\2/g'
}
_get_tests() {
local -r filter="$1"
if [ -n "$filter" ]; then
_get_all_tests \
| grep "$filter"
else
_get_all_tests
fi
}
_navi_tldr() {
_navi --tldr docker --query ps --print --best-match \
| test::equals "docker ps"
}
_navi_cheatsh() {
_navi --cheatsh docker --query remove --print --best-match \
| test::equals "docker rm container_name"
}
_navi_widget() {
_navi widget "$1" \
| grep -q "#!/usr/bin/env $1"
}
_navi_cheatspath() {
_navi info cheats-path \
| grep -q "/cheats"
}
_integration() {
local -r log_file="${NAVI_HOME}/target/ci.log"
tmux new-session -d -s ci "export NAVI_TEST_PATH='$($NAVI_EXE info cheats-path)'; $0 _navi repo browse |& tee '${log_file}'"
sleep 5
tmux send-key -t ci denisidoro/cheats; tmux send-key -t ci Enter
sleep 1
tmux send-key -t ci y; tmux send-key -t ci Enter
sleep 6
tmux send-key -t ci pwd
sleep 1
tmux send-key -t ci Enter
sleep 2
cat "$log_file" | tail -n10 | grep -q "/navi"
}
if ! command_exists fzf; then
export PATH="$PATH:$HOME/.fzf/bin"
fi
cd "$NAVI_HOME"
filter="${1:-}"
# TODO: remove this
if [[ $filter == "_navi" ]]; then
shift
_navi "$@"
exit 0
fi
test::set_suite "cases"
ifs="$IFS"
IFS=$'\n'
for i in $(_get_tests "$filter"); do
IFS="$ifs"
query="$(echo "$i" | cut -d'|' -f1)"
expected="$(echo "$i" | cut -d'|' -f2)"
test::run "$query" _navi_cases_test "$query" "$expected"
done
test::set_suite "tldr"
test::run "$query" _navi_tldr
test::set_suite "cheatsh"
test::run "$query" _navi_cheatsh
test::set_suite "widget - bash"
test::run "$query" _navi_widget "bash"
test::set_suite "widget - zsh"
test::run "$query" _navi_widget "zsh"
test::set_suite "widget - fish"
test::run "$query" _navi_widget "fish"
test::set_suite "info - cheats-path"
test::run "$query" _navi_cheatspath
test::set_suite "integration"
test::run "$query" _integration
test::finish