Permalink
Browse files

coverage deals with multiple packages

  • Loading branch information...
1 parent 86a76a3 commit 8891ab35b30c71a55c9898c396335ac39c5f78b6 @schettino72 schettino72 committed Feb 21, 2014
Showing with 85 additions and 37 deletions.
  1. +1 −0 CHANGES
  2. +3 −3 dodo.py
  3. +81 −34 doitpy/coverage.py
View
@@ -7,3 +7,4 @@ Changes
==========
- added pyflakes module
+ - added coverage module
View
@@ -1,5 +1,5 @@
from doitpy.pyflakes import Pyflakes
-from doitpy.coverage import Coverage
+from doitpy.coverage import PythonPackage, Coverage
DOIT_CONFIG = {'default_tasks': ['pyflakes']}
@@ -10,8 +10,8 @@ def task_pyflakes():
def task_coverage():
- cov = Coverage(source='doitpy', test='tests')
+ cov = Coverage([PythonPackage('doitpy', test_path='tests')],
+ omit=['"tests/sample_*"'])
yield cov.all()
yield cov.src()
yield cov.by_module()
-
View
@@ -1,61 +1,108 @@
+"""
+create tasks for coverage.py
+
+ * test files must be named in format 'test_xxx.py'
+ * packages does not contain sub-packages
+"""
import glob
+class PythonPackage(object):
+ # TODO should track sub-packages
+ TEST_PREFIX = 'test_'
+ def __init__(self, name, test_path=None):
+ """if test_path is not given assume it is 'tests' inside source package"""
+ self.name = name
+ self.src_base = name if name else ''
+ if test_path is None:
+ self.test_base = '{}/tests'.format(self.src_base)
+ else:
+ self.test_base = test_path
+ self.src = glob.glob("{}/*.py".format(self.src_base))
+ self.test = glob.glob("{}/*.py".format(self.test_base))
+ self.test_files = glob.glob("{}/{}*.py".format(
+ self.test_base, self.TEST_PREFIX))
+
+ def all_modules(self):
+ for mod in self.src + self.test:
+ yield mod
+
+
class Coverage(object):
"""python code coverage"""
- TEST_MOD_PREFIX = 'test_' # FIXME use this
- # TODO handle more than one package
- # TODO handle package with sub-packages
+ CMD_RUN_TEST = "`which py.test`"
+
+ def __init__(self, pkgs, branch=True, parallel=False, omit=()):
+ self.branch = branch
+ self.parallel = parallel
+ self.omit = omit
+ self.pkgs = []
+ for pkg in pkgs:
+ if isinstance(pkg, PythonPackage):
+ self.pkgs.append(pkg)
+ else:
+ self.pkgs.append(PythonPackage(pkg))
+
+
+ def _action_list(self, modules, test=''):
+ run_options = ''
+ if self.branch:
+ run_options += '--branch '
+ if self.parallel:
+ run_options += '--parallel-mode '
+
+ report_options = ''
+ if self.omit:
+ report_options += '--omit {}'.format(','.join(self.omit))
+
+ actions = ["coverage run {} {} {}".format(
+ run_options, self.CMD_RUN_TEST, test)]
+ if self.parallel:
+ actions.append('coverage combine')
+ actions.append("coverage report --show-missing {} {}".format(
+ report_options, " ".join(modules)))
+ return actions
- def __init__(self, source, test):
- self.source_dir = source
- self.test_dir = test
def all(self):
"""show coverage for all modules including tests"""
all_modules = []
- sources = glob.glob(self.source_dir + "/*.py")
- tests = glob.glob(self.test_dir + "/test_*.py")
- for module in sources + tests:
- all_modules.append(module)
+ for pkg in self.pkgs:
+ for module in pkg.all_modules():
+ all_modules.append(module)
yield {
'basename': 'coverage',
- 'actions':[
- "coverage run --branch `which py.test`",
- "coverage report --show-missing %s" % " ".join(all_modules),
- ],
+ 'actions': self._action_list(all_modules),
'verbosity': 2}
def src(self):
"""show coverage for all modules (exclude tests)"""
all_modules = []
- sources = glob.glob(self.source_dir + "/*.py")
- for module in sources:
- all_modules.append(module)
+ for pkg in self.pkgs:
+ for module in pkg.src:
+ all_modules.append(module)
yield {
- 'basename': 'cov_code',
- 'actions':[
- "coverage run --branch `which py.test`",
- "coverage report --show-missing %s" % " ".join(all_modules),
- ],
- 'verbosity': 2}
+ 'basename': 'coverage_src',
+ 'actions': self._action_list(all_modules),
+ 'verbosity': 2,
+ }
def by_module(self):
"""show coverage for individual modules"""
- to_strip = len(self.test_dir + '/test_')
- tests = glob.glob(self.test_dir + "/test_*.py")
- for test in tests:
- source = self.source_dir + '/' + test[to_strip:]
- yield {
- 'basename': 'cov_module',
- 'name': test,
- 'actions':
- ["coverage run --branch `which py.test` -v %s" % test,
- "coverage report --show-missing %s %s" % (source, test)],
- 'verbosity': 2}
+ for pkg in self.pkgs:
+ to_strip = len(pkg.test_base + '/test_')
+ tests = glob.glob(pkg.test_base + "/test_*.py")
+ for test in tests:
+ source = pkg.src_base + '/' + test[to_strip:]
+ yield {
+ 'basename': 'coverage_module',
+ 'name': test,
+ 'actions': self._action_list([source, test], test),
+ 'verbosity': 2,
+ }

0 comments on commit 8891ab3

Please sign in to comment.