From 26c8523589387721224798fdbcf6927d6d1ef8c7 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 21 Apr 2019 13:45:51 +0200 Subject: [PATCH] fix first errors found by testsuite --- bashbot.sh | 156 +++++++++--------- test/ALL-tests.inc.sh | 2 +- test/ALL-tests.sh | 2 +- test/a-commit-test.sh | 2 +- test/a-push-test.sh | 2 +- test/c-init-test.sh | 2 +- test/d-process_message-test.sh | 29 +++- ...est.input => d-process_message-test.input} | 30 ++-- 8 files changed, 127 insertions(+), 98 deletions(-) mode change 100755 => 100644 test/a-commit-test.sh mode change 100755 => 100644 test/a-push-test.sh rename test/d-process_message-test/{d-process-message-test.input => d-process_message-test.input} (82%) diff --git a/bashbot.sh b/bashbot.sh index 4c258d3..4093791 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$$ 0.70-dev-19-g3183419 +#### $$VERSION$$ 0.70-dev-21-gd4cd756 # # Exit Codes: # - 0 sucess (hopefully) @@ -390,7 +390,8 @@ remove_keyboard() { } get_file() { - [ "$1" != "" ] && echo "$FILE_URL$(curl -s "$GET_URL" -F "file_id=$1" | ./JSON.sh/JSON.sh -s | JsonGetString '"result","file_path"')" + [ "$1" = "" ] && return + echo "$FILE_URL$(curl -s "$GET_URL" -F "file_id=$1" | ./JSON.sh/JSON.sh -s | grep '\["result","file_path"\]' | cut -f 2 | cut -d '"' -f 2)" } send_file() { @@ -514,81 +515,8 @@ process_updates() { fi done } -JsonGetString() { - sed -n -e '/\['"$1"'\]/ s/.*\][ \t]"\(.*\)"$/\1/p' -} -JsonGetValue() { - sed -n -e '/\['"$1"'\]/ s/.*\][ \t]//p' -} process_client() { - local TMP="${TMPDIR:-.}/$RANDOM$RANDOM-MESSAGE" - echo "$UPDATE" >"$TMP" - # Message - MESSAGE[0]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","text"' <"$TMP")" | sed 's#\\/#/#g')" - MESSAGE[ID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","message_id"' <"$TMP" )" - - # Chat - CHAT[ID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","chat","id"' <"$TMP" )" - CHAT[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","first_name"' <"$TMP")")" - CHAT[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","last_name"' <"$TMP")")" - CHAT[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","username"' <"$TMP")")" - CHAT[TITLE]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","title"' <"$TMP")")" - CHAT[TYPE]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","type"' <"$TMP")")" - CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","all_members_are_administrators"' <"$TMP")")" - - # User - USER[ID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","from","id"' <"$TMP" )" - USER[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","from","first_name"' <"$TMP")")" - USER[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","from","last_name"' <"$TMP")")" - USER[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","from","username"' <"$TMP")")" - - # in reply to message from - REPLYTO[UID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","id"' <"$TMP" )" - if [ "${REPLYTO[UID]}" != "" ]; then - REPLYTO[0]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","text"' <"$TMP")")" - REPLYTO[ID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","reply_to_message","message_id"' <"$TMP")" - REPLYTO[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","first_name"' <"$TMP")")" - REPLYTO[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","last_name"' <"$TMP")")" - REPLYTO[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","username"' <"$TMP")")" - fi - - # forwarded message from - FORWARD[UID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","forward_from","id"' <"$TMP" )" - if [ "${FORWARD[UID]}" != "" ]; then - FORWARD[ID]="${MESSAGE[ID]}" # same as message ID - FORWARD[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","forward_from","first_name"' <"$TMP")")" - FORWARD[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","forward_from","last_name"' <"$TMP")")" - FORWARD[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","forward_from","username"' <"$TMP")")" - fi - - # Audio - URLS[AUDIO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","audio","file_id"' <"$TMP")")")" - # Document - URLS[DOCUMENT]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","document","file_id"' <"$TMP")")")" - # Photo - URLS[PHOTO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","photo",.*,"file_id"' <"$TMP")")")" - # Sticker - URLS[STICKER]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","sticker","file_id"' <"$TMP")")")" - # Video - URLS[VIDEO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","video","file_id"' <"$TMP")")")" - # Voice - URLS[VOICE]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","voice","file_id"' <"$TMP")")")" - - # Contact - CONTACT[NUMBER]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","contact","phone_number"' <"$TMP")")" - CONTACT[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","contact","first_name"' <"$TMP")")" - CONTACT[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","contact","last_name"' <"$TMP")")" - CONTACT[USER_ID]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","contact","user_id"' <"$TMP")")" - - # Caption - CAPTION="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","caption"' <"$TMP")")" - - # Location - LOCATION[LONGITUDE]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","location","longitude"' <"$TMP")")" - LOCATION[LATITUDE]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","location","latitude"' <"$TMP")")" - NAME="$(echo "${URLS[*]}" | sed 's/.*\///g')" - rm "$TMP" - + process_message "$PROCESS_NUMBER" # Tmux copname="$ME"_"${CHAT[ID]}" @@ -598,6 +526,82 @@ process_client() { grep -q "$tmpcount" <"${COUNTFILE}" >/dev/null 2>&1 || echo "$tmpcount">>${COUNTFILE} # To get user count execute bash bashbot.sh count } +JsonGetString() { + sed -n -e '/\['"$1"'\]/ s/.*\][ \t]"\(.*\)"$/\1/p' +} +JsonGetValue() { + sed -n -e '/\['"$1"'\]/ s/.*\][ \t]//p' +} +process_message() { + local num="$1" + local TMP="${TMPDIR:-.}/$RANDOM$RANDOM-MESSAGE" + echo "$UPDATE" >"$TMP" + # Message + MESSAGE[0]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","text"' <"$TMP")" | sed 's#\\/#/#g')" + MESSAGE[ID]="$(JsonGetValue '"result",'"${num}"',"message","message_id"' <"$TMP" )" + + # Chat + CHAT[ID]="$(JsonGetValue '"result",'"${num}"',"message","chat","id"' <"$TMP" )" + CHAT[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","chat","first_name"' <"$TMP")")" + CHAT[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","chat","last_name"' <"$TMP")")" + CHAT[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","chat","username"' <"$TMP")")" + CHAT[TITLE]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","chat","title"' <"$TMP")")" + CHAT[TYPE]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","chat","type"' <"$TMP")")" + CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","chat","all_members_are_administrators"' <"$TMP")")" + + # User + USER[ID]="$(JsonGetValue '"result",'"${num}"',"message","from","id"' <"$TMP" )" + USER[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","from","first_name"' <"$TMP")")" + USER[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","from","last_name"' <"$TMP")")" + USER[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","from","username"' <"$TMP")")" + + # in reply to message from + REPLYTO[UID]="$(JsonGetValue '"result",'"${num}"',"message","reply_to_message","from","id"' <"$TMP" )" + if [ "${REPLYTO[UID]}" != "" ]; then + REPLYTO[0]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","reply_to_message","text"' <"$TMP")")" + REPLYTO[ID]="$(JsonGetValue '"result",'"${num}"',"message","reply_to_message","message_id"' <"$TMP")" + REPLYTO[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","reply_to_message","from","first_name"' <"$TMP")")" + REPLYTO[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","reply_to_message","from","last_name"' <"$TMP")")" + REPLYTO[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","reply_to_message","from","username"' <"$TMP")")" + fi + + # forwarded message from + FORWARD[UID]="$(JsonGetValue '"result",'"${num}"',"message","forward_from","id"' <"$TMP" )" + if [ "${FORWARD[UID]}" != "" ]; then + FORWARD[ID]="${MESSAGE[ID]}" # same as message ID + FORWARD[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","forward_from","first_name"' <"$TMP")")" + FORWARD[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","forward_from","last_name"' <"$TMP")")" + FORWARD[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","forward_from","username"' <"$TMP")")" + fi + + # Audio + URLS[AUDIO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","audio","file_id"' <"$TMP")")")" + # Document + URLS[DOCUMENT]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","document","file_id"' <"$TMP")")")" + # Photo + URLS[PHOTO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","photo",.*,"file_id"' <"$TMP")")")" + # Sticker + URLS[STICKER]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","sticker","file_id"' <"$TMP")")")" + # Video + URLS[VIDEO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","video","file_id"' <"$TMP")")")" + # Voice + URLS[VOICE]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","voice","file_id"' <"$TMP")")")" + + # Contact + CONTACT[NUMBER]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","contact","phone_number"' <"$TMP")")" + CONTACT[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","contact","first_name"' <"$TMP")")" + CONTACT[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","contact","last_name"' <"$TMP")")" + CONTACT[USER_ID]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","contact","user_id"' <"$TMP")")" + + # Caption + CAPTION="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","caption"' <"$TMP")")" + + # Location + LOCATION[LONGITUDE]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","location","longitude"' <"$TMP")")" + LOCATION[LATITUDE]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","location","latitude"' <"$TMP")")" + NAME="$(echo "${URLS[*]}" | sed 's/.*\///g')" + #rm "$TMP" +} # get bot name getBotName() { res="$(curl -s "$ME_URL")" diff --git a/test/ALL-tests.inc.sh b/test/ALL-tests.inc.sh index 4c6f0c8..b91723c 100755 --- a/test/ALL-tests.inc.sh +++ b/test/ALL-tests.inc.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-20-g753f1b3 +#### $$VERSION$$ 0.70-dev-21-gd4cd756 # common variables export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME diff --git a/test/ALL-tests.sh b/test/ALL-tests.sh index 1a3b889..4ef0e30 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-20-g753f1b3 +#### $$VERSION$$ 0.70-dev-21-gd4cd756 # 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 old mode 100755 new mode 100644 index 969be9f..1afc51e --- a/test/a-commit-test.sh +++ b/test/a-commit-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-20-g753f1b3 +#### $$VERSION$$ 0.70-dev-21-gd4cd756 ../dev/hooks/pre-commit.sh diff --git a/test/a-push-test.sh b/test/a-push-test.sh old mode 100755 new mode 100644 index aba15fa..3e5d1f1 --- a/test/a-push-test.sh +++ b/test/a-push-test.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-20-g753f1b3 +#### $$VERSION$$ 0.70-dev-21-gd4cd756 ../dev/hooks/pre-push.sh diff --git a/test/c-init-test.sh b/test/c-init-test.sh index a07c00a..a6c2561 100755 --- a/test/c-init-test.sh +++ b/test/c-init-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-20-g753f1b3 +#### $$VERSION$$ 0.70-dev-21-gd4cd756 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-process_message-test.sh b/test/d-process_message-test.sh index fadd929..fc9a275 100755 --- a/test/d-process_message-test.sh +++ b/test/d-process_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-20-g753f1b3 +#### $$VERSION$$ 0.70-dev-21-gd4cd756 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -11,6 +11,33 @@ set -e cd "${TESTDIR}" || exit 1 # shellcheck source=./bashbot.sh source "${TESTDIR}/bashbot.sh" source + +export UPDATE +UPDATE="$(cat ${DIRME}/${REFDIR}/${REFDIR}.input)" + +# overwrite get_file for test +get_file() { + echo "$1" +} + +set -x +process_message "0" >>${LOGFILE} 2>&1 +set +x cd "${DIRME}" || exit 1 +# output processed input + +print_array() { + local idx t + local arrays=( "${@}" ) + for idx in "${arrays[@]}"; do + declare -n temp="$idx" + for t in "${!temp[@]}"; do + printf "%s:\t%s\t%s\n" "$idx" "$t" "${temp[$t]}" + done | sort + done | grep -v '^USER: 0' +} + +print_array "USER" "CHAT" "REPLYTO" "FORWARD" "URLS" "CONTACT" "CAPTION" "LOCATION" "MESSAGE" + 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 similarity index 82% rename from test/d-process_message-test/d-process-message-test.input rename to test/d-process_message-test/d-process_message-test.input index 38bbe46..274bee0 100644 --- a/test/d-process_message-test/d-process-message-test.input +++ b/test/d-process_message-test/d-process_message-test.input @@ -18,7 +18,6 @@ ["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" @@ -30,27 +29,26 @@ ["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","reply_to_message","entities",0,"offset"] 61 +["result",0,"message","reply_to_message","entities",0,"length"] 5 +["result",0,"message","reply_to_message","entities",0,"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","forward_from","id"] 123456789 +["result",0,"message","forward_from","is_bot"] false +["result",0,"message","forward_from","first_name"] "Kay" +["result",0,"message","forward_from","last_name"] "M" +["result",0,"message","forward_from","username"] "Gnadelwartz" +["result",0,"message","forward_from","language_code"] "de" +["result",0,"message","forward_date"] 1555822879 ["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",0,"file_id"] "AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOFli9a5TF3rbPgDAAEC" +["result",0,"message","photo",0,"file_size"] 25125 +["result",0,"message","photo",0,"width"] 240 +["result",0,"message","photo",0,"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