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)', ) out, err = capfd.readouterr() assert out.splitlines() == [ 'Name Stmts Miss Cover', '---------------------------' ] assert err == ''
def test_coveragedata_empty(covdata_empty): from covimerage.coveragepy import CoverageData f = StringIO() data = CoverageData() data.cov_data.write_fileobj(f) f.seek(0) assert f.read() == covdata_empty
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_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_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 msgs = [(r.name, r.levelname) for r in caplog.records] assert msgs == [('covimerage', 'WARNING')] assert caplog.records[0].message.startswith( "Could not parse count/times (fake:7, 'Cannot open file!'): ") assert 'ValueError' in caplog.records[0].message
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_coveragedata(coverage_fileobj): import coverage from covimerage.coveragepy import (CoverageData, CoveragePyData, CoverageWrapperException) with pytest.raises(TypeError) as excinfo: CoverageData(data_file='foo', cov_data=CoveragePyData()) assert excinfo.value.args == ( 'data and data_file are mutually exclusive.', ) data = CoverageData() try: from coverage.data import CoverageJsonData except ImportError: assert isinstance(data.cov_data, coverage.data.CoverageData) else: assert isinstance(data.cov_data, CoverageJsonData) with pytest.raises(TypeError) as excinfo: CoverageData(cov_data='foo') 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': [3, 8, 9, 11, 13, 14, 15, 17, 23] }
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' 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_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
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_handles_unmatched_defined(defined_format, defined_lnum, caplog): from covimerage import Profile defined = "Defined: invalid_defined.vim" if defined_format == "old": defined += " line " + str(defined_lnum) else: defined += ":" + str(defined_lnum) file_object = StringIO( textwrap.dedent(""" SCRIPT invalid_defined.vim Sourced 1 time Total time: 0.000037 Self time: 0.000032 count total (s) self (s) 1 0.000015 execute "function! F_via_execute_1()\\nreturn 0\\nendfunction" 1 0.000011 0.000007 call F_via_execute_1() 1 0.000006 0.000005 call F_via_execute_1() FUNCTION F_via_execute_1() {defined} Called 2 times Total time: 0.000005 Self time: 0.000005 count total (s) self (s) 2 0.000003 return 0 FUNCTIONS SORTED ON TOTAL TIME count total (s) self (s) function 2 0.000005 F_via_execute_1() FUNCTIONS SORTED ON SELF TIME count total (s) self (s) function 2 0.000005 F_via_execute_1() """.format(defined=defined))) p = Profile(file_object) p.parse() assert len(p.scripts) == 1 s = p.scripts[0] assert [ (l.count, l.line) for l in s.lines.values() if not l.line.startswith('"') ] == [ (1, 'execute "function! F_via_execute_1()\\nreturn 0\\nendfunction"'), (1, 'call F_via_execute_1()'), (1, 'call F_via_execute_1()'), ] logmsgs = [x[1:] for x in caplog.record_tuples] if defined_lnum == -1: assert logmsgs == [ (30, "Could not find script line for function F_via_execute_1 (-1, 1)" ), (40, "Could not find source for function: F_via_execute_1"), ] else: assert defined_lnum == 1 assert logmsgs == [ (30, "Script line does not match function line, ignoring: 'call F_via_execute_1()' != 'return 0'." ), (40, "Could not find source for function: F_via_execute_1"), ]
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"}}' ]))