summaryrefslogtreecommitdiff
path: root/tests/test_xml.py
blob: 5309ebceaf2c6e3ca13f9e8aafd7e69dadb132e7 (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
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
"""Tests for XML reports from coverage.py."""

import os
import re

import coverage

from tests.coveragetest import CoverageTest


class XmlTestHelpers(CoverageTest):
    """Methods to use from XML tests."""

    def run_mycode(self):
        """Run mycode.py, so we can report on it."""
        self.make_file("mycode.py", "print('hello')\n")
        self.run_command("coverage run mycode.py")

    def run_doit(self):
        """Construct a simple sub-package."""
        self.make_file("sub/__init__.py")
        self.make_file("sub/doit.py", "print('doit!')")
        self.make_file("main.py", "import sub.doit")
        cov = coverage.coverage()
        self.start_import_stop(cov, "main")
        return cov

    def make_tree(self, width, depth, curdir="."):
        """Make a tree of packages.

        Makes `width` directories, named d0 .. d{width-1}. Each directory has
        __init__.py, and `width` files, named f0.py .. f{width-1}.py.  Each
        directory also has `width` subdirectories, in the same fashion, until
        a depth of `depth` is reached.

        """
        if depth == 0:
            return

        def here(p):
            """A path for `p` in our currently interesting directory."""
            return os.path.join(curdir, p)

        for i in range(width):
            next_dir = here("d{0}".format(i))
            self.make_tree(width, depth-1, next_dir)
        if curdir != ".":
            self.make_file(here("__init__.py"), "")
            for i in range(width):
                filename = here("f{0}.py".format(i))
                self.make_file(filename, "# {0}\n".format(filename))


class XmlReportTest(XmlTestHelpers, CoverageTest):
    """Tests of the XML reports from coverage.py."""

    def test_default_file_placement(self):
        self.run_mycode()
        self.run_command("coverage xml")
        self.assert_exists("coverage.xml")

    def test_argument_affects_xml_placement(self):
        self.run_mycode()
        self.run_command("coverage xml -o put_it_there.xml")
        self.assert_doesnt_exist("coverage.xml")
        self.assert_exists("put_it_there.xml")

    def test_config_file_directory_does_not_exist(self):
        self.run_mycode()
        self.run_command("coverage xml -o nonexistent/put_it_there.xml")
        self.assert_doesnt_exist("coverage.xml")
        self.assert_doesnt_exist("put_it_there.xml")
        self.assert_exists("nonexistent/put_it_there.xml")

    def test_config_affects_xml_placement(self):
        self.run_mycode()
        self.make_file(".coveragerc", "[xml]\noutput = xml.out\n")
        self.run_command("coverage xml")
        self.assert_doesnt_exist("coverage.xml")
        self.assert_exists("xml.out")

    def test_no_data(self):
        # https://bitbucket.org/ned/coveragepy/issue/210
        self.run_command("coverage xml")
        self.assert_doesnt_exist("coverage.xml")

    def test_no_source(self):
        # Written while investigating a bug, might as well keep it.
        # https://bitbucket.org/ned/coveragepy/issue/208
        self.make_file("innocuous.py", "a = 4")
        cov = coverage.coverage()
        self.start_import_stop(cov, "innocuous")
        os.remove("innocuous.py")
        cov.xml_report(ignore_errors=True)
        self.assert_exists("coverage.xml")

    def test_filename_format_showing_everything(self):
        cov = self.run_doit()
        cov.xml_report(outfile="-")
        xml = self.stdout()
        doit_line = re_line(xml, "class.*doit")
        self.assertIn('filename="sub/doit.py"', doit_line)

    def test_filename_format_including_filename(self):
        cov = self.run_doit()
        cov.xml_report(["sub/doit.py"], outfile="-")
        xml = self.stdout()
        doit_line = re_line(xml, "class.*doit")
        self.assertIn('filename="sub/doit.py"', doit_line)

    def test_filename_format_including_module(self):
        cov = self.run_doit()
        import sub.doit                         # pylint: disable=import-error
        cov.xml_report([sub.doit], outfile="-")
        xml = self.stdout()
        doit_line = re_line(xml, "class.*doit")
        self.assertIn('filename="sub/doit.py"', doit_line)

    def test_reporting_on_nothing(self):
        # Used to raise a zero division error:
        # https://bitbucket.org/ned/coveragepy/issue/250
        self.make_file("empty.py", "")
        cov = coverage.coverage()
        empty = self.start_import_stop(cov, "empty")
        cov.xml_report([empty], outfile="-")
        xml = self.stdout()
        empty_line = re_line(xml, "class.*empty")
        self.assertIn('filename="empty.py"', empty_line)
        self.assertIn('line-rate="1"', empty_line)

    def test_empty_file_is_100_not_0(self):
        # https://bitbucket.org/ned/coveragepy/issue/345
        cov = self.run_doit()
        cov.xml_report(outfile="-")
        xml = self.stdout()
        init_line = re_line(xml, 'filename="sub/__init__.py"')
        self.assertIn('line-rate="1"', init_line)


class XmlPackageStructureTest(XmlTestHelpers, CoverageTest):
    """Tests about the package structure reported in the coverage.xml file."""

    def test_packages(self):
        self.make_tree(width=1, depth=3)
        self.make_file("main.py", """\
            from d0.d0 import f0
            """)
        cov = coverage.coverage(source=".")
        self.start_import_stop(cov, "main")
        cov.xml_report(outfile="-")
        xml = self.stdout()
        packages_and_classes = "".join(re_lines(xml, r"<package |<class "))
        scrubs = r' branch-rate="0"| complexity="0"| line-rate="[\d.]+"'
        self.assertMultiLineEqual(
            clean(packages_and_classes, scrubs),
            clean("""\
                <package name=".">
                    <class filename="main.py" name="main.py">
                <package name="d0">
                    <class filename="d0/__init__.py" name="__init__.py">
                    <class filename="d0/f0.py" name="f0.py">
                <package name="d0.d0">
                    <class filename="d0/d0/__init__.py" name="__init__.py">
                    <class filename="d0/d0/f0.py" name="f0.py">
                """)
            )


def re_lines(text, pat):
    """Return a list of lines that match `pat` in the string `text`."""
    lines = [l for l in text.splitlines(True) if re.search(pat, l)]
    return lines


def re_line(text, pat):
    """Return the one line in `text` that matches regex `pat`."""
    lines = re_lines(text, pat)
    assert len(lines) == 1
    return lines[0]


def clean(text, scrub=None):
    """Remove any text matching `scrub`, and all leading whitespace."""
    if scrub:
        text = re.sub(scrub, "", text)
    text = re.sub(r"(?m)^\s+", "", text)
    return text