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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
# coding: utf-8
# 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 XML reports from coverage.py."""
import os
import os.path
import re
from xml.etree import ElementTree
from unittest_mixins import change_dir
import coverage
from coverage.backward import import_local_file
from coverage.files import abs_file
from tests.coveragetest import CoverageTest
from tests.goldtest import compare, gold_path
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(source=["."])
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` sub-directories, 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{}".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{}.py".format(i))
self.make_file(filename, "# {}\n".format(filename))
def assert_source(self, xmldom, src):
"""Assert that the XML has a <source> element with `src`."""
src = abs_file(src)
elts = xmldom.findall(".//sources/source")
assert any(elt.text == src for elt in elts)
class XmlTestHelpersTest(XmlTestHelpers, CoverageTest):
"""Tests of methods in XmlTestHelpers."""
run_in_temp_dir = False
def test_assert_source(self):
dom = ElementTree.fromstring("""\
<doc>
<src>foo</src>
<sources>
<source>{cwd}something</source>
<source>{cwd}another</source>
</sources>
</doc>
""".format(cwd=abs_file(".")+os.sep))
self.assert_source(dom, "something")
self.assert_source(dom, "another")
with self.assertRaises(AssertionError):
self.assert_source(dom, "hello")
with self.assertRaises(AssertionError):
self.assert_source(dom, "foo")
with self.assertRaises(AssertionError):
self.assert_source(dom, "thing")
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://github.com/nedbat/coveragepy/issues/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://github.com/nedbat/coveragepy/issues/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()
dom = ElementTree.parse("coverage.xml")
elts = dom.findall(".//class[@name='doit.py']")
assert len(elts) == 1
assert elts[0].get('filename') == "sub/doit.py"
def test_filename_format_including_filename(self):
cov = self.run_doit()
cov.xml_report(["sub/doit.py"])
dom = ElementTree.parse("coverage.xml")
elts = dom.findall(".//class[@name='doit.py']")
assert len(elts) == 1
assert elts[0].get('filename') == "sub/doit.py"
def test_filename_format_including_module(self):
cov = self.run_doit()
import sub.doit # pylint: disable=import-error
cov.xml_report([sub.doit])
dom = ElementTree.parse("coverage.xml")
elts = dom.findall(".//class[@name='doit.py']")
assert len(elts) == 1
assert elts[0].get('filename') == "sub/doit.py"
def test_reporting_on_nothing(self):
# Used to raise a zero division error:
# https://github.com/nedbat/coveragepy/issues/250
self.make_file("empty.py", "")
cov = coverage.Coverage()
empty = self.start_import_stop(cov, "empty")
cov.xml_report([empty])
dom = ElementTree.parse("coverage.xml")
elts = dom.findall(".//class[@name='empty.py']")
assert len(elts) == 1
assert elts[0].get('filename') == "empty.py"
assert elts[0].get('line-rate') == '1'
def test_empty_file_is_100_not_0(self):
# https://github.com/nedbat/coveragepy/issues/345
cov = self.run_doit()
cov.xml_report()
dom = ElementTree.parse("coverage.xml")
elts = dom.findall(".//class[@name='__init__.py']")
assert len(elts) == 1
assert elts[0].get('line-rate') == '1'
def test_empty_file_is_skipped(self):
cov = self.run_doit()
cov.xml_report(skip_empty=True)
dom = ElementTree.parse("coverage.xml")
elts = dom.findall(".//class[@name='__init__.py']")
assert len(elts) == 0
def test_curdir_source(self):
# With no source= option, the XML report should explain that the source
# is in the current directory.
cov = self.run_doit()
cov.xml_report()
dom = ElementTree.parse("coverage.xml")
self.assert_source(dom, ".")
sources = dom.findall(".//source")
assert len(sources) == 1
def test_deep_source(self):
# When using source=, the XML report needs to mention those directories
# in the <source> elements.
# https://github.com/nedbat/coveragepy/issues/439
self.make_file("src/main/foo.py", "a = 1")
self.make_file("also/over/there/bar.py", "b = 2")
cov = coverage.Coverage(source=["src/main", "also/over/there", "not/really"])
cov.start()
mod_foo = import_local_file("foo", "src/main/foo.py") # pragma: nested
mod_bar = import_local_file("bar", "also/over/there/bar.py") # pragma: nested
cov.stop() # pragma: nested
cov.xml_report([mod_foo, mod_bar])
dom = ElementTree.parse("coverage.xml")
self.assert_source(dom, "src/main")
self.assert_source(dom, "also/over/there")
sources = dom.findall(".//source")
assert len(sources) == 2
foo_class = dom.findall(".//class[@name='foo.py']")
assert len(foo_class) == 1
assert foo_class[0].attrib == {
'branch-rate': '0',
'complexity': '0',
'filename': 'foo.py',
'line-rate': '1',
'name': 'foo.py',
}
bar_class = dom.findall(".//class[@name='bar.py']")
assert len(bar_class) == 1
assert bar_class[0].attrib == {
'branch-rate': '0',
'complexity': '0',
'filename': 'bar.py',
'line-rate': '1',
'name': 'bar.py',
}
def test_nonascii_directory(self):
# https://github.com/nedbat/coveragepy/issues/573
self.make_file("테스트/program.py", "a = 1")
with change_dir("테스트"):
cov = coverage.Coverage()
self.start_import_stop(cov, "program")
cov.xml_report()
def unbackslash(v):
"""Find strings in `v`, and replace backslashes with slashes throughout."""
if isinstance(v, (tuple, list)):
return [unbackslash(vv) for vv in v]
elif isinstance(v, dict):
return {k: unbackslash(vv) for k, vv in v.items()}
else:
assert isinstance(v, str)
return v.replace("\\", "/")
class XmlPackageStructureTest(XmlTestHelpers, CoverageTest):
"""Tests about the package structure reported in the coverage.xml file."""
def package_and_class_tags(self, cov):
"""Run an XML report on `cov`, and get the package and class tags."""
cov.xml_report()
dom = ElementTree.parse("coverage.xml")
for node in dom.iter():
if node.tag in ('package', 'class'):
yield (node.tag, {a:v for a,v in node.items() if a in ('name', 'filename')})
def assert_package_and_class_tags(self, cov, result):
"""Check the XML package and class tags from `cov` match `result`."""
self.assertEqual(
unbackslash(list(self.package_and_class_tags(cov))),
unbackslash(result),
)
def test_package_names(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")
self.assert_package_and_class_tags(cov, [
('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 test_package_depth_1(self):
self.make_tree(width=1, depth=4)
self.make_file("main.py", """\
from d0.d0 import f0
""")
cov = coverage.Coverage(source=".")
self.start_import_stop(cov, "main")
cov.set_option("xml:package_depth", 1)
self.assert_package_and_class_tags(cov, [
('package', {'name': "."}),
('class', {'filename': "main.py", 'name': "main.py"}),
('package', {'name': "d0"}),
('class', {'filename': "d0/__init__.py", 'name': "__init__.py"}),
('class', {'filename': "d0/d0/__init__.py", 'name': "d0/__init__.py"}),
('class', {'filename': "d0/d0/d0/__init__.py", 'name': "d0/d0/__init__.py"}),
('class', {'filename': "d0/d0/d0/f0.py", 'name': "d0/d0/f0.py"}),
('class', {'filename': "d0/d0/f0.py", 'name': "d0/f0.py"}),
('class', {'filename': "d0/f0.py", 'name': "f0.py"}),
])
def test_package_depth_2(self):
self.make_tree(width=1, depth=4)
self.make_file("main.py", """\
from d0.d0 import f0
""")
cov = coverage.Coverage(source=".")
self.start_import_stop(cov, "main")
cov.set_option("xml:package_depth", 2)
self.assert_package_and_class_tags(cov, [
('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/d0/__init__.py", 'name': "d0/__init__.py"}),
('class', {'filename': "d0/d0/d0/f0.py", 'name': "d0/f0.py"}),
('class', {'filename': "d0/d0/f0.py", 'name': "f0.py"}),
])
def test_package_depth_3(self):
self.make_tree(width=1, depth=4)
self.make_file("main.py", """\
from d0.d0 import f0
""")
cov = coverage.Coverage(source=".")
self.start_import_stop(cov, "main")
cov.set_option("xml:package_depth", 3)
self.assert_package_and_class_tags(cov, [
('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"}),
('package', {'name': "d0.d0.d0"}),
('class', {'filename': "d0/d0/d0/__init__.py", 'name': "__init__.py"}),
('class', {'filename': "d0/d0/d0/f0.py", 'name': "f0.py"}),
])
def test_source_prefix(self):
# https://github.com/nedbat/coveragepy/issues/465
# https://github.com/nedbat/coveragepy/issues/526
self.make_file("src/mod.py", "print(17)")
cov = coverage.Coverage(source=["src"])
self.start_import_stop(cov, "mod", modfile="src/mod.py")
self.assert_package_and_class_tags(cov, [
('package', {'name': "."}),
('class', {'filename': "mod.py", 'name': "mod.py"}),
])
dom = ElementTree.parse("coverage.xml")
self.assert_source(dom, "src")
def test_relative_source(self):
self.make_file("src/mod.py", "print(17)")
cov = coverage.Coverage(source=["src"])
cov.set_option("run:relative_files", True)
self.start_import_stop(cov, "mod", modfile="src/mod.py")
cov.xml_report()
with open("coverage.xml") as x:
print(x.read())
dom = ElementTree.parse("coverage.xml")
elts = dom.findall(".//sources/source")
assert [elt.text for elt in elts] == ["src"]
def compare_xml(expected, actual, **kwargs):
"""Specialized compare function for our XML files."""
source_path = coverage.files.relative_directory().rstrip(r"\/")
scrubs=[
(r' timestamp="\d+"', ' timestamp="TIMESTAMP"'),
(r' version="[-.\w]+"', ' version="VERSION"'),
(r'<source>\s*.*?\s*</source>', '<source>%s</source>' % re.escape(source_path)),
(r'/coverage.readthedocs.io/?[-.\w/]*', '/coverage.readthedocs.io/VER'),
]
compare(expected, actual, scrubs=scrubs, **kwargs)
class XmlGoldTest(CoverageTest):
"""Tests of XML reporting that use gold files."""
def test_a_xml_1(self):
self.make_file("a.py", """\
if 1 < 2:
# Needed a < to look at HTML entities.
a = 3
else:
a = 4
""")
cov = coverage.Coverage()
a = self.start_import_stop(cov, "a")
cov.xml_report(a, outfile="coverage.xml")
compare_xml(gold_path("xml/x_xml"), ".", actual_extra=True)
def test_a_xml_2(self):
self.make_file("a.py", """\
if 1 < 2:
# Needed a < to look at HTML entities.
a = 3
else:
a = 4
""")
self.make_file("run_a_xml_2.ini", """\
# Put all the XML output in xml_2
[xml]
output = xml_2/coverage.xml
""")
cov = coverage.Coverage(config_file="run_a_xml_2.ini")
a = self.start_import_stop(cov, "a")
cov.xml_report(a)
compare_xml(gold_path("xml/x_xml"), "xml_2")
def test_y_xml_branch(self):
self.make_file("y.py", """\
def choice(x):
if x < 2:
return 3
else:
return 4
assert choice(1) == 3
""")
cov = coverage.Coverage(branch=True)
y = self.start_import_stop(cov, "y")
cov.xml_report(y, outfile="y_xml_branch/coverage.xml")
compare_xml(gold_path("xml/y_xml_branch"), "y_xml_branch")
|