diff --git a/doc/7_develop.md b/doc/7_develop.md index 0a2000e..8e985c2 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -34,9 +34,17 @@ In addition you can run ```dev/hooks/pre-commit.sh``` every time you want to she ## bashbot tests Starting with version 0.70 bashbot has a test suite. To start testsuite run ```test/ALL-tests.sh```. ALL-tests.sh will only return 'SUCCESS' if all tests pass. +### enabling / disabling tests + +All tests are placed in the directory ```test```. To disable a test remove the x flag from the '*-test.sh' test script, to (re)enable +a test make the script executable again. + + ### creating new tests -To create a new test create a new bash script named ```p-name-test.sh```, where p is pass 'a-z' and name the name of your test. -All tests with the same pass are performed together. +Each test consists of a script script named like ```p-name-test.sh``` *(where p is test pass 'a-z' and name the name of your test)* and +an optional dir ```p-name-test/``` *(script name minus '.sh')* for additional files. + +The file ```ALL-tests.inc.sh``` must be included from all tests, do not forget the shellcheck source directive to statisfy shellcheck. Tests with no dependency to other tests will run in pass 'a', tests which need an initialized bahsbot environment must run in pass 'd' or later. If '$1' is present the script is started from 'ALL-tests.sh' and a temporary test environment is setup in directory '$1'. @@ -44,24 +52,23 @@ The temporary test environment is created when 'ALL-tests.sh' starts and deleted Example test ```bash - #!/usr/bin/env bash -# file: z-bashbot-test.sh +# file: b-example-test.sh -# this test should always pass :-) -echo "Running test if bashbot.sh exists" -echo "................................." +# include common functions and definitions +# shellcheck source=test/ALL-tests.inc.sh +source "./ALL-tests.inc.sh" if [ -f "bashbot.sh" ]; then - echo "bashbot.sh OK!" + echo "${SUCCESS} bashbot.sh exist!" exit 0 else - echo "bashbot.sh missing!" + echo "${NOSUCCESS} bashbot.sh missing!" exit 1 fi ``` #### [Prev Function Reference](6_function.md) -#### $$VERSION$$ 0.70-dev-18-g7512681 +#### $$VERSION$$ 0.70-dev-20-g753f1b3 diff --git a/test/ALL-tests.inc.sh b/test/ALL-tests.inc.sh new file mode 100755 index 0000000..4c6f0c8 --- /dev/null +++ b/test/ALL-tests.inc.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +#### $$VERSION$$ 0.70-dev-20-g753f1b3 + +# common variables +export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME + TESTME="$(basename "$0")" + DIRME="$(pwd)" + TESTDIR="$1" + LOGFILE="${TESTDIR}/${TESTME}.log" + REFDIR="${TESTME%.sh}" + TESTNAME="${REFDIR//-/ }" + +# common filenames +export TOKENFILE ACLFILE COUNTFILE ADMINFILE + TOKENFILE="token" + ACLFILE="botacl" + COUNTFILE="count" + ADMINFILE="botadmin" + +# SUCCESS NOSUCCES +export SUCCESS NOSUCCESS +SUCCESS=" OK" +NOSUCCESS=" FAILED!" + +echo "Running ${TESTNAME#? } ..." +echo "............................" +[ "${TESTDIR}" = "" ] && echo "${NOSUCCESS} not called from testsuite, exit" && exit 1 + +# reset env for test +unset IFS; set -f +export TERM="" + diff --git a/test/ALL-tests.sh b/test/ALL-tests.sh index 2c06344..1a3b889 100755 --- a/test/ALL-tests.sh +++ b/test/ALL-tests.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ 0.70-dev-19-g3183419 +#### $$VERSION$$ 0.70-dev-20-g753f1b3 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/test/a-commit-test.sh b/test/a-commit-test.sh index 51cafcb..969be9f 100755 --- a/test/a-commit-test.sh +++ b/test/a-commit-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-19-g3183419 +#### $$VERSION$$ 0.70-dev-20-g753f1b3 ../dev/hooks/pre-commit.sh diff --git a/test/a-push-test.sh b/test/a-push-test.sh index 9a7b754..aba15fa 100755 --- a/test/a-push-test.sh +++ b/test/a-push-test.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-19-g3183419 +#### $$VERSION$$ 0.70-dev-20-g753f1b3 ../dev/hooks/pre-push.sh diff --git a/test/b-example-test.sh b/test/b-example-test.sh new file mode 100644 index 0000000..042f1d4 --- /dev/null +++ b/test/b-example-test.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# file: b-example-test.sh + +# include common functions and definitions +# shellcheck source=test/ALL-tests.inc.sh +source "./ALL-tests.inc.sh" + +if [ -f "${TESTDIR}/bashbot.sh" ]; then + echo "${SUCCESS} bashbot.sh exist!" + exit 0 +else + echo "${NOSUCCESS} bashbot.sh missing!" + exit 1 +fi + diff --git a/test/c-init-test.sh b/test/c-init-test.sh index c586dc4..a07c00a 100755 --- a/test/c-init-test.sh +++ b/test/c-init-test.sh @@ -1,36 +1,22 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-19-g3183419 +#### $$VERSION$$ 0.70-dev-20-g753f1b3 -TESTME="$(basename "$0")" -DIRME="$(pwd)" -TESTDIR="$1" +# include common functions and definitions +# shellcheck source=test/ALL-tests.inc.sh +source "./ALL-tests.inc.sh" -LOGFILE="${TESTDIR}/${TESTME}.log" -REFDIR="${TESTME%.sh}" - -TOKENFILE="token" TESTTOKEN="bashbottestscript" -TESTFILES="${TOKENFILE} botacl count botadmin" +TESTFILES="${TOKENFILE} ${ACLFILE} ${COUNTFILE} ${ADMINFILE}" set -e -# let's fake failing test for now -echo "Running bashbot init" -echo "............................" -# change to test env -[ "${TESTDIR}" = "" ] && echo "not called from testsuite, exit" && exit - - -unset IFS; set -f - # run bashbot first time with init -export TERM="" "${TESTDIR}/bashbot.sh" init >"${LOGFILE}" <>"${LOGFILE}" - if ! diff -q "${TESTDIR}/${file}" "${REFDIR}/${file}" >>"${LOGFILE}"; then echo " ERROR: Fail diff ${file}!"; FAIL="1"; fi + if ! diff -q "${TESTDIR}/${file}" "${REFDIR}/${file}" >>"${LOGFILE}"; then echo "${NOSUCCESS} Fail diff ${file}!"; FAIL="1"; fi done [ "${FAIL}" != "0" ] && exit "${FAIL}" -echo "OK" +echo "${SUCCESS}" echo "Test Sourcing of bashbot.sh ..." trap exit 1 EXIT @@ -52,9 +38,9 @@ cd "${TESTDIR}" || exit source "${TESTDIR}/bashbot.sh" source trap '' EXIT cd "${DIRME}" || exit 1 +echo "${SUCCESS}" echo "Test bashbot.sh count" cp "${REFDIR}/count.test" "${TESTDIR}/count" "${TESTDIR}/bashbot.sh" count -exit 1 diff --git a/test/c-init-test/count b/test/c-init-test/count new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/test/c-init-test/count @@ -0,0 +1 @@ + diff --git a/test/c-init-test/token b/test/c-init-test/token new file mode 100644 index 0000000..60557b1 --- /dev/null +++ b/test/c-init-test/token @@ -0,0 +1 @@ +bashbottestscript diff --git a/test/d-process_message-test.sh b/test/d-process_message-test.sh new file mode 100755 index 0000000..fadd929 --- /dev/null +++ b/test/d-process_message-test.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +#### $$VERSION$$ 0.70-dev-20-g753f1b3 + +# include common functions and definitions +# shellcheck source=test/ALL-tests.inc.sh +source "./ALL-tests.inc.sh" + +set -e + +# source bashbot.sh functionw +cd "${TESTDIR}" || exit 1 +# shellcheck source=./bashbot.sh +source "${TESTDIR}/bashbot.sh" source +cd "${DIRME}" || exit 1 + +echo "${SUCCESS}" diff --git a/test/d-process_message-test/d-process-message-test.input b/test/d-process_message-test/d-process-message-test.input new file mode 100644 index 0000000..38bbe46 --- /dev/null +++ b/test/d-process_message-test/d-process-message-test.input @@ -0,0 +1,85 @@ +["ok"] true +["result",0,"update_id"] 146860800 +["result",0,"message","message_id"] 6541 +["result",0,"message","from","id"] 123456789 +["result",0,"message","from","is_bot"] false +["result",0,"message","from","first_name"] "Kay" +["result",0,"message","from","last_name"] "M" +["result",0,"message","from","username"] "Gnadelwartz" +["result",0,"message","from","language_code"] "de" +["result",0,"message","chat","id"] 123456789 +["result",0,"message","chat","first_name"] "Test" +["result",0,"message","chat","last_name"] "Bot" +["result",0,"message","chat","username"] "BotTest" +["result",0,"message","chat","type"] "private" +["result",0,"message","date"] 1555822879 +["result",0,"message","reply_to_message","message_id"] 6542 +["result",0,"message","reply_to_message","from","id"] 987654321 +["result",0,"message","reply_to_message","from","is_bot"] true +["result",0,"message","reply_to_message","from","first_name"] "dealzbot" +["result",0,"message","reply_to_message","from","username"] "Deal_O_Mat_bot" +["result",0,"message","reply_to_message","from"] {"id":987654321,"is_bot":true,"first_name":"dealzbot","username":"Deal_O_Mat_bot"} +["result",0,"message","reply_to_message","chat","id"] 123456789 +["result",0,"message","reply_to_message","chat","first_name"] "Kay" +["result",0,"message","reply_to_message","chat","last_name"] "M" +["result",0,"message","reply_to_message","chat","username"] "Gnadelwartz" +["result",0,"message","reply_to_message","chat","type"] "private" +["result",0,"message","reply_to_message","date"] 1555822747 +["result",0,"message","reply_to_message","text"] "Ich bin der Deal-O-Mat Bot. F\u00fcr eine Liste der Befehle sende /help" +["result",0,"message","reply_to_message","entities",0,"offset"] 12 +["result",0,"message","reply_to_message","entities",0,"length"] 14 +["result",0,"message","reply_to_message","entities",0,"type"] "bold" +["result",0,"message","reply_to_message","entities",0] {"offset":12,"length":14,"type":"bold"} +["result",0,"message","reply_to_message","entities",1,"offset"] 61 +["result",0,"message","reply_to_message","entities",1,"length"] 5 +["result",0,"message","reply_to_message","entities",1,"type"] "bot_command" +["result",0,"message","text"] "\ud83d\ude02\ud83d\ude1d\ud83d\udc4c\u263a\u2764\ud83d\ude15\ud83d\ude08#\u20e3\ud83c\udf0f\ud83c\udf89\ud83d\ude4a\ud83d\ude49\u2615\ud83d\ude80\u2708\ud83d\ude82\ud83d\udcaf\u2714\u303d\ud83d\udd1a" +["result",1,"message","forward_from","id"] 123456789 +["result",1,"message","forward_from","is_bot"] false +["result",1,"message","forward_from","first_name"] "Kay" +["result",1,"message","forward_from","last_name"] "M" +["result",1,"message","forward_from","username"] "Gnadelwartz" +["result",1,"message","forward_from","language_code"] "de" +["result",1,"message","forward_date"] 1555822879 +["result",1,"message","text"] "he, dies ist eine Antwort \u00e4\u00e2 \u00f6\u00f4 \u00fc\u00f9 \u20ac\u00a3\u00a5\u03c0\u00a9\u00ae\u2122" +["result",0,"message","photo",0,"file_id"] "AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa_gDAAEC" +["result",0,"message","photo",0,"file_size"] 1468 +["result",0,"message","photo",0,"width"] 67 +["result",0,"message","photo",0,"height"] 90 +["result",0,"message","photo",0] {"file_id":"AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa_gDAAEC","file_size":1468,"width":67,"height":90} +["result",0,"message","photo",1,"file_id"] "AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOFli9a5TF3rbPgDAAEC" +["result",0,"message","photo",1,"file_size"] 25125 +["result",0,"message","photo",1,"width"] 240 +["result",0,"message","photo",1,"height"] 320 +["result",0,"message","photo",1] {"file_id":"AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOFli9a5TF3rbPgDAAEC","file_size":25125,"width":240,"height":320} +["result",0,"message","photo",2,"file_id"] "AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABCVM2ZSdqfMZbfgDAAEC" +["result",0,"message","photo",2,"file_size"] 127851 +["result",0,"message","photo",2,"width"] 600 +["result",0,"message","photo",2,"height"] 800 +["result",0,"message","photo",2] {"file_id":"AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABCVM2ZSdqfMZbfgDAAEC","file_size":127851,"width":600,"height":800} +["result",0,"message","photo",3,"file_id"] "AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABGs7gD6jTEJuafgDAAEC" +["result",0,"message","photo",3,"file_size"] 245679 +["result",0,"message","photo",3,"width"] 1200 +["result",0,"message","photo",3,"height"] 1600 +["result",0,"message","photo",3] {"file_id":"AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABGs7gD6jTEJuafgDAAEC","file_size":245679,"width":1200,"height":1600} +["result",0,"message","photo",4,"file_id"] "AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABDsKqM0vfngJavgDAAEC" +["result",0,"message","photo",4,"file_size"] 272842 +["result",0,"message","photo",4,"width"] 960 +["result",0,"message","photo",4,"height"] 1280 +["result",0,"message","photo",4] {"file_id":"AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABDsKqM0vfngJavgDAAEC","file_size":272842,"width":960,"height":1280} +["result",0,"message","location","latitude"] 49.631824 +["result",0,"message","location","longitude"] 8.377072 +["result",0,"message","location"] {"latitude":49.631824,"longitude":8.377072} +["result",0,"message","venue","location","latitude"] 49.631824 +["result",0,"message","venue","location","longitude"] 8.377072 +["result",0,"message","venue","location"] {"latitude":49.631824,"longitude":8.377072} +["result",0,"message","venue","title"] "Kolb's Biergarten" +["result",0,"message","venue","address"] "Am Rhein 1" +["result",0,"message","venue","foursquare_id"] "4c4321afce54e21eee980d1a" +["result",0,"message","contact","phone_number"] "222222" +["result",0,"message","contact","first_name"] "ADAC Pannenhilfe" +["result",0,"message","contact","vcard"] "BEGIN:VCARD\nVERSION:2.1\nN:Pannenhilfe;ADAC;;;\nFN:ADAC Pannenhilfe\nTEL;CELL;PREF:+49179222222\nTEL;X-Mobil:222222\nEND:VCARD" +["result",0,"message","voice","duration"] 2 +["result",0,"message","voice","mime_type"] "audio/ogg" +["result",0,"message","voice","file_id"] "AwADAgADOAQAAqd24Emnm_VGmmVEuAI" +["result",0,"message","voice","file_size"] 4262