def test_annotate_dir(self): self.make_multi() cov = coverage.Coverage(source=["."]) self.start_import_stop(cov, "multi") cov.annotate(directory="out_anno_dir") compare(gold_path("annotate/anno_dir"), "out_anno_dir", "*,cover")
def test_multi(self): self.make_multi() cov = coverage.Coverage() self.start_import_stop(cov, "multi") cov.annotate() compare(gold_path("annotate/multi"), ".", "*,cover")
def test_styled(self): self.output_dir("out/styled") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage() cov.start() import a # pragma: nested cov.stop() # pragma: nested cov.html_report(a, directory="../out/styled", extra_css="extra.css") compare_html("gold_styled", "out/styled") compare("gold_styled", "out/styled", size_within=10, file_pattern="*.css") contains("out/styled/a_py.html", '<link rel="stylesheet" href="extra.css" type="text/css">', ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">67%</span>') contains("out/styled/index.html", '<link rel="stylesheet" href="extra.css" type="text/css">', '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>')
def test_b_branch(self): self.output_dir("out/b_branch") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage(branch=True) cov.start() import b # pragma: nested cov.stop() # pragma: nested cov.html_report(b, directory="../out/b_branch") compare("gold_b_branch", "out/b_branch", size_within=10, file_pattern="*.html") contains( "out/b_branch/b_py.html", ('<span class="key">if</span> <span class="nam">x</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">70%</span>', ('<span class="annotate short">8 ↛ 11</span>' '<span class="annotate long">line 8 didn\'t jump to line 11, because the condition on line 8 was never false</span>'), ('<span class="annotate short">17 ↛ exit</span>' '<span class="annotate long">line 17 didn\'t return from function \'two\', because the condition on line 17 was never false</span>'), ('<span class="annotate short">25 ↛ 26, ' '25 ↛ 28</span>' '<span class="annotate long">2 missed branches: 1) line 25 didn\'t jump to line 26, because the condition on line 25 was never true, 2) line 25 didn\'t jump to line 28, because the condition on line 25 was never false</span>'), ) contains( "out/b_branch/index.html", '<a href="b_py.html">b.py</a>', '<span class="pc_cov">70%</span>', '<td class="right" data-ratio="16 23">70%</td>', )
def test_styled(self): self.output_dir("out/styled") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage() cov.start() import a # pragma: nested cov.stop() # pragma: nested cov.html_report(a, directory="../out/styled", extra_css="extra.css") compare("gold_styled", "out/styled", size_within=10, file_pattern="*.html") compare("gold_styled", "out/styled", size_within=10, file_pattern="*.css") contains( "out/styled/a_py.html", '<link rel="stylesheet" href="extra.css" type="text/css">', ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">67%</span>' ) contains( "out/styled/index.html", '<link rel="stylesheet" href="extra.css" type="text/css">', '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>' )
def compare_html(expected, actual, extra_scrubs=None): """Specialized compare function for our HTML files.""" __tracebackhide__ = True # pytest, please don't show me this function. scrubs = [ (r'/coverage.readthedocs.io/?[-.\w/]*', '/coverage.readthedocs.io/VER'), (r'coverage.py v[\d.abc]+', 'coverage.py vVER'), (r'created at \d\d\d\d-\d\d-\d\d \d\d:\d\d [-+]\d\d\d\d', 'created at DATE'), (r'created at \d\d\d\d-\d\d-\d\d \d\d:\d\d', 'created at DATE'), # Occasionally an absolute path is in the HTML report. (filepath_to_regex(TESTS_DIR), 'TESTS_DIR'), (filepath_to_regex(flat_rootname(str(TESTS_DIR))), '_TESTS_DIR'), # The temp dir the tests make. (filepath_to_regex(os.getcwd()), 'TEST_TMPDIR'), (filepath_to_regex(flat_rootname(str(os.getcwd()))), '_TEST_TMPDIR'), (filepath_to_regex(abs_file(os.getcwd())), 'TEST_TMPDIR'), (filepath_to_regex(flat_rootname(str(abs_file(os.getcwd())))), '_TEST_TMPDIR'), (r'/private/var/[\w/]+/pytest-of-\w+/pytest-\d+/(popen-gw\d+/)?t\d+', 'TEST_TMPDIR'), ] if env.WINDOWS: # For file paths... scrubs += [(r"\\", "/")] if extra_scrubs: scrubs += extra_scrubs compare(expected, actual, file_pattern="*.html", scrubs=scrubs)
def compare_html(expected, actual): """Specialized compare function for our HTML files.""" scrubs = [ (r'/coverage.readthedocs.io/?[-.\w/]*', '/coverage.readthedocs.io/VER'), (r'coverage.py v[\d.abc]+', 'coverage.py vVER'), (r'created at \d\d\d\d-\d\d-\d\d \d\d:\d\d [-+]\d\d\d\d', 'created at DATE'), (r'created at \d\d\d\d-\d\d-\d\d \d\d:\d\d', 'created at DATE'), # Some words are identifiers in one version, keywords in another. (r'<span class="(nam|key)">(print|True|False)</span>', r'<span class="nam">\2</span>'), # Occasionally an absolute path is in the HTML report. (filepath_to_regex(TESTS_DIR), 'TESTS_DIR'), (filepath_to_regex(flat_rootname(unicode_class(TESTS_DIR))), '_TESTS_DIR'), # The temp dir the tests make. (filepath_to_regex(os.getcwd()), 'TEST_TMPDIR'), (filepath_to_regex(flat_rootname(unicode_class(os.getcwd()))), '_TEST_TMPDIR'), (filepath_to_regex(abs_file(os.getcwd())), 'TEST_TMPDIR'), (filepath_to_regex(flat_rootname(unicode_class(abs_file( os.getcwd())))), '_TEST_TMPDIR'), (r'/private/var/folders/[\w/]{35}/coverage_test/tests_test_html_\w+_\d{8}', 'TEST_TMPDIR'), (r'_private_var_folders_\w{35}_coverage_test_tests_test_html_\w+_\d{8}', '_TEST_TMPDIR'), ] if env.WINDOWS: # For file paths... scrubs += [(r"\\", "/")] compare(expected, actual, file_pattern="*.html", scrubs=scrubs)
def test_styled(self): 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. */") cov = coverage.Coverage() cov.start() import a # pragma: nested # pylint: disable=import-error cov.stop() # pragma: nested cov.html_report(a, directory="out", extra_css="extra.css") compare_html("out", gold_path("html/gold_styled")) compare("out", gold_path("html/gold_styled"), size_within=10, file_pattern="*.css") contains( "out/a_py.html", '<link rel="stylesheet" href="extra.css" type="text/css">', ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">67%</span>' ) contains( "out/index.html", '<link rel="stylesheet" href="extra.css" type="text/css">', '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>' )
def test_a(self): self.output_dir("out/a") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage() cov.start() import a # pragma: nested cov.stop() # pragma: nested cov.html_report(a, directory='../out/a') compare("gold_a", "out/a", size_within=10, file_pattern="*.html") contains( "out/a/a_py.html", ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> ' '<span class="op">=</span> <span class="num">3</span>'), '<span class="pc_cov">67%</span>', ) contains( "out/a/index.html", '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>', '<td class="right" data-ratio="2 3">67%</td>', )
def test_styled(self): 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", extra_css="extra.css") compare_html(gold_path("html/styled"), "out") compare(gold_path("html/styled"), "out", file_pattern="*.css") contains( "out/a_py.html", '<link rel="stylesheet" href="extra.css" type="text/css">', ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">67%</span>' ) contains( "out/index.html", '<link rel="stylesheet" href="extra.css" type="text/css">', '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>' )
def test_unicode(self): self.output_dir("out/unicode") with change_dir("src"): # pylint: disable=import-error, redefined-builtin cov = coverage.Coverage() cov.start() import unicode # pragma: nested cov.stop() # pragma: nested cov.html_report(unicode, directory="../out/unicode") compare("gold_unicode", "out/unicode", size_within=10, file_pattern="*.html") contains( "out/unicode/unicode_py.html", '<span class="str">"ʎd˙ǝbɐɹǝʌoɔ"</span>', ) contains_any( "out/unicode/unicode_py.html", '<span class="str">"db40,dd00: x��"</span>', '<span class="str">"db40,dd00: x󠄀"</span>', )
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") cov.start() import a # pragma: nested # pylint: disable=import-error cov.stop() # pragma: nested cov.xml_report(a) source_path = coverage.files.relative_directory().rstrip(r"\/") compare("xml_2", gold_path("html/gold_x_xml"), 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'), ])
def test_styled(self): 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", '<link rel="stylesheet" href="extra.css" type="text/css">', ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">67%</span>' ) contains( "out/styled/index.html", '<link rel="stylesheet" href="extra.css" type="text/css">', '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>' )
def test_other(self): self.output_dir("out/other") with change_dir("src"): # pylint: disable=import-error, unused-variable sys.path.insert(0, "../othersrc") cov = coverage.Coverage(include=["./*", "../othersrc/*"]) cov.start() import here # pragma: nested cov.stop() # pragma: nested cov.html_report(directory="../out/other") # Different platforms will name the "other" file differently. Rename it for p in glob.glob("out/other/*_other_py.html"): os.rename(p, "out/other/blah_blah_other_py.html") compare("gold_other", "out/other", size_within=10, file_pattern="*.html") contains( "out/other/index.html", '<a href="here_py.html">here.py</a>', 'other_py.html">', 'other.py</a>', )
def test_b_branch(self): self.output_dir("out/b_branch") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage(branch=True) cov.start() import b # pragma: nested cov.stop() # pragma: nested cov.html_report(b, directory="../out/b_branch") compare("gold_b_branch", "out/b_branch", size_within=10, file_pattern="*.html") contains( "out/b_branch/b_py.html", ('<span class="key">if</span> <span class="nam">x</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">70%</span>', ('<span class="annotate" title="Line 8 was executed, but never jumped to line 11">' '8 ↛ 11 [?]</span>'), ('<span class="annotate" title="Line 17 was executed, but never jumped ' 'to the function exit">17 ↛ exit [?]</span>'), ('<span class="annotate" title="Line 25 was executed, but never jumped ' 'to line 26 or line 28">25 ↛ 26, ' '25 ↛ 28 [?]</span>'), ) contains( "out/b_branch/index.html", '<a href="b_py.html">b.py</a>', '<span class="pc_cov">70%</span>', '<td class="right" data-ratio="16 23">70%</td>', )
def test_partial(self): self.output_dir("out/partial") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage(branch=True) cov.start() import partial # pragma: nested cov.stop() # pragma: nested cov.html_report(partial, directory="../out/partial") compare("gold_partial", "out/partial", size_within=10, file_pattern="*.html") contains( "out/partial/partial_py.html", '<p id="t8" class="stm run hide_run">', '<p id="t11" class="stm run hide_run">', '<p id="t14" class="stm run hide_run">', # The "if 0" and "if 1" statements are optimized away. '<p id="t17" class="pln">', ) contains( "out/partial/index.html", '<a href="partial_py.html">partial.py</a>', ) contains( "out/partial/index.html", '<span class="pc_cov">100%</span>' )
def test_encoding(self): self.make_file("utf8.py", """\ # -*- coding: utf-8 -*- # This comment has an accent: é print("spam eggs") """) cov = coverage.Coverage() self.start_import_stop(cov, "utf8") cov.annotate() compare(gold_path("annotate/encodings"), ".", "*,cover")
def test_xml_good(self): self.make_file( "out/output.xml", """\ <?xml version="1.0" ?> <the_root c="three" b="222" a="one"> <also z="nine" x="seven" y="888"> Goodie </also> </the_root> """) compare(gold_path("testing/xml"), "out", scrubs=SCRUBS)
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)
def test_omit_3(self): self.output_dir("out/omit_3") with change_dir("src"): # pylint: disable=import-error, unused-variable cov = coverage.Coverage(include=["./*"]) cov.start() import main # pragma: nested cov.stop() # pragma: nested cov.html_report(directory="../out/omit_3", omit=["m1.py", "m2.py"]) compare("gold_omit_3", "out/omit_3", size_within=10, file_pattern="*.html")
def test_omit_5(self): self.output_dir("out/omit_5") with change_dir("src"): # pylint: disable=import-error, unused-variable cov = coverage.Coverage(config_file="omit5.ini", include=["./*"]) cov.start() import main # pragma: nested cov.stop() # pragma: nested cov.html_report() compare("gold_omit_5", "out/omit_5", size_within=10, file_pattern="*.html")
def test_good_needs_scrubs(self): # Comparing the "good" result without scrubbing the variable parts will fail. self.make_file("out/gettysburg.txt", GOOD_GETTY) # compare() raises an assertion. msg = rf"Files differ: .*{GOLD_PATH_RX} != {OUT_PATH_RX}" with pytest.raises(AssertionError, match=msg): compare(gold_path("testing/getty"), "out") stdout = self.stdout() assert "- 11/19/1863, Gettysburg, Pennsylvania" in stdout assert "+ 11/19/9999, Gettysburg, Pennsylvania" in stdout
def test_omit_5(self): self.output_dir("out/omit_5") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage(config_file="omit5.ini", include=["./*"]) cov.start() import main # pragma: nested cov.stop() # pragma: nested cov.html_report() compare("gold_omit_5", "out/omit_5", size_within=10, file_pattern="*.html")
def test_omit_3(self): self.output_dir("out/omit_3") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage(include=["./*"]) cov.start() import main # pragma: nested cov.stop() # pragma: nested cov.html_report(directory="../out/omit_3", omit=["m1.py", "m2.py"]) compare("gold_omit_3", "out/omit_3", size_within=10, file_pattern="*.html")
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)
def test_bom(self): self.output_dir("out/bom") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage() cov.start() import bom # pragma: nested cov.stop() # pragma: nested cov.html_report(bom, directory="../out/bom") compare("gold_bom", "out/bom", size_within=10, file_pattern="*.html") contains( "out/bom/bom_py.html", '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', )
def test_isolatin1(self): self.output_dir("out/isolatin1") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage() cov.start() import isolatin1 # pragma: nested cov.stop() # pragma: nested cov.html_report(isolatin1, directory="../out/isolatin1") compare("gold_isolatin1", "out/isolatin1", size_within=10, file_pattern="*.html") contains( "out/isolatin1/isolatin1_py.html", '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', )
def test_a_xml_2(self): self.output_dir("out/xml_2") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage(config_file="run_a_xml_2.ini") cov.start() import a # pragma: nested cov.stop() # pragma: nested cov.xml_report(a) source_path = coverage.files.relative_directory().rstrip(r"\/") compare("gold_x_xml", "out/xml_2", scrubs=[ (r' timestamp="\d+"', ' timestamp="TIMESTAMP"'), (r' version="[-.\w]+"', ' version="VERSION"'), (r'<source>\s*.*?\s*</source>', '<source>%s</source>' % source_path), (r'/coverage.readthedocs.io/?[-.\w/]*', '/coverage.readthedocs.io/VER'), ])
def test_y_xml_branch(self): self.output_dir("out/y_xml_branch") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage(branch=True) cov.start() import y # pragma: nested cov.stop() # pragma: nested cov.xml_report(y, outfile="../out/y_xml_branch/coverage.xml") source_path = coverage.files.relative_directory().rstrip(r"\/") compare("gold_y_xml_branch", "out/y_xml_branch", scrubs=[ (r' timestamp="\d+"', ' timestamp="TIMESTAMP"'), (r' version="[-.\w]+"', ' version="VERSION"'), (r'<source>\s*.*?\s*</source>', '<source>%s</source>' % source_path), (r'/coverage.readthedocs.io/?[-.\w/]*', '/coverage.readthedocs.io/VER'), ])
def test_white(self): self.make_file( "white.py", """\ # A test case sent to me by Steve White def f(self): if self==1: pass elif self.m('fred'): pass elif (g==1) and (b==2): pass elif self.m('fred')==True: pass elif ((g==1) and (b==2))==True: pass else: pass def g(x): if x == 1: a = 1 else: a = 2 g(1) def h(x): if 0: #pragma: no cover pass if x == 1: a = 1 else: a = 2 h(2) """) cov = coverage.Coverage() self.start_import_stop(cov, "white") cov.annotate() compare(gold_path("annotate/white"), ".", "*,cover")
def test_b_branch(self): self.output_dir("out/b_branch") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage(branch=True) cov.start() import b # pragma: nested cov.stop() # pragma: nested cov.html_report(b, directory="../out/b_branch") compare("gold_b_branch", "out/b_branch", size_within=10, file_pattern="*.html") contains( "out/b_branch/b_py.html", ('<span class="key">if</span> <span class="nam">x</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">70%</span>', ('<span class="annotate short">8 ↛ 11</span>' '<span class="annotate long">line 8 didn\'t jump to line 11, ' 'because the condition on line 8 was never false</span>'), ('<span class="annotate short">17 ↛ exit</span>' '<span class="annotate long">line 17 didn\'t return from function \'two\', ' 'because the condition on line 17 was never false</span>'), ('<span class="annotate short">25 ↛ 26, ' '25 ↛ 28</span>' '<span class="annotate long">2 missed branches: ' '1) line 25 didn\'t jump to line 26, ' 'because the condition on line 25 was never true, ' '2) line 25 didn\'t jump to line 28, ' 'because the condition on line 25 was never false</span>'), ) contains( "out/b_branch/index.html", '<a href="b_py.html">b.py</a>', '<span class="pc_cov">70%</span>', '<td class="right" data-ratio="16 23">70%</td>', )
def test_white(self): self.make_file("white.py", """\ # A test case sent to me by Steve White def f(self): if self==1: pass elif self.m('fred'): pass elif (g==1) and (b==2): pass elif self.m('fred')==True: pass elif ((g==1) and (b==2))==True: pass else: pass def g(x): if x == 1: a = 1 else: a = 2 g(1) def h(x): if 0: #pragma: no cover pass if x == 1: a = 1 else: a = 2 h(2) """) cov = coverage.Coverage() self.start_import_stop(cov, "white") cov.annotate() compare(gold_path("annotate/annotate"), ".", "*,cover")
def test_bom(self): self.output_dir("out/bom") with change_dir("src"): # It's important that the source file really have a BOM, which can # get lost, so check that it's really there. with open("bom.py", "rb") as f: first_three = f.read(3) assert first_three == b"\xef\xbb\xbf" # pylint: disable=import-error cov = coverage.Coverage() cov.start() import bom # pragma: nested cov.stop() # pragma: nested cov.html_report(bom, directory="../out/bom") compare("gold_bom", "out/bom", size_within=10, file_pattern="*.html") contains( "out/bom/bom_py.html", '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', )
def test_other(self): self.output_dir("out/other") with change_dir("src"): # pylint: disable=import-error sys.path.insert(0, "../othersrc") cov = coverage.Coverage(include=["./*", "../othersrc/*"]) cov.start() import here # pragma: nested cov.stop() # pragma: nested cov.html_report(directory="../out/other") # Different platforms will name the "other" file differently. Rename it for p in glob.glob("out/other/*_other_py.html"): os.rename(p, "out/other/blah_blah_other_py.html") compare("gold_other", "out/other", size_within=10, file_pattern="*.html") contains( "out/other/index.html", '<a href="here_py.html">here.py</a>', 'other_py.html">', 'other.py</a>', )
def test_bad(self): self.make_file("out/gettysburg.txt", BAD_GETTY) # compare() raises an assertion. msg = rf"Files differ: .*{GOLD_PATH_RX} != {OUT_PATH_RX}" with pytest.raises(AssertionError, match=msg): compare(gold_path("testing/getty"), "out", scrubs=SCRUBS) # Stdout has a description of the diff. The diff shows the scrubbed content. stdout = self.stdout() assert "- Four score" in stdout assert "+ Five score" in stdout assert re_line(rf"^:::: diff '.*{GOLD_PATH_RX}' and '{OUT_PATH_RX}'", stdout) assert re_line( rf"^:::: end diff '.*{GOLD_PATH_RX}' and '{OUT_PATH_RX}'", stdout) assert " D/D/D, Gxxx, Pennsylvania" in stdout # The actual file was saved. with open(ACTUAL_GETTY_FILE) as f: saved = f.read() assert saved == BAD_GETTY
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() cov.start() import a # pragma: nested # pylint: disable=import-error cov.stop() # pragma: nested cov.xml_report(a, outfile="coverage.xml") source_path = coverage.files.relative_directory().rstrip(r"\/") compare(".", gold_path("html/gold_x_xml"), left_extra=True, scrubs=[ (r' timestamp="\d+"', ' timestamp="TIMESTAMP"'), (r' version="[-.\w]+"', ' version="VERSION"'), (r'<source>\s*.*?\s*</source>', '<source>%s</source>' % source_path), (r'/coverage.readthedocs.io/?[-.\w/]*', '/coverage.readthedocs.io/VER'), ])
def test_xml_bad(self): self.make_file( "out/output.xml", """\ <?xml version="1.0" ?> <the_root c="nine" b="2" a="one"> <also z="three" x="seven" y="8"> Goodbye </also> </the_root> """) # compare() raises an exception. gold_rx = path_regex(gold_path("testing/xml/output.xml")) out_rx = path_regex("out/output.xml") msg = rf"Files differ: .*{gold_rx} != {out_rx}" with pytest.raises(AssertionError, match=msg): compare(gold_path("testing/xml"), "out", scrubs=SCRUBS) # Stdout has a description of the diff. The diff shows the # canonicalized and scrubbed content. stdout = self.stdout() assert '- <the_root a="one" b="D" c="three">' in stdout assert '+ <the_root a="one" b="D" c="nine">' in stdout
def compare_html(expected, actual): """Specialized compare function for our HTML files.""" scrubs = [ (r'/coverage.readthedocs.io/?[-.\w/]*', '/coverage.readthedocs.io/VER'), (r'coverage.py v[\d.abc]+', 'coverage.py vVER'), (r'created at \d\d\d\d-\d\d-\d\d \d\d:\d\d', 'created at DATE'), # Some words are identifiers in one version, keywords in another. (r'<span class="(nam|key)">(print|True|False)</span>', r'<span class="nam">\2</span>'), # Occasionally an absolute path is in the HTML report. (filepath_to_regex(TESTS_DIR), 'TESTS_DIR'), (r'/Users/ned/coverage/trunk/tests', 'TESTS_DIR'), (filepath_to_regex(flat_rootname(unicode_class(TESTS_DIR))), '_TESTS_DIR'), (flat_rootname(u'/Users/ned/coverage/trunk/tests'), '_TESTS_DIR'), # The temp dir the tests make. (filepath_to_regex(os.getcwd()), 'TEST_TMPDIR'), (filepath_to_regex(flat_rootname(unicode_class(os.getcwd()))), '_TEST_TMPDIR'), (r'/private/var/folders/[\w/]{35}/coverage_test/tests_test_html_\w+_\d{8}', 'TEST_TMPDIR'), (r'_private_var_folders_\w{35}_coverage_test_tests_test_html_\w+_\d{8}', '_TEST_TMPDIR'), ] if env.WINDOWS: # For file paths... scrubs += [(r"\\", "/")] compare(expected, actual, file_pattern="*.html", scrubs=scrubs)
def test_missing_after_else(self): self.make_file( "mae.py", """\ def f(x): if x == 1: print("1") else: print("2") if f(1): print("nope") if f(2): print("nope") """) cov = coverage.Coverage() self.start_import_stop(cov, "mae") cov.annotate() assert self.stdout() == ( "1\n" + "2\n" + "The annotate command will be removed in a future version.\n" + "Get in touch if you still use it: [email protected]\n") compare(gold_path("annotate/mae"), ".", "*,cover")