OLD | NEW |
| (Empty) |
1 """XML reporting for coverage.py""" | |
2 | |
3 import os, sys, time | |
4 import xml.dom.minidom | |
5 | |
6 from coverage import __url__, __version__ | |
7 from coverage.backward import sorted, rpartition # pylint: disable=W0622 | |
8 from coverage.report import Reporter | |
9 | |
10 def rate(hit, num): | |
11 """Return the fraction of `hit`/`num`, as a string.""" | |
12 return "%.4g" % (float(hit) / (num or 1.0)) | |
13 | |
14 | |
15 class XmlReporter(Reporter): | |
16 """A reporter for writing Cobertura-style XML coverage results.""" | |
17 | |
18 def __init__(self, coverage, config): | |
19 super(XmlReporter, self).__init__(coverage, config) | |
20 | |
21 self.packages = None | |
22 self.xml_out = None | |
23 self.arcs = coverage.data.has_arcs() | |
24 | |
25 def report(self, morfs, outfile=None): | |
26 """Generate a Cobertura-compatible XML report for `morfs`. | |
27 | |
28 `morfs` is a list of modules or filenames. | |
29 | |
30 `outfile` is a file object to write the XML to. | |
31 | |
32 """ | |
33 # Initial setup. | |
34 outfile = outfile or sys.stdout | |
35 | |
36 # Create the DOM that will store the data. | |
37 impl = xml.dom.minidom.getDOMImplementation() | |
38 docType = impl.createDocumentType( | |
39 "coverage", None, | |
40 "http://cobertura.sourceforge.net/xml/coverage-03.dtd" | |
41 ) | |
42 self.xml_out = impl.createDocument(None, "coverage", docType) | |
43 | |
44 # Write header stuff. | |
45 xcoverage = self.xml_out.documentElement | |
46 xcoverage.setAttribute("version", __version__) | |
47 xcoverage.setAttribute("timestamp", str(int(time.time()*1000))) | |
48 xcoverage.appendChild(self.xml_out.createComment( | |
49 " Generated by coverage.py: %s " % __url__ | |
50 )) | |
51 xpackages = self.xml_out.createElement("packages") | |
52 xcoverage.appendChild(xpackages) | |
53 | |
54 # Call xml_file for each file in the data. | |
55 self.packages = {} | |
56 self.report_files(self.xml_file, morfs) | |
57 | |
58 lnum_tot, lhits_tot = 0, 0 | |
59 bnum_tot, bhits_tot = 0, 0 | |
60 | |
61 # Populate the XML DOM with the package info. | |
62 for pkg_name in sorted(self.packages.keys()): | |
63 pkg_data = self.packages[pkg_name] | |
64 class_elts, lhits, lnum, bhits, bnum = pkg_data | |
65 xpackage = self.xml_out.createElement("package") | |
66 xpackages.appendChild(xpackage) | |
67 xclasses = self.xml_out.createElement("classes") | |
68 xpackage.appendChild(xclasses) | |
69 for class_name in sorted(class_elts.keys()): | |
70 xclasses.appendChild(class_elts[class_name]) | |
71 xpackage.setAttribute("name", pkg_name.replace(os.sep, '.')) | |
72 xpackage.setAttribute("line-rate", rate(lhits, lnum)) | |
73 xpackage.setAttribute("branch-rate", rate(bhits, bnum)) | |
74 xpackage.setAttribute("complexity", "0") | |
75 | |
76 lnum_tot += lnum | |
77 lhits_tot += lhits | |
78 bnum_tot += bnum | |
79 bhits_tot += bhits | |
80 | |
81 xcoverage.setAttribute("line-rate", rate(lhits_tot, lnum_tot)) | |
82 xcoverage.setAttribute("branch-rate", rate(bhits_tot, bnum_tot)) | |
83 | |
84 # Use the DOM to write the output file. | |
85 outfile.write(self.xml_out.toprettyxml()) | |
86 | |
87 # Return the total percentage. | |
88 return 100.0 * (lhits_tot + bhits_tot) / (lnum_tot + bnum_tot) | |
89 | |
90 def xml_file(self, cu, analysis): | |
91 """Add to the XML report for a single file.""" | |
92 | |
93 # Create the 'lines' and 'package' XML elements, which | |
94 # are populated later. Note that a package == a directory. | |
95 package_name = rpartition(cu.name, ".")[0] | |
96 className = cu.name | |
97 | |
98 package = self.packages.setdefault(package_name, [{}, 0, 0, 0, 0]) | |
99 | |
100 xclass = self.xml_out.createElement("class") | |
101 | |
102 xclass.appendChild(self.xml_out.createElement("methods")) | |
103 | |
104 xlines = self.xml_out.createElement("lines") | |
105 xclass.appendChild(xlines) | |
106 | |
107 xclass.setAttribute("name", className) | |
108 filename = cu.file_locator.relative_filename(cu.filename) | |
109 xclass.setAttribute("filename", filename.replace("\\", "/")) | |
110 xclass.setAttribute("complexity", "0") | |
111 | |
112 branch_stats = analysis.branch_stats() | |
113 | |
114 # For each statement, create an XML 'line' element. | |
115 for line in analysis.statements: | |
116 xline = self.xml_out.createElement("line") | |
117 xline.setAttribute("number", str(line)) | |
118 | |
119 # Q: can we get info about the number of times a statement is | |
120 # executed? If so, that should be recorded here. | |
121 xline.setAttribute("hits", str(int(line not in analysis.missing))) | |
122 | |
123 if self.arcs: | |
124 if line in branch_stats: | |
125 total, taken = branch_stats[line] | |
126 xline.setAttribute("branch", "true") | |
127 xline.setAttribute("condition-coverage", | |
128 "%d%% (%d/%d)" % (100*taken/total, taken, total) | |
129 ) | |
130 xlines.appendChild(xline) | |
131 | |
132 class_lines = len(analysis.statements) | |
133 class_hits = class_lines - len(analysis.missing) | |
134 | |
135 if self.arcs: | |
136 class_branches = sum([t for t,k in branch_stats.values()]) | |
137 missing_branches = sum([t-k for t,k in branch_stats.values()]) | |
138 class_br_hits = class_branches - missing_branches | |
139 else: | |
140 class_branches = 0.0 | |
141 class_br_hits = 0.0 | |
142 | |
143 # Finalize the statistics that are collected in the XML DOM. | |
144 xclass.setAttribute("line-rate", rate(class_hits, class_lines)) | |
145 xclass.setAttribute("branch-rate", rate(class_br_hits, class_branches)) | |
146 package[0][className] = xclass | |
147 package[1] += class_hits | |
148 package[2] += class_lines | |
149 package[3] += class_br_hits | |
150 package[4] += class_branches | |
OLD | NEW |