Skip to content

Commit 44af3b8

Browse files
authored
Merge pull request #747 from microsoft/pytest_36
3.6 tests failing due to remove_mention_text
2 parents 9a5e20a + cbbc76d commit 44af3b8

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
# SPECIAL_CHARS
5+
# closing ')', '}' and ']'
6+
# '-' (a range in character set)
7+
# '&', '~', (extended character set operations)
8+
# '#' (comment) and WHITESPACE (ignored) in verbose mode
9+
SPECIAL_CHARS_MAP = {i: "\\" + chr(i) for i in b"()[]{}?*+-|^$\\.&~# \t\n\r\v\f"}
10+
11+
12+
def escape(pattern):
13+
"""
14+
Escape special characters in a string.
15+
16+
This is a copy of the re.escape function in Python 3.8. This was done
17+
because the 3.6.x version didn't escape in the same way and handling
18+
bot names with regex characters in it would fail in TurnContext.remove_mention_text
19+
without escaping the text.
20+
"""
21+
if isinstance(pattern, str):
22+
return pattern.translate(SPECIAL_CHARS_MAP)
23+
24+
pattern = str(pattern, "latin1")
25+
return pattern.translate(SPECIAL_CHARS_MAP).encode("latin1")

libraries/botbuilder-core/botbuilder/core/turn_context.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Mention,
1414
ResourceResponse,
1515
)
16+
from .re_escape import escape
1617

1718

1819
class TurnContext:
@@ -362,7 +363,7 @@ def remove_mention_text(activity: Activity, identifier: str) -> str:
362363
if mention.additional_properties["mentioned"]["id"] == identifier:
363364
mention_name_match = re.match(
364365
r"<at(.*)>(.*?)<\/at>",
365-
re.escape(mention.additional_properties["text"]),
366+
escape(mention.additional_properties["text"]),
366367
re.IGNORECASE,
367368
)
368369
if mention_name_match:

libraries/botbuilder-core/tests/test_turn_context.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ def test_should_remove_at_mention_from_activity(self):
322322

323323
text = TurnContext.remove_recipient_mention(activity)
324324

325-
assert text, " test activity"
326-
assert activity.text, " test activity"
325+
assert text == " test activity"
326+
assert activity.text == " test activity"
327327

328328
def test_should_remove_at_mention_with_regex_characters(self):
329329
activity = Activity(
@@ -343,8 +343,8 @@ def test_should_remove_at_mention_with_regex_characters(self):
343343

344344
text = TurnContext.remove_recipient_mention(activity)
345345

346-
assert text, " test activity"
347-
assert activity.text, " test activity"
346+
assert text == " test activity"
347+
assert activity.text == " test activity"
348348

349349
async def test_should_send_a_trace_activity(self):
350350
context = TurnContext(SimpleAdapter(), ACTIVITY)

0 commit comments

Comments
 (0)