summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2015-02-21 20:40:39 -0500
committerNed Batchelder <ned@nedbatchelder.com>2015-02-21 20:40:39 -0500
commit4ce67933b19cd067046a55a8100f3a56881858ce (patch)
tree3aa0dca73477d6aa52ef7840e1b48635d611188c
parent64c7f2cb2959af7196182e6a745c8654d6cf6519 (diff)
downloadpython-coveragepy-4ce67933b19cd067046a55a8100f3a56881858ce.tar.gz
Get rid of CodeUnit, FileReporter is the new thing.
-rw-r--r--coverage/codeunit.py38
-rw-r--r--coverage/control.py2
-rw-r--r--coverage/python.py39
-rw-r--r--coverage/xmlreport.py7
-rw-r--r--tests/test_codeunit.py10
5 files changed, 36 insertions, 60 deletions
diff --git a/coverage/codeunit.py b/coverage/codeunit.py
deleted file mode 100644
index ef7e848..0000000
--- a/coverage/codeunit.py
+++ /dev/null
@@ -1,38 +0,0 @@
-"""Code unit (module) handling for Coverage."""
-
-import os
-
-from coverage.files import FileLocator
-from coverage.plugin import FileReporter
-
-
-class CodeUnit(FileReporter):
- """Code unit: a filename or module.
-
- Instance attributes:
-
- `name` is a human-readable name for this code unit.
- `filename` is the os path from which we can read the source.
-
- """
-
- def __init__(self, morf, file_locator=None):
- self.file_locator = file_locator or FileLocator()
-
- if hasattr(morf, '__file__'):
- filename = morf.__file__
- else:
- filename = morf
- filename = self._adjust_filename(filename)
- self.filename = self.file_locator.canonical_filename(filename)
-
- if hasattr(morf, '__name__'):
- name = morf.__name__
- name = name.replace(".", os.sep) + ".py"
- else:
- name = self.file_locator.relative_filename(filename)
- self.name = name
-
- def _adjust_filename(self, f):
- # TODO: This shouldn't be in the base class, right?
- return f
diff --git a/coverage/control.py b/coverage/control.py
index cb85da1..a0c21a4 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -989,7 +989,7 @@ class Coverage(object):
outfile = open(self.config.xml_output, "w")
file_to_close = outfile
try:
- reporter = XmlReporter(self, self.config)
+ reporter = XmlReporter(self, self.config, self.file_locator)
return reporter.report(morfs, outfile=outfile)
except CoverageException:
delete_file = True
diff --git a/coverage/python.py b/coverage/python.py
index 53da561..7a9b842 100644
--- a/coverage/python.py
+++ b/coverage/python.py
@@ -7,10 +7,11 @@ import zipimport
from coverage import env
from coverage.backward import unicode_class
-from coverage.codeunit import CodeUnit
+from coverage.files import FileLocator
from coverage.misc import NoSource, join_regex
from coverage.parser import PythonParser
from coverage.phystokens import source_token_lines, source_encoding
+from coverage.plugin import FileReporter
def read_python_source(filename):
@@ -86,13 +87,33 @@ def get_zip_bytes(filename):
return None
-class PythonCodeUnit(CodeUnit):
- """Represents a Python file."""
+class PythonCodeUnit(FileReporter):
+ """Report support for a Python file."""
def __init__(self, morf, coverage=None):
self.coverage = coverage
- file_locator = coverage.file_locator if coverage else None
- super(PythonCodeUnit, self).__init__(morf, file_locator)
+ file_locator = coverage.file_locator if coverage else FileLocator()
+
+ if hasattr(morf, '__file__'):
+ filename = morf.__file__
+ else:
+ filename = morf
+
+ # .pyc files should always refer to a .py instead.
+ if filename.endswith(('.pyc', '.pyo')):
+ filename = filename[:-1]
+ elif filename.endswith('$py.class'): # Jython
+ filename = filename[:-9] + ".py"
+
+ super(PythonCodeUnit, self).__init__(file_locator.canonical_filename(filename))
+
+ if hasattr(morf, '__name__'):
+ name = morf.__name__
+ name = name.replace(".", os.sep) + ".py"
+ else:
+ name = file_locator.relative_filename(filename)
+ self.name = name
+
self._source = None
self._parser = None
self._statements = None
@@ -138,14 +159,6 @@ class PythonCodeUnit(CodeUnit):
def exit_counts(self):
return self.parser.exit_counts()
- def _adjust_filename(self, fname):
- # .pyc files should always refer to a .py instead.
- if fname.endswith(('.pyc', '.pyo')):
- fname = fname[:-1]
- elif fname.endswith('$py.class'): # Jython
- fname = fname[:-9] + ".py"
- return fname
-
def source(self):
if self._source is None:
self._source = get_python_source(self.filename)
diff --git a/coverage/xmlreport.py b/coverage/xmlreport.py
index f7ad2b8..df48993 100644
--- a/coverage/xmlreport.py
+++ b/coverage/xmlreport.py
@@ -20,9 +20,10 @@ def rate(hit, num):
class XmlReporter(Reporter):
"""A reporter for writing Cobertura-style XML coverage results."""
- def __init__(self, coverage, config):
+ def __init__(self, coverage, config, file_locator):
super(XmlReporter, self).__init__(coverage, config)
+ self.file_locator = file_locator
self.source_paths = set()
self.packages = {}
self.xml_out = None
@@ -121,7 +122,7 @@ class XmlReporter(Reporter):
# Create the 'lines' and 'package' XML elements, which
# are populated later. Note that a package == a directory.
- filename = cu.file_locator.relative_filename(cu.filename)
+ filename = self.file_locator.relative_filename(cu.filename)
filename = filename.replace("\\", "/")
dirname = os.path.dirname(filename) or "."
parts = dirname.split("/")
@@ -129,7 +130,7 @@ class XmlReporter(Reporter):
package_name = dirname.replace("/", ".")
className = cu.name
- self.source_paths.add(cu.file_locator.relative_dir.rstrip('/'))
+ self.source_paths.add(self.file_locator.relative_dir.rstrip('/'))
package = self.packages.setdefault(package_name, [{}, 0, 0, 0, 0])
xclass = self.xml_out.createElement("class")
diff --git a/tests/test_codeunit.py b/tests/test_codeunit.py
index ea65d85..3b873cd 100644
--- a/tests/test_codeunit.py
+++ b/tests/test_codeunit.py
@@ -3,7 +3,7 @@
import os
import sys
-from coverage.codeunit import CodeUnit
+from coverage.plugin import FileReporter
from coverage.python import PythonCodeUnit
from tests.coveragetest import CoverageTest
@@ -93,10 +93,10 @@ class CodeUnitTest(CoverageTest):
self.assertEqual(ccu.source(), "# cfile.py\n")
def test_comparison(self):
- acu = CodeUnit("aa/afile.py")
- acu2 = CodeUnit("aa/afile.py")
- zcu = CodeUnit("aa/zfile.py")
- bcu = CodeUnit("aa/bb/bfile.py")
+ acu = FileReporter("aa/afile.py")
+ acu2 = FileReporter("aa/afile.py")
+ zcu = FileReporter("aa/zfile.py")
+ bcu = FileReporter("aa/bb/bfile.py")
assert acu == acu2 and acu <= acu2 and acu >= acu2
assert acu < zcu and acu <= zcu and acu != zcu
assert zcu > acu and zcu >= acu and zcu != acu