From 54a30fa97b4a771dc088c2965df19cf9bd93c1de Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Fri, 5 May 2023 19:06:03 +0200 Subject: [PATCH] More robustly escape input character to cope with special regexp inputs like brackets --- asciinema_automation/instruction.py | 7 ++----- examples/special_character.sh | 2 ++ tests/reference_output/special_character.cast | 3 +++ 3 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 examples/special_character.sh create mode 100644 tests/reference_output/special_character.cast diff --git a/asciinema_automation/instruction.py b/asciinema_automation/instruction.py index 1744049..c957837 100644 --- a/asciinema_automation/instruction.py +++ b/asciinema_automation/instruction.py @@ -1,5 +1,6 @@ import logging import random +import re import time logger = logging.getLogger(__name__) @@ -55,11 +56,7 @@ def run(self, script): self.receive_value = self.send_value # Check for special character - if "\\" in self.send_value: - self.receive_value = [ - character if character != "\\" else character + "\\" - for character in list(self.send_value) - ] + self.receive_value = [re.escape(c) for c in list(self.send_value)] # Write intruction for send_character, receive_character in zip( diff --git a/examples/special_character.sh b/examples/special_character.sh new file mode 100644 index 0000000..af03fce --- /dev/null +++ b/examples/special_character.sh @@ -0,0 +1,2 @@ +#$ sendcharacter # +This is a test with URL (https://github.com) diff --git a/tests/reference_output/special_character.cast b/tests/reference_output/special_character.cast new file mode 100644 index 0000000..e45483e --- /dev/null +++ b/tests/reference_output/special_character.cast @@ -0,0 +1,3 @@ +[?2004h$ #This is a test with URL (https://github.com) +[?2004l [?2004h$ [?2004l +exit