Skip to content

Commit

Permalink
Merge an amended version of #896.
Browse files Browse the repository at this point in the history
Adds an option to specify the location of the coverage config file.
  • Loading branch information
jszakmeister committed Nov 28, 2015
2 parents 6d3768e + 63fd6d4 commit 0f592c4
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions nose/plugins/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ def options(self, parser, env):
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):
"""
Expand All @@ -110,8 +115,8 @@ def configure(self, options, conf):
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)):
Expand All @@ -135,11 +140,15 @@ def configure(self, options, conf):
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]')
Expand Down
103 changes: 92 additions & 11 deletions unit_tests/test_cover_plugin.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,106 @@
import os
import sys
from optparse import OptionParser
from nose.config import Config
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

0 comments on commit 0f592c4

Please # to comment.