Skip to content

Commit

Permalink
Merge pull request #352 from realvitya/fix/read_callback-case-insensi…
Browse files Browse the repository at this point in the history
…tive-for-contains

fix read_callback user provided input case (in)sensitivity
  • Loading branch information
carlmontanari authored Oct 11, 2024
2 parents 6613d8a + f2472a8 commit 50680e8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions scrapli/driver/generic/base_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ def contains_bytes(self) -> bytes:
"""
if self.contains and not self._contains_bytes:
self._contains_bytes = self.contains.encode()
if self.case_insensitive:
self._contains_bytes = self._contains_bytes.lower()

return self._contains_bytes

Expand All @@ -143,6 +145,8 @@ def not_contains_bytes(self) -> bytes:
"""
if self.not_contains and not self._not_contains_bytes:
self._not_contains_bytes = self.not_contains.encode()
if self.case_insensitive:
self._not_contains_bytes = self._not_contains_bytes.lower()

return self._not_contains_bytes

Expand Down
9 changes: 8 additions & 1 deletion tests/unit/driver/generic/test_generic_async_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ async def callback_two(cls, read_output):
case_insensitive=False,
only_once=True,
),
ReadCallback(
contains="RTR1#",
callback=callback_one,
name="call1.5",
case_insensitive=True,
only_once=True,
),
ReadCallback(
contains_re=r"^rtr1#",
callback=callback_two,
Expand All @@ -151,5 +158,5 @@ async def callback_two(cls, read_output):

await async_generic_driver.read_callback(callbacks=callbacks, initial_input="nada")

assert callback_one_counter == 1
assert callback_one_counter == 2
assert callback_two_counter == 1
9 changes: 8 additions & 1 deletion tests/unit/driver/generic/test_generic_sync_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ def callback_two(cls, read_output):
case_insensitive=False,
only_once=True,
),
ReadCallback(
contains="RTR1#",
callback=callback_one,
name="call1.5",
case_insensitive=True,
only_once=True,
),
ReadCallback(
contains_re=r"^rtr1#",
callback=callback_two,
Expand All @@ -140,5 +147,5 @@ def callback_two(cls, read_output):

sync_generic_driver.read_callback(callbacks=callbacks, initial_input="nada")

assert callback_one_counter == 1
assert callback_one_counter == 2
assert callback_two_counter == 1

0 comments on commit 50680e8

Please sign in to comment.