From fbd86c3412acab6f7d3997426ead3b3f9d5e45cb Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 21 Apr 2019 17:15:46 +0200 Subject: [PATCH] fix errors in JsonGet found by testsuite --- bashbot.sh | 29 ++++++++++--------- test/ALL-tests.inc.sh | 2 +- test/ALL-tests.sh | 4 +-- test/a-commit-test.sh | 2 +- test/a-push-test.sh | 2 +- test/c-init-test.sh | 2 +- test/d-process_message-test.sh | 27 ++++++++++------- .../d-process_message-test.input | 17 ++++++++--- .../d-process_message-test.result | 14 ++++----- 9 files changed, 58 insertions(+), 41 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 7828813..8c08629 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-22-g26c8523 +#### $$VERSION$$ 0.70-dev-23-g48e6f64 # # Exit Codes: # - 0 sucess (hopefully) @@ -391,7 +391,7 @@ remove_keyboard() { get_file() { [ "$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)" + 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() { @@ -519,18 +519,19 @@ process_client() { process_message "$PROCESS_NUMBER" # Tmux copname="$ME"_"${CHAT[ID]}" - source commands.sh - tmpcount="COUNT${CHAT[ID]}" 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' + sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]"\(.*\)"$/\1/p' +} +JsonGetLine() { + sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\]\][ \t]//p' } JsonGetValue() { - sed -n -e '/\['"$1"'\]/ s/.*\][ \t]//p' + sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]\([0-9.,]*\).*/\1/p' } process_message() { local num="$1" @@ -575,17 +576,17 @@ process_message() { fi # Audio - URLS[AUDIO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","audio","file_id"' <"$TMP")")")" + URLS[AUDIO]="$(get_file "$(JsonGetString '"result",'"${num}"',"message","audio","file_id"' <"$TMP")")" # Document - URLS[DOCUMENT]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","document","file_id"' <"$TMP")")")" + URLS[DOCUMENT]="$(get_file "$(JsonGetString '"result",'"${num}"',"message","document","file_id"' <"$TMP")")" # Photo - URLS[PHOTO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","photo",0,"file_id"' <"$TMP")")")" + URLS[PHOTO]="$(get_file "$(JsonGetString '"result",'"${num}"',"message","photo",0,"file_id"' <"$TMP")")" # Sticker - URLS[STICKER]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","sticker","file_id"' <"$TMP")")")" + URLS[STICKER]="$(get_file "$(JsonGetString '"result",'"${num}"',"message","sticker","file_id"' <"$TMP")")" # Video - URLS[VIDEO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","video","file_id"' <"$TMP")")")" + URLS[VIDEO]="$(get_file "$(JsonGetString '"result",'"${num}"',"message","video","file_id"' <"$TMP")")" # Voice - URLS[VOICE]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","voice","file_id"' <"$TMP")")")" + URLS[VOICE]="$(get_file "$(JsonGetString '"result",'"${num}"',"message","voice","file_id"' <"$TMP")")" # Contact CONTACT[NUMBER]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","contact","phone_number"' <"$TMP")")" @@ -600,7 +601,7 @@ process_message() { LOCATION[LONGITUDE]="$(JsonGetValue '"result",'"${num}"',"message","location","longitude"' <"$TMP")" LOCATION[LATITUDE]="$(JsonGetValue '"result",'"${num}"',"message","location","latitude"' <"$TMP")" NAME="$(echo "${URLS[*]}" | sed 's/.*\///g')" - #rm "$TMP" + rm "$TMP" } # get bot name getBotName() { @@ -619,7 +620,7 @@ if [ "$ME" = "" ]; then fi # use phyton JSON to decode JSON UFT-8, provide bash implementaion as fallback -if which python >/dev/null 2>&1 || which phyton2 >/dev/null 2>&1; then +if [ "${BASHDECODE}" != "yes" ] && which python >/dev/null 2>&1 ; then JsonDecode() { printf '"%s\\n"' "${1//\"/\\\"}" | python -c 'import json, sys; sys.stdout.write(json.load(sys.stdin).encode("utf-8"))' } diff --git a/test/ALL-tests.inc.sh b/test/ALL-tests.inc.sh index 0e1b414..72f1e52 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-22-g26c8523 +#### $$VERSION$$ 0.70-dev-23-g48e6f64 # common variables export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME diff --git a/test/ALL-tests.sh b/test/ALL-tests.sh index a0bdc0a..ef2419e 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-22-g26c8523 +#### $$VERSION$$ 0.70-dev-23-g48e6f64 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -19,7 +19,7 @@ tests=0 passed=0 #all_tests=${__dirname:} #echo PLAN ${#all_tests} -for test in $(find ./*-test.sh | sort -u) ; +for test in $(find ./${pass}*-test.sh | sort -u) ; do [ "${test}" = "test/all-tests.sh" ] && continue [ ! -x "${test}" ] && continue diff --git a/test/a-commit-test.sh b/test/a-commit-test.sh index 1755b4f..161ab93 100644 --- a/test/a-commit-test.sh +++ b/test/a-commit-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-22-g26c8523 +#### $$VERSION$$ 0.70-dev-23-g48e6f64 ../dev/hooks/pre-commit.sh diff --git a/test/a-push-test.sh b/test/a-push-test.sh index 300a8ca..26a5a26 100644 --- a/test/a-push-test.sh +++ b/test/a-push-test.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-22-g26c8523 +#### $$VERSION$$ 0.70-dev-23-g48e6f64 ../dev/hooks/pre-push.sh diff --git a/test/c-init-test.sh b/test/c-init-test.sh index 5519e3f..a13cec5 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-22-g26c8523 +#### $$VERSION$$ 0.70-dev-23-g48e6f64 # 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 ecbc737..9156691 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-22-g26c8523 +#### $$VERSION$$ 0.70-dev-23-g48e6f64 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -21,13 +21,20 @@ get_file() { export UPDATE UPDATE="$(cat "${INPUTFILE}")" -set -x -process_message "0" >>"${LOGFILE}" 2>&1; set +x +# run process_message with and without phyton +echo "Check process_message ..." +for i in 1 2 +do + [ "${i}" = "1" ] && ! which python >/dev/null 2>&1 && continue + [ "${i}" = "1" ] && echo " ... JsonDecode Phyton" + [ "${i}" = "2" ] && echo " ... JsonDecode Bash" && export BASHDECODE="yes" + set -x + { process_message "0"; set +x; } >>"${LOGFILE}" 2>&1; + + # output processed input + print_array "USER" "CHAT" "REPLYTO" "FORWARD" "URLS" "CONTACT" "CAPTION" "LOCATION" "MESSAGE" >"${OUTPUTFILE}" + diff -c "${REFFILE}" "${OUTPUTFILE}" || exit 1 + echo "${SUCCESS}" +done + cd "${DIRME}" || exit 1 - -# output processed input -echo "Diff process_message input and output ..." -print_array "USER" "CHAT" "REPLYTO" "FORWARD" "URLS" "CONTACT" "CAPTION" "LOCATION" "MESSAGE" >"${OUTPUTFILE}" -diff -c "${REFFILE}" "${OUTPUTFILE}" || 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 index e99b23b..8aaebb4 100644 --- a/test/d-process_message-test/d-process_message-test.input +++ b/test/d-process_message-test/d-process_message-test.input @@ -1,6 +1,8 @@ ["ok"] true ["result",0,"update_id"] 146860800 -["result",0,"message","message_id"] 6541 +["result",0,"message","message_id"] 6541 6541 # this is a cross check for JsonGetValue +["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",0,"message","text"] "Cross check, this second text must be ignored" ["result",0,"message","from","id"] 123456789 ["result",0,"message","from","is_bot"] false ["result",0,"message","from","first_name"] "Kay" @@ -11,6 +13,7 @@ ["result",0,"message","chat","first_name"] "Test" ["result",0,"message","chat","last_name"] "Bot" ["result",0,"message","chat","username"] "BotTest" +["result",0,"message","chat","title"] "BotTestTitle" ["result",0,"message","chat","type"] "private" ["result",0,"message","date"] 1555822879 ["result",0,"message","reply_to_message","message_id"] 6542 @@ -32,7 +35,6 @@ ["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",0,"message","forward_from","id"] 123456789 ["result",0,"message","forward_from","is_bot"] false ["result",0,"message","forward_from","first_name"] "Kay" @@ -40,13 +42,20 @@ ["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","audio","file_id"] "audio-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa" +["result",0,"message","video","file_id"] "video-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa" +["result",0,"message","document","file_id"] "document-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa" +["result",0,"message","sticker","file_id"] "sticker-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa" +["result",0,"message","sticker","file_id"] "sticker-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa" +["result",0,"message","voice","file_id"] "voice-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa" + +["result",0,"message","photo",0,"file_id"] "photo-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa" ["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,"file_size"] 2512 ["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} diff --git a/test/d-process_message-test/d-process_message-test.result b/test/d-process_message-test/d-process_message-test.result index 783bdfc..116cf0d 100644 --- a/test/d-process_message-test/d-process_message-test.result +++ b/test/d-process_message-test/d-process_message-test.result @@ -6,7 +6,7 @@ CHAT: ALL_MEMBERS_ARE_ADMINISTRATORS CHAT: FIRST_NAME Test CHAT: ID 123456789 CHAT: LAST_NAME Bot -CHAT: TITLE +CHAT: TITLE BotTestTitle CHAT: TYPE private CHAT: USERNAME BotTest REPLYTO: 0 Ich bin der Deal-O-Mat Bot. Für eine Liste der Befehle sende /help @@ -20,12 +20,12 @@ FORWARD: ID 6541 FORWARD: LAST_NAME M FORWARD: UID 123456789 FORWARD: USERNAME Gnadelwartz -URLS: AUDIO -URLS: DOCUMENT -URLS: PHOTO AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa_gDAAEC -URLS: STICKER -URLS: VIDEO -URLS: VOICE AwADAgADOAQAAqd24Emnm_VGmmVEuAI +URLS: AUDIO audio-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa +URLS: DOCUMENT document-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa +URLS: PHOTO photo-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa +URLS: STICKER sticker-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa +URLS: VIDEO video-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa +URLS: VOICE voice-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa CONTACT: FIRST_NAME ADAC CONTACT: LAST_NAME Pannenhilfe CONTACT: NUMBER 222222