From 3f1a8364ae1be1a21eafd0fa693ee494bcb361f5 Mon Sep 17 00:00:00 2001 From: Tim Byrne Date: Sat, 21 Jan 2017 17:04:48 -0600 Subject: [PATCH] Add tests for bootstrap command (#42) --- test/001_unit_paths.bats | 41 ++++++++++++-- test/005_unit_bootstrap_available.bats | 66 ++++++++++++++++++++++ test/112_accept_bootstrap.bats | 78 ++++++++++++++++++++++++++ test/common.bash | 2 + 4 files changed, 181 insertions(+), 6 deletions(-) create mode 100644 test/005_unit_bootstrap_available.bats create mode 100644 test/112_accept_bootstrap.bats diff --git a/test/001_unit_paths.bats b/test/001_unit_paths.bats index 48c99f6..bf479e8 100644 --- a/test/001_unit_paths.bats +++ b/test/001_unit_paths.bats @@ -16,12 +16,13 @@ function configuration_test() { echo -e "STATUS:$status\nOUTPUT:$output" echo "CONFIGURED PATHS:" - echo " YADM_DIR:$YADM_DIR" - echo " YADM_REPO:$YADM_REPO" - echo " YADM_CONFIG:$YADM_CONFIG" - echo "YADM_ENCRYPT:$YADM_ENCRYPT" - echo "YADM_ARCHIVE:$YADM_ARCHIVE" - echo " GIT_DIR:$GIT_DIR" + echo " YADM_DIR:$YADM_DIR" + echo " YADM_REPO:$YADM_REPO" + echo " YADM_CONFIG:$YADM_CONFIG" + echo " YADM_ENCRYPT:$YADM_ENCRYPT" + echo " YADM_ARCHIVE:$YADM_ARCHIVE" + echo "YADM_BOOTSTRAP:$YADM_BOOTSTRAP" + echo " GIT_DIR:$GIT_DIR" } @test "Default paths" { @@ -32,6 +33,7 @@ function configuration_test() { YADM_CONFIG=$DEFAULT_YADM_DIR/$DEFAULT_CONFIG YADM_ENCRYPT=$DEFAULT_YADM_DIR/$DEFAULT_ENCRYPT YADM_ARCHIVE=$DEFAULT_YADM_DIR/$DEFAULT_ARCHIVE + YADM_BOOTSTRAP=$DEFAULT_YADM_DIR/$DEFAULT_BOOTSTRAP GIT_DIR=$DEFAULT_YADM_DIR/$DEFAULT_REPO " @@ -43,6 +45,7 @@ function configuration_test() { [ "$YADM_CONFIG" = "$DEFAULT_YADM_DIR/$DEFAULT_CONFIG" ] [ "$YADM_ENCRYPT" = "$DEFAULT_YADM_DIR/$DEFAULT_ENCRYPT" ] [ "$YADM_ARCHIVE" = "$DEFAULT_YADM_DIR/$DEFAULT_ARCHIVE" ] + [ "$YADM_BOOTSTRAP" = "$DEFAULT_YADM_DIR/$DEFAULT_BOOTSTRAP" ] [ "$GIT_DIR" = "$DEFAULT_YADM_DIR/$DEFAULT_REPO" ] } @@ -61,6 +64,7 @@ function configuration_test() { [ "$YADM_CONFIG" = "$T_DIR_YADM/$DEFAULT_CONFIG" ] [ "$YADM_ENCRYPT" = "$T_DIR_YADM/$DEFAULT_ENCRYPT" ] [ "$YADM_ARCHIVE" = "$T_DIR_YADM/$DEFAULT_ARCHIVE" ] + [ "$YADM_BOOTSTRAP" = "$T_DIR_YADM/$DEFAULT_BOOTSTRAP" ] [ "$GIT_DIR" = "$T_DIR_YADM/$DEFAULT_REPO" ] } @@ -178,3 +182,28 @@ function configuration_test() { [ "$status" == 1 ] [[ "$output" =~ must\ specify\ a\ fully\ qualified ]] } + +@test "Override YADM_BOOTSTRAP" { + echo " + Override YADM_BOOTSTRAP using --yadm-bootstrap /custom/bootstrap + YADM_BOOTSTRAP should become /custom/bootstrap + " + + TEST_ARGS=(--yadm-bootstrap /custom/bootstrap) + configuration_test "${TEST_ARGS[@]}" + + [ "$YADM_BOOTSTRAP" = "/custom/bootstrap" ] +} + +@test "Override YADM_BOOTSTRAP (not fully qualified)" { + echo " + Override YADM_BOOTSTRAP using --yadm-bootstrap relative/bootstrap + yadm should fail, and report the error + " + + TEST_ARGS=(--yadm-bootstrap relative/bootstrap) + configuration_test "${TEST_ARGS[@]}" + + [ "$status" == 1 ] + [[ "$output" =~ must\ specify\ a\ fully\ qualified ]] +} diff --git a/test/005_unit_bootstrap_available.bats b/test/005_unit_bootstrap_available.bats new file mode 100644 index 0000000..fc4cc0d --- /dev/null +++ b/test/005_unit_bootstrap_available.bats @@ -0,0 +1,66 @@ +load common +T_YADM_BOOTSTRAP=; # populated by load_fixtures +load_fixtures +status=; # populated by bats run() + +setup() { + destroy_tmp + make_parents "$T_YADM_BOOTSTRAP" +} + +teardown() { + destroy_tmp +} + +function available_test() { + # shellcheck source=/dev/null + YADM_TEST=1 source "$T_YADM" + # shellcheck disable=SC2034 + YADM_BOOTSTRAP="$T_YADM_BOOTSTRAP" + status=0 + { bootstrap_available; } || { + status=$? + true + } + + echo -e "STATUS:$status" + +} + +@test "Bootstrap missing" { + echo " + When bootstrap command is missing + return 1 + " + + available_test + [ "$status" == 1 ] + +} + +@test "Bootstrap not executable" { + echo " + When bootstrap command is not executable + return 1 + " + + touch "$T_YADM_BOOTSTRAP" + + available_test + [ "$status" == 1 ] + +} + +@test "Bootstrap executable" { + echo " + When bootstrap command is not executable + return 0 + " + + touch "$T_YADM_BOOTSTRAP" + chmod a+x "$T_YADM_BOOTSTRAP" + + available_test + [ "$status" == 0 ] + +} diff --git a/test/112_accept_bootstrap.bats b/test/112_accept_bootstrap.bats new file mode 100644 index 0000000..3f23df2 --- /dev/null +++ b/test/112_accept_bootstrap.bats @@ -0,0 +1,78 @@ +load common +load_fixtures +status=;output=; #; populated by bats run() + +setup() { + destroy_tmp + build_repo +} + +@test "Command 'bootstrap' (missing file)" { + echo " + When 'bootstrap' command is provided, + and the bootstrap file is missing + Report error + Exit with 1 + " + + #; run clone + run "${T_YADM_Y[@]}" bootstrap + echo "STATUS:$status" + echo "OUTPUT:$output" + + #; validate status and output + [[ "$output" =~ Cannot\ execute\ bootstrap ]] + [ "$status" -eq 1 ] + +} + +@test "Command 'bootstrap' (not executable)" { + echo " + When 'bootstrap' command is provided, + and the bootstrap file is present + but is not executable + Report error + Exit with 1 + " + + touch "$T_YADM_BOOTSTRAP" + + #; run clone + run "${T_YADM_Y[@]}" bootstrap + echo "STATUS:$status" + echo "OUTPUT:$output" + + #; validate status and output + [[ "$output" =~ is\ not\ an\ executable\ program ]] + [ "$status" -eq 1 ] + +} + +@test "Command 'bootstrap' (bootstrap run)" { + echo " + When 'bootstrap' command is provided, + and the bootstrap file is present + and is executable + Announce the execution + Execute bootstrap + Exit with the exit code of bootstrap + " + + { + echo "#!/bin/bash" + echo "echo Bootstrap successful" + echo "exit 123" + } > "$T_YADM_BOOTSTRAP" + chmod a+x "$T_YADM_BOOTSTRAP" + + #; run clone + run "${T_YADM_Y[@]}" bootstrap + echo "STATUS:$status" + echo "OUTPUT:$output" + + #; validate status and output + [[ "$output" =~ Executing\ $T_YADM_BOOTSTRAP ]] + [[ "$output" =~ Bootstrap\ successful ]] + [ "$status" -eq 123 ] + +} diff --git a/test/common.bash b/test/common.bash index cad0887..54e08da 100644 --- a/test/common.bash +++ b/test/common.bash @@ -6,6 +6,7 @@ function load_fixtures() { export DEFAULT_CONFIG="config" export DEFAULT_ENCRYPT="encrypt" export DEFAULT_ARCHIVE="files.gpg" + export DEFAULT_BOOTSTRAP="bootstrap" export T_YADM="$PWD/yadm" export T_TMP="$BATS_TMPDIR/ytmp" @@ -15,6 +16,7 @@ function load_fixtures() { 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" + export T_YADM_BOOTSTRAP="$T_DIR_YADM/bootstrap" export T_YADM_Y T_YADM_Y=( "$T_YADM" -Y "$T_DIR_YADM" )