navi/tests/run

161 lines
3.5 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
2020-09-16 00:45:40 +03:00
set -euo pipefail
export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)"
source "${NAVI_HOME}/tests/core.bash"
2020-09-16 00:45:40 +03:00
export TEST_CHEAT_PATH="${NAVI_HOME}/tests/no_prompt_cheats"
export NAVI_EXE="${NAVI_HOME}/target/debug/navi"
if ! command_exists navi; then
navi() {
"$NAVI_EXE" "$@"
}
export -f navi
fi
_navi() {
stty sane || true
2020-09-15 22:38:28 +03:00
RUST_BACKTRACE=1 NAVI_PATH="${NAVI_TEST_PATH:-$TEST_CHEAT_PATH}" "$NAVI_EXE" "$@"
}
_navi_cases() {
2021-01-18 19:03:39 +03:00
local -r filter="${1::-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
}
2020-09-15 22:30:30 +03:00
_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() {
2021-01-18 19:03:39 +03:00
echo "$(_navi widget "$1")" \
2020-09-15 22:30:30 +03:00
| grep -q "#!/usr/bin/env $1"
}
_navi_cheatspath() {
_navi info cheats-path \
| grep -q "/cheats"
}
2020-09-16 00:45:40 +03:00
_kill_tmux() {
pkill -f tmux 2>/dev/null || true
}
_assert_tmux() {
local -r log_file="$1"
local -r sessions="$(tmux list-sessions)"
if [ -z "$sessions" ]; then
_kill_tmux
cat "$log_file"
return 1
fi
}
2020-09-15 22:30:30 +03:00
_integration() {
2020-09-16 00:45:40 +03:00
_kill_tmux
2020-09-15 22:30:30 +03:00
local -r log_file="${NAVI_HOME}/target/ci.log"
2020-09-16 00:45:40 +03:00
local -r cheats_path="$($NAVI_EXE info cheats-path)"
mkdir -p "$cheats_path" 2>/dev/null || true
local -r bak_cheats_path="$(mktemp -d "${cheats_path}_XXXXX")"
rm "$log_file" 2>/dev/null || true
mv "$cheats_path" "$bak_cheats_path" 2>/dev/null || true
2021-04-19 15:54:35 +03:00
log::note "Starting sessions..."
2020-09-16 00:45:40 +03:00
tmux new-session -d -s ci "export NAVI_TEST_PATH='${cheats_path}'; ${NAVI_HOME}/tests/run _navi |& tee '${log_file}'"
2020-09-15 22:30:30 +03:00
sleep 5
2020-09-16 00:45:40 +03:00
_assert_tmux "$log_file"
2021-04-19 15:54:35 +03:00
log::note "Downloading default cheatsheets..."
2020-09-16 00:45:40 +03:00
tmux send-key -t ci "download default"; tmux send-key -t ci "Enter"
2020-09-15 22:30:30 +03:00
sleep 1
2020-09-16 00:45:40 +03:00
_assert_tmux "$log_file"
2021-04-19 15:54:35 +03:00
log::note "Confirming import..."
2020-09-16 00:45:40 +03:00
tmux send-key -t ci "y"; tmux send-key -t ci "Enter"
2020-09-15 22:30:30 +03:00
sleep 6
2020-09-16 00:45:40 +03:00
_assert_tmux "$log_file"
2021-04-19 15:54:35 +03:00
log::note "Running snippet..."
2020-09-16 00:45:40 +03:00
tmux send-key -t ci "pwd"
2020-10-08 23:45:01 +03:00
sleep 1
2020-09-16 00:45:40 +03:00
tmux send-key -t ci "Enter"
2020-09-15 22:30:30 +03:00
2021-04-19 15:54:35 +03:00
log::note "Checking paths..."
2020-09-15 22:30:30 +03:00
sleep 2
2021-04-11 13:43:25 +03:00
local -r downloaded_path="$(cat "$log_file" | grep 'They are now located at' | sed 's/They are now located at //')"
ls "$downloaded_path" | grep -q '^pkg_mgr__brew.cheat$'
2020-09-15 22:30:30 +03:00
}
if ! command_exists fzf; then
export PATH="$PATH:$HOME/.fzf/bin"
fi
cd "$NAVI_HOME"
filter="${1:-}"
2020-09-15 22:30:30 +03:00
# 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
2021-04-11 13:43:25 +03:00
test::set_suite "info"
test::run "cheats_path" _navi_cheatspath
test::set_suite "integration"
test::run "welcome->pwd" _integration
2020-09-15 22:30:30 +03:00
2020-09-16 00:45:40 +03:00
test::set_suite "widget"
test::run "bash" _navi_widget "bash"
test::run "zsh" _navi_widget "zsh"
test::run "zsh" _navi_widget "fish"
2020-09-15 22:30:30 +03:00
2021-04-11 13:43:25 +03:00
test::set_suite "3rd party"
test::run "tldr" _navi_tldr
test::run "cheatsh" _navi_cheatsh
2020-09-15 22:30:30 +03:00
test::finish