summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2015-01-01 10:33:35 -0500
committerNed Batchelder <ned@nedbatchelder.com>2015-01-01 10:33:35 -0500
commitb88a6b2d44ef513503547fe2908fb41d298e87ec (patch)
treeaac12a9fdfd88755c4922e2b63e552d6d1215420
parent17d7b2265c5a2d239e5c114cb86c00be0a30bdf2 (diff)
downloadpython-coveragepy-git-b88a6b2d44ef513503547fe2908fb41d298e87ec.tar.gz
Refactoring FileLocators and codeunit factories, more to come.
-rw-r--r--coverage/codeunit.py18
-rw-r--r--tests/test_codeunit.py32
2 files changed, 25 insertions, 25 deletions
diff --git a/coverage/codeunit.py b/coverage/codeunit.py
index dc9a7ee6..a607ae1a 100644
--- a/coverage/codeunit.py
+++ b/coverage/codeunit.py
@@ -4,13 +4,13 @@ import os
import sys
from coverage.backward import string_class, unicode_class
-from coverage.files import get_python_source
+from coverage.files import get_python_source, FileLocator
from coverage.misc import CoverageException
from coverage.parser import PythonParser
from coverage.phystokens import source_token_lines, source_encoding
-def code_units_factory(morfs, file_locator, get_plugin=None):
+def code_units_factory(morfs, file_locator=None, get_plugin=None):
"""Construct a list of CodeUnits from modules or filenames.
`morfs` is a module or filename, or a list of the same.
@@ -36,7 +36,7 @@ def code_units_factory(morfs, file_locator, get_plugin=None):
return code_units
-def code_unit_factory(morf, file_locator, get_plugin=None):
+def code_unit_factory(morf, file_locator=None, get_plugin=None):
"""Construct a CodeUnit from a module or filename.
`morfs` is a module or a filename.
@@ -80,8 +80,8 @@ class CodeUnit(object):
"""
- def __init__(self, morf, file_locator):
- self.file_locator = file_locator
+ def __init__(self, morf, file_locator=None):
+ self.file_locator = file_locator or FileLocator()
if hasattr(morf, '__file__'):
f = morf.__file__
@@ -106,7 +106,11 @@ class CodeUnit(object):
self.modname = modname
def __repr__(self):
- return "<{self.__class__.__name__} name={self.name!r} filename={self.filename!r}>".format(self=self)
+ return (
+ "<{self.__class__.__name__}"
+ " name={self.name!r}"
+ " filename={self.filename!r}>".format(self=self)
+ )
def _adjust_filename(self, f):
# TODO: This shouldn't be in the base class, right?
@@ -175,7 +179,7 @@ class CodeUnit(object):
class PythonCodeUnit(CodeUnit):
"""Represents a Python file."""
- def __init__(self, morf, file_locator):
+ def __init__(self, morf, file_locator=None):
super(PythonCodeUnit, self).__init__(morf, file_locator)
self._source = None
diff --git a/tests/test_codeunit.py b/tests/test_codeunit.py
index b3b188b4..66635092 100644
--- a/tests/test_codeunit.py
+++ b/tests/test_codeunit.py
@@ -3,8 +3,7 @@
import os
import sys
-from coverage.codeunit import code_unit_factory, code_units_factory
-from coverage.files import FileLocator
+from coverage.codeunit import CodeUnit, PythonCodeUnit, code_units_factory
from tests.coveragetest import CoverageTest
@@ -24,9 +23,9 @@ class CodeUnitTest(CoverageTest):
sys.path.append(testmods)
def test_filenames(self):
- acu = code_unit_factory("aa/afile.py", FileLocator())
- bcu = code_unit_factory("aa/bb/bfile.py", FileLocator())
- ccu = code_unit_factory("aa/bb/cc/cfile.py", FileLocator())
+ acu = PythonCodeUnit("aa/afile.py")
+ bcu = PythonCodeUnit("aa/bb/bfile.py")
+ ccu = PythonCodeUnit("aa/bb/cc/cfile.py")
self.assertEqual(acu.name, "aa/afile")
self.assertEqual(bcu.name, "aa/bb/bfile")
self.assertEqual(ccu.name, "aa/bb/cc/cfile")
@@ -38,9 +37,9 @@ class CodeUnitTest(CoverageTest):
self.assertEqual(ccu.source(), "# cfile.py\n")
def test_odd_filenames(self):
- acu = code_unit_factory("aa/afile.odd.py", FileLocator())
- bcu = code_unit_factory("aa/bb/bfile.odd.py", FileLocator())
- b2cu = code_unit_factory("aa/bb.odd/bfile.py", FileLocator())
+ acu = PythonCodeUnit("aa/afile.odd.py")
+ bcu = PythonCodeUnit("aa/bb/bfile.odd.py")
+ b2cu = PythonCodeUnit("aa/bb.odd/bfile.py")
self.assertEqual(acu.name, "aa/afile.odd")
self.assertEqual(bcu.name, "aa/bb/bfile.odd")
self.assertEqual(b2cu.name, "aa/bb.odd/bfile")
@@ -56,7 +55,7 @@ class CodeUnitTest(CoverageTest):
import aa.bb
import aa.bb.cc
- cu = code_units_factory([aa, aa.bb, aa.bb.cc], FileLocator())
+ cu = code_units_factory([aa, aa.bb, aa.bb.cc])
self.assertEqual(cu[0].name, "aa")
self.assertEqual(cu[1].name, "aa.bb")
self.assertEqual(cu[2].name, "aa.bb.cc")
@@ -72,10 +71,7 @@ class CodeUnitTest(CoverageTest):
import aa.bb.bfile
import aa.bb.cc.cfile
- cu = code_units_factory(
- [aa.afile, aa.bb.bfile, aa.bb.cc.cfile],
- FileLocator()
- )
+ cu = code_units_factory([aa.afile, aa.bb.bfile, aa.bb.cc.cfile])
self.assertEqual(cu[0].name, "aa.afile")
self.assertEqual(cu[1].name, "aa.bb.bfile")
self.assertEqual(cu[2].name, "aa.bb.cc.cfile")
@@ -87,10 +83,10 @@ class CodeUnitTest(CoverageTest):
self.assertEqual(cu[2].source(), "# cfile.py\n")
def test_comparison(self):
- acu = code_unit_factory("aa/afile.py", FileLocator())
- acu2 = code_unit_factory("aa/afile.py", FileLocator())
- zcu = code_unit_factory("aa/zfile.py", FileLocator())
- bcu = code_unit_factory("aa/bb/bfile.py", FileLocator())
+ acu = CodeUnit("aa/afile.py")
+ acu2 = CodeUnit("aa/afile.py")
+ zcu = CodeUnit("aa/zfile.py")
+ bcu = CodeUnit("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
@@ -108,6 +104,6 @@ class CodeUnitTest(CoverageTest):
# in the path is actually the .egg zip file.
self.assert_doesnt_exist(egg1.__file__)
- cu = code_units_factory([egg1, egg1.egg1], FileLocator())
+ cu = code_units_factory([egg1, egg1.egg1])
self.assertEqual(cu[0].source(), u"")
self.assertEqual(cu[1].source().split("\n")[0], u"# My egg file!")