diff --git a/navi b/navi index 99d6a5a..93f66c0 100755 --- a/navi +++ b/navi @@ -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 "$@" diff --git a/src/health.sh b/src/health.sh index 90ed69d..3e7eb6e 100644 --- a/src/health.sh +++ b/src/health.sh @@ -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 diff --git a/src/main.sh b/src/main.sh index 321d38f..fe82da6 100644 --- a/src/main.sh +++ b/src/main.sh @@ -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 diff --git a/src/ui.sh b/src/ui.sh index 91bc74b..5846b6e 100644 --- a/src/ui.sh +++ b/src/ui.sh @@ -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 diff --git a/test/core.sh b/test/core.sh index fcc630d..0af5d48 100644 --- a/test/core.sh +++ b/test/core.sh @@ -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}" diff --git a/test/integration_test.sh b/test/integration_test.sh new file mode 100644 index 0000000..7c7dea5 --- /dev/null +++ b/test/integration_test.sh @@ -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 diff --git a/test/playground.cheat b/test/playground.cheat index 89ff2a5..29acd98 100644 --- a/test/playground.cheat +++ b/test/playground.cheat @@ -1,7 +1,10 @@ % test, playground -# single and double quotes + newlines +# this should be the first test. single and double quotes + newlines echo -$ x: echo -e '1\n2\n3' -$ y: echo -e "${x}_a\n${x}_b" \ No newline at end of file +# return a constant number +echo 42 + +$ x: echo -e '2\n3\n4' +$ y: echo -e "$((x+10))\n$((x+20))" \ No newline at end of file