Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_fname_and_fobj_and_str(devnull):
from covimerage.utils import get_fname_and_fobj_and_str
F = get_fname_and_fobj_and_str
assert F('foo') == ('foo', None, 'foo')
assert F(None) == (None, None, 'None')
assert F(devnull) == ('/dev/null', devnull, '/dev/null')
s = StringIO('')
assert F(s) == (None, s, str(s))
def test_mergedprofiles_fixes_line_count():
"""Ref: https://github.com/vim/vim/issues/2103"""
from covimerage import MergedProfiles, Profile
profile = textwrap.dedent("""
SCRIPT /path/to/t.vim
Sourced 1 time
Total time: 0.000009
Self time: 0.000009
count total (s) self (s)
let foo = 1
1 0.000002 let bar = 2
""")
p = Profile(StringIO(profile))
p.parse()
script = p.scripts[0]
assert [(l.count, l.line) for l in script.lines.values()] == [
(None, 'let foo = 1'),
(1, 'let bar = 2'),
]
m = MergedProfiles([p])
assert [(l.count, l.line) for l in m.lines[script.path].values()] == [
(1, 'let foo = 1'),
(1, 'let bar = 2'),
]
def test_merged_profiles_write_coveragepy_data_handles_fname_and_fobj(
mocker, caplog):
from covimerage import MergedProfiles
m = MergedProfiles([])
mocked_data = mocker.Mock()
mocker.patch.object(m, 'get_coveragepy_data', return_value=mocked_data)
m.write_coveragepy_data()
assert mocked_data.write_file.call_args_list == [mocker.call('.coverage')]
assert mocked_data.write_fileobj.call_args_list == []
mocked_data.reset_mock()
fileobj = StringIO()
m.write_coveragepy_data(data_file=fileobj)
assert mocked_data.write_file.call_args_list == []
assert mocked_data.write_fileobj.call_args_list == [mocker.call(fileobj)]
msgs = [r.message for r in caplog.records]
assert msgs == [
'Writing coverage file .coverage.',
'Writing coverage file %s.' % fileobj]
def test_coveragedata_empty(covdata_empty):
from covimerage.coveragepy import CoverageData
f = StringIO()
data = CoverageData()
try:
write_fileobj = data.cov_data.write_fileobj
except AttributeError:
# coveragepy 5
write_fileobj = data.cov_data._write_fileobj
write_fileobj(f)
f.seek(0)
assert f.read() == covdata_empty
def test_profile_fname_or_fobj(caplog, devnull):
from covimerage import Profile
with pytest.raises(FileNotFoundError) as excinfo:
Profile('/does/not/exist').parse()
assert str(excinfo.value) == \
"[Errno 2] No such file or directory: '/does/not/exist'"
with caplog.at_level(logging.DEBUG, logger='covimerage'):
Profile(devnull).parse()
msgs = [(r.levelname, r.message) for r in caplog.records]
assert msgs == [('DEBUG', 'Parsing file: /dev/null')]
fileobj = StringIO('')
with caplog.at_level(logging.DEBUG, logger='covimerage'):
Profile(fileobj).parse()
msgs = [(r.levelname, r.message) for r in caplog.records]
assert msgs[-1] == ('DEBUG', 'Parsing file: %s' % fileobj)
assert len(msgs) == 2
'/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_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:
def test_profile_parse_handles_cannot_open_file(caplog):
from covimerage import Profile
file_object = StringIO('\n'.join([
'SCRIPT /tmp/nvimDG3tAV/801',
'Sourced 1 time',
'Total time: 0.046577',
' Self time: 0.000136',
'',
'count total (s) self (s)',
'Cannot open file!',
]))
p = Profile('fake')
p._parse(file_object)
assert len(p.scripts) == 1
s = p.scripts[0]
assert not s.lines
assert len(s.lines) == 0
assert s.sourced_count == 1
try:
from coverage.data import CoverageJsonData
except ImportError:
assert excinfo.value.args == (
'data needs to be of type coverage.data.CoverageData',)
else:
assert excinfo.value.args == (
'data needs to be of type coverage.data.CoverageJsonData',)
with pytest.raises(CoverageWrapperException) as excinfo:
CoverageData(data_file='/does/not/exist')
assert excinfo.value.args == (
'Coverage could not read data_file: /does/not/exist',)
assert isinstance(excinfo.value.orig_exc, coverage.misc.CoverageException)
f = StringIO()
with pytest.raises(CoverageWrapperException) as excinfo:
CoverageData(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)
cov_data = CoverageData(data_file=coverage_fileobj)
with pytest.raises(attr.exceptions.FrozenInstanceError):
cov_data.data = 'foo'
assert cov_data.lines == {
'/test_plugin/conditional_function.vim': [
def coverage_fileobj():
return StringIO('\n'.join(['!coverage.py: This is a private format, don\'t read it directly!{"lines":{"/test_plugin/conditional_function.vim":[17,3,23,8,9,11,13,14,15]},"file_tracers":{"/test_plugin/conditional_function.vim":"covimerage.CoveragePlugin"}}']))