eric7/DebugClients/Python/coverage/xmlreport.py

branch
eric7
changeset 8775
0802ae193343
parent 8312
800c432b34c8
child 8929
fcca2fa618bf
equal deleted inserted replaced
8774:d728227e8ebb 8775:0802ae193343
1 # coding: utf-8
2 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
3 # For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt 2 # For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
4 3
5 """XML reporting for coverage.py""" 4 """XML reporting for coverage.py"""
6 5
8 import os.path 7 import os.path
9 import sys 8 import sys
10 import time 9 import time
11 import xml.dom.minidom 10 import xml.dom.minidom
12 11
13 from coverage import env
14 from coverage import __url__, __version__, files 12 from coverage import __url__, __version__, files
15 from coverage.backward import iitems 13 from coverage.misc import isolate_module, human_sorted, human_sorted_items
16 from coverage.misc import isolate_module
17 from coverage.report import get_analysis_to_report 14 from coverage.report import get_analysis_to_report
18 15
19 os = isolate_module(os) 16 os = isolate_module(os)
20 17
21 18
28 return "1" 25 return "1"
29 else: 26 else:
30 return "%.4g" % (float(hit) / num) 27 return "%.4g" % (float(hit) / num)
31 28
32 29
33 class XmlReporter(object): 30 class XmlReporter:
34 """A reporter for writing Cobertura-style XML coverage results.""" 31 """A reporter for writing Cobertura-style XML coverage results."""
32
33 report_type = "XML report"
35 34
36 def __init__(self, coverage): 35 def __init__(self, coverage):
37 self.coverage = coverage 36 self.coverage = coverage
38 self.config = self.coverage.config 37 self.config = self.coverage.config
39 38
78 77
79 xsources = self.xml_out.createElement("sources") 78 xsources = self.xml_out.createElement("sources")
80 xcoverage.appendChild(xsources) 79 xcoverage.appendChild(xsources)
81 80
82 # Populate the XML DOM with the source info. 81 # Populate the XML DOM with the source info.
83 for path in sorted(self.source_paths): 82 for path in human_sorted(self.source_paths):
84 xsource = self.xml_out.createElement("source") 83 xsource = self.xml_out.createElement("source")
85 xsources.appendChild(xsource) 84 xsources.appendChild(xsource)
86 txt = self.xml_out.createTextNode(path) 85 txt = self.xml_out.createTextNode(path)
87 xsource.appendChild(txt) 86 xsource.appendChild(txt)
88 87
91 90
92 xpackages = self.xml_out.createElement("packages") 91 xpackages = self.xml_out.createElement("packages")
93 xcoverage.appendChild(xpackages) 92 xcoverage.appendChild(xpackages)
94 93
95 # Populate the XML DOM with the package info. 94 # Populate the XML DOM with the package info.
96 for pkg_name, pkg_data in sorted(iitems(self.packages)): 95 for pkg_name, pkg_data in human_sorted_items(self.packages.items()):
97 class_elts, lhits, lnum, bhits, bnum = pkg_data 96 class_elts, lhits, lnum, bhits, bnum = pkg_data
98 xpackage = self.xml_out.createElement("package") 97 xpackage = self.xml_out.createElement("package")
99 xpackages.appendChild(xpackage) 98 xpackages.appendChild(xpackage)
100 xclasses = self.xml_out.createElement("classes") 99 xclasses = self.xml_out.createElement("classes")
101 xpackage.appendChild(xclasses) 100 xpackage.appendChild(xclasses)
102 for _, class_elt in sorted(iitems(class_elts)): 101 for _, class_elt in human_sorted_items(class_elts.items()):
103 xclasses.appendChild(class_elt) 102 xclasses.appendChild(class_elt)
104 xpackage.setAttribute("name", pkg_name.replace(os.sep, '.')) 103 xpackage.setAttribute("name", pkg_name.replace(os.sep, '.'))
105 xpackage.setAttribute("line-rate", rate(lhits, lnum)) 104 xpackage.setAttribute("line-rate", rate(lhits, lnum))
106 if has_arcs: 105 if has_arcs:
107 branch_rate = rate(bhits, bnum) 106 branch_rate = rate(bhits, bnum)
156 break 155 break
157 else: 156 else:
158 rel_name = fr.relative_filename() 157 rel_name = fr.relative_filename()
159 self.source_paths.add(fr.filename[:-len(rel_name)].rstrip(r"\/")) 158 self.source_paths.add(fr.filename[:-len(rel_name)].rstrip(r"\/"))
160 159
161 dirname = os.path.dirname(rel_name) or u"." 160 dirname = os.path.dirname(rel_name) or "."
162 dirname = "/".join(dirname.split("/")[:self.config.xml_package_depth]) 161 dirname = "/".join(dirname.split("/")[:self.config.xml_package_depth])
163 package_name = dirname.replace("/", ".") 162 package_name = dirname.replace("/", ".")
164 163
165 package = self.packages.setdefault(package_name, [{}, 0, 0, 0, 0]) 164 package = self.packages.setdefault(package_name, [{}, 0, 0, 0, 0])
166 165
226 package[4] += class_branches 225 package[4] += class_branches
227 226
228 227
229 def serialize_xml(dom): 228 def serialize_xml(dom):
230 """Serialize a minidom node to XML.""" 229 """Serialize a minidom node to XML."""
231 out = dom.toprettyxml() 230 return dom.toprettyxml()
232 if env.PY2:
233 out = out.encode("utf8")
234 return out

eric ide

mercurial