diff --git a/JSON.sh b/JSON.sh deleted file mode 160000 index 022ec33..0000000 --- a/JSON.sh +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 022ec337c3225d5857856fd924cef0ab20443088 diff --git a/README.md b/README.md index 0ded09a..20127e9 100644 --- a/README.md +++ b/README.md @@ -102,4 +102,4 @@ Bashbot is not more (in)secure as any other Bot written in any other language, w If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.61-0-g3b17bc2 +#### $$VERSION$$ 0.70-dev-11-g41b8e69 diff --git a/README.txt b/README.txt index 5c41795..6482eff 100644 --- a/README.txt +++ b/README.txt @@ -166,5 +166,5 @@ That's it! If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -latexmath:[\[VERSION\]] v0.61-0-g3b17bc2 -++++++++++++++++++++++++++++++++++++++++ +latexmath:[\[VERSION\]] 0.70-dev-11-g41b8e69 +++++++++++++++++++++++++++++++++++++++++++++ diff --git a/bashbot.rc b/bashbot.rc index 10932cc..f897dc1 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.61-0-g3b17bc2 +#### $$VERSION$$ 0.70-dev-11-g41b8e69 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index 02b72c7..57e1bf6 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -10,7 +10,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.61-0-g3b17bc2 +#### $$VERSION$$ 0.70-dev-11-g41b8e69 # # Exit Codes: # - 0 sucess (hopefully) @@ -44,9 +44,9 @@ if [ ! -w "." ]; then ls -ld . fi -TOKEN="./token" -if [ ! -f "${TOKEN}" ]; then - if [ "${CLEAR}" = "" ]; then +TOKENFILE="./token" +if [ ! -f "${TOKENFILE}" ]; then + if [ "${CLEAR}" = "" ] && [ "$1" != "init" ]; then echo "Running headless, run ${SCRIPT} init first!" exit 2 else @@ -54,13 +54,13 @@ if [ ! -f "${TOKEN}" ]; then echo -e "${RED}TOKEN MISSING.${NC}" echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}" read -r token - echo "${token}" > "${TOKEN}" + echo "${token}" > "${TOKENFILE}" fi fi if [ ! -f "JSON.sh/JSON.sh" ]; then - echo "You did not clone recursively! Downloading JSON.sh..." - git clone http://github.com/dominictarr/JSON.sh + echo "Seems to be first run, Downloading JSON.sh..." + git clone https://github.com/dominictarr/JSON.sh/ 2>&1 echo "JSON.sh has been downloaded. Proceeding." fi @@ -603,8 +603,12 @@ getBotName() { ME="$(getBotName)" if [ "$ME" = "" ]; then + if [ "$(cat "${TOKENFILE}")" = "bashbottestscript" ]; then + ME="bashbottestscript" + else echo -e "${RED}ERROR: Can't connect to Telegram Bot! May be your TOKEN is invalid ...${NC}" exit 1 + fi fi # use phyton JSON to decode JSON UFT-8, provide bash implementaion as fallback @@ -687,14 +691,13 @@ case "$1" in echo -e "${RED}User \"$TOUSER\" not found!${NC}" exit 3 else - echo "Adjusting user in bashbot.rc ..." + echo "Adjusting user \"${TOUSER}\" files and permissions ..." sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' bashbot.rc - echo "Adjusting Owner and Permissions ..." chown -R "$TOUSER" . ./* chmod 711 . chmod -R a-w ./* chmod -R u+w "${COUNT}" "${TMPDIR}" "${BOTADMIN}" ./*.log 2>/dev/null - chmod -R o-r,o-w "${COUNT}" "${TMPDIR}" "${TOKEN}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null + chmod -R o-r,o-w "${COUNT}" "${TMPDIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null ls -la exit fi diff --git a/commands.sh b/commands.sh index 15ffe50..6d9f1a8 100755 --- a/commands.sh +++ b/commands.sh @@ -4,7 +4,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.61-0-g3b17bc2 +#### $$VERSION$$ 0.70-dev-11-g41b8e69 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/dev/hooks/pre-commit.sh b/dev/hooks/pre-commit.sh index dfc4a6a..f5b5621 100755 --- a/dev/hooks/pre-commit.sh +++ b/dev/hooks/pre-commit.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.61-2-gfd4dd8c +#### $$VERSION$$ 0.70-dev-11-g41b8e69 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -28,8 +28,10 @@ else fi # run shellcheck before commit -echo "read files from dev/shellcheck.files ..." -FILES=$(sed '/^#/d' <"dev/shellcheck.files") +set +f +FILES="$(find ./* -name '*.sh')" +set -f +FILES="${FILES} $(sed '/^#/d' <"dev/shellcheck.files")" if [ "$FILES" != "" ]; then # shellcheck disable=SC2086 shellcheck -x ${FILES} || exit 1 diff --git a/dev/hooks/pre-push.sh b/dev/hooks/pre-push.sh index 2f68e43..93587d4 100755 --- a/dev/hooks/pre-push.sh +++ b/dev/hooks/pre-push.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.61-2-gfd4dd8c +#### $$VERSION$$ 0.70-dev-11-g41b8e69 # 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/dev/install-hooks.sh b/dev/install-hooks.sh index 2cc2bc2..c83f945 100755 --- a/dev/install-hooks.sh +++ b/dev/install-hooks.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$$ v0.61-2-gfd4dd8c +#### $$VERSION$$ 0.70-dev-11-g41b8e69 # 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/dev/shellcheck.files b/dev/shellcheck.files index 70769c7..d6366c1 100644 --- a/dev/shellcheck.files +++ b/dev/shellcheck.files @@ -1,15 +1,3 @@ -# list of files to run shellscheck against -#### $$VERSION$$ v0.61-2-gfd4dd8c -# main files -bashbot.sh -commands.sh +# list of additional files to check from shellcheck +#### $$VERSION$$ 0.70-dev-11-g41b8e69 bashbot.rc -# example files -examples/calc.sh -examples/notify.sh -examples/question.sh -# dev files -dev/version.sh -dev/install-hooks.sh -dev/hooks/pre-commit.sh -dev/hooks/pre-push.sh diff --git a/dev/version.sh b/dev/version.sh index 1310b62..0aaeffb 100755 --- a/dev/version.sh +++ b/dev/version.sh @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.61-2-gfd4dd8c +#### $$VERSION$$ 0.70-dev-11-g41b8e69 # shellcheck disable=SC2016 # # Easy Versioning in git: @@ -45,7 +45,7 @@ unset IFS VERSION="$(git describe --tags --long)" echo "Update to version $VERSION ..." -FILES="* doc/* dev/* dev/*/*" +FILES="$(find ./*)" [ "$1" != "" ] && FILES="$*" for file in $FILES diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 3b9a6af..be910c1 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -61,5 +61,5 @@ group. This step is up to you actually. #### [Next Getting started](2_usage.md) -#### $$VERSION$$ v0.61-2-gfd4dd8c +#### $$VERSION$$ 0.70-dev-11-g41b8e69 diff --git a/doc/2_usage.md b/doc/2_usage.md index 3374fa8..678049e 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -159,5 +159,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.61-2-gfd4dd8c +#### $$VERSION$$ 0.70-dev-11-g41b8e69 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 2615edb..89dbff0 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -156,5 +156,5 @@ answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" #### [Prev Advanced Usage](3_advanced.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v0.61-2-gfd4dd8c +#### $$VERSION$$ 0.70-dev-11-g41b8e69 diff --git a/doc/4_expert.md b/doc/4_expert.md index d42b34a..d078646 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -104,5 +104,5 @@ An example crontab is provided in ```examples/bashbot.cron```. #### [Prev Expert Use](4_expert.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v0.61-2-gfd4dd8c +#### $$VERSION$$ 0.70-dev-11-g41b8e69 diff --git a/doc/5_practice.md b/doc/5_practice.md index 5a66418..04a3552 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -112,5 +112,5 @@ The second warning is about an unused variable, this is true because in our exam #### [Prev Best Practice](5_practice.md) #### [Next Functions Reference](6_reference.md) -#### $$VERSION$$ v0.61-2-gfd4dd8c +#### $$VERSION$$ 0.70-dev-11-g41b8e69 diff --git a/doc/6_reference.md b/doc/6_reference.md index c3865e0..5f7f10b 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -348,5 +348,5 @@ Send Input from Telegram to waiting Interactive Chat. #### [Prev Best Practice](5_practice.md) #### [Next Developer Rules](7_develop.md) -#### $$VERSION$$ v0.61-2-gfd4dd8c +#### $$VERSION$$ 0.70-dev-11-g41b8e69 diff --git a/doc/7_develop.md b/doc/7_develop.md index a4c2d1e..f20af33 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -31,7 +31,37 @@ For a shell script running as a service it's important to be paranoid about quot In addition you can run ```dev/hooks/pre-commit.sh``` every time you want to shellcheck all files given in 'dev/shellcheck.files'. +## 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. + +### 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. + +Tests with no dependency to other tests will run in pass 'a', tests which need an initialized bahsbot environment must run in pass 'c' or later. +If '$1' is present the script is started from 'ALL-tests.sh' and the script runs in a temporary test environment in directory '$1'. +The temporary test environment is created when 'ALL-tests.sh' starts and deleted after all tests are finished. + +Example test +```bash + +#!/usr/bin/env bash +# file: z-bashbot-test.sh + +# this test should always pass :-) +echo "Running test if bashbot.sh exists" +echo "................................." + +if [ -f "bashbot.sh" ]; then + echo "bashbot.sh OK!" + exit 0 +else + echo "bashbot.sh missing!" + exit 1 +fi +``` + #### [Prev Function Reference](6_function.md) -#### $$VERSION$$ v0.61-2-gfd4dd8c +#### $$VERSION$$ 0.70-dev-11-g41b8e69 diff --git a/examples/bashbot.cron b/examples/bashbot.cron index ad1fc7e..9a7b4be 100644 --- a/examples/bashbot.cron +++ b/examples/bashbot.cron @@ -7,7 +7,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ 0.70-dev-4-g12fd839 +#### $$VERSION$$ 0.70-dev-11-g41b8e69 SHELL=/bin/sh diff --git a/examples/calc.sh b/examples/calc.sh index 203fdfe..a4ad950 100755 --- a/examples/calc.sh +++ b/examples/calc.sh @@ -3,7 +3,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ 0.70-dev-4-g12fd839 +#### $$VERSION$$ 0.70-dev-11-g41b8e69 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/notify.sh b/examples/notify.sh index f63f12f..e07cd4f 100755 --- a/examples/notify.sh +++ b/examples/notify.sh @@ -2,7 +2,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ 0.70-dev-4-g12fd839 +#### $$VERSION$$ 0.70-dev-11-g41b8e69 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/question,sh b/examples/question,sh index d3c86c3..ee55964 100755 --- a/examples/question,sh +++ b/examples/question,sh @@ -3,7 +3,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ 0.70-dev-4-g12fd839 +#### $$VERSION$$ 0.70-dev-11-g41b8e69 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/question.sh b/examples/question.sh new file mode 100755 index 0000000..ee55964 --- /dev/null +++ b/examples/question.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# This file is public domain in the USA and all free countries. +# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) + +#### $$VERSION$$ 0.70-dev-11-g41b8e69 + +# adjust your language setting here +# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment +export 'LC_ALL=C.UTF-8' +export 'LANG=C.UTF-8' +export 'LANGUAGE=C.UTF-8' + +unset IFS +# set -f # if you are paranoid use set -f to disable globbing + +echo "Why hello there. +Would you like some tea (y/n)?" +read -r answer +[[ $answer =~ ^([yY][eE][sS]|[yY])$ ]] && echo "OK then, here you go: http://www.rivertea.com/blog/wp-content/uploads/2013/12/Green-Tea.jpg" || echo "OK then." +until [ "$SUCCESS" = "y" ] ;do + echo 'Do you like Music? mykeyboardstartshere "Yass!" , "No"' + read -r answer + case $answer in + 'Yass!') echo "Goody! mykeyboardendshere";SUCCESS=y;; + 'No') echo "Well that's weird. mykeyboardendshere";SUCCESS=y;; + *) SUCCESS=n;; + esac +done +exit diff --git a/test/ALL-tests.sh b/test/ALL-tests.sh new file mode 100755 index 0000000..8590960 --- /dev/null +++ b/test/ALL-tests.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# this has to run once atfer git clone +# and every time we create new hooks +#### $$VERSION$$ 0.70-dev-11-g41b8e69 + +# magic to ensure that we're always inside the root of our application, +# no matter from which directory we'll run script +GIT_DIR=$(git rev-parse --git-dir) +cd "${GIT_DIR}/.." || exit 1 + +# create test environment +TESTENV="/tmp/bashbot.test$$" +cp -r . "${TESTENV}" + +#set -e +fail=0 +tests=0 +passed=0 +#all_tests=${__dirname:} +#echo PLAN ${#all_tests} +for test in $(find test/*-test.sh | sort -u) ; +do + [ "${test}" = "test/all-tests.sh" ] && continue + [ ! -x "${test}" ] && continue + tests=$((tests+1)) + echo "TEST: ${test}" + "${test}" "${TESTENV}" + ret=$? + if [ "$ret" -eq 0 ] ; then + echo "OK: ---- ${test}" + passed=$((passed+1)) + else + echo "FAIL: $test $fail" + fail=$((fail+ret)) + break + fi +done + +if [ "$fail" -eq 0 ]; then + /bin/echo -n 'SUCCESS ' + exitcode=0 +else + /bin/echo -n 'FAILURE ' + exitcode=1 +fi + +rm -rf "${TESTENV}" +echo "${passed} / ${tests}" +exit ${exitcode} diff --git a/test/a-commit-test.sh b/test/a-commit-test.sh new file mode 100755 index 0000000..f27a3cd --- /dev/null +++ b/test/a-commit-test.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +#### $$VERSION$$ 0.70-dev-11-g41b8e69 + +# magic to ensure that we're always inside the root of our application, +# no matter from which directory we'll run script +GIT_DIR=$(git rev-parse --git-dir) +cd "$GIT_DIR/.." || exit 1 + +dev/hooks/pre-commit.sh + diff --git a/test/a-push-test.sh b/test/a-push-test.sh new file mode 100755 index 0000000..ea8b468 --- /dev/null +++ b/test/a-push-test.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +#### $$VERSION$$ 0.70-dev-11-g41b8e69 + +# magic to ensure that we're always inside the root of our application, +# no matter from which directory we'll run script +GIT_DIR=$(git rev-parse --git-dir) +cd "$GIT_DIR/.." || exit 1 + +dev/hooks/pre-push.sh diff --git a/test/b-init-test.sh b/test/b-init-test.sh new file mode 100755 index 0000000..f00b9f6 --- /dev/null +++ b/test/b-init-test.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +#### $$VERSION$$ 0.70-dev-11-g41b8e69 + +# magic to ensure that we're always inside the root of our application, +# no matter from which directory we'll run script +GIT_DIR=$(git rev-parse --git-dir) +cd "$GIT_DIR/.." || exit 1 + +TESTME="$(basename "$0")" +set -e + +# let's fake failing test for now +echo "Running bashbot init" +echo "............................" +# change to test env +[ "$1" = "" ] && echo "not called from testsuite, exit" && exit +cd "$1" || exit 1 + + +unset IFS; set -f + +# run bashbot first time with init +export TERM="" +"${1}/bashbot.sh" init >"${TESTME}.log" <