From 5164d566f03894f0fe9f29dc4e9721edfdf32818 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Mon, 31 Aug 2015 07:51:51 -0400 Subject: Convert farm html tests to goldtests. --- tests/farm/html/run_a.py | 29 ---- tests/farm/html/run_a_xml_1.py | 25 --- tests/farm/html/run_a_xml_2.py | 25 --- tests/farm/html/run_b_branch.py | 32 ---- tests/farm/html/run_bom.py | 24 --- tests/farm/html/run_isolatin1.py | 24 --- tests/farm/html/run_omit_1.py | 15 -- tests/farm/html/run_omit_2.py | 15 -- tests/farm/html/run_omit_3.py | 15 -- tests/farm/html/run_omit_4.py | 15 -- tests/farm/html/run_omit_5.py | 15 -- tests/farm/html/run_other.py | 29 ---- tests/farm/html/run_partial.py | 34 ---- tests/farm/html/run_styled.py | 31 ---- tests/farm/html/run_tabbed.py | 27 ---- tests/farm/html/run_unicode.py | 27 ---- tests/farm/html/run_y_xml_branch.py | 25 --- tests/farm/html/src/omit5.ini | 2 +- tests/farm/html/src/run_a_xml_2.ini | 2 +- tests/goldtest.py | 42 +++++ tests/test_html.py | 301 ++++++++++++++++++++++++++++++++---- tests/test_xml.py | 62 ++++++++ 22 files changed, 375 insertions(+), 441 deletions(-) delete mode 100644 tests/farm/html/run_a.py delete mode 100644 tests/farm/html/run_a_xml_1.py delete mode 100644 tests/farm/html/run_a_xml_2.py delete mode 100644 tests/farm/html/run_b_branch.py delete mode 100644 tests/farm/html/run_bom.py delete mode 100644 tests/farm/html/run_isolatin1.py delete mode 100644 tests/farm/html/run_omit_1.py delete mode 100644 tests/farm/html/run_omit_2.py delete mode 100644 tests/farm/html/run_omit_3.py delete mode 100644 tests/farm/html/run_omit_4.py delete mode 100644 tests/farm/html/run_omit_5.py delete mode 100644 tests/farm/html/run_other.py delete mode 100644 tests/farm/html/run_partial.py delete mode 100644 tests/farm/html/run_styled.py delete mode 100644 tests/farm/html/run_tabbed.py delete mode 100644 tests/farm/html/run_unicode.py delete mode 100644 tests/farm/html/run_y_xml_branch.py create mode 100644 tests/goldtest.py (limited to 'tests') diff --git a/tests/farm/html/run_a.py b/tests/farm/html/run_a.py deleted file mode 100644 index 1ec6220f..00000000 --- a/tests/farm/html/run_a.py +++ /dev/null @@ -1,29 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -def html_it(): - """Run coverage.py and make an HTML report for a.""" - import coverage - cov = coverage.Coverage() - cov.start() - import a # pragma: nested - cov.stop() # pragma: nested - cov.html_report(a, directory="../html_a") - -runfunc(html_it, rundir="src") - -# HTML files will change often. Check that the sizes are reasonable, -# and check that certain key strings are in the output. -compare("gold_a", "html_a", size_within=10, file_pattern="*.html") -contains("html_a/a_py.html", - 'if 1 < 2', - '    a = 3', - '67%' - ) -contains("html_a/index.html", - 'a.py', - '67%', - '67%', - ) - -clean("html_a") diff --git a/tests/farm/html/run_a_xml_1.py b/tests/farm/html/run_a_xml_1.py deleted file mode 100644 index 5bf2f460..00000000 --- a/tests/farm/html/run_a_xml_1.py +++ /dev/null @@ -1,25 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -source_path = None - -def html_it(): - """Run coverage.py and make an XML report for a.""" - import coverage, coverage.files - cov = coverage.Coverage() - cov.start() - import a # pragma: nested - cov.stop() # pragma: nested - cov.xml_report(a, outfile="../xml_1/coverage.xml") - global source_path - source_path = coverage.files.relative_directory().rstrip('/') - -runfunc(html_it, rundir="src") - -compare("gold_x_xml", "xml_1", scrubs=[ - (r' timestamp="\d+"', ' timestamp="TIMESTAMP"'), - (r' version="[-.\w]+"', ' version="VERSION"'), - (r'\s*.*?\s*', '%s' % source_path), - (r'/coverage.readthedocs.org/?[-.\w/]*', '/coverage.readthedocs.org/VER'), - ]) -clean("xml_1") diff --git a/tests/farm/html/run_a_xml_2.py b/tests/farm/html/run_a_xml_2.py deleted file mode 100644 index 5d8778f3..00000000 --- a/tests/farm/html/run_a_xml_2.py +++ /dev/null @@ -1,25 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -source_path = None - -def html_it(): - """Run coverage.py and make an XML report for a.""" - import coverage, coverage.files - cov = coverage.Coverage(config_file="run_a_xml_2.ini") - cov.start() - import a # pragma: nested - cov.stop() # pragma: nested - cov.xml_report(a) - global source_path - source_path = coverage.files.relative_directory().rstrip('/') - -runfunc(html_it, rundir="src") - -compare("gold_x_xml", "xml_2", scrubs=[ - (r' timestamp="\d+"', ' timestamp="TIMESTAMP"'), - (r' version="[-.\w]+"', ' version="VERSION"'), - (r'\s*.*?\s*', '%s' % source_path), - (r'/coverage.readthedocs.org/?[-.\w/]*', '/coverage.readthedocs.org/VER'), - ]) -clean("xml_2") diff --git a/tests/farm/html/run_b_branch.py b/tests/farm/html/run_b_branch.py deleted file mode 100644 index 382e3110..00000000 --- a/tests/farm/html/run_b_branch.py +++ /dev/null @@ -1,32 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -def html_it(): - """Run coverage.py with branches and make an HTML report for b.""" - import coverage - cov = coverage.Coverage(branch=True) - cov.start() - import b # pragma: nested - cov.stop() # pragma: nested - cov.html_report(b, directory="../html_b_branch") - -runfunc(html_it, rundir="src") - -# HTML files will change often. Check that the sizes are reasonable, -# and check that certain key strings are in the output. -compare("gold_b_branch", "html_b_branch", size_within=10, file_pattern="*.html") -contains("html_b_branch/b_py.html", - 'if x < 2', - '    a = 3', - '70%', - '8 ↛ 11 [?]', - '17 ↛ exit [?]', - '25 ↛ 26,   25 ↛ 28 [?]', - ) -contains("html_b_branch/index.html", - 'b.py', - '70%', - '70%', - ) - -clean("html_b_branch") diff --git a/tests/farm/html/run_bom.py b/tests/farm/html/run_bom.py deleted file mode 100644 index a34fab9f..00000000 --- a/tests/farm/html/run_bom.py +++ /dev/null @@ -1,24 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -import sys - -def html_it(): - """Run coverage.py and make an HTML report for bom.py.""" - import coverage - cov = coverage.Coverage() - cov.start() - import bom # pragma: nested - cov.stop() # pragma: nested - cov.html_report(bom, directory="../html_bom") - -runfunc(html_it, rundir="src") - -# HTML files will change often. Check that the sizes are reasonable, -# and check that certain key strings are in the output. -compare("gold_bom", "html_bom", size_within=10, file_pattern="*.html") -contains("html_bom/bom_py.html", - '"3×4 = 12, ÷2 = 6±0"', - ) - -clean("html_bom") diff --git a/tests/farm/html/run_isolatin1.py b/tests/farm/html/run_isolatin1.py deleted file mode 100644 index 6729a275..00000000 --- a/tests/farm/html/run_isolatin1.py +++ /dev/null @@ -1,24 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -import sys - -def html_it(): - """Run coverage.py and make an HTML report for isolatin1.py.""" - import coverage - cov = coverage.Coverage() - cov.start() - import isolatin1 # pragma: nested - cov.stop() # pragma: nested - cov.html_report(isolatin1, directory="../html_isolatin1") - -runfunc(html_it, rundir="src") - -# HTML files will change often. Check that the sizes are reasonable, -# and check that certain key strings are in the output. -compare("gold_isolatin1", "html_isolatin1", size_within=10, file_pattern="*.html") -contains("html_isolatin1/isolatin1_py.html", - '"3×4 = 12, ÷2 = 6±0"', - ) - -clean("html_isolatin1") diff --git a/tests/farm/html/run_omit_1.py b/tests/farm/html/run_omit_1.py deleted file mode 100644 index 137d37ca..00000000 --- a/tests/farm/html/run_omit_1.py +++ /dev/null @@ -1,15 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -def html_it(): - """Run coverage.py and make an HTML report for main.""" - import coverage - cov = coverage.Coverage(include=["./*"]) - cov.start() - import main # pragma: nested - cov.stop() # pragma: nested - cov.html_report(directory="../html_omit_1") - -runfunc(html_it, rundir="src") -compare("gold_omit_1", "html_omit_1", size_within=10, file_pattern="*.html") -clean("html_omit_1") diff --git a/tests/farm/html/run_omit_2.py b/tests/farm/html/run_omit_2.py deleted file mode 100644 index dd021b96..00000000 --- a/tests/farm/html/run_omit_2.py +++ /dev/null @@ -1,15 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -def html_it(): - """Run coverage.py and make an HTML report for main.""" - import coverage - cov = coverage.Coverage(include=["./*"]) - cov.start() - import main # pragma: nested - cov.stop() # pragma: nested - cov.html_report(directory="../html_omit_2", omit=["m1.py"]) - -runfunc(html_it, rundir="src") -compare("gold_omit_2", "html_omit_2", size_within=10, file_pattern="*.html") -clean("html_omit_2") diff --git a/tests/farm/html/run_omit_3.py b/tests/farm/html/run_omit_3.py deleted file mode 100644 index 96ed0822..00000000 --- a/tests/farm/html/run_omit_3.py +++ /dev/null @@ -1,15 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -def html_it(): - """Run coverage.py and make an HTML report for main.""" - import coverage - cov = coverage.Coverage(include=["./*"]) - cov.start() - import main # pragma: nested - cov.stop() # pragma: nested - cov.html_report(directory="../html_omit_3", omit=["m1.py", "m2.py"]) - -runfunc(html_it, rundir="src") -compare("gold_omit_3", "html_omit_3", size_within=10, file_pattern="*.html") -clean("html_omit_3") diff --git a/tests/farm/html/run_omit_4.py b/tests/farm/html/run_omit_4.py deleted file mode 100644 index b212ef40..00000000 --- a/tests/farm/html/run_omit_4.py +++ /dev/null @@ -1,15 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -def html_it(): - """Run coverage.py and make an HTML report for main.""" - import coverage - cov = coverage.Coverage(config_file="omit4.ini", include=["./*"]) - cov.start() - import main # pragma: nested - cov.stop() # pragma: nested - cov.html_report(directory="../html_omit_4") - -runfunc(html_it, rundir="src") -compare("gold_omit_4", "html_omit_4", size_within=10, file_pattern="*.html") -clean("html_omit_4") diff --git a/tests/farm/html/run_omit_5.py b/tests/farm/html/run_omit_5.py deleted file mode 100644 index ed61d209..00000000 --- a/tests/farm/html/run_omit_5.py +++ /dev/null @@ -1,15 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -def html_it(): - """Run coverage.py and make an HTML report for main.""" - import coverage - cov = coverage.Coverage(config_file="omit5.ini", include=["./*"]) - cov.start() - import main # pragma: nested - cov.stop() # pragma: nested - cov.html_report() - -runfunc(html_it, rundir="src") -compare("gold_omit_5", "html_omit_5", size_within=10, file_pattern="*.html") -clean("html_omit_5") diff --git a/tests/farm/html/run_other.py b/tests/farm/html/run_other.py deleted file mode 100644 index 92753d4b..00000000 --- a/tests/farm/html/run_other.py +++ /dev/null @@ -1,29 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -def html_it(): - """Run coverage.py and make an HTML report for everything.""" - import coverage - cov = coverage.Coverage(include=["./*", "../othersrc/*"]) - cov.start() - import here # pragma: nested - cov.stop() # pragma: nested - cov.html_report(directory="../html_other") - -runfunc(html_it, rundir="src", addtopath="../othersrc") - -# Different platforms will name the "other" file differently. Rename it -import os, glob - -for p in glob.glob("html_other/*_other_py.html"): - os.rename(p, "html_other/blah_blah_other_py.html") - -# HTML files will change often. Check that the sizes are reasonable, -# and check that certain key strings are in the output. -compare("gold_other", "html_other", size_within=10, file_pattern="*.html") -contains("html_other/index.html", - 'here.py', - 'other_py.html">', 'other.py', - ) - -clean("html_other") diff --git a/tests/farm/html/run_partial.py b/tests/farm/html/run_partial.py deleted file mode 100644 index fedf7082..00000000 --- a/tests/farm/html/run_partial.py +++ /dev/null @@ -1,34 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -import sys - -def html_it(): - """Run coverage.py and make an HTML report for partial.""" - import coverage - cov = coverage.Coverage(branch=True) - cov.start() - import partial # pragma: nested - cov.stop() # pragma: nested - cov.html_report(partial, directory="../html_partial") - -runfunc(html_it, rundir="src") - -# HTML files will change often. Check that the sizes are reasonable, -# and check that certain key strings are in the output. -compare("gold_partial", "html_partial", size_within=10, file_pattern="*.html") -contains("html_partial/partial_py.html", - '

', - '

', - '

', - # The "if 0" and "if 1" statements are optimized away. - '

', - ) -contains("html_partial/index.html", - 'partial.py', - ) -contains("html_partial/index.html", - '100%' - ) - -clean("html_partial") diff --git a/tests/farm/html/run_styled.py b/tests/farm/html/run_styled.py deleted file mode 100644 index eeebfe61..00000000 --- a/tests/farm/html/run_styled.py +++ /dev/null @@ -1,31 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -def html_it(): - """Run coverage.py and make an HTML report for a.""" - import coverage - cov = coverage.Coverage() - cov.start() - import a # pragma: nested - cov.stop() # pragma: nested - cov.html_report(a, directory="../html_styled", extra_css="extra.css") - -runfunc(html_it, rundir="src") - -# HTML files will change often. Check that the sizes are reasonable, -# and check that certain key strings are in the output. -compare("gold_styled", "html_styled", size_within=10, file_pattern="*.html") -compare("gold_styled", "html_styled", size_within=10, file_pattern="*.css") -contains("html_styled/a_py.html", - '', - 'if 1 < 2', - '    a = 3', - '67%' - ) -contains("html_styled/index.html", - '', - 'a.py', - '67%' - ) - -clean("html_styled") diff --git a/tests/farm/html/run_tabbed.py b/tests/farm/html/run_tabbed.py deleted file mode 100644 index 6dacff53..00000000 --- a/tests/farm/html/run_tabbed.py +++ /dev/null @@ -1,27 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -def html_it(): - """Run coverage.py and make an HTML report for tabbed.""" - import coverage - cov = coverage.Coverage() - cov.start() - import tabbed # pragma: nested - cov.stop() # pragma: nested - cov.html_report(tabbed, directory="../html_tabbed") - -runfunc(html_it, rundir="src") - -# Editors like to change things, make sure our source file still has tabs. -contains("src/tabbed.py", "\tif x:\t\t\t\t\t# look nice") - -contains("html_tabbed/tabbed_py.html", - '>        if ' - 'x:' - '                    ' - '               ' - '# look nice' - ) - -doesnt_contain("html_tabbed/tabbed_py.html", "\t") -clean("html_tabbed") diff --git a/tests/farm/html/run_unicode.py b/tests/farm/html/run_unicode.py deleted file mode 100644 index 888d7e54..00000000 --- a/tests/farm/html/run_unicode.py +++ /dev/null @@ -1,27 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -def html_it(): - """Run coverage.py and make an HTML report for unicode.py.""" - import coverage - cov = coverage.Coverage() - cov.start() - import unicode # pragma: nested - cov.stop() # pragma: nested - cov.html_report(unicode, directory="../html_unicode") - -runfunc(html_it, rundir="src") - -# HTML files will change often. Check that the sizes are reasonable, -# and check that certain key strings are in the output. -compare("gold_unicode", "html_unicode", size_within=10, file_pattern="*.html") -contains("html_unicode/unicode_py.html", - '"ʎd˙ǝbɐɹǝʌoɔ"', - ) - -contains_any("html_unicode/unicode_py.html", - '"db40,dd00: x��"', - '"db40,dd00: x󠄀"', - ) - -clean("html_unicode") diff --git a/tests/farm/html/run_y_xml_branch.py b/tests/farm/html/run_y_xml_branch.py deleted file mode 100644 index 921da9e2..00000000 --- a/tests/farm/html/run_y_xml_branch.py +++ /dev/null @@ -1,25 +0,0 @@ -# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 -# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt - -source_path = None - -def xml_it(): - """Run coverage.py and make an XML report for y.""" - import coverage, coverage.files - cov = coverage.Coverage(branch=True) - cov.start() - import y # pragma: nested - cov.stop() # pragma: nested - cov.xml_report(y, outfile="../xml_branch/coverage.xml") - global source_path - source_path = coverage.files.relative_directory().rstrip('/') - -runfunc(xml_it, rundir="src") - -compare("gold_y_xml_branch", "xml_branch", scrubs=[ - (r' timestamp="\d+"', ' timestamp="TIMESTAMP"'), - (r' version="[-.\w]+"', ' version="VERSION"'), - (r'\s*.*?\s*', '%s' % source_path), - (r'/coverage.readthedocs.org/?[-.\w/]*', '/coverage.readthedocs.org/VER'), - ]) -clean("xml_branch") diff --git a/tests/farm/html/src/omit5.ini b/tests/farm/html/src/omit5.ini index 3d8dbcf2..3b6add29 100644 --- a/tests/farm/html/src/omit5.ini +++ b/tests/farm/html/src/omit5.ini @@ -8,4 +8,4 @@ omit = helloworld [html] -directory = ../html_omit_5 +directory = ../out/omit_5 diff --git a/tests/farm/html/src/run_a_xml_2.ini b/tests/farm/html/src/run_a_xml_2.ini index 5b5f18dc..f632bd09 100644 --- a/tests/farm/html/src/run_a_xml_2.ini +++ b/tests/farm/html/src/run_a_xml_2.ini @@ -3,4 +3,4 @@ # Put all the XML output in xml_2 [xml] -output = ../xml_2/coverage.xml +output = ../out/xml_2/coverage.xml diff --git a/tests/goldtest.py b/tests/goldtest.py new file mode 100644 index 00000000..f3b37710 --- /dev/null +++ b/tests/goldtest.py @@ -0,0 +1,42 @@ +# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 +# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt + +"""A test base class for tests based on gold file comparison.""" + +import os +import sys + +from tests.coveragetest import CoverageTest + +from coverage.test_helpers import change_dir +from tests.test_farm import clean +# Import helpers, eventually test_farm.py will go away. +from tests.test_farm import ( # pylint: disable=unused-import + compare, contains, doesnt_contain, contains_any, +) + + +class CoverageGoldTest(CoverageTest): + """A test based on gold files.""" + + run_in_temp_dir = False + + def setUp(self): + super(CoverageGoldTest, self).setUp() + self.chdir(self.root_dir) + # Modules should be importable from the current directory. + sys.path.insert(0, '') + + def output_dir(self, the_dir): + """Declare where the output directory is. + + The output directory is deleted at the end of the test, unless the + COVERAGE_KEEP_OUTPUT environment variable is set. + + """ + self.addCleanup(self.cleanup_output_dir, the_dir) + + def cleanup_output_dir(self, the_dir): + """Clean up the output directory of the test.""" + if not os.environ.get("COVERAGE_KEEP_OUTPUT"): # pragma: partial + clean(the_dir) diff --git a/tests/test_html.py b/tests/test_html.py index 360f967a..13fdf9bb 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -5,15 +5,20 @@ """Tests that HTML generation is awesome.""" import datetime +import glob +import os import os.path import re import sys import coverage +import coverage.files import coverage.html from coverage.misc import CoverageException, NotPython, NoSource from tests.coveragetest import CoverageTest +from tests.goldtest import CoverageGoldTest +from tests.goldtest import change_dir, compare, contains, doesnt_contain, contains_any class HtmlTestHelpers(CoverageTest): @@ -468,50 +473,282 @@ class HtmlStaticFileTest(CoverageTest): cov.html_report() -class CoverageGoldTest(CoverageTest): - - run_in_temp_dir = False - - def setUp(self): - super(CoverageGoldTest, self).setUp() - self.chdir(self.root_dir) - # Modules should be importable from the current directory. - sys.path.insert(0, '') - - def output_dir(self, the_dir): - self.addCleanup(self.cleanup_output_dir, the_dir) - - def cleanup_output_dir(self, the_dir): - if not os.environ.get("COVERAGE_KEEP_OUTPUT"): - clean(the_dir) - -from coverage.test_helpers import change_dir -from tests.test_farm import clean, compare, contains - class HtmlGoldTests(CoverageGoldTest): root_dir = 'tests/farm/html' def test_a(self): - self.output_dir("html_a") + self.output_dir("out/a") with change_dir("src"): cov = coverage.Coverage() cov.start() - import a - cov.stop() - cov.html_report(a, directory='../html_a') - - compare("gold_a", "html_a", size_within=10, file_pattern="*.html") - contains("html_a/a_py.html", - 'if 1 ' - '< 2', - '    a ' - '= 3', + 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", + ('if 1 ' + '< 2'), + ('    a ' + '= 3'), '67%', ) - contains("html_a/index.html", + contains( + "out/a/index.html", 'a.py', '67%', '67%', ) + + def test_b_branch(self): + self.output_dir("out/b_branch") + + with change_dir("src"): + 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", + ('if x ' + '< 2'), + ('    a = ' + '3'), + '70%', + ('' + '8 ↛ 11 [?]'), + ('17 ↛ exit [?]'), + ('25 ↛ 26,   ' + '25 ↛ 28 [?]'), + ) + contains( + "out/b_branch/index.html", + 'b.py', + '70%', + '70%', + ) + + def test_bom(self): + self.output_dir("out/bom") + + with change_dir("src"): + 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", + '"3×4 = 12, ÷2 = 6±0"', + ) + + def test_isolatin1(self): + self.output_dir("out/isolatin1") + + with change_dir("src"): + 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", + '"3×4 = 12, ÷2 = 6±0"', + ) + + def test_omit_1(self): + self.output_dir("out/omit_1") + + with change_dir("src"): + cov = coverage.Coverage(include=["./*"]) + cov.start() + import main # pragma: nested + cov.stop() # pragma: nested + cov.html_report(directory="../out/omit_1") + + compare("gold_omit_1", "out/omit_1", size_within=10, file_pattern="*.html") + + def test_omit_2(self): + self.output_dir("out/omit_2") + + with change_dir("src"): + cov = coverage.Coverage(include=["./*"]) + cov.start() + import main # pragma: nested + cov.stop() # pragma: nested + cov.html_report(directory="../out/omit_2", omit=["m1.py"]) + + compare("gold_omit_2", "out/omit_2", size_within=10, file_pattern="*.html") + + def test_omit_3(self): + self.output_dir("out/omit_3") + + with change_dir("src"): + 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_4(self): + self.output_dir("out/omit_4") + + with change_dir("src"): + cov = coverage.Coverage(config_file="omit4.ini", include=["./*"]) + cov.start() + import main # pragma: nested + cov.stop() # pragma: nested + cov.html_report(directory="../out/omit_4") + + compare("gold_omit_4", "out/omit_4", size_within=10, file_pattern="*.html") + + def test_omit_5(self): + self.output_dir("out/omit_5") + + with change_dir("src"): + 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_other(self): + self.output_dir("out/other") + + with change_dir("src"): + 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", + 'here.py', + 'other_py.html">', 'other.py', + ) + + def test_partial(self): + self.output_dir("out/partial") + + with change_dir("src"): + 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", + '

', + '

', + '

', + # The "if 0" and "if 1" statements are optimized away. + '

', + ) + contains( + "out/partial/index.html", + 'partial.py', + ) + contains( + "out/partial/index.html", + '100%' + ) + + def test_styled(self): + self.output_dir("out/styled") + + with change_dir("src"): + 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", + '', + ('if 1 ' + '< 2'), + ('    a = ' + '3'), + '67%' + ) + contains( + "out/styled/index.html", + '', + 'a.py', + '67%' + ) + + def test_tabbed(self): + self.output_dir("out/tabbed") + + with change_dir("src"): + cov = coverage.Coverage() + cov.start() + import tabbed # pragma: nested + cov.stop() # pragma: nested + cov.html_report(tabbed, directory="../out/tabbed") + + # Editors like to change things, make sure our source file still has tabs. + contains("src/tabbed.py", "\tif x:\t\t\t\t\t# look nice") + + contains( + "out/tabbed/tabbed_py.html", + '>        if ' + 'x:' + '                    ' + '               ' + '# look nice' + ) + + doesnt_contain("out/tabbed/tabbed_py.html", "\t") + + def test_unicode(self): + self.output_dir("out/unicode") + + with change_dir("src"): + 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", + ('"ʎd˙ǝbɐɹǝʌoɔ"' + ''), + ) + + contains_any( + "out/unicode/unicode_py.html", + '"db40,dd00: x��"', + '"db40,dd00: x󠄀"', + ) diff --git a/tests/test_xml.py b/tests/test_xml.py index 3806c58d..e46e5d64 100644 --- a/tests/test_xml.py +++ b/tests/test_xml.py @@ -9,6 +9,8 @@ import re import coverage from tests.coveragetest import CoverageTest +from tests.goldtest import CoverageGoldTest +from tests.goldtest import change_dir, compare, contains class XmlTestHelpers(CoverageTest): @@ -252,3 +254,63 @@ def clean(text, scrub=None): text = re.sub(r"(?m)^\s+", "", text) text = re.sub(r"\\", "/", text) return text + + +class XmlGoldTest(CoverageGoldTest): + + # TODO: this should move out of html. + root_dir = 'tests/farm/html' + + def test_a_xml_1(self): + self.output_dir("out/xml_1") + + with change_dir("src"): + cov = coverage.Coverage() + cov.start() + import a # pragma: nested + cov.stop() # pragma: nested + cov.xml_report(a, outfile="../out/xml_1/coverage.xml") + source_path = coverage.files.relative_directory().rstrip('/') + + compare("gold_x_xml", "out/xml_1", scrubs=[ + (r' timestamp="\d+"', ' timestamp="TIMESTAMP"'), + (r' version="[-.\w]+"', ' version="VERSION"'), + (r'\s*.*?\s*', '%s' % source_path), + (r'/coverage.readthedocs.org/?[-.\w/]*', '/coverage.readthedocs.org/VER'), + ]) + + def test_a_xml_2(self): + self.output_dir("out/xml_2") + + with change_dir("src"): + 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('/') + + compare("gold_x_xml", "out/xml_2", scrubs=[ + (r' timestamp="\d+"', ' timestamp="TIMESTAMP"'), + (r' version="[-.\w]+"', ' version="VERSION"'), + (r'\s*.*?\s*', '%s' % source_path), + (r'/coverage.readthedocs.org/?[-.\w/]*', '/coverage.readthedocs.org/VER'), + ]) + + def test_y_xml_branch(self): + self.output_dir("out/y_xml_branch") + + with change_dir("src"): + 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('/') + + compare("gold_y_xml_branch", "out/y_xml_branch", scrubs=[ + (r' timestamp="\d+"', ' timestamp="TIMESTAMP"'), + (r' version="[-.\w]+"', ' version="VERSION"'), + (r'\s*.*?\s*', '%s' % source_path), + (r'/coverage.readthedocs.org/?[-.\w/]*', '/coverage.readthedocs.org/VER'), + ]) -- cgit v1.2.1