Skip to content

3.6 tests failing due to remove_mention_text #747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions libraries/botbuilder-core/botbuilder/core/re_escape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

# SPECIAL_CHARS
# closing ')', '}' and ']'
# '-' (a range in character set)
# '&', '~', (extended character set operations)
# '#' (comment) and WHITESPACE (ignored) in verbose mode
SPECIAL_CHARS_MAP = {i: "\\" + chr(i) for i in b"()[]{}?*+-|^$\\.&~# \t\n\r\v\f"}


def escape(pattern):
"""
Escape special characters in a string.

This is a copy of the re.escape function in Python 3.8. This was done
because the 3.6.x version didn't escape in the same way and handling
bot names with regex characters in it would fail in TurnContext.remove_mention_text
without escaping the text.
"""
if isinstance(pattern, str):
return pattern.translate(SPECIAL_CHARS_MAP)

pattern = str(pattern, "latin1")
return pattern.translate(SPECIAL_CHARS_MAP).encode("latin1")
3 changes: 2 additions & 1 deletion libraries/botbuilder-core/botbuilder/core/turn_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Mention,
ResourceResponse,
)
from .re_escape import escape


class TurnContext:
Expand Down Expand Up @@ -362,7 +363,7 @@ def remove_mention_text(activity: Activity, identifier: str) -> str:
if mention.additional_properties["mentioned"]["id"] == identifier:
mention_name_match = re.match(
r"<at(.*)>(.*?)<\/at>",
re.escape(mention.additional_properties["text"]),
escape(mention.additional_properties["text"]),
re.IGNORECASE,
)
if mention_name_match:
Expand Down
8 changes: 4 additions & 4 deletions libraries/botbuilder-core/tests/test_turn_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ def test_should_remove_at_mention_from_activity(self):

text = TurnContext.remove_recipient_mention(activity)

assert text, " test activity"
assert activity.text, " test activity"
assert text == " test activity"
assert activity.text == " test activity"

def test_should_remove_at_mention_with_regex_characters(self):
activity = Activity(
Expand All @@ -343,8 +343,8 @@ def test_should_remove_at_mention_with_regex_characters(self):

text = TurnContext.remove_recipient_mention(activity)

assert text, " test activity"
assert activity.text, " test activity"
assert text == " test activity"
assert activity.text == " test activity"

async def test_should_send_a_trace_activity(self):
context = TurnContext(SimpleAdapter(), ACTIVITY)
Expand Down