2020-03-19 12:19:50 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-09-15 21:45:40 +00:00
|
|
|
set -euo pipefail
|
2020-03-19 12:19:50 +00:00
|
|
|
|
|
|
|
export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)"
|
|
|
|
source "${NAVI_HOME}/tests/core.bash"
|
|
|
|
|
2020-09-15 21:45:40 +00: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
|
2020-03-19 12:19:50 +00:00
|
|
|
|
|
|
|
_navi() {
|
|
|
|
stty sane || true
|
2021-06-15 12:43:40 +00:00
|
|
|
local path="${NAVI_TEST_PATH:-$TEST_CHEAT_PATH}"
|
|
|
|
path="${path//$HOME/~}"
|
2021-08-07 13:31:53 +00:00
|
|
|
export NAVI_ENV_VAR_PATH="$path"
|
2021-11-23 12:54:25 +00:00
|
|
|
RUST_BACKTRACE=1 NAVI_PATH='$NAVI_ENV_VAR_PATH' NAVI_CONFIG="${NAVI_HOME}/tests/config.yaml" "$NAVI_EXE" "$@"
|
2020-08-28 12:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_navi_cases() {
|
2021-01-18 16:03:39 +00:00
|
|
|
local -r filter="${1::-2}"
|
2020-08-28 12:58:35 +00:00
|
|
|
_navi --query "$filter" --best-match
|
2020-03-19 12:19:50 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 12:58:35 +00:00
|
|
|
_navi_cases_test() {
|
|
|
|
_navi_cases "$1" \
|
2020-03-19 12:19:50 +00:00
|
|
|
| test::equals "$2"
|
|
|
|
}
|
|
|
|
|
|
|
|
_get_all_tests() {
|
|
|
|
cat "${TEST_CHEAT_PATH}/cases.cheat" \
|
2020-08-28 12:58:35 +00:00
|
|
|
| grep '^#' \
|
2020-03-19 12:19:50 +00:00
|
|
|
| grep ' ->' \
|
2021-08-07 15:24:39 +00:00
|
|
|
| sed 's/\\n/'"$(printf "$NEWLINE_CHAR")"'/g' \
|
2020-03-19 12:19:50 +00:00
|
|
|
| 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 19:30:30 +00:00
|
|
|
_navi_tldr() {
|
|
|
|
_navi --tldr docker --query ps --print --best-match \
|
2022-01-12 11:50:07 +00:00
|
|
|
| test::contains "docker ps"
|
2020-09-15 19:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_navi_cheatsh() {
|
|
|
|
_navi --cheatsh docker --query remove --print --best-match \
|
2022-01-12 11:50:07 +00:00
|
|
|
| test::contains "docker rm"
|
2020-09-15 19:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_navi_widget() {
|
2021-09-16 12:11:35 +00:00
|
|
|
local -r out="$(_navi widget "$1")"
|
|
|
|
if ! echo "$out" | grep -q "navi "; then
|
|
|
|
echo "$out"
|
|
|
|
return 1
|
|
|
|
fi
|
2020-09-15 19:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_navi_cheatspath() {
|
|
|
|
_navi info cheats-path \
|
|
|
|
| grep -q "/cheats"
|
|
|
|
}
|
|
|
|
|
2020-09-15 21:45:40 +00: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 19:30:30 +00:00
|
|
|
_integration() {
|
2020-09-15 21:45:40 +00:00
|
|
|
_kill_tmux
|
2020-09-15 19:30:30 +00:00
|
|
|
local -r log_file="${NAVI_HOME}/target/ci.log"
|
2020-09-15 21:45:40 +00:00
|
|
|
local -r cheats_path="$($NAVI_EXE info cheats-path)"
|
2021-08-07 13:31:53 +00:00
|
|
|
rm -rf "$cheats_path" 2>/dev/null || true
|
2020-09-15 21:45:40 +00:00
|
|
|
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 12:54:35 +00:00
|
|
|
log::note "Starting sessions..."
|
2020-09-15 21:45:40 +00:00
|
|
|
tmux new-session -d -s ci "export NAVI_TEST_PATH='${cheats_path}'; ${NAVI_HOME}/tests/run _navi |& tee '${log_file}'"
|
2020-09-15 19:30:30 +00:00
|
|
|
sleep 5
|
2020-09-15 21:45:40 +00:00
|
|
|
_assert_tmux "$log_file"
|
|
|
|
|
2021-04-19 12:54:35 +00:00
|
|
|
log::note "Downloading default cheatsheets..."
|
2020-09-15 21:45:40 +00:00
|
|
|
tmux send-key -t ci "download default"; tmux send-key -t ci "Enter"
|
2020-09-15 19:30:30 +00:00
|
|
|
sleep 1
|
2020-09-15 21:45:40 +00:00
|
|
|
_assert_tmux "$log_file"
|
|
|
|
|
2021-04-19 12:54:35 +00:00
|
|
|
log::note "Confirming import..."
|
2021-08-07 13:31:53 +00:00
|
|
|
tmux send-key -t ci "y"
|
|
|
|
sleep 1
|
|
|
|
tmux send-key -t ci "Enter"
|
2020-09-15 19:30:30 +00:00
|
|
|
sleep 6
|
2020-09-15 21:45:40 +00:00
|
|
|
_assert_tmux "$log_file"
|
|
|
|
|
2021-04-19 12:54:35 +00:00
|
|
|
log::note "Running snippet..."
|
2020-09-15 21:45:40 +00:00
|
|
|
tmux send-key -t ci "pwd"
|
2020-10-08 20:45:01 +00:00
|
|
|
sleep 1
|
2020-09-15 21:45:40 +00:00
|
|
|
tmux send-key -t ci "Enter"
|
2020-09-15 19:30:30 +00:00
|
|
|
|
2021-04-19 12:54:35 +00:00
|
|
|
log::note "Checking paths..."
|
2020-09-15 19:30:30 +00:00
|
|
|
sleep 2
|
2021-04-11 10:43:25 +00: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 19:30:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 12:19:50 +00:00
|
|
|
if ! command_exists fzf; then
|
|
|
|
export PATH="$PATH:$HOME/.fzf/bin"
|
|
|
|
fi
|
|
|
|
|
|
|
|
cd "$NAVI_HOME"
|
|
|
|
|
|
|
|
filter="${1:-}"
|
|
|
|
|
2020-09-15 19:30:30 +00:00
|
|
|
# TODO: remove this
|
|
|
|
if [[ $filter == "_navi" ]]; then
|
|
|
|
shift
|
|
|
|
_navi "$@"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-03-19 12:19:50 +00:00
|
|
|
test::set_suite "cases"
|
|
|
|
ifs="$IFS"
|
|
|
|
IFS=$'\n'
|
|
|
|
for i in $(_get_tests "$filter"); do
|
|
|
|
IFS="$ifs"
|
|
|
|
query="$(echo "$i" | cut -d'|' -f1)"
|
2021-08-07 15:24:39 +00:00
|
|
|
expected="$(echo "$i" | tr "$NEWLINE_CHAR" '\n' | cut -d'|' -f2)"
|
2020-08-28 12:58:35 +00:00
|
|
|
test::run "$query" _navi_cases_test "$query" "$expected"
|
2020-03-19 12:19:50 +00:00
|
|
|
done
|
|
|
|
|
2021-04-11 10:43:25 +00:00
|
|
|
test::set_suite "info"
|
|
|
|
test::run "cheats_path" _navi_cheatspath
|
|
|
|
|
2020-09-15 21:45:40 +00:00
|
|
|
test::set_suite "widget"
|
|
|
|
test::run "bash" _navi_widget "bash"
|
|
|
|
test::run "zsh" _navi_widget "zsh"
|
2021-09-16 12:11:35 +00:00
|
|
|
test::run "fish" _navi_widget "fish"
|
2021-09-08 21:51:44 +00:00
|
|
|
test::run "elvish" _navi_widget "elvish"
|
2020-09-15 19:30:30 +00:00
|
|
|
|
2021-04-11 10:43:25 +00:00
|
|
|
test::set_suite "3rd party"
|
|
|
|
test::run "tldr" _navi_tldr
|
|
|
|
test::run "cheatsh" _navi_cheatsh
|
2020-09-15 19:30:30 +00:00
|
|
|
|
2021-09-16 12:11:35 +00:00
|
|
|
test::set_suite "integration"
|
|
|
|
test::run "welcome->pwd" _integration
|
|
|
|
|
2020-04-16 12:40:11 +00:00
|
|
|
test::finish
|