Skip to content

Commit 2f2943e

Browse files
committed
fix(tests): properly fix linting and type checking issues in waiter tests
- Fixed all mypy type errors by using proper type annotations - Updated string | list handling to avoid 'in' operator type issues - Fixed line length issues - Cleaned up imports - Maintained test coverage at 70% for waiter module
1 parent 4ed1e5e commit 2f2943e

File tree

1 file changed

+60
-30
lines changed

1 file changed

+60
-30
lines changed

tests/test/test_waiter.py

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import re
66
import time
7-
from collections.abc import Callable, Generator, Sequence
7+
from collections.abc import Callable, Generator
88
from typing import TYPE_CHECKING
99

1010
import pytest
@@ -402,7 +402,11 @@ def add_content() -> None:
402402
thread.start()
403403

404404
# Wait for any of these patterns
405-
patterns: Sequence[str] = ["Success", "Error:", "timeout"]
405+
patterns: list[str | re.Pattern[str] | Callable[[list[str]], bool]] = [
406+
"Success",
407+
"Error:",
408+
"timeout",
409+
]
406410
result = wait_for_any_content(
407411
wait_pane,
408412
patterns,
@@ -436,7 +440,7 @@ def test_wait_for_any_content_mixed_match_types(wait_pane: Pane) -> None:
436440
]
437441

438442
# One will be searched with CONTAINS, the other with REGEX
439-
patterns = [
443+
patterns: list[str | re.Pattern[str] | Callable[[list[str]], bool]] = [
440444
"test line", # String for CONTAINS
441445
r"number \d+", # Regex pattern for REGEX
442446
]
@@ -495,9 +499,13 @@ def add_content() -> None:
495499
thread.start()
496500

497501
# Wait for all patterns to appear
502+
patterns: list[str | re.Pattern[str] | Callable[[list[str]], bool]] = [
503+
"Database connected",
504+
"Server started",
505+
]
498506
result = wait_for_all_content(
499507
wait_pane,
500-
["Database connected", "Server started"],
508+
patterns,
501509
ContentMatchType.CONTAINS,
502510
timeout=3,
503511
)
@@ -511,14 +519,8 @@ def add_content() -> None:
511519
assert isinstance(matched_list, list)
512520

513521
# Check that both strings are in the matched patterns
514-
assert any(
515-
"Database connected" in item
516-
for item in matched_list
517-
if isinstance(item, str)
518-
)
519-
assert any(
520-
"Server started" in item for item in matched_list if isinstance(item, str)
521-
)
522+
assert any("Database connected" in str(item) for item in matched_list)
523+
assert any("Server started" in str(item) for item in matched_list)
522524

523525

524526
def test_wait_for_all_content_no_raise(wait_pane: Pane) -> None:
@@ -531,7 +533,10 @@ def test_wait_for_all_content_no_raise(wait_pane: Pane) -> None:
531533
time.sleep(0.5)
532534

533535
# Look for one pattern that exists and one that doesn't
534-
patterns = ["Found text", "this will never be found in a million years"]
536+
patterns: list[str | re.Pattern[str] | Callable[[list[str]], bool]] = [
537+
"Found text",
538+
"this will never be found in a million years",
539+
]
535540

536541
# Without raising, it should return a failed result
537542
result = wait_for_all_content(
@@ -564,7 +569,7 @@ def test_wait_for_all_content_mixed_match_types(wait_pane: Pane) -> None:
564569
ContentMatchType.REGEX, # For regex match
565570
]
566571

567-
patterns = [
572+
patterns: list[str | re.Pattern[str] | Callable[[list[str]], bool]] = [
568573
"contains", # String for CONTAINS
569574
r"number \d+", # Regex pattern for REGEX
570575
]
@@ -579,10 +584,17 @@ def test_wait_for_all_content_mixed_match_types(wait_pane: Pane) -> None:
579584

580585
assert result.success
581586
assert isinstance(result.matched_content, list)
587+
assert len(result.matched_content) >= 2
582588

583589
# The first match should be "contains" and the second should contain "number"
584-
assert "contains" in result.matched_content[0]
585-
assert "number" in result.matched_content[1]
590+
first_match = str(result.matched_content[0])
591+
second_match = str(result.matched_content[1])
592+
593+
assert result.matched_content[0] is not None
594+
assert "contains" in first_match
595+
596+
assert result.matched_content[1] is not None
597+
assert "number" in second_match
586598

587599

588600
def test_wait_for_all_content_type_error(wait_pane: Pane) -> None:
@@ -618,8 +630,8 @@ def test_wait_for_pane_content_exact_match(wait_pane: Pane) -> None:
618630
# Wait for content to ensure it's present
619631
time.sleep(0.5)
620632

621-
# Instead of trying exact match on a line (which is prone to shell prompt variations)
622-
# Let's test if the content contains our string
633+
# Instead of trying exact match on a line (which is prone to shell prompt
634+
# variations) Let's test if the content contains our string
623635
result = wait_for_pane_content(
624636
wait_pane,
625637
test_content,
@@ -750,7 +762,10 @@ def test_pane_content_waiter_wait_for_exact_text(wait_pane: Pane) -> None:
750762
)
751763

752764
assert result.success
753-
assert "Exact Test" in result.matched_content if result.matched_content else False
765+
assert result.matched_content is not None
766+
matched_content = result.matched_content
767+
if matched_content is not None:
768+
assert "Exact Test" in matched_content
754769

755770

756771
def test_pane_content_waiter_wait_for_regex(wait_pane: Pane) -> None:
@@ -765,11 +780,10 @@ def test_pane_content_waiter_wait_for_regex(wait_pane: Pane) -> None:
765780
)
766781

767782
assert result.success
768-
assert (
769-
"Pattern 123 Test" in result.matched_content
770-
if result.matched_content
771-
else False
772-
)
783+
assert result.matched_content is not None
784+
matched_content = result.matched_content
785+
if matched_content is not None:
786+
assert "Pattern 123 Test" in matched_content
773787

774788

775789
def test_pane_content_waiter_wait_for_predicate(wait_pane: Pane) -> None:
@@ -867,7 +881,9 @@ def test_pane_content_waiter_with_line_range(wait_pane: Pane) -> None:
867881

868882
assert result.success is True
869883
assert result.matched_content is not None
870-
assert "target-text" in result.matched_content
884+
matched_content = result.matched_content
885+
if matched_content is not None:
886+
assert "target-text" in matched_content
871887

872888

873889
def test_pane_content_waiter_wait_until_ready(wait_pane: Pane) -> None:
@@ -953,7 +969,10 @@ def test_wait_for_pane_content_regex_line_match(wait_pane: Pane) -> None:
953969
)
954970

955971
assert result.success is True
956-
assert "pattern abc123" in result.matched_content
972+
assert result.matched_content is not None
973+
matched_content = result.matched_content
974+
if matched_content is not None:
975+
assert "pattern abc123" in matched_content
957976
assert result.match_line is not None
958977

959978
# The match should be on the second line we added
@@ -972,7 +991,10 @@ def test_wait_for_all_content_with_line_range(wait_pane: Pane) -> None:
972991
wait_pane.send_keys("echo 'Line 2'", enter=True)
973992
time.sleep(0.2) # Wait for commands to complete
974993

975-
patterns: Sequence[str] = ["Line 1", "Line 2"]
994+
patterns: list[str | re.Pattern[str] | Callable[[list[str]], bool]] = [
995+
"Line 1",
996+
"Line 2",
997+
]
976998

977999
result = wait_for_all_content(
9781000
wait_pane,
@@ -983,7 +1005,10 @@ def test_wait_for_all_content_with_line_range(wait_pane: Pane) -> None:
9831005
)
9841006

9851007
assert result.success
986-
assert result.matched_content == patterns
1008+
assert result.matched_content is not None
1009+
assert len(result.matched_content) == 2
1010+
assert "Line 1" in str(result.matched_content[0])
1011+
assert "Line 2" in str(result.matched_content[1])
9871012

9881013

9891014
def test_wait_for_all_content_timeout(wait_pane: Pane) -> None:
@@ -993,7 +1018,9 @@ def test_wait_for_all_content_timeout(wait_pane: Pane) -> None:
9931018
time.sleep(0.2) # Ensure the clear command completes
9941019

9951020
# Pattern that won't be found in the pane content
996-
patterns: Sequence[str] = ["pattern that doesn't exist"]
1021+
patterns: list[str | re.Pattern[str] | Callable[[list[str]], bool]] = [
1022+
"pattern that doesn't exist"
1023+
]
9971024
result = wait_for_all_content(
9981025
wait_pane,
9991026
patterns,
@@ -1123,7 +1150,10 @@ def has_ten_lines(content: list[str]) -> bool:
11231150
return len(content) >= 10
11241151

11251152
# Test with predicates
1126-
predicates: Sequence[Callable[[list[str]], bool]] = [has_two_lines, has_ten_lines]
1153+
predicates: list[str | re.Pattern[str] | Callable[[list[str]], bool]] = [
1154+
has_two_lines,
1155+
has_ten_lines,
1156+
]
11271157
result = wait_for_any_content(
11281158
wait_pane,
11291159
predicates,

0 commit comments

Comments
 (0)