# 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 that HTML generation is awesome.""" from __future__ import annotations import collections import datetime import glob import json import os import os.path import re import sys from unittest import mock from typing import Any, Dict, IO, List, Optional, Set, Tuple import pytest import coverage from coverage import env, Coverage from coverage.exceptions import NoDataError, NotPython, NoSource from coverage.files import abs_file, flat_rootname import coverage.html from coverage.report_core import get_analysis_to_report from coverage.types import TLineNo, TMorf from tests.coveragetest import CoverageTest, TESTS_DIR from tests.goldtest import gold_path from tests.goldtest import compare, contains, contains_rx, doesnt_contain, contains_any from tests.helpers import assert_coverage_warnings, change_dir class HtmlTestHelpers(CoverageTest): """Methods that help with HTML tests.""" def create_initial_files(self) -> None: """Create the source files we need to run these tests.""" self.make_file("main_file.py", """\ import helper1, helper2 helper1.func1(12) helper2.func2(12) """) self.make_file("helper1.py", """\ def func1(x): if x % 2: print("odd") """) self.make_file("helper2.py", """\ def func2(x): print("x is %d" % x) """) def run_coverage( self, covargs: Optional[Dict[str, Any]] = None, htmlargs: Optional[Dict[str, Any]] = None, ) -> float: """Run coverage.py on main_file.py, and create an HTML report.""" self.clean_local_file_imports() cov = coverage.Coverage(**(covargs or {})) self.start_import_stop(cov, "main_file") ret = cov.html_report(**(htmlargs or {})) self.assert_valid_hrefs() return ret def get_html_report_content(self, module: str) -> str: """Return the content of the HTML report for `module`.""" filename = flat_rootname(module) + ".html" filename = os.path.join("htmlcov", filename) with open(filename) as f: return f.read() def get_html_index_content(self) -> str: """Return the content of index.html. Time stamps are replaced with a placeholder so that clocks don't matter. """ with open("htmlcov/index.html") as f: index = f.read() index = re.sub( r"created at \d{4}-\d{2}-\d{2} \d{2}:\d{2} \+\d{4}", r"created at YYYY-MM-DD HH:MM +ZZZZ", index, ) index = re.sub( r"created at \d{4}-\d{2}-\d{2} \d{2}:\d{2}", r"created at YYYY-MM-DD HH:MM", index, ) return index def assert_correct_timestamp(self, html: str) -> None: """Extract the time stamp from `html`, and assert it is recent.""" timestamp_pat = r"created at (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})" m = re.search(timestamp_pat, html) assert m, "Didn't find a time stamp!" timestamp = datetime.datetime(*[int(v) for v in m.groups()]) # type: ignore[arg-type] # The time stamp only records the minute, so the delta could be from # 12:00 to 12:01:59, or two minutes. self.assert_recent_datetime( timestamp, seconds=120, msg=f"Time stamp is wrong: {timestamp}", ) def assert_valid_hrefs(self) -> None: """Assert that the hrefs in htmlcov/*.html to see the references are valid. Doesn't check external links (those with a protocol). """ hrefs = collections.defaultdict(set) for fname in glob.glob("htmlcov/*.html"): with open(fname) as fhtml: html = fhtml.read() for href in re.findall(r""" href=['"]([^'"]*)['"]""", html): if href.startswith("#"): assert re.search(rf""" id=['"]{href[1:]}['"]""", html), ( f"Fragment {href!r} in {fname} has no anchor" ) continue if "://" in href: continue hrefs[href].add(fname) for href, sources in hrefs.items(): assert os.path.exists(f"htmlcov/{href}"), ( f"These files link to {href!r}, which doesn't exist: {', '.join(sources)}" ) class FileWriteTracker: """A fake object to track how `open` is used to write files.""" def __init__(self, written: Set[str]) -> None: self.written = written def open(self, filename: str, mode: str = "r") -> IO[str]: """Be just like `open`, but write written file names to `self.written`.""" if mode.startswith("w"): self.written.add(filename.replace('\\', '/')) return open(filename, mode) class HtmlDeltaTest(HtmlTestHelpers, CoverageTest): """Tests of the HTML delta speed-ups.""" def setUp(self) -> None: super().setUp() # At least one of our tests monkey-patches the version of coverage.py, # so grab it here to restore it later. self.real_coverage_version = coverage.__version__ self.addCleanup(setattr, coverage, "__version__", self.real_coverage_version) self.files_written: Set[str] def run_coverage( self, covargs: Optional[Dict[str, Any]] = None, htmlargs: Optional[Dict[str, Any]] = None, ) -> float: """Run coverage in-process for the delta tests. For the delta tests, we always want `source=.` and we want to track which files are written. `self.files_written` will be the file names that were opened for writing in html.py. """ covargs = covargs or {} covargs['source'] = "." self.files_written = set() mock_open = FileWriteTracker(self.files_written).open with mock.patch("coverage.html.open", mock_open): return super().run_coverage(covargs=covargs, htmlargs=htmlargs) def assert_htmlcov_files_exist(self) -> None: """Assert that all the expected htmlcov files exist.""" self.assert_exists("htmlcov/index.html") self.assert_exists("htmlcov/main_file_py.html") self.assert_exists("htmlcov/helper1_py.html") self.assert_exists("htmlcov/helper2_py.html") self.assert_exists("htmlcov/style.css") self.assert_exists("htmlcov/coverage_html.js") self.assert_exists("htmlcov/.gitignore") def test_html_created(self) -> None: # Test basic HTML generation: files should be created. self.create_initial_files() self.run_coverage() self.assert_htmlcov_files_exist() def test_html_delta_from_source_change(self) -> None: # HTML generation can create only the files that have changed. # In this case, helper1 changes because its source is different. self.create_initial_files() self.run_coverage() index1 = self.get_html_index_content() # Now change a file (but only in a comment) and do it again. self.make_file("helper1.py", """\ def func1(x): # A nice function if x % 2: print("odd") """) self.run_coverage() # Only the changed files should have been created. self.assert_htmlcov_files_exist() assert "htmlcov/index.html" in self.files_written assert "htmlcov/helper1_py.html" in self.files_written assert "htmlcov/helper2_py.html" not in self.files_written assert "htmlcov/main_file_py.html" not in self.files_written # Because the source change was only a comment, the index is the same. index2 = self.get_html_index_content() assert index1 == index2 def test_html_delta_from_coverage_change(self) -> None: # HTML generation can create only the files that have changed. # In this case, helper1 changes because its coverage is different. self.create_initial_files() self.run_coverage() # Now change a file and do it again. main_file is different, and calls # helper1 differently. self.make_file("main_file.py", """\ import helper1, helper2 helper1.func1(23) helper2.func2(23) """) self.run_coverage() # Only the changed files should have been created. self.assert_htmlcov_files_exist() assert "htmlcov/index.html" in self.files_written assert "htmlcov/helper1_py.html" in self.files_written assert "htmlcov/helper2_py.html" not in self.files_written assert "htmlcov/main_file_py.html" in self.files_written def test_html_delta_from_settings_change(self) -> None: # HTML generation can create only the files that have changed. # In this case, everything changes because the coverage.py settings # have changed. self.create_initial_files() self.run_coverage(covargs=dict(omit=[])) index1 = self.get_html_index_content() self.run_coverage(covargs=dict(omit=['xyzzy*'])) # All the files have been reported again. self.assert_htmlcov_files_exist() assert "htmlcov/index.html" in self.files_written assert "htmlcov/helper1_py.html" in self.files_written assert "htmlcov/helper2_py.html" in self.files_written assert "htmlcov/main_file_py.html" in self.files_written index2 = self.get_html_index_content() assert index1 == index2 def test_html_delta_from_coverage_version_change(self) -> None: # HTML generation can create only the files that have changed. # In this case, everything changes because the coverage.py version has # changed. self.create_initial_files() self.run_coverage() index1 = self.get_html_index_content() # "Upgrade" coverage.py! coverage.__version__ = "XYZZY" self.run_coverage() # All the files have been reported again. self.assert_htmlcov_files_exist() assert "htmlcov/index.html" in self.files_written assert "htmlcov/helper1_py.html" in self.files_written assert "htmlcov/helper2_py.html" in self.files_written assert "htmlcov/main_file_py.html" in self.files_written index2 = self.get_html_index_content() fixed_index2 = index2.replace("XYZZY", self.real_coverage_version) assert index1 == fixed_index2 def test_file_becomes_100(self) -> None: self.create_initial_files() self.run_coverage() # Now change a file and do it again self.make_file("main_file.py", """\ import helper1, helper2 # helper1 is now 100% helper1.func1(12) helper1.func1(23) """) self.run_coverage(htmlargs=dict(skip_covered=True)) # The 100% file, skipped, shouldn't be here. self.assert_doesnt_exist("htmlcov/helper1_py.html") def test_status_format_change(self) -> None: self.create_initial_files() self.run_coverage() with open("htmlcov/status.json") as status_json: status_data = json.load(status_json) assert status_data['format'] == 2 status_data['format'] = 99 with open("htmlcov/status.json", "w") as status_json: json.dump(status_data, status_json) self.run_coverage() # All the files have been reported again. self.assert_htmlcov_files_exist() assert "htmlcov/index.html" in self.files_written assert "htmlcov/helper1_py.html" in self.files_written assert "htmlcov/helper2_py.html" in self.files_written assert "htmlcov/main_file_py.html" in self.files_written def test_dont_overwrite_gitignore(self) -> None: self.create_initial_files() self.make_file("htmlcov/.gitignore", "# ignore nothing") self.run_coverage() with open("htmlcov/.gitignore") as fgi: assert fgi.read() == "# ignore nothing" def test_dont_write_gitignore_into_existing_directory(self) -> None: self.create_initial_files() self.make_file("htmlcov/README", "My files: don't touch!") self.run_coverage() self.assert_doesnt_exist("htmlcov/.gitignore") self.assert_exists("htmlcov/index.html") class HtmlTitleTest(HtmlTestHelpers, CoverageTest): """Tests of the HTML title support.""" def test_default_title(self) -> None: self.create_initial_files() self.run_coverage() index = self.get_html_index_content() assert "
.* id="t4"', r'
.* id="t7"', # The "if 0" and "if 1" statements are marked as run. r'
.* id="t10"', # The "raise ZeroDivisionError" is excluded by regex in the .ini. r'
.* id="t17"', ) contains( "out/partial_626/index.html", 'partial.py', '87%', ) else: cov.html_report(partial, directory="out/partial") compare_html(gold_path("html/partial"), "out/partial") contains_rx( "out/partial/partial_py.html", r'
.* id="t4"', r'
.* id="t7"', # The "if 0" and "if 1" statements are optimized away. r'
.* id="t10"', # The "raise ZeroDivisionError" is excluded by regex in the .ini. r'
.* id="t17"', ) contains( "out/partial/index.html", 'partial.py', '91%', ) def test_styled(self) -> None: self.make_file("a.py", """\ if 1 < 2: # Needed a < to look at HTML entities. a = 3 else: a = 4 """) self.make_file("extra.css", "/* Doesn't matter what goes in here, it gets copied. */\n") cov = coverage.Coverage() a = self.start_import_stop(cov, "a") cov.html_report(a, directory="out/styled", extra_css="extra.css") compare_html(gold_path("html/styled"), "out/styled") compare(gold_path("html/styled"), "out/styled", file_pattern="*.css") contains( "out/styled/a_py.html", '', ('if 1 ' + '< 2'), (' a = ' + '3'), '67%', ) contains( "out/styled/index.html", '', 'a.py', '67%', ) def test_tabbed(self) -> None: # The file contents would look like this with 8-space tabs: # x = 1 # if x: # a = "tabbed" # aligned comments # if x: # look nice # b = "no spaces" # when they # c = "done" # line up. self.make_file("tabbed.py", """\ x = 1 if x: \ta = "Tabbed"\t\t\t\t# Aligned comments \tif x:\t\t\t\t\t# look nice \t\tb = "No spaces"\t\t\t# when they \tc = "Done"\t\t\t\t# line up. """) cov = coverage.Coverage() tabbed = self.start_import_stop(cov, "tabbed") cov.html_report(tabbed, directory="out") # Editors like to change things, make sure our source file still has tabs. contains("tabbed.py", "\tif x:\t\t\t\t\t# look nice") contains( "out/tabbed_py.html", '> if ' + 'x:' + ' ' + '# look nice', ) doesnt_contain("out/tabbed_py.html", "\t") def test_unicode(self) -> None: surrogate = "\U000e0100" self.make_file("unicode.py", """\ # -*- coding: utf-8 -*- # A Python source file with exotic characters. upside_down = "ʎd˙ǝbɐɹǝʌoɔ" surrogate = "db40,dd00: x@" """.replace("@", surrogate)) cov = coverage.Coverage() unimod = self.start_import_stop(cov, "unicode") cov.html_report(unimod, directory="out/unicode") compare_html(gold_path("html/unicode"), "out/unicode") contains( "out/unicode/unicode_py.html", '"ʎd˙ǝbɐɹǝʌoɔ"', ) contains_any( "out/unicode/unicode_py.html", '"db40,dd00: x"', '"db40,dd00: x󠄀"', ) def test_accented_dot_py(self) -> None: # Make a file with a non-ascii character in the filename. self.make_file("h\xe2t.py", "print('accented')") self.make_data_file(lines={abs_file("h\xe2t.py"): [1]}) cov = coverage.Coverage() cov.load() cov.html_report() self.assert_exists("htmlcov/h\xe2t_py.html") with open("htmlcov/index.html") as indexf: index = indexf.read() assert 'hât.py' in index def test_accented_directory(self) -> None: # Make a file with a non-ascii character in the directory name. self.make_file("\xe2/accented.py", "print('accented')") self.make_data_file(lines={abs_file("\xe2/accented.py"): [1]}) # The HTML report uses ascii-encoded HTML entities. cov = coverage.Coverage() cov.load() cov.html_report() self.assert_exists("htmlcov/d_5786906b6f0ffeb4_accented_py.html") with open("htmlcov/index.html") as indexf: index = indexf.read() expected = 'â%saccented.py' assert expected % os.sep in index class HtmlWithContextsTest(HtmlTestHelpers, CoverageTest): """Tests of the HTML reports with shown contexts.""" EMPTY = coverage.html.HtmlDataGeneration.EMPTY def html_data_from_cov(self, cov: Coverage, morf: TMorf) -> coverage.html.FileData: """Get HTML report data from a `Coverage` object for a morf.""" with self.assert_warnings(cov, []): datagen = coverage.html.HtmlDataGeneration(cov) fr, analysis = next(get_analysis_to_report(cov, [morf])) file_data = datagen.data_for_file(fr, analysis) return file_data SOURCE = """\ def helper(lineno): x = 2 def test_one(): a = 5 helper(6) def test_two(): a = 9 b = 10 if a > 11: b = 12 assert a == (13-4) assert b == (14-4) helper( 16 ) test_one() x = 20 helper(21) test_two() """ OUTER_LINES = [1, 4, 8, 19, 20, 21, 2, 22] TEST_ONE_LINES = [5, 6, 2] TEST_TWO_LINES = [9, 10, 11, 13, 14, 15, 2] def test_dynamic_contexts(self) -> None: self.make_file("two_tests.py", self.SOURCE) cov = coverage.Coverage(source=["."]) cov.set_option("run:dynamic_context", "test_function") cov.set_option("html:show_contexts", True) mod = self.start_import_stop(cov, "two_tests") d = self.html_data_from_cov(cov, mod) context_labels = [self.EMPTY, 'two_tests.test_one', 'two_tests.test_two'] expected_lines = [self.OUTER_LINES, self.TEST_ONE_LINES, self.TEST_TWO_LINES] for label, expected in zip(context_labels, expected_lines): actual = [ ld.number for ld in d.lines if label == ld.contexts_label or label in (ld.contexts or ()) ] assert sorted(expected) == sorted(actual) cov.html_report(mod, directory="out/contexts") compare_html(gold_path("html/contexts"), "out/contexts") def test_filtered_dynamic_contexts(self) -> None: self.make_file("two_tests.py", self.SOURCE) cov = coverage.Coverage(source=["."]) cov.set_option("run:dynamic_context", "test_function") cov.set_option("html:show_contexts", True) cov.set_option("report:contexts", ["test_one"]) mod = self.start_import_stop(cov, "two_tests") d = self.html_data_from_cov(cov, mod) context_labels = [self.EMPTY, 'two_tests.test_one', 'two_tests.test_two'] expected_lines: List[List[TLineNo]] = [[], self.TEST_ONE_LINES, []] for label, expected in zip(context_labels, expected_lines): actual = [ld.number for ld in d.lines if label in (ld.contexts or ())] assert sorted(expected) == sorted(actual) def test_no_contexts_warns_no_contexts(self) -> None: # If no contexts were collected, then show_contexts emits a warning. self.make_file("two_tests.py", self.SOURCE) cov = coverage.Coverage(source=["."]) cov.set_option("html:show_contexts", True) self.start_import_stop(cov, "two_tests") with self.assert_warnings(cov, ["No contexts were measured"]): cov.html_report() def test_dynamic_contexts_relative_files(self) -> None: self.make_file("two_tests.py", self.SOURCE) self.make_file("config", "[run]\nrelative_files = True") cov = coverage.Coverage(source=["."], config_file="config") cov.set_option("run:dynamic_context", "test_function") cov.set_option("html:show_contexts", True) mod = self.start_import_stop(cov, "two_tests") d = self.html_data_from_cov(cov, mod) context_labels = [self.EMPTY, 'two_tests.test_one', 'two_tests.test_two'] expected_lines = [self.OUTER_LINES, self.TEST_ONE_LINES, self.TEST_TWO_LINES] for label, expected in zip(context_labels, expected_lines): actual = [ ld.number for ld in d.lines if label == ld.contexts_label or label in (ld.contexts or ()) ] assert sorted(expected) == sorted(actual) class HtmlHelpersTest(HtmlTestHelpers, CoverageTest): """Tests of the helpers in HtmlTestHelpers.""" def test_bad_link(self) -> None: # Does assert_valid_hrefs detect links to non-existent files? self.make_file("htmlcov/index.html", "Nothing") msg = "These files link to 'nothing.html', which doesn't exist: htmlcov.index.html" with pytest.raises(AssertionError, match=msg): self.assert_valid_hrefs() def test_bad_anchor(self) -> None: # Does assert_valid_hrefs detect fragments that go nowhere? self.make_file("htmlcov/index.html", "Nothing") msg = "Fragment '#nothing' in htmlcov.index.html has no anchor" with pytest.raises(AssertionError, match=msg): self.assert_valid_hrefs() @pytest.mark.parametrize("n, key", [ (0, "a"), (1, "b"), (999999999, "e9S_p"), ]) def test_encode_int(n: int, key: str) -> None: assert coverage.html.encode_int(n) == key