From 557ce93fab5adc28f6218ec1e45d6245617a4c4c Mon Sep 17 00:00:00 2001 From: Tim Byrne Date: Wed, 5 Jul 2017 16:32:10 -0500 Subject: [PATCH] Add tests for hooks --- test/117_accept_hooks.bats | 181 +++++++++++++++++++++++++++++++++++++ test/common.bash | 1 + 2 files changed, 182 insertions(+) create mode 100644 test/117_accept_hooks.bats diff --git a/test/117_accept_hooks.bats b/test/117_accept_hooks.bats new file mode 100644 index 0000000..5e8f348 --- /dev/null +++ b/test/117_accept_hooks.bats @@ -0,0 +1,181 @@ +load common +load_fixtures +status=;output=; #; populated by bats run() + +version_regex="yadm [[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+" + +setup() { + destroy_tmp + build_repo + mkdir -p "$T_DIR_HOOKS" +} + +function create_hook() { + hook_name="$1" + hook_exit="$2" + hook_file="$T_DIR_HOOKS/$hook_name" + { + echo "#!/bin/sh" + echo "echo ran $hook_name" + echo "env" + echo "exit $hook_exit" + } > "$hook_file" + chmod a+x "$hook_file" +} + +@test "Hooks (no hook)" { + echo " + When no hook is present + do no not run the hook + run command + Exit with 0 + " + + #; run yadm with no command + run "${T_YADM_Y[@]}" version + + [ $status -eq 0 ] + [[ "$output" =~ $version_regex ]] +} + +@test "Hooks (successful pre hook)" { + echo " + When hook is present + run hook + run command + Exit with 0 + " + + create_hook "pre_version" "0" + + #; run yadm with no command + run "${T_YADM_Y[@]}" version + + [ $status -eq 0 ] + [[ "$output" =~ ran\ pre_version ]] + [[ "$output" =~ $version_regex ]] +} + +@test "Hooks (unsuccessful pre hook)" { + echo " + When hook is present + run hook + report hook failure + do no not run command + Exit with 13 + " + + create_hook "pre_version" "13" + + #; run yadm with no command + run "${T_YADM_Y[@]}" version + + [ $status -eq 13 ] + [[ "$output" =~ ran\ pre_version ]] + [[ "$output" =~ pre_version\ was\ not\ successful ]] + [[ ! "$output" =~ $version_regex ]] +} + +@test "Hooks (successful post hook)" { + echo " + When hook is present + run command + run hook + Exit with 0 + " + + create_hook "post_version" "0" + + #; run yadm with no command + run "${T_YADM_Y[@]}" version + + [ $status -eq 0 ] + [[ "$output" =~ $version_regex ]] + [[ "$output" =~ ran\ post_version ]] +} + +@test "Hooks (unsuccessful post hook)" { + echo " + When hook is present + run command + run hook + Exit with 0 + " + + create_hook "post_version" "13" + + #; run yadm with no command + run "${T_YADM_Y[@]}" version + + [ $status -eq 0 ] + [[ "$output" =~ $version_regex ]] + [[ "$output" =~ ran\ post_version ]] +} + +@test "Hooks (successful pre hook + post hook)" { + echo " + When hook is present + run hook + run command + run hook + Exit with 0 + " + + create_hook "pre_version" "0" + create_hook "post_version" "0" + + #; run yadm with no command + run "${T_YADM_Y[@]}" version + + [ $status -eq 0 ] + [[ "$output" =~ ran\ pre_version ]] + [[ "$output" =~ $version_regex ]] + [[ "$output" =~ ran\ post_version ]] +} + +@test "Hooks (unsuccessful pre hook + post hook)" { + echo " + When hook is present + run hook + report hook failure + do no not run command + do no not run post hook + Exit with 13 + " + + create_hook "pre_version" "13" + create_hook "post_version" "0" + + #; run yadm with no command + run "${T_YADM_Y[@]}" version + + [ $status -eq 13 ] + [[ "$output" =~ ran\ pre_version ]] + [[ "$output" =~ pre_version\ was\ not\ successful ]] + [[ ! "$output" =~ $version_regex ]] + [[ ! "$output" =~ ran\ post_version ]] +} + +@test "Hooks (environment variables)" { + echo " + When hook is present + run command + run hook + hook should have access to environment variables + Exit with 0 + " + + create_hook "post_version" "0" + + #; run yadm with no command + run "${T_YADM_Y[@]}" version extra_args + + [ $status -eq 0 ] + [[ "$output" =~ $version_regex ]] + [[ "$output" =~ ran\ post_version ]] + [[ "$output" =~ YADM_HOOK_COMMAND=version ]] + [[ "$output" =~ YADM_HOOK_EXIT=0 ]] + [[ "$output" =~ YADM_HOOK_FULL_COMMAND=version\ extra_args ]] + [[ "$output" =~ YADM_HOOK_REPO=${T_DIR_REPO} ]] + [[ "$output" =~ YADM_HOOK_WORK=${T_DIR_WORK} ]] +} diff --git a/test/common.bash b/test/common.bash index 46a38b1..32ba8e1 100644 --- a/test/common.bash +++ b/test/common.bash @@ -13,6 +13,7 @@ function load_fixtures() { export T_DIR_YADM="$T_TMP/.yadm" export T_DIR_WORK="$T_TMP/yadm-work" export T_DIR_REPO="$T_DIR_YADM/repo.git" + export T_DIR_HOOKS="$T_DIR_YADM/hooks" export T_YADM_CONFIG="$T_DIR_YADM/config" export T_YADM_ENCRYPT="$T_DIR_YADM/encrypt" export T_YADM_ARCHIVE="$T_DIR_YADM/files.gpg"