From 7680b1997973de2c8d5ea1072507cf191e698c04 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 23 Sep 2022 20:54:18 -0700 Subject: [PATCH] Inline setup and cleanup functions in completion script test (#1352) --- justfile | 3 ++ tests/completions/just.bash | 55 ++++++------------------------------- 2 files changed, 11 insertions(+), 47 deletions(-) diff --git a/justfile b/justfile index 17318d7..26283f1 100755 --- a/justfile +++ b/justfile @@ -170,6 +170,9 @@ watch-readme: generate-completions: ./bin/generate-completions +test-completions: + ./tests/completions/just.bash + build-book: cargo run --package generate-book mdbook build book/en diff --git a/tests/completions/just.bash b/tests/completions/just.bash index d9c74bb..79469ca 100755 --- a/tests/completions/just.bash +++ b/tests/completions/just.bash @@ -1,30 +1,14 @@ #!/usr/bin/env bash # --- Shared functions --- - -cdroot() { - cd "$(git rev-parse --show-toplevel)" > /dev/null -} - -setup() { - cdroot - cd tests/completions > /dev/null -} - -cleanup() { - unset COMP_WORDS - unset COMP_CWORD - unset COMPREPLY -} - reply_equals() { local reply=$(declare -p COMPREPLY) - local expected="$1" + local expected=$1 if [ "$reply" = "$expected" ]; then echo "${FUNCNAME[1]}: ok" else - exit_code='1' + exit_code=1 echo >&2 "${FUNCNAME[1]}: failed! Completion for \`${COMP_WORDS[*]}\` does not match." echo @@ -34,33 +18,18 @@ reply_equals() { } # --- Initial Setup --- -cdroot source ./completions/just.bash +cd tests/completions +cargo build PATH="$(git rev-parse --show-toplevel)/target/debug:$PATH" -exit_code='0' +exit_code=0 # --- Tests --- - -test_just_is_accessible() { - if just --version > /dev/null; then - echo "${FUNCNAME[0]}: ok" - else - echo "${FUNCNAME[0]}: failed! Can't find just binary." - echo " PATH=$PATH" - echo - exit_code='1' - fi -} -setup -test_just_is_accessible - test_complete_all_recipes() { COMP_WORDS=(just) COMP_CWORD=1 _just just reply_equals 'declare -a COMPREPLY=([0]="deploy" [1]="install" [2]="publish" [3]="push" [4]="test")' } -cleanup -setup test_complete_all_recipes test_complete_recipes_starting_with_i() { @@ -68,18 +37,13 @@ test_complete_recipes_starting_with_i() { COMP_CWORD=1 _just just reply_equals 'declare -a COMPREPLY=([0]="install")' } -cleanup -setup test_complete_recipes_starting_with_i test_complete_recipes_starting_with_p() { - setup COMP_WORDS=(just p) COMP_CWORD=1 _just just reply_equals 'declare -a COMPREPLY=([0]="publish" [1]="push")' } -cleanup -setup test_complete_recipes_starting_with_p test_complete_recipes_from_subdirs() { @@ -87,16 +51,13 @@ test_complete_recipes_from_subdirs() { COMP_CWORD=1 _just just reply_equals 'declare -a COMPREPLY=([0]="subdir/special" [1]="subdir/surprise")' } -cleanup -setup test_complete_recipes_from_subdirs -cleanup # --- Conclusion --- - -if [ "$exit_code" = '0' ]; then +if [ $exit_code = 0 ]; then echo "All tests passed." else echo "Some test[s] failed." fi -exit "$exit_code" + +exit $exit_code