Improve test runner (#41)

This commit is contained in:
Denis Isidoro 2019-09-23 08:56:15 -03:00 committed by GitHub
parent be92d158ad
commit b3f8e0b374
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 60 additions and 34 deletions

2
navi
View file

@ -28,7 +28,7 @@ source "${SCRIPT_DIR}/src/main.sh"
##? cheats query git
##?
##? More info:
##? search
##? search
##? Queries cheatsheets from http://cheat.sh
##? Please note that these cheatsheets may not work out of the box
##? Always check the preview window before executing commands!

View file

@ -1,9 +1,9 @@
#!/usr/bin/env bash
cheat::find() {
for path in $(echo "$NAVI_PATH" | tr ':' '\n'); do
find "$path" -iname '*.cheat'
done
for path in $(echo "$NAVI_PATH" | tr ':' '\n'); do
find "$path" -iname '*.cheat'
done
}
cheat::read_many() {

View file

@ -52,18 +52,18 @@ handler::preview() {
main() {
case ${entry_point:-} in
preview)
preview)
handler::preview "$@" \
|| echo "Unable to find command for '${query:-}'"
;;
search)
health::fzf
|| echo "Unable to find command for '${query:-}'"
;;
search)
health::fzf
search::save "$query" || true
handler::main "$@"
;;
*)
health::fzf
handler::main "$@"
*)
health::fzf
handler::main "$@"
;;
esac
}

View file

@ -40,9 +40,9 @@ opts::eval() {
--command-for) wait_for="command-for" ;;
--no-preview) preview=false ;;
--path) wait_for="path" ;;
search) entry_point="search"; wait_for="search";;
preview) entry_point="preview"; wait_for="preview";;
q|query) wait_for="query";;
search) entry_point="search"; wait_for="search" ;;
preview) entry_point="preview"; wait_for="preview" ;;
q|query) wait_for="query" ;;
esac
done
}

View file

@ -29,7 +29,7 @@ search::save() {
local readonly filepath="$(search::full_path "$cmd")"
local readonly filedir="$(dirname "$filepath")"
if [ -f "$filepath" ]; then
return
fi

4
test/cheat_test.sh Normal file
View file

@ -0,0 +1,4 @@
#!/usr/bin/env bash
test::run "We can find at least one known cheatsheet" \
'cheat::find | grep -q "docker.cheat"'

View file

@ -2,13 +2,18 @@
source "${SCRIPT_DIR}/src/main.sh"
PASSED=0
FAILED=0
test::success() {
PASSED=$((PASSED+1))
echo "Test passed!"
}
test::fail() {
FAILED=$((FAILED+1))
echo "Test failed..."
exit 42
return
}
test::run() {
@ -16,4 +21,15 @@ test::run() {
echo "-> $1"
shift
eval "$*" && test::success || test::fail
}
}
test::finish() {
echo
if [ $FAILED -gt 0 ]; then
echo "${PASSED} tests passed but ${FAILED} failed... :("
exit "${FAILED}"
else
echo "All ${PASSED} tests passed! :)"
exit 0
fi
}

View file

@ -4,21 +4,10 @@ set -euo pipefail
export SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
source "${SCRIPT_DIR}/test/core.sh"
check_all_vars() {
local arg
IFS=$'\n'
for var in $(cat "$1" | grep -Eo "<[^>]*>"); do
if ! echo "$var" | grep -qE "$ARG_REGEX"; then
echoerr "$var isn't a valid variable name!"
exit 1
fi
done
}
tests="$(find "$SCRIPT_DIR/test" -iname '*_test.sh')"
test::run "We can find at least one known cheatsheet" \
'cheat::find | grep -q "docker.cheat"'
for cheat in $(cheat::find); do
test::run "All variables in $(basename $cheat) are valid" \
'check_all_vars "$cheat"'
for test in $tests; do
source "$test"
done
test::finish

17
test/var_test.sh Normal file
View file

@ -0,0 +1,17 @@
#!/usr/bin/env bash
check_all_vars() {
local arg
IFS=$'\n'
for var in $(cat "$1" | grep -Eo "<[^>]*>"); do
if ! echo "$var" | grep -qE "$ARG_REGEX"; then
echoerr "$var isn't a valid variable name!"
return 1
fi
done
}
for cheat in $(cheat::find); do
test::run "All variables in $(basename $cheat) are valid" \
'check_all_vars "$cheat"'
done