예제 #1
0
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 == ''
예제 #2
0
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)
예제 #3
0
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')
예제 #4
0
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]
    }
예제 #5
0
def test_coveragewrapper_accepts_data():
    from covimerage.coveragepy import CoverageData, CoverageWrapper

    data = CoverageData()
    cov = CoverageWrapper(data=data)
    assert cov.data is data
예제 #6
0
def test_cli_run_args(runner, mocker, devnull, tmpdir):
    m = mocker.patch('subprocess.Popen')
    m_wait = m.return_value.wait

    m_wait.return_value = 1
    result = runner.invoke(cli.run,
                           ['--no-wrap-profile', 'printf', '--headless'])
    assert m.call_args[0] == (['printf', '--headless'], )
    assert result.output.splitlines() == [
        'Running cmd: printf --headless (in %s)' % os.getcwd(),
        'Error: Command exited non-zero: 1.'
    ]
    assert result.exit_code == 1

    m_wait.return_value = 2
    result = runner.invoke(cli.run,
                           ['--no-wrap-profile', '--', 'printf', '--headless'])
    assert m.call_args[0] == (['printf', '--headless'], )
    assert result.output.splitlines() == [
        'Running cmd: printf --headless (in %s)' % os.getcwd(),
        'Error: Command exited non-zero: 2.'
    ]
    assert result.exit_code == 2

    m_wait.return_value = 3
    result = runner.invoke(cli.run,
                           ['--no-wrap-profile', 'printf', '--', '--headless'])
    assert m.call_args[0] == (['printf', '--', '--headless'], )
    assert result.output.splitlines() == [
        'Running cmd: printf -- --headless (in %s)' % os.getcwd(),
        'Error: Command exited non-zero: 3.'
    ]
    assert result.exit_code == 3

    result = runner.invoke(cli.run, [
        '--no-wrap-profile', '--no-report', '--profile-file', devnull.name,
        '--no-write-data', 'printf', '--', '--headless'
    ])
    assert m.call_args[0] == (['printf', '--', '--headless'], )
    assert result.output.splitlines() == [
        'Running cmd: printf -- --headless (in %s)' % os.getcwd(),
        'Error: Command exited non-zero: 3.'
    ]

    result = runner.invoke(cli.run, [
        '--no-wrap-profile', '--no-report', '--profile-file', devnull.name,
        '--source', devnull.name, '--write-data', 'printf', '--', '--headless'
    ])
    assert m.call_args[0] == (['printf', '--', '--headless'], )
    assert result.output.splitlines() == [
        'Running cmd: printf -- --headless (in %s)' % os.getcwd(),
        'Parsing profile file /dev/null.',
        'Not writing coverage file: no data to report!',
        'Error: Command exited non-zero: 3.'
    ]

    # Write data with non-sources only.
    m_wait.return_value = 0
    with tmpdir.as_cwd() as old_dir:
        profile_file = str(
            old_dir.join('tests/fixtures/conditional_function.profile'))
        result = runner.invoke(cli.run, [
            '--no-wrap-profile', '--no-report', '--profile-file', profile_file,
            '--write-data', 'printf', '--', '--headless'
        ])
        assert not os.path.exists(DEFAULT_COVERAGE_DATA_FILE)
    assert m.call_args[0] == (['printf', '--', '--headless'], )
    out = result.output.splitlines()
    assert out == [
        'Running cmd: printf -- --headless (in %s)' % str(tmpdir),
        'Parsing profile file %s.' % profile_file,
        'Ignoring non-source: %s' %
        str(tmpdir.join('tests/test_plugin/conditional_function.vim')),
        'Not writing coverage file: no data to report!'
    ]

    profiled_file = 'tests/test_plugin/conditional_function.vim'
    profiled_file_content = open(profiled_file, 'r').read()
    with tmpdir.as_cwd() as old_dir:
        profile_file = str(
            old_dir.join('tests/fixtures/conditional_function.profile'))
        tmpdir.join(profiled_file).write(profiled_file_content, ensure=True)
        tmpdir.join('not-profiled.vim').write('')
        tmpdir.join('not-a-vim-file').write('')
        result = runner.invoke(cli.run, [
            '--no-wrap-profile', '--no-report', '--profile-file', profile_file,
            '--write-data', 'printf', '--', '--headless'
        ])

        # Read coverage.
        from covimerage.coveragepy import CoverageWrapper
        cov = CoverageWrapper(data_file=DEFAULT_COVERAGE_DATA_FILE)

    assert m.call_args[0] == (['printf', '--', '--headless'], )
    assert result.output.splitlines() == [
        'Running cmd: printf -- --headless (in %s)' % str(tmpdir),
        'Parsing profile file %s.' % profile_file,
        'Writing coverage file %s.' % DEFAULT_COVERAGE_DATA_FILE
    ]
    assert result.exit_code == 0

    expected_cov_lines = {
        str(tmpdir.join('not-profiled.vim')): [],
        str(tmpdir.join('tests/test_plugin/conditional_function.vim')):
        [3, 8, 9, 11, 13, 14, 15, 17, 23]
    }
    assert cov.lines == expected_cov_lines