Improve test coverage (#84)

Add integration tests
This commit is contained in:
Denis Isidoro 2019-09-26 18:53:50 -03:00
parent beab843c1e
commit b6a5ac1495
7 changed files with 74 additions and 12 deletions

1
navi
View file

@ -36,6 +36,7 @@ source "${SCRIPT_DIR}/src/main.sh"
##? Please refer to the README at https://github.com/denisidoro/navi
VERSION="0.9.4"
NAVI_ENV="${NAVI_ENV:-prod}"
opts::eval "$@"
main "$@"

View file

@ -1,7 +1,7 @@
#!/usr/bin/env bash
health::fzf() {
if ! command_exists fzf; then
if ! command_exists fzf && ! [ $NAVI_ENV -eq "test" ]; then
echoerr "You need to install fzf before using navi"
echoerr "Please refer to https://github.com/junegunn/fzf for install instructions"
exit 66

View file

@ -1,6 +1,6 @@
#!/usr/bin/env bash
if ${NAVI_FORCE_GNU:-false}; then
if ${NAVI_FORCE_GNU:-false} && [ -n "${DOTFILES:-}" ]; then
source "${DOTFILES}/scripts/core/main.sh"
fi

View file

@ -10,7 +10,8 @@ ui::pick() {
args+=("--select-1")
fi
fzf "${args[@]:-}" --inline-info "$@"
local -r fzf_cmd="$([ $NAVI_ENV == "test" ] && echo "fzf_mock" || echo "fzf")"
"$fzf_cmd" "${args[@]:-}" --inline-info "$@"
}
# TODO: separation of concerns

View file

@ -1,5 +1,7 @@
#!/usr/bin/env bash
export NAVI_FORCE_GNU=true
source "${SCRIPT_DIR}/src/main.sh"
source "${SCRIPT_DIR}/test/log.sh"
@ -7,8 +9,13 @@ opts::eval "$@"
PASSED=0
FAILED=0
SKIPPED=0
SUITE=""
test::set_suite() {
SUITE="$*"
}
test::success() {
PASSED=$((PASSED+1))
log::success "Test passed!"
@ -20,8 +27,12 @@ test::fail() {
return
}
test::set_suite() {
SUITE="$*"
test::skip() {
echo
log::note "${SUITE:-unknown} - ${1:-unknown}"
SKIPPED=$((SKIPPED+1))
log::warning "Test skipped..."
return
}
test::run() {
@ -41,12 +52,11 @@ test::equals() {
fi
}
test::skip() {
:
}
test::finish() {
echo
if [ $SKIPPED -gt 0 ]; then
log::warning "${SKIPPED} tests skipped!"
fi
if [ $FAILED -gt 0 ]; then
log::error "${PASSED} tests passed but ${FAILED} failed... :("
exit "${FAILED}"

47
test/integration_test.sh Normal file
View file

@ -0,0 +1,47 @@
#!/usr/bin/env bash
NAVI_BIN="${SCRIPT_DIR}/navi"
TEST_DIR="${SCRIPT_DIR}/test"
_navi() {
"$NAVI_BIN" "$@"
}
fzf_mock() {
head -n1 | sed 's/\x1b\[[0-9;]*m//g'
}
assert_version() {
local -r version="$(cat "$NAVI_BIN" | grep VERSION | cut -d'=' -f2 | tr -d '"')"
_navi --version \
| test::equals "$version"
}
assert_help() {
_navi --help \
| grep -q 'Options:'
}
assert_home() {
_navi home \
| grep -q '/'
}
assert_best() {
_navi best constant --path "$TEST_DIR" \
| test::equals 42
}
assert_query() {
NAVI_ENV="test" _navi --path "$TEST_DIR" \
| test::equals "2 12"
}
test::set_suite "integration"
export -f fzf_mock
test::run "version" assert_version
test::run "help" assert_help
test::run "home" assert_home
test::skip "best" assert_best # FZF setup needed in CircleCI
test::skip "query" assert_query # FZF setup needed in CircleCI

View file

@ -1,7 +1,10 @@
% test, playground
# single and double quotes + newlines
# this should be the first test. single and double quotes + newlines
echo <x> <y>
$ x: echo -e '1\n2\n3'
$ y: echo -e "${x}_a\n${x}_b"
# return a constant number
echo 42
$ x: echo -e '2\n3\n4'
$ y: echo -e "$((x+10))\n$((x+20))"