File: test_parser.py

package info (click to toggle)
python-coverage 7.6.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,120 kB
  • sloc: python: 30,196; ansic: 1,181; javascript: 773; makefile: 293; sh: 107; xml: 48
file content (1251 lines) | stat: -rw-r--r-- 42,305 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt

"""Tests for coverage.py's code parsing."""

from __future__ import annotations

import textwrap

import pytest

from coverage import env
from coverage.exceptions import NotPython
from coverage.parser import PythonParser

from tests.coveragetest import CoverageTest
from tests.helpers import arcz_to_arcs, xfail_pypy38


class PythonParserTestBase(CoverageTest):
    """Tests for coverage.py's Python code parsing."""

    run_in_temp_dir = False

    def parse_text(self, text: str, exclude: str = "nocover") -> PythonParser:
        """Parse `text` as source, and return the `PythonParser` used."""
        text = textwrap.dedent(text)
        parser = PythonParser(text=text, exclude=exclude)
        parser.parse_source()
        return parser


class PythonParserTest(PythonParserTestBase):
    """Tests of coverage.parser."""

    def test_exit_counts(self) -> None:
        parser = self.parse_text("""\
            # check some basic branch counting
            class Foo:
                def foo(self, a):
                    if a:
                        return 5
                    else:
                        return 7

            class Bar:
                pass
            """)
        assert parser.exit_counts() == {
            2:1, 3:1, 4:2, 5:1, 7:1, 9:1, 10:1,
        }

    def test_generator_exit_counts(self) -> None:
        # https://github.com/nedbat/coveragepy/issues/324
        parser = self.parse_text("""\
            def gen(input):
                for n in inp:
                    yield (i * 2 for i in range(n))

            list(gen([1,2,3]))
            """)
        assert parser.exit_counts() == {
            1:1,    # def -> list
            2:2,    # for -> yield; for -> exit
            3:2,    # yield -> for;  genexp exit
            5:1,    # list -> exit
        }

    def test_try_except(self) -> None:
        parser = self.parse_text("""\
            try:
                a = 2
            except ValueError:
                a = 4
            except ZeroDivideError:
                a = 6
            except:
                a = 8
            b = 9
            """)
        assert parser.exit_counts() == {
            1: 1, 2:1, 3:2, 4:1, 5:2, 6:1, 7:1, 8:1, 9:1,
        }

    def test_excluded_classes(self) -> None:
        parser = self.parse_text("""\
            class Foo:
                def __init__(self):
                    pass

            if len([]):     # nocover
                class Bar:
                    pass
            """)
        assert parser.exit_counts() == {
            1:0, 2:1, 3:1,
        }

    def test_missing_branch_to_excluded_code(self) -> None:
        parser = self.parse_text("""\
            if fooey:
                a = 2
            else:   # nocover
                a = 4
            b = 5
            """)
        assert parser.exit_counts() == { 1:1, 2:1, 5:1 }
        parser = self.parse_text("""\
            def foo():
                if fooey:
                    a = 3
                else:
                    a = 5
            b = 6
            """)
        assert parser.exit_counts() == { 1:1, 2:2, 3:1, 5:1, 6:1 }
        parser = self.parse_text("""\
            def foo():
                if fooey:
                    a = 3
                else:   # nocover
                    a = 5
            b = 6
            """)
        assert parser.exit_counts() == { 1:1, 2:1, 3:1, 6:1 }

    @pytest.mark.parametrize("text", [
        pytest.param("0 spaces\n  2\n 1", id="bad_indent"),
        pytest.param("'''", id="string_eof"),
        pytest.param("$hello", id="dollar"),
        # on 3.10 this passes ast.parse but fails on tokenize.generate_tokens
        pytest.param(
            "\r'\\\n'''",
            id="leading_newline_eof",
            marks=[
                pytest.mark.skipif(env.PYVERSION >= (3, 12), reason="parses fine in 3.12"),
            ]
        )
    ])
    def test_not_python(self, text: str) -> None:
        msg = r"Couldn't parse '<code>' as Python source: '.*' at line \d+"
        with pytest.raises(NotPython, match=msg):
            _ = self.parse_text(text)

    def test_empty_decorated_function(self) -> None:
        parser = self.parse_text("""\
            def decorator(func):
                return func

            @decorator
            def foo(self):
                '''Docstring'''

            @decorator
            def bar(self):
                pass
            """)

        expected_statements = {1, 2, 4, 5, 8, 9, 10}
        expected_arcs = set(arcz_to_arcs(".1 14 45 58 89 9.  .2 2.  -8A A-8"))
        expected_exits = {1: 1, 2: 1, 4: 1, 5: 1, 8: 1, 9: 1, 10: 1}

        if env.PYBEHAVIOR.docstring_only_function:
            # 3.7 changed how functions with only docstrings are numbered.
            expected_arcs.update(set(arcz_to_arcs("-46 6-4")))
            expected_exits.update({6: 1})

        if env.PYBEHAVIOR.trace_decorator_line_again:
            expected_arcs.update(set(arcz_to_arcs("54 98")))
            expected_exits.update({9: 2, 5: 2})

        assert expected_statements == parser.statements
        assert expected_arcs == parser.arcs()
        assert expected_exits == parser.exit_counts()

    def test_module_docstrings(self) -> None:
        parser = self.parse_text("""\
            '''The docstring on line 1'''
            a = 2
            """)
        assert {2} == parser.statements

        parser = self.parse_text("""\
            # Docstring is not line 1
            '''The docstring on line 2'''
            a = 3
            """)
        assert {3} == parser.statements

    def test_fuzzed_double_parse(self) -> None:
        # https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=50381
        # The second parse used to raise `TypeError: 'NoneType' object is not iterable`
        msg = (
            r"(EOF in multi-line statement)"        # before 3.12.0b1
            + r"|(unmatched ']')"                   # after 3.12.0b1
        )
        with pytest.raises(NotPython, match=msg):
            self.parse_text("]")
        with pytest.raises(NotPython, match=msg):
            self.parse_text("]")


class ExclusionParserTest(PythonParserTestBase):
    """Tests for the exclusion code in PythonParser."""

    def test_simple(self) -> None:
        parser = self.parse_text("""\
            a = 1; b = 2

            if len([]):
                a = 4   # nocover
            """,
        )
        assert parser.statements == {1,3}

    def test_excluding_if_suite(self) -> None:
        parser = self.parse_text("""\
            a = 1; b = 2

            if len([]):     # nocover
                a = 4
                b = 5
                c = 6
            assert a == 1 and b == 2
            """,
        )
        assert parser.statements == {1,7}

    def test_excluding_if_but_not_else_suite(self) -> None:
        parser = self.parse_text("""\
            a = 1; b = 2

            if len([]):     # nocover
                a = 4
                b = 5
                c = 6
            else:
                a = 8
                b = 9
            assert a == 8 and b == 9
            """,
        )
        assert parser.statements == {1,8,9,10}

    def test_excluding_else_suite(self) -> None:
        parser = self.parse_text("""\
            a = 1; b = 2

            if 1==1:
                a = 4
                b = 5
                c = 6
            else:          # nocover
                a = 8
                b = 9
            assert a == 4 and b == 5 and c == 6
            """,
        )
        assert parser.statements == {1,3,4,5,6,10}
        parser = self.parse_text("""\
            a = 1; b = 2

            if 1==1:
                a = 4
                b = 5
                c = 6

            # Lots of comments to confuse the else handler.
            # more.

            else:          # nocover

            # Comments here too.

                a = 8
                b = 9
            assert a == 4 and b == 5 and c == 6
            """,
        )
        assert parser.statements == {1,3,4,5,6,17}

    def test_excluding_oneline_if(self) -> None:
        parser = self.parse_text("""\
            def foo():
                a = 2
                if len([]): x = 3       # nocover
                b = 4

            foo()
            """,
        )
        assert parser.statements == {1,2,4,6}

    def test_excluding_a_colon_not_a_suite(self) -> None:
        parser = self.parse_text("""\
            def foo():
                l = list(range(10))
                a = l[:3]   # nocover
                b = 4

            foo()
            """,
        )
        assert parser.statements == {1,2,4,6}

    def test_excluding_for_suite(self) -> None:
        parser = self.parse_text("""\
            a = 0
            for i in [1,2,3,4,5]:     # nocover
                a += i
            assert a == 15
            """,
        )
        assert parser.statements == {1,4}
        parser = self.parse_text("""\
            a = 0
            for i in [1,
                2,3,4,
                5]:                # nocover
                a += i
            assert a == 15
            """,
        )
        assert parser.statements == {1,6}
        parser = self.parse_text("""\
            a = 0
            for i in [1,2,3,4,5
                ]:                        # nocover
                a += i
                break
                a = 99
            assert a == 1
            """,
        )
        assert parser.statements == {1,7}

    def test_excluding_for_else(self) -> None:
        parser = self.parse_text("""\
            a = 0
            for i in range(5):
                a += i+1
                break
            else:               # nocover
                a = 123
            assert a == 1
            """,
        )
        assert parser.statements == {1,2,3,4,7}

    def test_excluding_while(self) -> None:
        parser = self.parse_text("""\
            a = 3; b = 0
            while a*b:           # nocover
                b += 1
                break
            assert a == 3 and b == 0
            """,
        )
        assert parser.statements == {1,5}
        parser = self.parse_text("""\
            a = 3; b = 0
            while (
                a*b
                ):           # nocover
                b += 1
                break
            assert a == 3 and b == 0
            """,
        )
        assert parser.statements == {1,7}

    def test_excluding_while_else(self) -> None:
        parser = self.parse_text("""\
            a = 3; b = 0
            while a:
                b += 1
                break
            else:           # nocover
                b = 123
            assert a == 3 and b == 1
            """,
        )
        assert parser.statements == {1,2,3,4,7}

    def test_excluding_try_except(self) -> None:
        parser = self.parse_text("""\
            a = 0
            try:
                a = 1
            except:           # nocover
                a = 99
            assert a == 1
            """,
        )
        assert parser.statements == {1,2,3,6}
        parser = self.parse_text("""\
            a = 0
            try:
                a = 1
                raise Exception("foo")
            except:
                a = 99
            assert a == 99
            """,
        )
        assert parser.statements == {1,2,3,4,5,6,7}
        parser = self.parse_text("""\
            a = 0
            try:
                a = 1
                raise Exception("foo")
            except ImportError:    # nocover
                a = 99
            except:
                a = 123
            assert a == 123
            """,
        )
        assert parser.statements == {1,2,3,4,7,8,9}

    def test_excluding_if_pass(self) -> None:
        # From a comment on the coverage.py page by Michael McNeil Forbes:
        parser = self.parse_text("""\
            def f():
                if False:    # pragma: nocover
                    pass     # This line still reported as missing
                if False:    # pragma: nocover
                    x = 1    # Now it is skipped.

            f()
            """,
        )
        assert parser.statements == {1,7}

    def test_multiline_if_no_branch(self) -> None:
        # From https://github.com/nedbat/coveragepy/issues/754
        parser = self.parse_text("""\
            if (this_is_a_verylong_boolean_expression == True   # pragma: no branch
                and another_long_expression and here_another_expression):
                do_something()
            """,
        )
        parser2 = self.parse_text("""\
            if this_is_a_verylong_boolean_expression == True and another_long_expression \\
                and here_another_expression:  # pragma: no branch
                do_something()
            """,
        )
        assert parser.statements == parser2.statements == {1, 3}
        pragma_re = ".*pragma: no branch.*"
        assert parser.lines_matching(pragma_re) == parser2.lines_matching(pragma_re)

    def test_excluding_function(self) -> None:
        parser = self.parse_text("""\
            def fn(foo):      # nocover
                a = 1
                b = 2
                c = 3

            x = 1
            assert x == 1
            """,
        )
        assert parser.statements == {6,7}
        parser = self.parse_text("""\
            a = 0
            def very_long_function_to_exclude_name(very_long_argument1,
            very_long_argument2):
                pass
            assert a == 0
            """,
            exclude="function_to_exclude",
        )
        assert parser.statements == {1,5}
        parser = self.parse_text("""\
            a = 0
            def very_long_function_to_exclude_name(
                very_long_argument1,
                very_long_argument2
            ):
                pass
            assert a == 0
            """,
            exclude="function_to_exclude",
        )
        assert parser.statements == {1,7}
        parser = self.parse_text("""\
            def my_func(
                super_long_input_argument_0=0,
                super_long_input_argument_1=1,
                super_long_input_argument_2=2):
                pass

            def my_func_2(super_long_input_argument_0=0, super_long_input_argument_1=1, super_long_input_argument_2=2):
                pass
            """,
            exclude="my_func",
        )
        assert parser.statements == set()
        parser = self.parse_text("""\
            def my_func(
                super_long_input_argument_0=0,
                super_long_input_argument_1=1,
                super_long_input_argument_2=2):
                pass

            def my_func_2(super_long_input_argument_0=0, super_long_input_argument_1=1, super_long_input_argument_2=2):
                pass
            """,
            exclude="my_func_2",
        )
        assert parser.statements == {1,5}
        parser = self.parse_text("""\
            def my_func       (
                super_long_input_argument_0=0,
                super_long_input_argument_1=1,
                super_long_input_argument_2=2):
                pass

            def my_func_2    (super_long_input_argument_0=0, super_long_input_argument_1=1, super_long_input_argument_2=2):
                pass
            """,
            exclude="my_func_2",
        )
        assert parser.statements == {1,5}
        parser = self.parse_text("""\
            def my_func       (
                super_long_input_argument_0=0,
                super_long_input_argument_1=1,
                super_long_input_argument_2=2):
                pass

            def my_func_2    (super_long_input_argument_0=0, super_long_input_argument_1=1, super_long_input_argument_2=2):
                pass
            """,
            exclude="my_func",
        )
        assert parser.statements == set()
        parser = self.parse_text("""\
            def my_func \
                (
                super_long_input_argument_0=0,
                super_long_input_argument_1=1
                ):
                pass

            def my_func_2(super_long_input_argument_0=0, super_long_input_argument_1=1, super_long_input_argument_2=2):
                pass
            """,
            exclude="my_func_2",
        )
        assert parser.statements == {1,5}
        parser = self.parse_text("""\
            def my_func \
                (
                super_long_input_argument_0=0,
                super_long_input_argument_1=1
                ):
                pass

            def my_func_2(super_long_input_argument_0=0, super_long_input_argument_1=1, super_long_input_argument_2=2):
                pass
            """,
            exclude="my_func",
        )
        assert parser.statements == set()

    def test_excluding_bug1713(self) -> None:
        if env.PYVERSION >= (3, 10):
            parser = self.parse_text("""\
                print("1")

                def hello_3(a):  # pragma: nocover
                    match a:
                        case ("5"
                              | "6"):
                            print("7")
                        case "8":
                            print("9")

                print("11")
                """,
            )
            assert parser.statements == {1, 11}
        parser = self.parse_text("""\
            print("1")

            def hello_3(a):  # nocover
                if ("4" or
                    "5"):
                    print("6")
                else:
                    print("8")

            print("10")
            """,
        )
        assert parser.statements == {1, 10}
        parser = self.parse_text("""\
            print(1)

            def func(a, b):
                if a == 4:      # nocover
                    func5()
                    if b:
                        print(7)
                    func8()

            print(10)
            """,
        )
        assert parser.statements == {1, 3, 10}
        parser = self.parse_text("""\
            class Foo:  # pragma: nocover
                def greet(self):
                    print("hello world")
            """,
        )
        assert parser.statements == set()

    def test_excluding_method(self) -> None:
        parser = self.parse_text("""\
            class Fooey:
                def __init__(self):
                    self.a = 1

                def foo(self):     # nocover
                    return self.a

            x = Fooey()
            assert x.a == 1
            """,
        )
        assert parser.statements == {1,2,3,8,9}
        parser = self.parse_text("""\
            class Fooey:
                def __init__(self):
                    self.a = 1

                def very_long_method_to_exclude_name(
                    very_long_argument1,
                    very_long_argument2
                ):
                    pass

            x = Fooey()
            assert x.a == 1
            """,
            exclude="method_to_exclude",
        )
        assert parser.statements == {1,2,3,11,12}

    def test_excluding_class(self) -> None:
        parser = self.parse_text("""\
            class Fooey:            # nocover
                def __init__(self):
                    self.a = 1

                def foo(self):
                    return self.a

            x = 1
            assert x == 1
            """,
        )
        assert parser.statements == {8,9}

    def test_excludes_non_ascii(self) -> None:
        parser = self.parse_text("""\
            # coding: utf-8
            a = 1; b = 2

            if len([]):
                a = 5   # ✘cover
            """,
            exclude="✘cover",
        )
        assert parser.statements == {2, 4}

    def test_formfeed(self) -> None:
        # https://github.com/nedbat/coveragepy/issues/461
        parser = self.parse_text("""\
            x = 1
            assert len([]) == 0, (
                "This won't happen %s" % ("hello",)
            )
            \f
            x = 6
            assert len([]) == 0, (
                "This won't happen %s" % ("hello",)
            )
            """,
            exclude="assert",
        )
        assert parser.statements == {1, 6}

    @xfail_pypy38
    def test_decorator_pragmas(self) -> None:
        parser = self.parse_text("""\
            # 1

            @foo(3)                     # nocover
            @bar
            def func(x, y=5):
                return 6

            class Foo:      # this is the only statement.
                '''9'''
                @foo                    # nocover
                def __init__(self):
                    '''12'''
                    return 13

                @foo(                   # nocover
                    16,
                    17,
                )
                def meth(self):
                    return 20

            @foo(                       # nocover
                23
            )
            def func(x=25):
                return 26
            """)
        raw_statements = {3, 4, 5, 6, 8, 9, 10, 11, 13, 15, 16, 17, 19, 20, 22, 23, 25, 26}
        assert parser.raw_statements == raw_statements
        assert parser.statements == {8}

    @xfail_pypy38
    def test_decorator_pragmas_with_colons(self) -> None:
        # A colon in a decorator expression would confuse the parser,
        # ending the exclusion of the decorated function.
        parser = self.parse_text("""\
            @decorate(X)        # nocover
            @decorate("Hello"[2])
            def f():
                x = 4

            @decorate(X)        # nocover
            @decorate("Hello"[:7])
            def g():
                x = 9
            """)
        raw_statements = {1, 2, 3, 4, 6, 7, 8, 9}
        assert parser.raw_statements == raw_statements
        assert parser.statements == set()

    @pytest.mark.xfail(
        env.PYPY and env.PYVERSION[:2] == (3, 8),
        reason="AST doesn't mark end of classes correctly",
    )
    def test_class_decorator_pragmas(self) -> None:
        parser = self.parse_text("""\
            class Foo(object):
                def __init__(self):
                    self.x = 3

            @foo                        # nocover
            class Bar(object):
                def __init__(self):
                    self.x = 8
            """)
        assert parser.raw_statements == {1, 2, 3, 5, 6, 7, 8}
        assert parser.statements == {1, 2, 3}

    def test_over_exclusion_bug1779(self) -> None:
        # https://github.com/nedbat/coveragepy/issues/1779
        parser = self.parse_text("""\
            import abc

            class MyProtocol:               # nocover 3
                @abc.abstractmethod         # nocover 4
                def my_method(self) -> int:
                    ...     # 6

            def function() -> int:
                return 9
            """)
        assert parser.raw_statements == {1, 3, 4, 5, 6, 8, 9}
        assert parser.statements == {1, 8, 9}

    def test_multiline_exclusion_single_line(self) -> None:
        regex = r"print\('.*'\)"
        parser = self.parse_text("""\
            def foo():
                print('Hello, world!')
            """, regex)
        assert parser.lines_matching(regex) == {2}
        assert parser.raw_statements == {1, 2}
        assert parser.statements == {1}

    def test_multiline_exclusion_suite(self) -> None:
        # A multi-line exclusion that matches a colon line still excludes the entire block.
        regex = r"if T:\n\s+print\('Hello, world!'\)"
        parser = self.parse_text("""\
            def foo():
                if T:
                    print('Hello, world!')
                    print('This is a multiline regex test.')
            a = 5
            """, regex)
        assert parser.lines_matching(regex) == {2, 3}
        assert parser.raw_statements == {1, 2, 3, 4, 5}
        assert parser.statements == {1, 5}

    def test_multiline_exclusion_no_match(self) -> None:
        regex = r"nonexistent"
        parser = self.parse_text("""\
            def foo():
                print('Hello, world!')
            """, regex)
        assert parser.lines_matching(regex) == set()
        assert parser.raw_statements == {1, 2}
        assert parser.statements == {1, 2}

    def test_multiline_exclusion_no_source(self) -> None:
        regex = r"anything"
        parser = PythonParser(text="", filename="dummy.py", exclude=regex)
        assert parser.lines_matching(regex) == set()
        assert parser.raw_statements == set()
        assert parser.statements == set()

    def test_multiline_exclusion_all_lines_must_match(self) -> None:
        # https://github.com/nedbat/coveragepy/issues/996
        regex = r"except ValueError:\n\s*print\('false'\)"
        parser = self.parse_text("""\
            try:
                a = 2
                print('false')
            except ValueError:
                print('false')
            except ValueError:
                print('something else')
            except IndexError:
                print('false')
            """, regex)
        assert parser.lines_matching(regex) == {4, 5}
        assert parser.raw_statements == {1, 2, 3, 4, 5, 6, 7, 8, 9}
        assert parser.statements == {1, 2, 3, 6, 7, 8, 9}

    def test_multiline_exclusion_multiple_matches(self) -> None:
        regex = r"print\('.*'\)\n\s+. = \d"
        parser = self.parse_text("""\
            def foo():
                print('Hello, world!')
                a = 5
            def bar():
                print('Hello again!')
                b = 6
            """, regex)
        assert parser.lines_matching(regex) == {2, 3, 5, 6}
        assert parser.raw_statements == {1, 2, 3, 4, 5, 6}
        assert parser.statements == {1, 4}

    def test_multiline_exclusion_suite2(self) -> None:
        regex = r"print\('Hello, world!'\)\n\s+if T:"
        parser = self.parse_text("""\
            def foo():
                print('Hello, world!')
                if T:
                    print('This is a test.')
            """, regex)
        assert parser.lines_matching(regex) == {2, 3}
        assert parser.raw_statements == {1, 2, 3, 4}
        assert parser.statements == {1}

    def test_multiline_exclusion_match_all(self) -> None:
        regex = (
            r"def foo\(\):\n\s+print\('Hello, world!'\)\n"
            + r"\s+if T:\n\s+print\('This is a test\.'\)"
        )
        parser = self.parse_text("""\
            def foo():
                print('Hello, world!')
                if T:
                    print('This is a test.')
            """, regex)
        assert parser.lines_matching(regex) == {1, 2, 3, 4}
        assert parser.raw_statements == {1, 2, 3, 4}
        assert parser.statements == set()

    def test_multiline_exclusion_block(self) -> None:
        # https://github.com/nedbat/coveragepy/issues/1803
        regex = "# no cover: start(?s:.)*?# no cover: stop"
        parser = self.parse_text("""\
            a = my_function1()
            if debug:
                msg = "blah blah"
                # no cover: start
                log_message(msg, a)
                b = my_function2()
                # no cover: stop
            """, regex)
        assert parser.lines_matching(regex) == {4, 5, 6, 7}
        assert parser.raw_statements == {1, 2, 3, 5, 6}
        assert parser.statements == {1, 2, 3}

    @pytest.mark.skipif(not env.PYBEHAVIOR.match_case, reason="Match-case is new in 3.10")
    def test_multiline_exclusion_block2(self) -> None:
        # https://github.com/nedbat/coveragepy/issues/1797
        regex = r"case _:\n\s+assert_never\("
        parser = self.parse_text("""\
            match something:
                case type_1():
                    logic_1()
                case type_2():
                    logic_2()
                case _:
                    assert_never(something)
            match something:
                case type_1():
                    logic_1()
                case type_2():
                    logic_2()
                case _:
                    print("Default case")
            """, regex)
        assert parser.lines_matching(regex) == {6, 7}
        assert parser.raw_statements == {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
        assert parser.statements == {1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 14}

    def test_multiline_exclusion_block3(self) -> None:
        # https://github.com/nedbat/coveragepy/issues/1741
        # This will only work if there's exactly one return statement in the rest of the function
        regex = r"# no cover: to return(?s:.)*?return"
        parser = self.parse_text("""\
            def my_function(args, j):
                if args.command == Commands.CMD.value:
                    return cmd_handler(j, args)
                # no cover: to return
                print(f"Command '{args.command}' was not handled.", file=sys.stderr)
                parser.print_help(file=sys.stderr)

                return os.EX_USAGE
            print("not excluded")
            """, regex)
        assert parser.lines_matching(regex) == {4, 5, 6, 7, 8}
        assert parser.raw_statements == {1, 2, 3, 5, 6, 8, 9}
        assert parser.statements == {1, 2, 3, 9}

    def test_multiline_exclusion_whole_source(self) -> None:
        # https://github.com/nedbat/coveragepy/issues/118
        regex = r"(?s)\A.*# pragma: exclude file.*\Z"
        parser = self.parse_text("""\
            import coverage
            # pragma: exclude file
            def the_void() -> None:
                if "py" not in __file__:
                    print("Not a Python file.")
                print("Everything here is excluded.")

                return
            print("Excluded too")
            """, regex)
        assert parser.lines_matching(regex) == {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
        assert parser.raw_statements == {1, 3, 4, 5, 6, 8, 9}
        assert parser.statements == set()

    def test_multiline_exclusion_from_marker(self) -> None:
        # https://github.com/nedbat/coveragepy/issues/118
        regex = r"# pragma: rest of file(?s:.)*\Z"
        parser = self.parse_text("""\
            import coverage
            # pragma: rest of file
            def the_void() -> None:
                if "py" not in __file__:
                    print("Not a Python file.")
                print("Everything here is excluded.")

                return
            print("Excluded too")
            """, regex)
        assert parser.lines_matching(regex) == {2, 3, 4, 5, 6, 7, 8, 9, 10}
        assert parser.raw_statements == {1, 3, 4, 5, 6, 8, 9}
        assert parser.statements == {1}


class ParserMissingArcDescriptionTest(PythonParserTestBase):
    """Tests for PythonParser.missing_arc_description."""

    def test_missing_arc_description(self) -> None:
        # This code is never run, so the actual values don't matter.
        parser = self.parse_text("""\
            if x:
                print(2)
            print(3)

            def func5():
                for x in range(6):
                    if x == 7:
                        break

            def func10():
                while something(11):
                    thing(12)
                more_stuff(13)
            """)
        expected = "line 1 didn't jump to line 2 because the condition on line 1 was never true"
        assert expected == parser.missing_arc_description(1, 2)
        expected = "line 1 didn't jump to line 3 because the condition on line 1 was always true"
        assert expected == parser.missing_arc_description(1, 3)
        expected = (
            "line 6 didn't return from function 'func5' " +
            "because the loop on line 6 didn't complete"
        )
        assert expected == parser.missing_arc_description(6, -5)
        expected = "line 6 didn't jump to line 7 because the loop on line 6 never started"
        assert expected == parser.missing_arc_description(6, 7)
        expected = "line 11 didn't jump to line 12 because the condition on line 11 was never true"
        assert expected == parser.missing_arc_description(11, 12)
        expected = (
            "line 11 didn't jump to line 13 " +
            "because the condition on line 11 was always true"
        )
        assert expected == parser.missing_arc_description(11, 13)

    def test_missing_arc_descriptions_for_small_callables(self) -> None:
        parser = self.parse_text("""\
            callables = [
                lambda: 2,
                (x for x in range(3)),
                {x:1 for x in range(4)},
                {x for x in range(5)},
            ]
            x = 7
            """)
        expected = "line 2 didn't finish the lambda on line 2"
        assert expected == parser.missing_arc_description(2, -2)
        expected = "line 3 didn't finish the generator expression on line 3"
        assert expected == parser.missing_arc_description(3, -3)
        if env.PYBEHAVIOR.comprehensions_are_functions:
            expected = "line 4 didn't finish the dictionary comprehension on line 4"
            assert expected == parser.missing_arc_description(4, -4)
            expected = "line 5 didn't finish the set comprehension on line 5"
            assert expected == parser.missing_arc_description(5, -5)

    def test_missing_arc_descriptions_for_exceptions(self) -> None:
        parser = self.parse_text("""\
            try:
                pass
            except ZeroDivideError:
                print("whoops")
            except ValueError:
                print("yikes")
            """)
        expected = (
            "line 3 didn't jump to line 4 " +
            "because the exception caught by line 3 didn't happen"
        )
        assert expected == parser.missing_arc_description(3, 4)
        expected = (
            "line 5 didn't jump to line 6 " +
            "because the exception caught by line 5 didn't happen"
        )
        assert expected == parser.missing_arc_description(5, 6)

    def test_missing_arc_descriptions_for_finally(self) -> None:
        parser = self.parse_text("""\
            def function():
                for i in range(2):
                    try:
                        if something(4):
                            break
                        elif something(6):
                            x = 7
                        else:
                            if something(9):
                                continue
                            else:
                                continue
                        if also_this(13):
                            return 14
                        else:
                            raise Exception(16)
                    finally:
                        this_thing(18)
                that_thing(19)
            """)
        if env.PYBEHAVIOR.finally_jumps_back:
            expected = "line 18 didn't jump to line 5 because the break on line 5 wasn't executed"
            assert expected == parser.missing_arc_description(18, 5)
            expected = "line 5 didn't jump to line 19 because the break on line 5 wasn't executed"
            assert expected == parser.missing_arc_description(5, 19)
            expected = (
                "line 18 didn't jump to line 10 " +
                "because the continue on line 10 wasn't executed"
            )
            assert expected == parser.missing_arc_description(18, 10)
            expected = (
                "line 10 didn't jump to line 2 " +
                "because the continue on line 10 wasn't executed"
            )
            assert expected == parser.missing_arc_description(10, 2)
            expected = (
                "line 18 didn't jump to line 14 " +
                "because the return on line 14 wasn't executed"
            )
            assert expected == parser.missing_arc_description(18, 14)
            expected = (
                "line 14 didn't return from function 'function' " +
                "because the return on line 14 wasn't executed"
            )
            assert expected == parser.missing_arc_description(14, -1)
            expected = (
                "line 18 didn't except from function 'function' " +
                "because the raise on line 16 wasn't executed"
            )
            assert expected == parser.missing_arc_description(18, -1)
        else:
            expected = (
                "line 18 didn't jump to line 19 " +
                "because the break on line 5 wasn't executed"
            )
            assert expected == parser.missing_arc_description(18, 19)
            expected = (
                "line 18 didn't jump to line 2 " +
                    "because the continue on line 10 wasn't executed" +
                " or " +
                    "the continue on line 12 wasn't executed"
            )
            assert expected == parser.missing_arc_description(18, 2)
            expected = (
                "line 18 didn't except from function 'function' " +
                    "because the raise on line 16 wasn't executed" +
                " or " +
                "line 18 didn't return from function 'function' " +
                    "because the return on line 14 wasn't executed"
            )
            assert expected == parser.missing_arc_description(18, -1)

    def test_missing_arc_descriptions_bug460(self) -> None:
        parser = self.parse_text("""\
            x = 1
            d = {
                3: lambda: [],
                4: lambda: [],
            }
            x = 6
            """)
        assert parser.missing_arc_description(2, -3) == "line 3 didn't finish the lambda on line 3"


@pytest.mark.skipif(not env.PYBEHAVIOR.match_case, reason="Match-case is new in 3.10")
class MatchCaseMissingArcDescriptionTest(PythonParserTestBase):
    """Missing arc descriptions for match/case."""

    def test_match_case(self) -> None:
        parser = self.parse_text("""\
            match command.split():
                case ["go", direction] if direction in "nesw":      # 2
                    match = f"go: {direction}"
                case ["go", _]:                                     # 4
                    match = "no go"
            print(match)                                            # 6
            """)
        assert parser.missing_arc_description(2, 3) == (
            "line 2 didn't jump to line 3 because the pattern on line 2 never matched"
        )
        assert parser.missing_arc_description(2, 4) == (
            "line 2 didn't jump to line 4 because the pattern on line 2 always matched"
        )
        assert parser.missing_arc_description(4, 6) == (
            "line 4 didn't jump to line 6 because the pattern on line 4 always matched"
        )

    def test_final_wildcard(self) -> None:
        parser = self.parse_text("""\
            match command.split():
                case ["go", direction] if direction in "nesw":      # 2
                    match = f"go: {direction}"
                case _:                                             # 4
                    match = "no go"
            print(match)                                            # 6
            """)
        assert parser.missing_arc_description(2, 3) == (
            "line 2 didn't jump to line 3 because the pattern on line 2 never matched"
        )
        assert parser.missing_arc_description(2, 4) == (
            "line 2 didn't jump to line 4 because the pattern on line 2 always matched"
        )
        # 4-6 isn't a possible arc, so the description is generic.
        assert parser.missing_arc_description(4, 6) == "line 4 didn't jump to line 6"


class ParserFileTest(CoverageTest):
    """Tests for coverage.py's code parsing from files."""

    def parse_file(self, filename: str) -> PythonParser:
        """Parse `text` as source, and return the `PythonParser` used."""
        parser = PythonParser(filename=filename, exclude="nocover")
        parser.parse_source()
        return parser

    @pytest.mark.parametrize("slug, newline", [
        ("unix", "\n"), ("dos", "\r\n"), ("mac", "\r"),
    ])
    def test_line_endings(self, slug: str, newline: str) -> None:
        text = """\
            # check some basic branch counting
            class Foo:
                def foo(self, a):
                    if a:
                        return 5
                    else:
                        return 7

            class Bar:
                pass
            """
        counts = { 2:1, 3:1, 4:2, 5:1, 7:1, 9:1, 10:1 }
        fname = slug + ".py"
        self.make_file(fname, text, newline=newline)
        parser = self.parse_file(fname)
        assert parser.exit_counts() == counts, f"Wrong for {fname!r}"

    def test_encoding(self) -> None:
        self.make_file("encoded.py", """\
            coverage = "\xe7\xf6v\xear\xe3g\xe9"
            """)
        parser = self.parse_file("encoded.py")
        assert parser.exit_counts() == {1: 1}

    def test_missing_line_ending(self) -> None:
        # Test that the set of statements is the same even if a final
        # multi-line statement has no final newline.
        # https://github.com/nedbat/coveragepy/issues/293

        self.make_file("normal.py", """\
            out, err = subprocess.Popen(
                [sys.executable, '-c', 'pass'],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE).communicate()
            """)

        parser = self.parse_file("normal.py")
        assert parser.statements == {1}

        self.make_file("abrupt.py", """\
            out, err = subprocess.Popen(
                [sys.executable, '-c', 'pass'],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE).communicate()""")   # no final newline.

        # Double-check that some test helper wasn't being helpful.
        with open("abrupt.py") as f:
            assert f.read()[-1] == ")"

        parser = self.parse_file("abrupt.py")
        assert parser.statements == {1}