summaryrefslogtreecommitdiff
path: root/tests/test_annotate.py
blob: 257094f7363af27d5188c918fac88e06e6f8c098 (plain)
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
# 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 annotation from coverage.py."""

from __future__ import annotations

import coverage

from tests.coveragetest import CoverageTest
from tests.goldtest import compare, gold_path


class AnnotationGoldTest(CoverageTest):
    """Test the annotate feature with gold files."""

    def make_multi(self) -> None:
        """Make a few source files we need for the tests."""
        self.make_file("multi.py", """\
            import a.a
            import b.b

            a.a.a(1)
            b.b.b(2)
            """)
        self.make_file("a/__init__.py")
        self.make_file("a/a.py", """\
            def a(x):
                if x == 1:
                    print("x is 1")
                else:
                    print("x is not 1")
            """)
        self.make_file("b/__init__.py")
        self.make_file("b/b.py", """\
            def b(x):
                msg = f"x is {x}"
                print(msg)
            """)

    def test_multi(self) -> None:
        self.make_multi()
        cov = coverage.Coverage()
        self.start_import_stop(cov, "multi")
        cov.annotate()

        compare(gold_path("annotate/multi"), ".", "*,cover")

    def test_annotate_dir(self) -> None:
        self.make_multi()
        cov = coverage.Coverage(source=["."])
        self.start_import_stop(cov, "multi")
        cov.annotate(directory="out_anno_dir")

        compare(gold_path("annotate/anno_dir"), "out_anno_dir", "*,cover")

    def test_encoding(self) -> None:
        self.make_file("utf8.py", """\
            # -*- coding: utf-8 -*-
            # This comment has an accent: é

            print("spam eggs")
            """)
        cov = coverage.Coverage()
        self.start_import_stop(cov, "utf8")
        cov.annotate()
        compare(gold_path("annotate/encodings"), ".", "*,cover")

    def test_white(self) -> None:
        self.make_file("white.py", """\
            # A test case sent to me by Steve White

            def f(self):
                if self==1:
                    pass
                elif self.m('fred'):
                    pass
                elif (g==1) and (b==2):
                    pass
                elif self.m('fred')==True:
                    pass
                elif ((g==1) and (b==2))==True:
                    pass
                else:
                    pass

            def g(x):
                if x == 1:
                    a = 1
                else:
                    a = 2

            g(1)

            def h(x):
                if 0:   #pragma: no cover
                    pass
                if x == 1:
                    a = 1
                else:
                    a = 2

            h(2)
            """)

        cov = coverage.Coverage()
        self.start_import_stop(cov, "white")
        cov.annotate()
        compare(gold_path("annotate/white"), ".", "*,cover")

    def test_missing_after_else(self) -> None:
        self.make_file("mae.py", """\
            def f(x):
                if x == 1:
                    print("1")
                else:
                    print("2")

            if f(1):
                print("nope")
            if f(2):
                print("nope")
            """)

        cov = coverage.Coverage()
        self.start_import_stop(cov, "mae")
        cov.annotate()
        assert self.stdout() == (
            "1\n" +
            "2\n" +
            "The annotate command will be removed in a future version.\n" +
            "Get in touch if you still use it: ned@nedbatchelder.com\n"
        )
        compare(gold_path("annotate/mae"), ".", "*,cover")