Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with pytest.raises(attr.exceptions.FrozenInstanceError):
cov.data = 'foo'
assert cov.lines == {
'/test_plugin/conditional_function.vim': [
3, 8, 9, 11, 13, 14, 15, 17, 23]}
assert isinstance(cov._cov_obj, coverage.control.Coverage)
if hasattr(cov._cov_obj, '_data'):
# coveragepy 5
assert cov._cov_obj._data is cov.data.cov_data
else:
assert cov._cov_obj.data is cov.data.cov_data
with pytest.raises(CoverageWrapperException) as excinfo:
CoverageWrapper(data_file=devnull.name)
assert excinfo.value.args == (
'Coverage could not read data_file: /dev/null',)
f = StringIO()
with pytest.raises(CoverageWrapperException) as excinfo:
CoverageWrapper(data_file=f)
e = excinfo.value
assert isinstance(e.orig_exc, coverage.misc.CoverageException)
assert e.message == 'Coverage could not read data_file: %s' % f
assert e.format_message() == "%s (CoverageException: Doesn't seem to be a coverage.py data file)" % (e.message,)
assert str(e) == e.format_message()
assert repr(e) == 'CoverageWrapperException(message=%r, orig_exc=%r)' % (
e.message, e.orig_exc)
def test_cli_writecoverage_datafile(runner):
from covimerage.coveragepy import CoverageWrapper
fname = tempfile.mktemp()
result = runner.invoke(cli.main, ['write_coverage', '--data-file', fname,
'tests/fixtures/conditional_function.profile'])
assert result.output == '\n'.join([
'Writing coverage file %s.' % fname,
''])
assert result.exit_code == 0
cov = CoverageWrapper(data_file=fname)
assert cov.lines == {
os.path.abspath('tests/test_plugin/conditional_function.vim'): [
3, 8, 9, 11, 13, 14, 15, 17, 23]}
from covimerage.coveragepy import (
CoverageData, CoveragePyData, CoverageWrapper, CoverageWrapperException)
cov_data = CoverageWrapper()
assert cov_data.lines == {}
assert isinstance(cov_data.data, CoverageData)
cov_data = CoverageWrapper(data=CoveragePyData())
assert cov_data.lines == {}
assert isinstance(cov_data.data, CoverageData)
with pytest.raises(TypeError):
CoverageWrapper(data_file='foo', data='bar')
with pytest.raises(TypeError):
CoverageWrapper(data_file='foo', data=CoveragePyData())
cov = CoverageWrapper(data_file=coverage_fileobj)
with pytest.raises(attr.exceptions.FrozenInstanceError):
cov.data = 'foo'
assert cov.lines == {
'/test_plugin/conditional_function.vim': [
3, 8, 9, 11, 13, 14, 15, 17, 23]}
assert isinstance(cov._cov_obj, coverage.control.Coverage)
if hasattr(cov._cov_obj, '_data'):
# coveragepy 5
assert cov._cov_obj._data is cov.data.cov_data
else:
assert cov._cov_obj.data is cov.data.cov_data
def test_coveragewrapper(coverage_fileobj, devnull):
import coverage
from covimerage.coveragepy import (
CoverageData, CoveragePyData, CoverageWrapper, CoverageWrapperException)
cov_data = CoverageWrapper()
assert cov_data.lines == {}
assert isinstance(cov_data.data, CoverageData)
cov_data = CoverageWrapper(data=CoveragePyData())
assert cov_data.lines == {}
assert isinstance(cov_data.data, CoverageData)
with pytest.raises(TypeError):
CoverageWrapper(data_file='foo', data='bar')
with pytest.raises(TypeError):
CoverageWrapper(data_file='foo', data=CoveragePyData())
cov = CoverageWrapper(data_file=coverage_fileobj)
with pytest.raises(attr.exceptions.FrozenInstanceError):
cov.data = 'foo'
def test_coveragewrapper_requires_jsondata():
pytest.importorskip('coverage.sqldata')
from covimerage.coveragepy import CoverageWrapper
with pytest.raises(TypeError) as excinfo:
CoverageWrapper(data=coverage.sqldata.CoverageSqliteData())
assert excinfo.value.args[0] == (
'data needs to be of type coverage.data.CoverageJsonData')
def test_coveragewrapper_uses_config_file(tmpdir, capfd):
from covimerage.coveragepy import CoverageWrapper, CoverageWrapperException
with tmpdir.as_cwd() as old_dir:
vim_src = '%s/tests/test_plugin/conditional_function.vim' % old_dir
coverage_fileobj = StringIO('!coverage.py: This is a private format, don\'t read it directly!{"lines":{"%s":[17,3,23,8,9,11,13,14,15]},"file_tracers":{"%s":"covimerage.CoveragePlugin"}}' % (vim_src, vim_src))
cov = CoverageWrapper(data_file=coverage_fileobj)
assert cov._cov_obj.config.report_include is None
assert cov.lines == {vim_src: [3, 8, 9, 11, 13, 14, 15, 17, 23]}
cov.report()
out, err = capfd.readouterr()
assert 'test_plugin/conditional_function.vim' in out
assert err == ''
coveragerc = str(tmpdir.join('.coveragerc'))
with open(coveragerc, 'w') as f:
f.write('[report]\ninclude = foo/*,bar/*')
coverage_fileobj.seek(0)
cov = CoverageWrapper(data_file=coverage_fileobj)
assert cov._cov_obj.config.report_include == ['foo/*', 'bar/*']
with pytest.raises(CoverageWrapperException) as excinfo:
cov.report()
assert excinfo.value.args == ('No data to report. (CoverageException)',)
vim_src = '%s/tests/test_plugin/conditional_function.vim' % old_dir
coverage_fileobj = StringIO('!coverage.py: This is a private format, don\'t read it directly!{"lines":{"%s":[17,3,23,8,9,11,13,14,15]},"file_tracers":{"%s":"covimerage.CoveragePlugin"}}' % (vim_src, vim_src))
cov = CoverageWrapper(data_file=coverage_fileobj)
assert cov._cov_obj.config.report_include is None
assert cov.lines == {vim_src: [3, 8, 9, 11, 13, 14, 15, 17, 23]}
cov.report()
out, err = capfd.readouterr()
assert 'test_plugin/conditional_function.vim' in out
assert err == ''
coveragerc = str(tmpdir.join('.coveragerc'))
with open(coveragerc, 'w') as f:
f.write('[report]\ninclude = foo/*,bar/*')
coverage_fileobj.seek(0)
cov = CoverageWrapper(data_file=coverage_fileobj)
assert cov._cov_obj.config.report_include == ['foo/*', 'bar/*']
with pytest.raises(CoverageWrapperException) as excinfo:
cov.report()
assert excinfo.value.args == ('No data to report. (CoverageException)',)
out, err = capfd.readouterr()
assert out.splitlines() == [
'Name Stmts Miss Cover',
'---------------------------']
assert err == ''
If PROFILE_FILE is provided this gets parsed, otherwise DATA_FILE is used.
"""
# Use None instead of empty set, for coveragepy to use the config file.
include = include if include else None
omit = omit if omit else None
if data:
data_file = None
elif profile_file:
data_file = None
m = MergedProfiles(source=source)
m.add_profile_files(*profile_file)
data = m.get_coveragepy_data()
config_file = ctx.obj.get('rcfile') if ctx.obj else None
CoverageWrapper(
data=data,
data_file=data_file,
config_file=config_file,
).report(
report_file=report_file,
show_missing=show_missing,
include=include,
omit=omit,
skip_covered=skip_covered,
)
def xml(ctx, data_file, include, omit, ignore_errors):
"""
A wrapper around `coverage xml`.
This will automatically add covimerage as a plugin, and then just forwards
most options.
"""
# Use None instead of empty set, for coveragepy to use the config file.
include = include if include else None
omit = omit if omit else None
config_file = ctx.obj.get('rcfile') if ctx.obj else None
CoverageWrapper(
data_file=data_file,
config_file=config_file,
).reportxml(
include=include,
omit=omit,
ignore_errors=ignore_errors,
)