Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing Wercker plugin with bats

TAKAHASHI Kazunari
November 29, 2014
480

Testing Wercker plugin with bats

TAKAHASHI Kazunari

November 29, 2014
Tweet

Transcript

  1. 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
  2. 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! }
  3. 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
  4. UFTUUFTU@IFMQFSCBTI success() {! echo $1! }! ! fail() {! echo

    $1! exit 1! }! ! warn() {! echo $1! }! ! export -f success! export -f fail! export -f warn
  5. 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"! }
  6. 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." ]! }
  7. 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! }
  8. 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"! }