Skip to content

Commit 05512ec

Browse files
author
8go
committed
bug fix in --event code
- calling read() twice crashed program, now fixed - added test case to test-event.sh to test with 3 JSON files - see Issue 8go#60
1 parent d10ce45 commit 05512ec

14 files changed

+35
-16
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ credentials.json.*
66

77
# Also do not include the media directory.
88
/media/
9+
/media.*/
910

1011
# ignore backups
1112
README.md.*
1213
matrix-commander.py.*
1314

1415
# ignore help file
1516
help.txt
16-
17-
# ignore test directory? /test/

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ $ TARGET_EVENT="\$...a.valid.event.id" # event to which to react
331331
$ REACT_EMOJI="😀" # how to react
332332
$ printf "$JSON_REACT_MSC2677" "$TARGET_EVENT" "$REACT_EMOJI" |
333333
matrix-commander.py --event -
334-
$ # for more examples of "matrix-commander.py --event" see test/test-event.sh
334+
$ # for more examples of "matrix-commander.py --event" see tests/test-event.sh
335335
```
336336

337337
# Usage
@@ -709,7 +709,7 @@ optional arguments:
709709
information program will continue to run. This is
710710
useful for having version number in the log files.
711711
712-
You are running version 2022-05-23. Enjoy, star on Github and contribute by
712+
You are running version 2022-05-24. Enjoy, star on Github and contribute by
713713
submitting a Pull Request.
714714
```
715715

@@ -804,7 +804,8 @@ See [GPL3 at FSF](https://www.fsf.org/licensing/).
804804

805805
- Thanks to all of you who already have contributed! So appreciated!
806806
- :heart: and :thumbsup: to @fyfe, @berlincount, @ezwen, @Scriptkiddi,
807-
@pelzvieh, @mizlan, @edwinsage, @jschwartzentruber, @nirgal, @benneti, etc.
807+
@pelzvieh, @mizlan, @edwinsage, @jschwartzentruber, @nirgal, @benneti,
808+
@opk12, etc.
808809
- Enjoy!
809810
- Pull requests are welcome :heart:
810811

matrix-commander.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@
338338
$ REACT_EMOJI="😀" # how to react
339339
$ printf "$JSON_REACT_MSC2677" "$TARGET_EVENT" "$REACT_EMOJI" |
340340
matrix-commander.py --event -
341-
$ # for more examples of "matrix-commander.py --event" see test/test-event.sh
341+
$ # for more examples of "matrix-commander.py --event" see tests/test-event.sh
342342
```
343343
344344
# Usage
@@ -716,7 +716,7 @@
716716
information program will continue to run. This is
717717
useful for having version number in the log files.
718718
719-
You are running version 2022-05-23. Enjoy, star on Github and contribute by
719+
You are running version 2022-05-24. Enjoy, star on Github and contribute by
720720
submitting a Pull Request.
721721
```
722722
@@ -811,7 +811,8 @@
811811
812812
- Thanks to all of you who already have contributed! So appreciated!
813813
- :heart: and :thumbsup: to @fyfe, @berlincount, @ezwen, @Scriptkiddi,
814-
@pelzvieh, @mizlan, @edwinsage, @jschwartzentruber, @nirgal, @benneti, etc.
814+
@pelzvieh, @mizlan, @edwinsage, @jschwartzentruber, @nirgal, @benneti,
815+
@opk12, etc.
815816
- Enjoy!
816817
- Pull requests are welcome :heart:
817818
@@ -914,7 +915,7 @@
914915
HAVE_NOTIFY = False
915916

916917
# version number
917-
VERSION = "2022-05-23"
918+
VERSION = "2022-05-24"
918919
# matrix-commander
919920
PROG_WITHOUT_EXT = os.path.splitext(os.path.basename(__file__))[0]
920921
# matrix-commander.py
@@ -2089,9 +2090,8 @@ async def send_event(client, rooms, event): # noqa: C901
20892090
if event == "-": # - means read as pipe from stdin
20902091
jsondata = sys.stdin.buffer.read()
20912092
else:
2092-
file = open(event, "r").read()
2093-
jsondata = file.read()
2094-
file.close()
2093+
with open(event, "r") as file:
2094+
jsondata = file.read()
20952095
logger.debug(
20962096
f"{len(jsondata)} bytes of event data read from file {event}."
20972097
)
@@ -4739,6 +4739,12 @@ def initial_check_of_args() -> None: # noqa: C901
47394739
if not is_download_media_dir_valid():
47404740
sys.exit(1)
47414741
if not are_arg_files_readable():
4742+
logger.debug(
4743+
f"{PROG_WITHOUT_EXT} forces an early abort. "
4744+
"To avoid partial execution, no action has been performed at all. "
4745+
"Nothing has been sent. Fix your arguments and run the command "
4746+
"again."
4747+
)
47424748
sys.exit(1)
47434749
create_pid_file()
47444750

mc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
matrix-commander.py

test/test-event.sh tests/test-event.sh

+16-4
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,28 @@ else
2525
fi
2626
TARGET_EVENT="$MC_EVENT"
2727

28-
# These 3 test cases should pass
28+
# These 4 test cases should pass
2929
printf "$JSON_EDIT_MSC2676" "Fallback body $(date +%H:%M:%S)" "Non-fallback body $(date +%H:%M:%S)" "$TARGET_EVENT" |
30-
./matrix-commander.py --event - $MC_OPTIONS
30+
matrix-commander.py --event - $MC_OPTIONS
3131
# https://unicode.org/emoji/charts/full-emoji-list.html
3232
# emoji: thumbs up: "👍"; heart: "♥"; smiley: "😀"
3333
printf "$JSON_REACT_MSC2677" "$TARGET_EVENT" "😀" |
34-
./matrix-commander.py --event - $MC_OPTIONS
34+
matrix-commander.py --event - $MC_OPTIONS
3535
printf "$JSON_REPLY_AS_THREAD_MSC3440" "Thread reply $(date +%H:%M:%S)" "$TARGET_EVENT" |
36-
./matrix-commander.py --event - $MC_OPTIONS
36+
matrix-commander.py --event - $MC_OPTIONS
37+
38+
printf "$JSON_EDIT_MSC2676" "Fallback body $(date +%H:%M:%S)" "Non-fallback body $(date +%H:%M:%S)" "$TARGET_EVENT" >event1.json
39+
printf "$JSON_REACT_MSC2677" "$TARGET_EVENT" "👍" >event2.json
40+
printf "$JSON_REPLY_AS_THREAD_MSC3440" "Thread reply $(date +%H:%M:%S)" "$TARGET_EVENT" >event3.json
41+
echo "##############################################################"
42+
echo "##############################################################"
43+
echo "##############################################################"
44+
matrix-commander.py --event event1.json event2.json event3.json -m "" $MC_OPTIONS
45+
rm event1.json event2.json event3.json
3746

47+
echo "##############################################################"
48+
echo "##############################################################"
49+
echo "##############################################################"
3850
#
3951

4052
# These 4 test cases should ***FAIL***. ***INCORRECT*** JSON objects.

test/test.l.jpg tests/test.l.jpg

File renamed without changes.

test/test.l.png tests/test.l.png

File renamed without changes.

test/test.ogg tests/test.ogg

File renamed without changes.

test/test.pdf tests/test.pdf

File renamed without changes.

test/test.s.jpg tests/test.s.jpg

File renamed without changes.

test/test.s.png tests/test.s.png

File renamed without changes.

test/test.svg tests/test.svg

File renamed without changes.

test/test.txt tests/test.txt

File renamed without changes.

test/test.wav tests/test.wav

File renamed without changes.

0 commit comments

Comments
 (0)