4
4
5
5
import re
6
6
import time
7
- from collections .abc import Callable , Generator , Sequence
7
+ from collections .abc import Callable , Generator
8
8
from typing import TYPE_CHECKING
9
9
10
10
import pytest
@@ -402,7 +402,11 @@ def add_content() -> None:
402
402
thread .start ()
403
403
404
404
# 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
+ ]
406
410
result = wait_for_any_content (
407
411
wait_pane ,
408
412
patterns ,
@@ -436,7 +440,7 @@ def test_wait_for_any_content_mixed_match_types(wait_pane: Pane) -> None:
436
440
]
437
441
438
442
# One will be searched with CONTAINS, the other with REGEX
439
- patterns = [
443
+ patterns : list [ str | re . Pattern [ str ] | Callable [[ list [ str ]], bool ]] = [
440
444
"test line" , # String for CONTAINS
441
445
r"number \d+" , # Regex pattern for REGEX
442
446
]
@@ -495,9 +499,13 @@ def add_content() -> None:
495
499
thread .start ()
496
500
497
501
# Wait for all patterns to appear
502
+ patterns : list [str | re .Pattern [str ] | Callable [[list [str ]], bool ]] = [
503
+ "Database connected" ,
504
+ "Server started" ,
505
+ ]
498
506
result = wait_for_all_content (
499
507
wait_pane ,
500
- [ "Database connected" , "Server started" ] ,
508
+ patterns ,
501
509
ContentMatchType .CONTAINS ,
502
510
timeout = 3 ,
503
511
)
@@ -511,14 +519,8 @@ def add_content() -> None:
511
519
assert isinstance (matched_list , list )
512
520
513
521
# 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 )
522
524
523
525
524
526
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:
531
533
time .sleep (0.5 )
532
534
533
535
# 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
+ ]
535
540
536
541
# Without raising, it should return a failed result
537
542
result = wait_for_all_content (
@@ -564,7 +569,7 @@ def test_wait_for_all_content_mixed_match_types(wait_pane: Pane) -> None:
564
569
ContentMatchType .REGEX , # For regex match
565
570
]
566
571
567
- patterns = [
572
+ patterns : list [ str | re . Pattern [ str ] | Callable [[ list [ str ]], bool ]] = [
568
573
"contains" , # String for CONTAINS
569
574
r"number \d+" , # Regex pattern for REGEX
570
575
]
@@ -579,10 +584,17 @@ def test_wait_for_all_content_mixed_match_types(wait_pane: Pane) -> None:
579
584
580
585
assert result .success
581
586
assert isinstance (result .matched_content , list )
587
+ assert len (result .matched_content ) >= 2
582
588
583
589
# 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
586
598
587
599
588
600
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:
618
630
# Wait for content to ensure it's present
619
631
time .sleep (0.5 )
620
632
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
623
635
result = wait_for_pane_content (
624
636
wait_pane ,
625
637
test_content ,
@@ -750,7 +762,10 @@ def test_pane_content_waiter_wait_for_exact_text(wait_pane: Pane) -> None:
750
762
)
751
763
752
764
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
754
769
755
770
756
771
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:
765
780
)
766
781
767
782
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
773
787
774
788
775
789
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:
867
881
868
882
assert result .success is True
869
883
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
871
887
872
888
873
889
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:
953
969
)
954
970
955
971
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
957
976
assert result .match_line is not None
958
977
959
978
# 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:
972
991
wait_pane .send_keys ("echo 'Line 2'" , enter = True )
973
992
time .sleep (0.2 ) # Wait for commands to complete
974
993
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
+ ]
976
998
977
999
result = wait_for_all_content (
978
1000
wait_pane ,
@@ -983,7 +1005,10 @@ def test_wait_for_all_content_with_line_range(wait_pane: Pane) -> None:
983
1005
)
984
1006
985
1007
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 ])
987
1012
988
1013
989
1014
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:
993
1018
time .sleep (0.2 ) # Ensure the clear command completes
994
1019
995
1020
# 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
+ ]
997
1024
result = wait_for_all_content (
998
1025
wait_pane ,
999
1026
patterns ,
@@ -1123,7 +1150,10 @@ def has_ten_lines(content: list[str]) -> bool:
1123
1150
return len (content ) >= 10
1124
1151
1125
1152
# 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
+ ]
1127
1157
result = wait_for_any_content (
1128
1158
wait_pane ,
1129
1159
predicates ,
0 commit comments