summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Szakmeister <john@szakmeister.net>2015-11-28 08:34:59 -0500
committerJohn Szakmeister <john@szakmeister.net>2015-11-28 08:34:59 -0500
commit0f592c4b022bf3226bd970ccc8c09fea899b2960 (patch)
treee7f138b70b2ff308bc6f3e6605770c0d577591f2
parent6d3768e043d44ed95cb5e73de62692653cf88ac5 (diff)
parent63fd6d41c91af01952289dd5322511276af1fd83 (diff)
downloadnose-0f592c4b022bf3226bd970ccc8c09fea899b2960.tar.gz
Merge an amended version of #896.
Adds an option to specify the location of the coverage config file.
-rw-r--r--README.txt4
-rw-r--r--nose/plugins/cover.py15
-rw-r--r--unit_tests/test_cover_plugin.py103
3 files changed, 108 insertions, 14 deletions
diff --git a/README.txt b/README.txt
index de3b748..67b8f29 100644
--- a/README.txt
+++ b/README.txt
@@ -389,6 +389,10 @@ Options
Produce XML coverage information in file
+--cover-config-file=DEFAULT
+
+ Location of coverage config file [NOSE_COVER_CONFIG_FILE]
+
--pdb
Drop into debugger on failures or errors
diff --git a/nose/plugins/cover.py b/nose/plugins/cover.py
index fbe2e30..0276f2d 100644
--- a/nose/plugins/cover.py
+++ b/nose/plugins/cover.py
@@ -89,6 +89,11 @@ class Coverage(Plugin):
dest="cover_xml_file",
metavar="FILE",
help="Produce XML coverage information in file")
+ parser.add_option("--cover-config-file", action="store",
+ default=env.get('NOSE_COVER_CONFIG_FILE'),
+ dest="cover_config_file",
+ help="Location of coverage config file "
+ "[NOSE_COVER_CONFIG_FILE]")
def configure(self, options, conf):
"""
@@ -110,8 +115,8 @@ class Coverage(Plugin):
self.enabled = False
return
self.conf = conf
- self.coverErase = options.cover_erase
- self.coverTests = options.cover_tests
+ self.coverErase = bool(options.cover_erase)
+ self.coverTests = bool(options.cover_tests)
self.coverPackages = []
if options.cover_packages:
if isinstance(options.cover_packages, (list, tuple)):
@@ -135,11 +140,15 @@ class Coverage(Plugin):
if options.cover_xml:
self.coverXmlFile = options.cover_xml_file
log.debug('Will put XML coverage report in %s', self.coverXmlFile)
+ # Coverage uses True to mean default
+ self.coverConfigFile = True
+ if options.cover_config_file:
+ self.coverConfigFile = options.cover_config_file
if self.enabled:
self.status['active'] = True
self.coverInstance = coverage.coverage(auto_data=False,
branch=self.coverBranches, data_suffix=conf.worker,
- source=self.coverPackages)
+ source=self.coverPackages, config_file=self.coverConfigFile)
self.coverInstance._warn_no_data = False
self.coverInstance.is_worker = conf.worker
self.coverInstance.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]')
diff --git a/unit_tests/test_cover_plugin.py b/unit_tests/test_cover_plugin.py
index 62052f2..b95c590 100644
--- a/unit_tests/test_cover_plugin.py
+++ b/unit_tests/test_cover_plugin.py
@@ -1,3 +1,4 @@
+import os
import sys
from optparse import OptionParser
from nose.config import Config
@@ -5,21 +6,101 @@ from nose.plugins.cover import Coverage
from nose.tools import eq_
import unittest
+
class TestCoveragePlugin(object):
- def test_cover_packages_option(self):
- parser = OptionParser()
- c = Coverage()
- c.addOptions(parser)
- options, args = parser.parse_args(['test_can_be_disabled',
- '--cover-package=pkg1,pkg2,pkg3'])
- c.configure(options, Config())
- eq_(['pkg1', 'pkg2', 'pkg3'], c.coverPackages)
+ def test_cover_options_packages(self):
+ _test_options_helper('--cover-package', 'coverPackages',
+ ['pkg1', 'pkg2', 'pkg3'], [],
+ 'pkg1,pkg2,pkg3', 'NOSE_COVER_PACKAGE')
+
+ def test_cover_options_erase(self):
+ _test_options_helper('--cover-erase', 'coverErase',
+ True, False,
+ env_key='NOSE_COVER_ERASE')
+
+ def test_cover_options_tests(self):
+ _test_options_helper('--cover-tests', 'coverTests',
+ True, False,
+ env_key='NOSE_COVER_TESTS')
+
+ def test_cover_options_config_file(self):
+ def get_sys_info(cov_inst):
+ # Older coverage uses sysinfo, while newer coverage uses sys_info.
+ if hasattr(cov_inst, 'sysinfo'):
+ return cov_inst.sysinfo()
+ else:
+ return cov_inst.sys_info()
+
+ def get_config_files(cov_info):
+ cov_info = dict(cov_info)
+ if 'config_files' in cov_info:
+ return cov_info['config_files']
+ return None
+
+ f = open('not_default_config_file', 'wb')
+ f.close()
+ try:
+ c1, c2 = _test_options_helper(
+ '--cover-config-file', 'coverConfigFile',
+ 'not_default_config_file', True,
+ arg_value='not_default_config_file',
+ env_key='NOSE_COVER_CONFIG_FILE')
- env = {'NOSE_COVER_PACKAGE': 'pkg1,pkg2,pkg3'}
+ cov_info = get_sys_info(c1.coverInstance)
+ config_files = get_config_files(cov_info)
+ if config_files is not None:
+ assert '.coveragerc' in config_files
+ else:
+ assert False, "coverage did not load default config file"
+
+ cov_info = get_sys_info(c2.coverInstance)
+ config_files = get_config_files(cov_info)
+ if config_files is not None:
+ assert 'not_default_config_file' in config_files
+ else:
+ assert False, "coverage did not load expected config file"
+ finally:
+ os.unlink('not_default_config_file')
+
+def _test_options_helper(arg_option, cover_option,
+ expected_set, expected_not_set,
+ arg_value=None, env_key=None):
+ prefix_args = ['test_can_be_disabled', '--with-coverage']
+
+ # Assert that the default works as expected
+ parser = OptionParser()
+ c_arg_not_set = Coverage()
+ c_arg_not_set.addOptions(parser)
+ options, _ = parser.parse_args(prefix_args)
+ c_arg_not_set.configure(options, Config())
+ eq_(expected_not_set, getattr(c_arg_not_set, cover_option))
+
+ # Assert that the argument parser picks up the expected value
+ parser = OptionParser()
+ c_arg_set = Coverage()
+ c_arg_set.addOptions(parser)
+
+ args = arg_option
+ if arg_value is not None:
+ args += '=' + arg_value
+
+ options, _ = parser.parse_args(prefix_args + [args])
+ c_arg_set.configure(options, Config())
+ eq_(expected_set, getattr(c_arg_set, cover_option))
+
+ # If the option supports environment variables, check that too
+ if env_key is not None:
+ args = 'true'
+ if arg_value is not None:
+ args = arg_value
+
+ env = {env_key: args}
c = Coverage()
parser = OptionParser()
c.addOptions(parser, env)
- options, args = parser.parse_args(['test_can_be_disabled'])
+ options, _ = parser.parse_args(prefix_args)
c.configure(options, Config())
- eq_(['pkg1', 'pkg2', 'pkg3'], c.coverPackages)
+ eq_(expected_set, getattr(c, cover_option))
+
+ return c_arg_not_set, c_arg_set