diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-08-09 09:48:51 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-08-09 09:48:51 -0400 |
commit | d7f2c64b7071d20378d6864cc4ec1c1c83b09175 (patch) | |
tree | 4b8733ebe02d867252b097ea443ebdce7eb26215 | |
parent | 3cc5b11a413426b07a51e86eab20a7e9ebab4f2d (diff) | |
download | python-coveragepy-git-d7f2c64b7071d20378d6864cc4ec1c1c83b09175.tar.gz |
Move flat_rootname from FileReporter to a utility function.
-rw-r--r-- | coverage/annotate.py | 3 | ||||
-rw-r--r-- | coverage/files.py | 14 | ||||
-rw-r--r-- | coverage/html.py | 13 | ||||
-rw-r--r-- | coverage/plugin.py | 18 | ||||
-rw-r--r-- | tests/test_filereporter.py | 12 | ||||
-rw-r--r-- | tests/test_files.py | 6 |
6 files changed, 28 insertions, 38 deletions
diff --git a/coverage/annotate.py b/coverage/annotate.py index 4b4966e9..60772656 100644 --- a/coverage/annotate.py +++ b/coverage/annotate.py @@ -7,6 +7,7 @@ import io import os import re +from coverage.files import flat_rootname from coverage.report import Reporter class AnnotateReporter(Reporter): @@ -57,7 +58,7 @@ class AnnotateReporter(Reporter): excluded = sorted(analysis.excluded) if self.directory: - dest_file = os.path.join(self.directory, fr.flat_rootname()) + dest_file = os.path.join(self.directory, flat_rootname(fr.relative_filename())) if dest_file.endswith("_py"): dest_file = dest_file[:-3] + ".py" dest_file += ",cover" diff --git a/coverage/files.py b/coverage/files.py index e3ebd6ce..d2742a39 100644 --- a/coverage/files.py +++ b/coverage/files.py @@ -67,6 +67,20 @@ def canonical_filename(filename): return CANONICAL_FILENAME_CACHE[filename] +def flat_rootname(filename): + """A base for a flat filename to correspond to this file. + + Useful for writing files about the code where you want all the files in + the same directory, but need to differentiate same-named files from + different directories. + + For example, the file a/b/c.py will return 'a_b_c_py' + + """ + name = os.path.splitdrive(filename)[1] + return re.sub(r"[\\/.:]", "_", name) + + if env.WINDOWS: _ACTUAL_PATH_CACHE = {} diff --git a/coverage/html.py b/coverage/html.py index 3a6c61c8..6d1bb434 100644 --- a/coverage/html.py +++ b/coverage/html.py @@ -12,6 +12,7 @@ import shutil import coverage from coverage import env from coverage.backward import iitems +from coverage.files import flat_rootname from coverage.misc import CoverageException, Hasher from coverage.report import Reporter from coverage.results import Numbers @@ -166,15 +167,15 @@ class HtmlReporter(Reporter): source = fr.source() # Find out if the file on disk is already correct. - flat_rootname = fr.flat_rootname() + rootname = flat_rootname(fr.relative_filename()) this_hash = self.file_hash(source.encode('utf-8'), fr) - that_hash = self.status.file_hash(flat_rootname) + that_hash = self.status.file_hash(rootname) if this_hash == that_hash: # Nothing has changed to require the file to be reported again. - self.files.append(self.status.index_info(flat_rootname)) + self.files.append(self.status.index_info(rootname)) return - self.status.set_file_hash(flat_rootname, this_hash) + self.status.set_file_hash(rootname, this_hash) # Get the numbers for this file. nums = analysis.numbers @@ -256,7 +257,7 @@ class HtmlReporter(Reporter): } html = spaceless(self.source_tmpl.render(template_values)) - html_filename = flat_rootname + ".html" + html_filename = rootname + ".html" html_path = os.path.join(self.directory, html_filename) self.write_html(html_path, html) @@ -267,7 +268,7 @@ class HtmlReporter(Reporter): 'relative_filename': fr.relative_filename(), } self.files.append(index_info) - self.status.set_index_info(flat_rootname, index_info) + self.status.set_index_info(rootname, index_info) def index_file(self): """Write the index.html file for this report.""" diff --git a/coverage/plugin.py b/coverage/plugin.py index f4182b0f..8cd6dd3f 100644 --- a/coverage/plugin.py +++ b/coverage/plugin.py @@ -3,9 +3,6 @@ """Plugin interfaces for coverage.py""" -import os -import re - from coverage import files from coverage.misc import _needs_to_implement @@ -226,18 +223,3 @@ class FileReporter(object): place. """ return False - - def flat_rootname(self): - """A base for a flat filename to correspond to this file. - - Useful for writing files about the code where you want all the files in - the same directory, but need to differentiate same-named files from - different directories. - - For example, the file a/b/c.py will return 'a_b_c_py' - - You should not need to override this method. - - """ - name = os.path.splitdrive(self.relative_filename())[1] - return re.sub(r"[\\/.:]", "_", name) diff --git a/tests/test_filereporter.py b/tests/test_filereporter.py index 380d92d3..a348a844 100644 --- a/tests/test_filereporter.py +++ b/tests/test_filereporter.py @@ -38,9 +38,6 @@ class FileReporterTest(CoverageTest): self.assertEqual(acu.relative_filename(), "aa/afile.py") self.assertEqual(bcu.relative_filename(), "aa/bb/bfile.py") self.assertEqual(ccu.relative_filename(), "aa/bb/cc/cfile.py") - self.assertEqual(acu.flat_rootname(), "aa_afile_py") - self.assertEqual(bcu.flat_rootname(), "aa_bb_bfile_py") - self.assertEqual(ccu.flat_rootname(), "aa_bb_cc_cfile_py") self.assertEqual(acu.source(), "# afile.py\n") self.assertEqual(bcu.source(), "# bfile.py\n") self.assertEqual(ccu.source(), "# cfile.py\n") @@ -52,9 +49,6 @@ class FileReporterTest(CoverageTest): self.assertEqual(acu.relative_filename(), "aa/afile.odd.py") self.assertEqual(bcu.relative_filename(), "aa/bb/bfile.odd.py") self.assertEqual(b2cu.relative_filename(), "aa/bb.odd/bfile.py") - self.assertEqual(acu.flat_rootname(), "aa_afile_odd_py") - self.assertEqual(bcu.flat_rootname(), "aa_bb_bfile_odd_py") - self.assertEqual(b2cu.flat_rootname(), "aa_bb_odd_bfile_py") self.assertEqual(acu.source(), "# afile.odd.py\n") self.assertEqual(bcu.source(), "# bfile.odd.py\n") self.assertEqual(b2cu.source(), "# bfile.py\n") @@ -70,9 +64,6 @@ class FileReporterTest(CoverageTest): self.assertEqual(acu.relative_filename(), native("aa.py")) self.assertEqual(bcu.relative_filename(), native("aa/bb.py")) self.assertEqual(ccu.relative_filename(), native("aa/bb/cc.py")) - self.assertEqual(acu.flat_rootname(), "aa_py") - self.assertEqual(bcu.flat_rootname(), "aa_bb_py") - self.assertEqual(ccu.flat_rootname(), "aa_bb_cc_py") self.assertEqual(acu.source(), "# aa\n") self.assertEqual(bcu.source(), "# bb\n") self.assertEqual(ccu.source(), "") # yes, empty @@ -88,9 +79,6 @@ class FileReporterTest(CoverageTest): self.assertEqual(acu.relative_filename(), native("aa/afile.py")) self.assertEqual(bcu.relative_filename(), native("aa/bb/bfile.py")) self.assertEqual(ccu.relative_filename(), native("aa/bb/cc/cfile.py")) - self.assertEqual(acu.flat_rootname(), "aa_afile_py") - self.assertEqual(bcu.flat_rootname(), "aa_bb_bfile_py") - self.assertEqual(ccu.flat_rootname(), "aa_bb_cc_cfile_py") self.assertEqual(acu.source(), "# afile.py\n") self.assertEqual(bcu.source(), "# bfile.py\n") self.assertEqual(ccu.source(), "# cfile.py\n") diff --git a/tests/test_files.py b/tests/test_files.py index b658853a..813f8612 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -9,7 +9,7 @@ import os.path from coverage import files from coverage.files import ( TreeMatcher, FnmatchMatcher, ModuleMatcher, PathAliases, - find_python_files, abs_file, actual_path + find_python_files, abs_file, actual_path, flat_rootname, ) from coverage.misc import CoverageException from coverage import env @@ -54,6 +54,10 @@ class FilesTest(CoverageTest): rel = os.path.join('sub', trick, 'file1.py') self.assertEqual(files.relative_filename(abs_file(rel)), rel) + def test_flat_rootname(self): + self.assertEqual(flat_rootname("a/b/c.py"), "a_b_c_py") + self.assertEqual(flat_rootname(r"c:\foo\bar.html"), "c__foo_bar_html") + class MatcherTest(CoverageTest): """Tests of file matchers.""" |