Testing Wercker plugin with bats

83899fe2d00874176098a3bb71b043be?s=47 TAKAHASHI Kazunari
November 29, 2014
400

Testing Wercker plugin with bats

83899fe2d00874176098a3bb71b043be?s=128

TAKAHASHI Kazunari

November 29, 2014
Tweet

Transcript

  1. 4.
  2. 16.
  3. 17.

    SVOTI source "$WERCKER_STEP_ROOT/functions.sh"! ! error_message=$(valid)! if [ $? = 1

    ]; then! warn "$error_message"! exit 0! fi! ! http_code=`curl \! --silent \! --data "format=html" \! --data-urlencode "source=$(message)" \! --output "$WERCKER_STEP_TEMP/result.txt" \! --write-out "%{http_code}" \! "https://idobata.io/hook/$WERCKER_IDOBATA_NOTIFY_TOKEN"`! ! if [ "$http_code" = "200" ]; then! success "Finished successfully!"! else! warn `cat $WERCKER_STEP_TEMP/result.txt`! warn "Finished unsuccessfully."! fi
  4. 18.

    GVODUJPOTTI ൈਮ valid() {! if [ ! -n "$WERCKER_IDOBATA_NOTIFY_TOKEN" ];

    then! echo 'Please specify token property.'! exit 1! fi! }! ! build_result() {! result=$(tr '[a-z]' '[A-Z]' <<<"$WERCKER_RESULT")! if [ "$WERCKER_RESULT" = "passed" ]; then! echo "<span class=\"label label-success\">$result</span>"! else! echo "<span class=\"label label-important\">$result</span>"! fi! }
  5. 20.

    UFTUUFTU@IFMQFSCBTI export PATH="$BATS_TEST_DIRNAME/..:$PATH"! ! if [ ! -n "$WERCKER" ];

    then! export WERCKER_GIT_DOMAIN="github.com"! export WERCKER_GIT_OWNER="1syo"! export WERCKER_GIT_REPOSITORY="wercker-step-idobata-notify"! export WERCKER_GIT_BRANCH="master"! export WERCKER_GIT_COMMIT="aff2f780d59346c0386d59db6aac62e02be2005a"! export WERCKER_STARTED_BY="TAKAHASHI Kazunari"! export WERCKER_APPLICATION_ID="5253e1053673929130009361"! export WERCKER_APPLICATION_NAME="wercker-step-idobata-notify"! export WERCKER_APPLICATION_OWNER_NAME="1syo"! export WERCKER_APPLICATION_URL=\! ! ! ! ”https://app.wercker.com/#applications/5253e1053673929130009361"! export WERCKER_STEP_ROOT="."! export WERCKER_STEP_TEMP=$BATS_TMPDIR! fi
  6. 21.

    UFTUUFTU@IFMQFSCBTI success() {! echo $1! }! ! fail() {! echo

    $1! exit 1! }! ! warn() {! echo $1! }! ! export -f success! export -f fail! export -f warn
  7. 23.

    UFTUUFTU@IFMQFSCBTI stub() {! local cmd=$1! local ret=$2! eval "$(echo -e

    "${cmd}() {\n echo $ret\n}")"! export -f ${cmd}! }! ! unstub() {! local cmd=$1! unset -f "$cmd"! }
  8. 26.

    UFTUSVOCBUT #!/usr/bin/env bats! load test_helper! ! @test "Token undefined" {!

    WERCKER_IDOBATA_NOTIFY_TOKEN="" \! WERCKER_STEP_ROOT="." \! run run.sh! ! [ "$status" = "0" ]! [ "$output" = "Please specify token property." ]! }
  9. 28.

    UFTUSVOCBUT @test "Send a message" {! stub curl 200! !

    WERCKER_IDOBATA_NOTIFY_TOKEN=token \! WERCKER_BUILD_URL="http://example.com/build/1" \! WERCKER_BUILD_ID=1 \! WERCKER_RESULT="passed" \! WERCKER_STEP_ROOT="." \! CI=true \! run run.sh! ! [ "$status" = "0" ]! [ "$output" = "Finished successfully!" ]! unstub curl! }
  10. 29.

    UFTUSVOCBUT @test "Can not send a message" {! echo "Error"

    > "$WERCKER_STEP_TEMP/result.txt"! stub curl 500! ! WERCKER_IDOBATA_NOTIFY_TOKEN=token \! WERCKER_RESULT="passed" \! WERCKER_STEP_ROOT="." \! CI=true \! run run.sh! ! [ "$status" = "0" ]! [ "${lines[0]}" = "Error" ]! [ "${lines[1]}" = "Finished unsuccessfully." ]! unstub curl! ! rm "$WERCKER_STEP_TEMP/result.txt"! }