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

Testing Wercker plugin with bats

TAKAHASHI Kazunari
November 29, 2014
430

Testing Wercker plugin with bats

TAKAHASHI Kazunari

November 29, 2014
Tweet

Transcript

  1. !TZP 5PLZV3VCZ,BJHJ 5FTUJOH 8FSDLFSQMVHJO XJUICBUT

  2. ͓࿳ͼ

  3. ͓஌Βͤ  ౦ژ3VCZ๨೥ձ  4BU  ๺ւಓ্໺޿খ࿏ళ  ߴڮձ௕ͱ೥Λ࿑͓͏

  4. None
  5. 8FSDLFSQMVHJOʹ CBUTͰ ςετΛॻ͘

  6. 8FSLFSͱ͸  5IF0QFOEFMJWFSZQMBUGPSN  ϓϥΠϕʔτϦϙδτϦͷ$*͕ແྉ  QMVHJOΛ3VCZ΍#BTIͰ࡞੒  νϟοτʹ$*ͷ݁ՌΛ௨஌

  7. CBUTͱ͸ʁ  #BTI"VUPNBUFE5FTUJOH4ZTUFN  SCFOW SVCZCVJMEͰར༻

  8. γΣϧεΫϦϓτͷ ςετͷ࿩Ͱ͢

  9. JEPCBUBOPUJGZ

  10. JEPCBUBOPUJGZ  ೥݄WϦϦʔε  JEPCBUBʹ$*ͷ݁ՌΛ௨஌  CVJMEEFQMPZʹରԠ  ͍͢͝؆୯ͳ4IFMM4DSJQU

  11. 1MVHJOͷલఏ৚݅  XFSDLFSͷ؀ڥม਺ ݸ͘Β͍   XFSDLFSͷGVODUJPO  QMVHJOͷ؀ڥม਺

  12. 1MVHJOͷ΍ͬͯΔ͜ͱ  ඞਢͷ؀ڥม਺ΛνΣοΫ  50,&/  ؀ڥม਺͔Β৘ใऩूͯ͠จݴ࡞੒  CVJMEͷ੒൱ 

    DVSMͰ1045
  13. 1MVHJOΛςετ͢Δૂ͍  લఏ৚݅Λίʔυʹ͍ͨ͠  લఏ৚݅Λίʔυʹ͍ͨ͠  લఏ৚݅Λίʔυʹ͍ͨ͠  લఏ৚݅Λίʔυʹ͍ͨ͠ 

    ύϥϝʔλΛม͑ͯݕূ͍ͨ͠
  14. ςετ͕ඞཁͳΒ3VCZ࢖͑͹͍ ͍͡ΌΜͱࢥ͏͚ΕͲ΋ɺϓϩμ Ϋγϣϯίʔυ͸௒γϯϓϧͳ༁ ͩ͠ɺલఏ৚݅ΛίʔυʹͰ͖Ε ͹͍͍ͷ͔ͩΒɺ4IFMM4DSJQU Ͱ΋ߦ͚ΔΜ͡Όͳ͍ߟ͑ͨ

  15. ײ૝  લఏ৚݅ΛίʔυʹͰ͖ͨ  ׳ΕΔ·Ͱ͕͔͔࣌ؒͬͨ  TUVC໰୊͕ະղܾ  4IFMM4DSJQUྗऑ͍ 

    ݱঢ়ͩͱ3VCZͰ΍ͬͨ΄͏ָ͕
  16. SVOTI

  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
  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! }
  19. UFTUUFTU@IFMQFSCBTI

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

    $1! exit 1! }! ! warn() {! echo $1! }! ! export -f success! export -f fail! export -f warn
  22. %FWDFOUFS  &OWJSPONFOU7BSJBCMFT  8SJUJOHPVUQVU  1SFEFpOE7BSJBCMFT

  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"! }
  24. TUVC  ୈҰҾ਺ʹTUVC͍ͨ͠ίϚϯυ  ୈೋҾ਺ʹίϚϯυͷ࣮ߦ݁Ռ TUVCDVSM

  25. UFTUSVOCBUT

  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." ]! }
  27. ςετͷߏ੒  ධՁ͍ͨ͠؀ڥม਺͸FYQPSU͠ͳ͍  TUBUVTʹऴྃίʔυ  PVUQVUʹඪ४ग़ྗ͔ඪ४Τϥʔ

  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! }
  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"! }
  30. CBUTUFTU 㾎5PLFOVOEFpOFE 㾎/PUJGZTLJQ 㾎#VJME1BTTFE 㾎%FQMPZQBTTFE 㾎#VJMEGBJMVSF 㾎%FQMPZGBJMVSF 㾎5PLFOVOEFpOFE 㾎4FOEBNFTTBHF 㾎$BOOPUTFOEB

    NFTTBHF ! UFTUT GBJMVSFT
  31. ײ૝  લఏ৚݅ΛίʔυʹͰ͖ͨ  ׳ΕΔ·Ͱ͕͔͔࣌ؒͬͨ  4IFMM4DSJQUྗऑ͍  TUVC໰୊͕ະղܾ 

    ݱঢ়ͩͱ3VCZͰ΍ͬͨ΄͏ָ͕
  32. -FU`TXSJUJOH XSFDLFSQMVHJO XJUICBUT