def xml_file(self, cu, analysis):
        package_name = rpartition(cu.name, '.')[0]
        className = cu.name
        package = self.packages.setdefault(package_name, [{},
         0,
         0,
         0,
         0])
        xclass = self.xml_out.createElement('class')
        xclass.appendChild(self.xml_out.createElement('methods'))
        xlines = self.xml_out.createElement('lines')
        xclass.appendChild(xlines)
        xclass.setAttribute('name', className)
        filename = cu.file_locator.relative_filename(cu.filename)
        xclass.setAttribute('filename', filename.replace('\\', '/'))
        xclass.setAttribute('complexity', '0')
        branch_stats = analysis.branch_stats()
        for line in analysis.statements:
            xline = self.xml_out.createElement('line')
            xline.setAttribute('number', str(line))
            xline.setAttribute('hits', str(int(line not in analysis.missing)))
            if self.arcs:
                if line in branch_stats:
                    total, taken = branch_stats[line]
                    xline.setAttribute('branch', 'true')
                    xline.setAttribute('condition-coverage', '%d%% (%d/%d)' % (100 * taken / total, taken, total))
            xlines.appendChild(xline)

        class_lines = len(analysis.statements)
        class_hits = class_lines - len(analysis.missing)
        if self.arcs:
            class_branches = sum([ t for t, k in branch_stats.values() ])
            missing_branches = sum([ t - k for t, k in branch_stats.values() ])
            class_br_hits = class_branches - missing_branches
        else:
            class_branches = 0.0
            class_br_hits = 0.0
        xclass.setAttribute('line-rate', rate(class_hits, class_lines))
        xclass.setAttribute('branch-rate', rate(class_br_hits, class_branches))
        package[0][className] = xclass
        package[1] += class_hits
        package[2] += class_lines
        package[3] += class_br_hits
        package[4] += class_branches
    def xml_file(self, cu, analysis):
        """Add to the XML report for a single file."""
        package_name = rpartition(cu.name, '.')[0]
        className = cu.name
        package = self.packages.setdefault(package_name, [{}, 0, 0, 0, 0])
        xclass = self.xml_out.createElement('class')
        xclass.appendChild(self.xml_out.createElement('methods'))
        xlines = self.xml_out.createElement('lines')
        xclass.appendChild(xlines)
        xclass.setAttribute('name', className)
        filename = cu.file_locator.relative_filename(cu.filename)
        xclass.setAttribute('filename', filename.replace('\\', '/'))
        xclass.setAttribute('complexity', '0')
        branch_stats = analysis.branch_stats()
        for line in analysis.statements:
            xline = self.xml_out.createElement('line')
            xline.setAttribute('number', str(line))
            xline.setAttribute('hits', str(int(line not in analysis.missing)))
            if self.arcs:
                if line in branch_stats:
                    total, taken = branch_stats[line]
                    xline.setAttribute('branch', 'true')
                    xline.setAttribute(
                        'condition-coverage',
                        '%d%% (%d/%d)' % (100 * taken / total, taken, total))
            xlines.appendChild(xline)

        class_lines = len(analysis.statements)
        class_hits = class_lines - len(analysis.missing)
        if self.arcs:
            class_branches = sum([t for t, k in branch_stats.values()])
            missing_branches = sum([t - k for t, k in branch_stats.values()])
            class_br_hits = class_branches - missing_branches
        else:
            class_branches = 0.0
            class_br_hits = 0.0
        xclass.setAttribute('line-rate', rate(class_hits, class_lines))
        xclass.setAttribute('branch-rate', rate(class_br_hits, class_branches))
        package[0][className] = xclass
        package[1] += class_hits
        package[2] += class_lines
        package[3] += class_br_hits
        package[4] += class_branches
    def test_sub_python_is_this_python(self):
        # Try it with a python command.
        os.environ['COV_FOOBAR'] = 'XYZZY'
        self.make_file("showme.py", """\
            import os, sys
            print(sys.executable)
            print(os.__file__)
            print(os.environ['COV_FOOBAR'])
            """)
        out = self.run_command("python showme.py").splitlines()
        self.assertEqual(out[0], sys.executable)
        self.assertEqual(out[1], os.__file__)
        self.assertEqual(out[2], 'XYZZY')

        # Try it with a "coverage debug sys" command.
        out = self.run_command("coverage debug sys").splitlines()
        # "environment: COV_FOOBAR = XYZZY" or "COV_FOOBAR = XYZZY"
        executable = [l for l in out if "executable:" in l][0]
        executable = executable.split(":", 1)[1].strip()
        self.assertTrue(same_python_executable(executable, sys.executable))
        environ = [l for l in out if "COV_FOOBAR" in l][0]
        _, _, environ = rpartition(environ, ":")
        self.assertEqual(environ.strip(), "COV_FOOBAR = XYZZY")
Exemple #4
0
    def test_sub_python_is_this_python(self):
        # Try it with a python command.
        os.environ['COV_FOOBAR'] = 'XYZZY'
        self.make_file(
            "showme.py", """\
            import os, sys
            print(sys.executable)
            print(os.__file__)
            print(os.environ['COV_FOOBAR'])
            """)
        out = self.run_command("python showme.py").splitlines()
        self.assertEqual(out[0], sys.executable)
        self.assertEqual(out[1], os.__file__)
        self.assertEqual(out[2], 'XYZZY')

        # Try it with a "coverage debug sys" command.
        out = self.run_command("coverage debug sys").splitlines()
        # "environment: COV_FOOBAR = XYZZY" or "COV_FOOBAR = XYZZY"
        executable = [l for l in out if "executable:" in l][0]
        executable = executable.split(":", 1)[1].strip()
        self.assertTrue(same_python_executable(executable, sys.executable))
        environ = [l for l in out if "COV_FOOBAR" in l][0]
        _, _, environ = rpartition(environ, ":")
        self.assertEqual(environ.strip(), "COV_FOOBAR = XYZZY")
Exemple #5
0
    def xml_file(self, cu, analysis):
        """Add to the XML report for a single file."""

        # Create the 'lines' and 'package' XML elements, which
        # are populated later.  Note that a package == a directory.
        package_name = rpartition(cu.name, ".")[0]
        className = cu.name

        package = self.packages.setdefault(package_name, [{}, 0, 0, 0, 0])

        xclass = self.xml_out.createElement("class")

        xclass.appendChild(self.xml_out.createElement("methods"))

        xlines = self.xml_out.createElement("lines")
        xclass.appendChild(xlines)

        xclass.setAttribute("name", className)
        filename = cu.file_locator.relative_filename(cu.filename)
        xclass.setAttribute("filename", filename.replace("\\", "/"))
        xclass.setAttribute("complexity", "0")

        branch_stats = analysis.branch_stats()

        # For each statement, create an XML 'line' element.
        for line in analysis.statements:
            xline = self.xml_out.createElement("line")
            xline.setAttribute("number", str(line))

            # Q: can we get info about the number of times a statement is
            # executed?  If so, that should be recorded here.
            xline.setAttribute("hits", str(int(line not in analysis.missing)))

            if self.arcs:
                if line in branch_stats:
                    total, taken = branch_stats[line]
                    xline.setAttribute("branch", "true")
                    xline.setAttribute("condition-coverage",
                        "%d%% (%d/%d)" % (100*taken/total, taken, total)
                        )
            xlines.appendChild(xline)

        class_lines = len(analysis.statements)
        class_hits = class_lines - len(analysis.missing)

        if self.arcs:
            class_branches = sum([t for t,k in branch_stats.values()])
            missing_branches = sum([t-k for t,k in branch_stats.values()])
            class_br_hits = class_branches - missing_branches
        else:
            class_branches = 0.0
            class_br_hits = 0.0

        # Finalize the statistics that are collected in the XML DOM.
        xclass.setAttribute("line-rate", rate(class_hits, class_lines))
        xclass.setAttribute("branch-rate", rate(class_br_hits, class_branches))
        package[0][className] = xclass
        package[1] += class_hits
        package[2] += class_lines
        package[3] += class_br_hits
        package[4] += class_branches