2019-04-21 09:34:17 +00:00
|
|
|
#!/usr/bin/env bash
|
2021-01-02 12:45:09 +00:00
|
|
|
#===============================================================================
|
|
|
|
#
|
|
|
|
# FILE: ALL-tests.inc.sh
|
|
|
|
#
|
|
|
|
# USAGE: source ALL-tests.inc.sh
|
|
|
|
#
|
|
|
|
# DESCRIPTION: must be included from all tests,
|
|
|
|
# setup bashbot test environment and common test functions
|
|
|
|
#
|
|
|
|
# LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/
|
|
|
|
# AUTHOR: KayM (gnadelwartz), kay@rrr.de
|
|
|
|
#
|
|
|
|
#### $$VERSION$$ v1.21-pre-32-gd70a461
|
|
|
|
#===============================================================================
|
|
|
|
|
|
|
|
############
|
|
|
|
# set where your bashbot lives
|
|
|
|
export BASHBOT_HOME BASHBOT_ETC BASHBOT_VAR FILE_REGEX
|
|
|
|
|
|
|
|
# default: one dir up
|
|
|
|
BASHBOT_HOME="$(cd "${BASH_SOURCE[0]%/*}" >/dev/null 2>&1 && pwd)/../"
|
|
|
|
[ "${BASHBOT_HOME}" = "/../" ] && BASHBOT_HOME="../"
|
|
|
|
|
|
|
|
# set you own BASHBOT_HOME if different, e.g.
|
|
|
|
# BASHBOT_HOME="/usr/local/telegram-bot-bash"
|
|
|
|
BASHBOT_VAR="${BASHBOT_HOME}"
|
|
|
|
BASHBOT_ETC="${BASHBOT_HOME}"
|
|
|
|
|
|
|
|
#####
|
|
|
|
# if files are not readable, eviroment is wrong or bashbot is not initialized
|
|
|
|
|
|
|
|
# source bashbot
|
|
|
|
if [ ! -r "${BASHBOT_HOME}/bashbot.sh" ]; then
|
|
|
|
echo "Bashbot.sh not found in \"${BASHBOT_HOME}\""
|
|
|
|
exit 4
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check for botconfig.jssh readable
|
|
|
|
if [ ! -r "${BASHBOT_ETC}/botconfig.jssh" ]; then
|
|
|
|
echo "Bashbot config file in \"${BASHBOT_ETC}\" does not exist or is not readable."
|
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
# check for count.jssh readable
|
|
|
|
if [ ! -r "${BASHBOT_VAR}/count.jssh" ]; then
|
|
|
|
echo "Bashbot count file in \"${BASHBOT_VAR}\" does not exist or is not readable. Did you run bashbot init?"
|
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
|
|
|
|
# shellcheck disable=SC1090
|
|
|
|
source "${BASHBOT_HOME}/bashbot.sh" source "$1"
|
|
|
|
|
|
|
|
# overwrite bot FILE regex to BASHBOT_VAR
|
|
|
|
# change this to the location you want to allow file uploads from
|
|
|
|
UPLOADDIR="${BASHBOT_VAR%/bin*}"
|
|
|
|
FILE_REGEX="${UPLOADDIR}/.*"
|
|
|
|
|
|
|
|
# get and check ADMIN and NAME
|
|
|
|
BOT_ADMIN="$(getConfigKey "botadmin")"
|
|
|
|
BOT_NAME="$(getConfigKey "botname")"
|
|
|
|
[[ -z "${BOT_ADMIN}" || "${BOT_ADMIN}" == "?" ]] && echo -e "${ORANGE}Warning: Botadmin not set, did you forget to sent command${NC} /start"
|
|
|
|
[[ -z "${BOT_NAME}" ]] && echo -e "${ORANGE}Warning: Botname not set, did you ever run bashbot?"
|
|
|
|
|
2019-04-21 09:34:17 +00:00
|
|
|
|
|
|
|
# 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
|
2020-06-01 09:47:07 +00:00
|
|
|
export TOKENFILE ACLFILE COUNTFILE BLOCKEDFILE ADMINFILE DATADIR JSONSHFILE
|
2020-06-27 16:31:35 +00:00
|
|
|
TOKENFILE="botconfig.jssh"
|
2019-04-21 09:34:17 +00:00
|
|
|
ACLFILE="botacl"
|
2020-06-01 09:47:07 +00:00
|
|
|
COUNTFILE="count.jssh"
|
|
|
|
BLOCKEDFILE="blocked.jssh"
|
2020-06-27 16:31:35 +00:00
|
|
|
ADMINFILE="botconfig.jssh"
|
2019-05-01 09:46:57 +00:00
|
|
|
DATADIR="data-bot-bash"
|
2019-05-16 09:23:27 +00:00
|
|
|
JSONSHFILE="JSON.sh/JSON.sh"
|
2019-04-21 09:34:17 +00:00
|
|
|
|
|
|
|
# SUCCESS NOSUCCES
|
|
|
|
export SUCCESS NOSUCCESS
|
2019-04-22 08:19:16 +00:00
|
|
|
SUCCESS=" OK"
|
|
|
|
NOSUCCESS=" FAILED!"
|
2019-04-21 09:34:17 +00:00
|
|
|
|
2019-04-21 12:52:55 +00:00
|
|
|
# default input, reference and output files
|
2020-06-29 09:13:25 +00:00
|
|
|
export INPUTFILE REFFILE INPUTFILE2 REFFILE2 OUTPUTFILE
|
|
|
|
OUTPUTFILE="${TESTDIR}/${REFDIR}.out"
|
2019-04-22 08:19:16 +00:00
|
|
|
INPUTFILE="${DIRME}/${REFDIR}/${REFDIR}.input"
|
|
|
|
REFFILE="${DIRME}/${REFDIR}/${REFDIR}.result"
|
2020-06-29 09:13:25 +00:00
|
|
|
INPUTFILE2="${DIRME}/${REFDIR}/${REFDIR}2.input"
|
|
|
|
REFFILE2="${DIRME}/${REFDIR}/${REFDIR}2.result"
|
2019-04-21 12:52:55 +00:00
|
|
|
|
2020-12-31 21:43:33 +00:00
|
|
|
# reset ENVIRONMENT
|
|
|
|
export BASHBOT_URL TESTTOKEN BOTTOKEN BASHBOT_HOME BASHBOT_VAR BASHBOT_ETC
|
2020-12-28 21:38:30 +00:00
|
|
|
BOTTOKEN=""
|
2020-12-31 21:43:33 +00:00
|
|
|
BASHBOT_HOME=""
|
|
|
|
BASHBOT_VAR=""
|
|
|
|
BASHBOT_ETC=""
|
|
|
|
# do not query telegram when testing
|
2019-05-24 08:47:27 +00:00
|
|
|
BASHBOT_URL="https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?"
|
2020-06-01 11:44:30 +00:00
|
|
|
TESTTOKEN="123456789:BASHBOTTESTSCRIPTbashbottestscript_"
|
2019-05-24 08:47:27 +00:00
|
|
|
|
2019-04-21 12:52:55 +00:00
|
|
|
# print arrays in reproducible order
|
|
|
|
print_array() {
|
|
|
|
local idx t
|
|
|
|
local arrays=( "${@}" )
|
|
|
|
for idx in "${arrays[@]}"; do
|
|
|
|
declare -n temp="$idx"
|
|
|
|
for t in "${!temp[@]}"; do
|
2019-04-22 18:34:43 +00:00
|
|
|
printf '%s:\t%s\t%s\n' "$idx" "$t" "${temp[$t]}"
|
2019-04-21 12:52:55 +00:00
|
|
|
done | sort
|
|
|
|
done | grep -v '^USER: 0'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-10 16:07:12 +00:00
|
|
|
compare_sorted() {
|
|
|
|
local ret=0
|
|
|
|
sort -d -o "${1}.sort" "${1}"
|
|
|
|
sort -d -o "${2}.sort" "${2}"
|
|
|
|
diff -c "${1}.sort" "${2}.sort" || ret=1
|
2020-06-29 09:17:58 +00:00
|
|
|
[[ "${1}" != "${TESTDIR}"* ]] && rm -f "${1}.sort"
|
|
|
|
[[ "${2}" != "${TESTDIR}"* ]] && rm -f "${2}.sort"
|
2020-06-10 16:07:12 +00:00
|
|
|
return "$ret"
|
|
|
|
}
|
|
|
|
|
2019-04-21 12:52:55 +00:00
|
|
|
######
|
|
|
|
# lets go ...
|
2021-01-02 12:45:09 +00:00
|
|
|
printf "Running %s ...\n" "${TESTNAME#? }"
|
|
|
|
printf "............................\n"
|
|
|
|
[ "${TESTDIR}" = "" ] && printf "%s not called from testsuite, exit" "${NOSUCCESS}" && exit 1
|
2019-04-21 09:34:17 +00:00
|
|
|
|
|
|
|
# reset env for test
|
2019-05-26 19:25:01 +00:00
|
|
|
unset IFS;
|
2019-04-21 09:34:17 +00:00
|
|
|
export TERM=""
|
|
|
|
|