test_html.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. # -*- coding: utf-8 -*-
  2. # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  3. # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  4. """Tests that HTML generation is awesome."""
  5. import datetime
  6. import glob
  7. import os
  8. import os.path
  9. import re
  10. import sys
  11. import coverage
  12. import coverage.files
  13. import coverage.html
  14. from coverage.misc import CoverageException, NotPython, NoSource
  15. from tests.coveragetest import CoverageTest
  16. from tests.goldtest import CoverageGoldTest
  17. from tests.goldtest import change_dir, compare, contains, doesnt_contain, contains_any
  18. class HtmlTestHelpers(CoverageTest):
  19. """Methods that help with HTML tests."""
  20. def create_initial_files(self):
  21. """Create the source files we need to run these tests."""
  22. self.make_file("main_file.py", """\
  23. import helper1, helper2
  24. helper1.func1(12)
  25. helper2.func2(12)
  26. """)
  27. self.make_file("helper1.py", """\
  28. def func1(x):
  29. if x % 2:
  30. print("odd")
  31. """)
  32. self.make_file("helper2.py", """\
  33. def func2(x):
  34. print("x is %d" % x)
  35. """)
  36. def run_coverage(self, covargs=None, htmlargs=None):
  37. """Run coverage.py on main_file.py, and create an HTML report."""
  38. self.clean_local_file_imports()
  39. cov = coverage.Coverage(**(covargs or {}))
  40. self.start_import_stop(cov, "main_file")
  41. cov.html_report(**(htmlargs or {}))
  42. def remove_html_files(self):
  43. """Remove the HTML files created as part of the HTML report."""
  44. os.remove("htmlcov/index.html")
  45. os.remove("htmlcov/main_file_py.html")
  46. os.remove("htmlcov/helper1_py.html")
  47. os.remove("htmlcov/helper2_py.html")
  48. def get_html_report_content(self, module):
  49. """Return the content of the HTML report for `module`."""
  50. filename = module.replace(".", "_").replace("/", "_") + ".html"
  51. filename = os.path.join("htmlcov", filename)
  52. with open(filename) as f:
  53. return f.read()
  54. def get_html_index_content(self):
  55. """Return the content of index.html.
  56. Timestamps are replaced with a placeholder so that clocks don't matter.
  57. """
  58. with open("htmlcov/index.html") as f:
  59. index = f.read()
  60. index = re.sub(
  61. r"created at \d{4}-\d{2}-\d{2} \d{2}:\d{2}",
  62. r"created at YYYY-MM-DD HH:MM",
  63. index,
  64. )
  65. return index
  66. def assert_correct_timestamp(self, html):
  67. """Extract the timestamp from `html`, and assert it is recent."""
  68. timestamp_pat = r"created at (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})"
  69. m = re.search(timestamp_pat, html)
  70. self.assertTrue(m, "Didn't find a timestamp!")
  71. timestamp = datetime.datetime(*map(int, m.groups()))
  72. # The timestamp only records the minute, so the delta could be from
  73. # 12:00 to 12:01:59, or two minutes.
  74. self.assert_recent_datetime(
  75. timestamp,
  76. seconds=120,
  77. msg="Timestamp is wrong: {0}".format(timestamp),
  78. )
  79. class HtmlDeltaTest(HtmlTestHelpers, CoverageTest):
  80. """Tests of the HTML delta speed-ups."""
  81. def setUp(self):
  82. super(HtmlDeltaTest, self).setUp()
  83. # At least one of our tests monkey-patches the version of coverage.py,
  84. # so grab it here to restore it later.
  85. self.real_coverage_version = coverage.__version__
  86. self.addCleanup(self.cleanup_coverage_version)
  87. def cleanup_coverage_version(self):
  88. """A cleanup."""
  89. coverage.__version__ = self.real_coverage_version
  90. def test_html_created(self):
  91. # Test basic HTML generation: files should be created.
  92. self.create_initial_files()
  93. self.run_coverage()
  94. self.assert_exists("htmlcov/index.html")
  95. self.assert_exists("htmlcov/main_file_py.html")
  96. self.assert_exists("htmlcov/helper1_py.html")
  97. self.assert_exists("htmlcov/helper2_py.html")
  98. self.assert_exists("htmlcov/style.css")
  99. self.assert_exists("htmlcov/coverage_html.js")
  100. def test_html_delta_from_source_change(self):
  101. # HTML generation can create only the files that have changed.
  102. # In this case, helper1 changes because its source is different.
  103. self.create_initial_files()
  104. self.run_coverage()
  105. index1 = self.get_html_index_content()
  106. self.remove_html_files()
  107. # Now change a file and do it again
  108. self.make_file("helper1.py", """\
  109. def func1(x): # A nice function
  110. if x % 2:
  111. print("odd")
  112. """)
  113. self.run_coverage()
  114. # Only the changed files should have been created.
  115. self.assert_exists("htmlcov/index.html")
  116. self.assert_exists("htmlcov/helper1_py.html")
  117. self.assert_doesnt_exist("htmlcov/main_file_py.html")
  118. self.assert_doesnt_exist("htmlcov/helper2_py.html")
  119. index2 = self.get_html_index_content()
  120. self.assertMultiLineEqual(index1, index2)
  121. def test_html_delta_from_coverage_change(self):
  122. # HTML generation can create only the files that have changed.
  123. # In this case, helper1 changes because its coverage is different.
  124. self.create_initial_files()
  125. self.run_coverage()
  126. self.remove_html_files()
  127. # Now change a file and do it again
  128. self.make_file("main_file.py", """\
  129. import helper1, helper2
  130. helper1.func1(23)
  131. helper2.func2(23)
  132. """)
  133. self.run_coverage()
  134. # Only the changed files should have been created.
  135. self.assert_exists("htmlcov/index.html")
  136. self.assert_exists("htmlcov/helper1_py.html")
  137. self.assert_exists("htmlcov/main_file_py.html")
  138. self.assert_doesnt_exist("htmlcov/helper2_py.html")
  139. def test_html_delta_from_settings_change(self):
  140. # HTML generation can create only the files that have changed.
  141. # In this case, everything changes because the coverage.py settings
  142. # have changed.
  143. self.create_initial_files()
  144. self.run_coverage(covargs=dict(omit=[]))
  145. index1 = self.get_html_index_content()
  146. self.remove_html_files()
  147. self.run_coverage(covargs=dict(omit=['xyzzy*']))
  148. # All the files have been reported again.
  149. self.assert_exists("htmlcov/index.html")
  150. self.assert_exists("htmlcov/helper1_py.html")
  151. self.assert_exists("htmlcov/main_file_py.html")
  152. self.assert_exists("htmlcov/helper2_py.html")
  153. index2 = self.get_html_index_content()
  154. self.assertMultiLineEqual(index1, index2)
  155. def test_html_delta_from_coverage_version_change(self):
  156. # HTML generation can create only the files that have changed.
  157. # In this case, everything changes because the coverage.py version has
  158. # changed.
  159. self.create_initial_files()
  160. self.run_coverage()
  161. index1 = self.get_html_index_content()
  162. self.remove_html_files()
  163. # "Upgrade" coverage.py!
  164. coverage.__version__ = "XYZZY"
  165. self.run_coverage()
  166. # All the files have been reported again.
  167. self.assert_exists("htmlcov/index.html")
  168. self.assert_exists("htmlcov/helper1_py.html")
  169. self.assert_exists("htmlcov/main_file_py.html")
  170. self.assert_exists("htmlcov/helper2_py.html")
  171. index2 = self.get_html_index_content()
  172. fixed_index2 = index2.replace("XYZZY", self.real_coverage_version)
  173. self.assertMultiLineEqual(index1, fixed_index2)
  174. class HtmlTitleTest(HtmlTestHelpers, CoverageTest):
  175. """Tests of the HTML title support."""
  176. def test_default_title(self):
  177. self.create_initial_files()
  178. self.run_coverage()
  179. index = self.get_html_index_content()
  180. self.assertIn("<title>Coverage report</title>", index)
  181. self.assertIn("<h1>Coverage report:", index)
  182. def test_title_set_in_config_file(self):
  183. self.create_initial_files()
  184. self.make_file(".coveragerc", "[html]\ntitle = Metrics & stuff!\n")
  185. self.run_coverage()
  186. index = self.get_html_index_content()
  187. self.assertIn("<title>Metrics &amp; stuff!</title>", index)
  188. self.assertIn("<h1>Metrics &amp; stuff!:", index)
  189. def test_non_ascii_title_set_in_config_file(self):
  190. self.create_initial_files()
  191. self.make_file(".coveragerc", "[html]\ntitle = «ταБЬℓσ» numbers")
  192. self.run_coverage()
  193. index = self.get_html_index_content()
  194. self.assertIn(
  195. "<title>&#171;&#964;&#945;&#1041;&#1068;&#8467;&#963;&#187;"
  196. " numbers", index
  197. )
  198. self.assertIn(
  199. "<h1>&#171;&#964;&#945;&#1041;&#1068;&#8467;&#963;&#187;"
  200. " numbers", index
  201. )
  202. def test_title_set_in_args(self):
  203. self.create_initial_files()
  204. self.make_file(".coveragerc", "[html]\ntitle = Good title\n")
  205. self.run_coverage(htmlargs=dict(title="«ταБЬℓσ» & stüff!"))
  206. index = self.get_html_index_content()
  207. self.assertIn(
  208. "<title>&#171;&#964;&#945;&#1041;&#1068;&#8467;&#963;&#187;"
  209. " &amp; st&#252;ff!</title>", index
  210. )
  211. self.assertIn(
  212. "<h1>&#171;&#964;&#945;&#1041;&#1068;&#8467;&#963;&#187;"
  213. " &amp; st&#252;ff!:", index
  214. )
  215. class HtmlWithUnparsableFilesTest(HtmlTestHelpers, CoverageTest):
  216. """Test the behavior when measuring unparsable files."""
  217. def test_dotpy_not_python(self):
  218. self.make_file("innocuous.py", "a = 1")
  219. cov = coverage.Coverage()
  220. self.start_import_stop(cov, "innocuous")
  221. self.make_file("innocuous.py", "<h1>This isn't python!</h1>")
  222. msg = "Couldn't parse '.*innocuous.py' as Python source: .* at line 1"
  223. with self.assertRaisesRegex(NotPython, msg):
  224. cov.html_report()
  225. def test_dotpy_not_python_ignored(self):
  226. self.make_file("innocuous.py", "a = 2")
  227. cov = coverage.Coverage()
  228. self.start_import_stop(cov, "innocuous")
  229. self.make_file("innocuous.py", "<h1>This isn't python!</h1>")
  230. cov.html_report(ignore_errors=True)
  231. self.assertEqual(
  232. len(cov._warnings),
  233. 1,
  234. "Expected a warning to be thrown when an invalid python file is parsed")
  235. self.assertIn(
  236. "Could not parse Python file",
  237. cov._warnings[0],
  238. "Warning message should be in 'invalid file' warning"
  239. )
  240. self.assertIn(
  241. "innocuous.py",
  242. cov._warnings[0],
  243. "Filename should be in 'invalid file' warning"
  244. )
  245. self.assert_exists("htmlcov/index.html")
  246. # This would be better as a glob, if the HTML layout changes:
  247. self.assert_doesnt_exist("htmlcov/innocuous.html")
  248. def test_dothtml_not_python(self):
  249. # We run a .html file, and when reporting, we can't parse it as
  250. # Python. Since it wasn't .py, no error is reported.
  251. # Run an "HTML" file
  252. self.make_file("innocuous.html", "a = 3")
  253. self.run_command("coverage run innocuous.html")
  254. # Before reporting, change it to be an HTML file.
  255. self.make_file("innocuous.html", "<h1>This isn't python at all!</h1>")
  256. output = self.run_command("coverage html")
  257. self.assertEqual(output.strip(), "No data to report.")
  258. def test_execed_liar_ignored(self):
  259. # Jinja2 sets __file__ to be a non-Python file, and then execs code.
  260. # If that file contains non-Python code, a TokenError shouldn't
  261. # have been raised when writing the HTML report.
  262. source = "exec(compile('','','exec'), {'__file__': 'liar.html'})"
  263. self.make_file("liar.py", source)
  264. self.make_file("liar.html", "{# Whoops, not python code #}")
  265. cov = coverage.Coverage()
  266. self.start_import_stop(cov, "liar")
  267. cov.html_report()
  268. self.assert_exists("htmlcov/index.html")
  269. def test_execed_liar_ignored_indentation_error(self):
  270. # Jinja2 sets __file__ to be a non-Python file, and then execs code.
  271. # If that file contains untokenizable code, we shouldn't get an
  272. # exception.
  273. source = "exec(compile('','','exec'), {'__file__': 'liar.html'})"
  274. self.make_file("liar.py", source)
  275. # Tokenize will raise an IndentationError if it can't dedent.
  276. self.make_file("liar.html", "0\n 2\n 1\n")
  277. cov = coverage.Coverage()
  278. self.start_import_stop(cov, "liar")
  279. cov.html_report()
  280. self.assert_exists("htmlcov/index.html")
  281. def test_decode_error(self):
  282. # https://bitbucket.org/ned/coveragepy/issue/351/files-with-incorrect-encoding-are-ignored
  283. # imp.load_module won't load a file with an undecodable character
  284. # in a comment, though Python will run them. So we'll change the
  285. # file after running.
  286. self.make_file("main.py", "import sub.not_ascii")
  287. self.make_file("sub/__init__.py")
  288. self.make_file("sub/not_ascii.py", """\
  289. # coding: utf8
  290. a = 1 # Isn't this great?!
  291. """)
  292. cov = coverage.Coverage()
  293. self.start_import_stop(cov, "main")
  294. # Create the undecodable version of the file. make_file is too helpful,
  295. # so get down and dirty with bytes.
  296. with open("sub/not_ascii.py", "wb") as f:
  297. f.write(b"# coding: utf8\na = 1 # Isn't this great?\xcb!\n")
  298. with open("sub/not_ascii.py", "rb") as f:
  299. undecodable = f.read()
  300. self.assertIn(b"?\xcb!", undecodable)
  301. cov.html_report()
  302. html_report = self.get_html_report_content("sub/not_ascii.py")
  303. expected = "# Isn't this great?&#65533;!"
  304. self.assertIn(expected, html_report)
  305. def test_formfeeds(self):
  306. # https://bitbucket.org/ned/coveragepy/issue/360/html-reports-get-confused-by-l-in-the-code
  307. self.make_file("formfeed.py", "line_one = 1\n\f\nline_two = 2\n")
  308. cov = coverage.Coverage()
  309. self.start_import_stop(cov, "formfeed")
  310. cov.html_report()
  311. formfeed_html = self.get_html_report_content("formfeed.py")
  312. self.assertIn("line_two", formfeed_html)
  313. class HtmlTest(HtmlTestHelpers, CoverageTest):
  314. """Moar HTML tests."""
  315. def test_missing_source_file_incorrect_message(self):
  316. # https://bitbucket.org/ned/coveragepy/issue/60
  317. self.make_file("thefile.py", "import sub.another\n")
  318. self.make_file("sub/__init__.py", "")
  319. self.make_file("sub/another.py", "print('another')\n")
  320. cov = coverage.Coverage()
  321. self.start_import_stop(cov, 'thefile')
  322. os.remove("sub/another.py")
  323. missing_file = os.path.join(self.temp_dir, "sub", "another.py")
  324. missing_file = os.path.realpath(missing_file)
  325. msg = "(?i)No source for code: '%s'" % re.escape(missing_file)
  326. with self.assertRaisesRegex(NoSource, msg):
  327. cov.html_report()
  328. def test_extensionless_file_collides_with_extension(self):
  329. # It used to be that "program" and "program.py" would both be reported
  330. # to "program.html". Now they are not.
  331. # https://bitbucket.org/ned/coveragepy/issue/69
  332. self.make_file("program", "import program\n")
  333. self.make_file("program.py", "a = 1\n")
  334. self.run_command("coverage run program")
  335. self.run_command("coverage html")
  336. self.assert_exists("htmlcov/index.html")
  337. self.assert_exists("htmlcov/program.html")
  338. self.assert_exists("htmlcov/program_py.html")
  339. def test_has_date_stamp_in_files(self):
  340. self.create_initial_files()
  341. self.run_coverage()
  342. with open("htmlcov/index.html") as f:
  343. self.assert_correct_timestamp(f.read())
  344. with open("htmlcov/main_file_py.html") as f:
  345. self.assert_correct_timestamp(f.read())
  346. def test_reporting_on_unmeasured_file(self):
  347. # It should be ok to ask for an HTML report on a file that wasn't even
  348. # measured at all. https://bitbucket.org/ned/coveragepy/issues/403
  349. self.create_initial_files()
  350. self.make_file("other.py", "a = 1\n")
  351. self.run_coverage(htmlargs=dict(morfs=['other.py']))
  352. self.assert_exists("htmlcov/index.html")
  353. self.assert_exists("htmlcov/other_py.html")
  354. def test_shining_panda_fix(self):
  355. # The ShiningPanda plugin looks for "status.dat" to find HTML reports.
  356. # Accomodate them, but only if we are running under Jenkins.
  357. self.set_environ("JENKINS_URL", "Something or other")
  358. self.create_initial_files()
  359. self.run_coverage()
  360. self.assert_exists("htmlcov/status.dat")
  361. class HtmlStaticFileTest(CoverageTest):
  362. """Tests of the static file copying for the HTML report."""
  363. def setUp(self):
  364. super(HtmlStaticFileTest, self).setUp()
  365. self.original_path = list(coverage.html.STATIC_PATH)
  366. self.addCleanup(self.cleanup_static_path)
  367. def cleanup_static_path(self):
  368. """A cleanup."""
  369. coverage.html.STATIC_PATH = self.original_path
  370. def test_copying_static_files_from_system(self):
  371. # Make a new place for static files.
  372. self.make_file("static_here/jquery.min.js", "Not Really JQuery!")
  373. coverage.html.STATIC_PATH.insert(0, "static_here")
  374. self.make_file("main.py", "print(17)")
  375. cov = coverage.Coverage()
  376. self.start_import_stop(cov, "main")
  377. cov.html_report()
  378. with open("htmlcov/jquery.min.js") as f:
  379. jquery = f.read()
  380. self.assertEqual(jquery, "Not Really JQuery!")
  381. def test_copying_static_files_from_system_in_dir(self):
  382. # Make a new place for static files.
  383. INSTALLED = [
  384. "jquery/jquery.min.js",
  385. "jquery-hotkeys/jquery.hotkeys.js",
  386. "jquery-isonscreen/jquery.isonscreen.js",
  387. "jquery-tablesorter/jquery.tablesorter.min.js",
  388. ]
  389. for fpath in INSTALLED:
  390. self.make_file(os.path.join("static_here", fpath), "Not real.")
  391. coverage.html.STATIC_PATH.insert(0, "static_here")
  392. self.make_file("main.py", "print(17)")
  393. cov = coverage.Coverage()
  394. self.start_import_stop(cov, "main")
  395. cov.html_report()
  396. for fpath in INSTALLED:
  397. the_file = os.path.basename(fpath)
  398. with open(os.path.join("htmlcov", the_file)) as f:
  399. contents = f.read()
  400. self.assertEqual(contents, "Not real.")
  401. def test_cant_find_static_files(self):
  402. # Make the path point to useless places.
  403. coverage.html.STATIC_PATH = ["/xyzzy"]
  404. self.make_file("main.py", "print(17)")
  405. cov = coverage.Coverage()
  406. self.start_import_stop(cov, "main")
  407. msg = "Couldn't find static file u?'.*'"
  408. with self.assertRaisesRegex(CoverageException, msg):
  409. cov.html_report()
  410. class HtmlGoldTests(CoverageGoldTest):
  411. """Tests of HTML reporting that use gold files."""
  412. root_dir = 'tests/farm/html'
  413. def test_a(self):
  414. self.output_dir("out/a")
  415. with change_dir("src"):
  416. # pylint: disable=import-error
  417. cov = coverage.Coverage()
  418. cov.start()
  419. import a # pragma: nested
  420. cov.stop() # pragma: nested
  421. cov.html_report(a, directory='../out/a')
  422. compare("gold_a", "out/a", size_within=10, file_pattern="*.html")
  423. contains(
  424. "out/a/a_py.html",
  425. ('<span class="key">if</span> <span class="num">1</span> '
  426. '<span class="op">&lt;</span> <span class="num">2</span>'),
  427. (' <span class="nam">a</span> '
  428. '<span class="op">=</span> <span class="num">3</span>'),
  429. '<span class="pc_cov">67%</span>',
  430. )
  431. contains(
  432. "out/a/index.html",
  433. '<a href="a_py.html">a.py</a>',
  434. '<span class="pc_cov">67%</span>',
  435. '<td class="right" data-ratio="2 3">67%</td>',
  436. )
  437. def test_b_branch(self):
  438. self.output_dir("out/b_branch")
  439. with change_dir("src"):
  440. # pylint: disable=import-error
  441. cov = coverage.Coverage(branch=True)
  442. cov.start()
  443. import b # pragma: nested
  444. cov.stop() # pragma: nested
  445. cov.html_report(b, directory="../out/b_branch")
  446. compare("gold_b_branch", "out/b_branch", size_within=10, file_pattern="*.html")
  447. contains(
  448. "out/b_branch/b_py.html",
  449. ('<span class="key">if</span> <span class="nam">x</span> '
  450. '<span class="op">&lt;</span> <span class="num">2</span>'),
  451. (' <span class="nam">a</span> <span class="op">=</span> '
  452. '<span class="num">3</span>'),
  453. '<span class="pc_cov">70%</span>',
  454. ('<span class="annotate short">8&#x202F;&#x219B;&#x202F;11</span>'
  455. '<span class="annotate long">line 8 didn\'t jump to line 11, '
  456. 'because the condition on line 8 was never false</span>'),
  457. ('<span class="annotate short">17&#x202F;&#x219B;&#x202F;exit</span>'
  458. '<span class="annotate long">line 17 didn\'t return from function \'two\', '
  459. 'because the condition on line 17 was never false</span>'),
  460. ('<span class="annotate short">25&#x202F;&#x219B;&#x202F;26,&nbsp;&nbsp; '
  461. '25&#x202F;&#x219B;&#x202F;28</span>'
  462. '<span class="annotate long">2 missed branches: '
  463. '1) line 25 didn\'t jump to line 26, '
  464. 'because the condition on line 25 was never true, '
  465. '2) line 25 didn\'t jump to line 28, '
  466. 'because the condition on line 25 was never false</span>'),
  467. )
  468. contains(
  469. "out/b_branch/index.html",
  470. '<a href="b_py.html">b.py</a>',
  471. '<span class="pc_cov">70%</span>',
  472. '<td class="right" data-ratio="16 23">70%</td>',
  473. )
  474. def test_bom(self):
  475. self.output_dir("out/bom")
  476. with change_dir("src"):
  477. # pylint: disable=import-error
  478. cov = coverage.Coverage()
  479. cov.start()
  480. import bom # pragma: nested
  481. cov.stop() # pragma: nested
  482. cov.html_report(bom, directory="../out/bom")
  483. compare("gold_bom", "out/bom", size_within=10, file_pattern="*.html")
  484. contains(
  485. "out/bom/bom_py.html",
  486. '<span class="str">"3&#215;4 = 12, &#247;2 = 6&#177;0"</span>',
  487. )
  488. def test_isolatin1(self):
  489. self.output_dir("out/isolatin1")
  490. with change_dir("src"):
  491. # pylint: disable=import-error
  492. cov = coverage.Coverage()
  493. cov.start()
  494. import isolatin1 # pragma: nested
  495. cov.stop() # pragma: nested
  496. cov.html_report(isolatin1, directory="../out/isolatin1")
  497. compare("gold_isolatin1", "out/isolatin1", size_within=10, file_pattern="*.html")
  498. contains(
  499. "out/isolatin1/isolatin1_py.html",
  500. '<span class="str">"3&#215;4 = 12, &#247;2 = 6&#177;0"</span>',
  501. )
  502. def test_omit_1(self):
  503. self.output_dir("out/omit_1")
  504. with change_dir("src"):
  505. # pylint: disable=import-error
  506. cov = coverage.Coverage(include=["./*"])
  507. cov.start()
  508. import main # pragma: nested
  509. cov.stop() # pragma: nested
  510. cov.html_report(directory="../out/omit_1")
  511. compare("gold_omit_1", "out/omit_1", size_within=10, file_pattern="*.html")
  512. def test_omit_2(self):
  513. self.output_dir("out/omit_2")
  514. with change_dir("src"):
  515. # pylint: disable=import-error
  516. cov = coverage.Coverage(include=["./*"])
  517. cov.start()
  518. import main # pragma: nested
  519. cov.stop() # pragma: nested
  520. cov.html_report(directory="../out/omit_2", omit=["m1.py"])
  521. compare("gold_omit_2", "out/omit_2", size_within=10, file_pattern="*.html")
  522. def test_omit_3(self):
  523. self.output_dir("out/omit_3")
  524. with change_dir("src"):
  525. # pylint: disable=import-error
  526. cov = coverage.Coverage(include=["./*"])
  527. cov.start()
  528. import main # pragma: nested
  529. cov.stop() # pragma: nested
  530. cov.html_report(directory="../out/omit_3", omit=["m1.py", "m2.py"])
  531. compare("gold_omit_3", "out/omit_3", size_within=10, file_pattern="*.html")
  532. def test_omit_4(self):
  533. self.output_dir("out/omit_4")
  534. with change_dir("src"):
  535. # pylint: disable=import-error
  536. cov = coverage.Coverage(config_file="omit4.ini", include=["./*"])
  537. cov.start()
  538. import main # pragma: nested
  539. cov.stop() # pragma: nested
  540. cov.html_report(directory="../out/omit_4")
  541. compare("gold_omit_4", "out/omit_4", size_within=10, file_pattern="*.html")
  542. def test_omit_5(self):
  543. self.output_dir("out/omit_5")
  544. with change_dir("src"):
  545. # pylint: disable=import-error
  546. cov = coverage.Coverage(config_file="omit5.ini", include=["./*"])
  547. cov.start()
  548. import main # pragma: nested
  549. cov.stop() # pragma: nested
  550. cov.html_report()
  551. compare("gold_omit_5", "out/omit_5", size_within=10, file_pattern="*.html")
  552. def test_other(self):
  553. self.output_dir("out/other")
  554. with change_dir("src"):
  555. # pylint: disable=import-error
  556. sys.path.insert(0, "../othersrc")
  557. cov = coverage.Coverage(include=["./*", "../othersrc/*"])
  558. cov.start()
  559. import here # pragma: nested
  560. cov.stop() # pragma: nested
  561. cov.html_report(directory="../out/other")
  562. # Different platforms will name the "other" file differently. Rename it
  563. for p in glob.glob("out/other/*_other_py.html"):
  564. os.rename(p, "out/other/blah_blah_other_py.html")
  565. compare("gold_other", "out/other", size_within=10, file_pattern="*.html")
  566. contains(
  567. "out/other/index.html",
  568. '<a href="here_py.html">here.py</a>',
  569. 'other_py.html">', 'other.py</a>',
  570. )
  571. def test_partial(self):
  572. self.output_dir("out/partial")
  573. with change_dir("src"):
  574. # pylint: disable=import-error
  575. cov = coverage.Coverage(branch=True)
  576. cov.start()
  577. import partial # pragma: nested
  578. cov.stop() # pragma: nested
  579. cov.html_report(partial, directory="../out/partial")
  580. compare("gold_partial", "out/partial", size_within=10, file_pattern="*.html")
  581. contains(
  582. "out/partial/partial_py.html",
  583. '<p id="t8" class="stm run hide_run">',
  584. '<p id="t11" class="stm run hide_run">',
  585. '<p id="t14" class="stm run hide_run">',
  586. # The "if 0" and "if 1" statements are optimized away.
  587. '<p id="t17" class="pln">',
  588. )
  589. contains(
  590. "out/partial/index.html",
  591. '<a href="partial_py.html">partial.py</a>',
  592. )
  593. contains(
  594. "out/partial/index.html",
  595. '<span class="pc_cov">100%</span>'
  596. )
  597. def test_styled(self):
  598. self.output_dir("out/styled")
  599. with change_dir("src"):
  600. # pylint: disable=import-error
  601. cov = coverage.Coverage()
  602. cov.start()
  603. import a # pragma: nested
  604. cov.stop() # pragma: nested
  605. cov.html_report(a, directory="../out/styled", extra_css="extra.css")
  606. compare("gold_styled", "out/styled", size_within=10, file_pattern="*.html")
  607. compare("gold_styled", "out/styled", size_within=10, file_pattern="*.css")
  608. contains(
  609. "out/styled/a_py.html",
  610. '<link rel="stylesheet" href="extra.css" type="text/css">',
  611. ('<span class="key">if</span> <span class="num">1</span> '
  612. '<span class="op">&lt;</span> <span class="num">2</span>'),
  613. (' <span class="nam">a</span> <span class="op">=</span> '
  614. '<span class="num">3</span>'),
  615. '<span class="pc_cov">67%</span>'
  616. )
  617. contains(
  618. "out/styled/index.html",
  619. '<link rel="stylesheet" href="extra.css" type="text/css">',
  620. '<a href="a_py.html">a.py</a>',
  621. '<span class="pc_cov">67%</span>'
  622. )
  623. def test_tabbed(self):
  624. self.output_dir("out/tabbed")
  625. with change_dir("src"):
  626. # pylint: disable=import-error
  627. cov = coverage.Coverage()
  628. cov.start()
  629. import tabbed # pragma: nested
  630. cov.stop() # pragma: nested
  631. cov.html_report(tabbed, directory="../out/tabbed")
  632. # Editors like to change things, make sure our source file still has tabs.
  633. contains("src/tabbed.py", "\tif x:\t\t\t\t\t# look nice")
  634. contains(
  635. "out/tabbed/tabbed_py.html",
  636. '> <span class="key">if</span> '
  637. '<span class="nam">x</span><span class="op">:</span>'
  638. ' '
  639. '<span class="com"># look nice</span>'
  640. )
  641. doesnt_contain("out/tabbed/tabbed_py.html", "\t")
  642. def test_unicode(self):
  643. self.output_dir("out/unicode")
  644. with change_dir("src"):
  645. # pylint: disable=import-error, redefined-builtin
  646. cov = coverage.Coverage()
  647. cov.start()
  648. import unicode # pragma: nested
  649. cov.stop() # pragma: nested
  650. cov.html_report(unicode, directory="../out/unicode")
  651. compare("gold_unicode", "out/unicode", size_within=10, file_pattern="*.html")
  652. contains(
  653. "out/unicode/unicode_py.html",
  654. '<span class="str">"&#654;d&#729;&#477;b&#592;&#633;&#477;&#652;o&#596;"</span>',
  655. )
  656. contains_any(
  657. "out/unicode/unicode_py.html",
  658. '<span class="str">"db40,dd00: x&#56128;&#56576;"</span>',
  659. '<span class="str">"db40,dd00: x&#917760;"</span>',
  660. )