Пример #1
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]
    }
Пример #2
0
def test_coveragewrapper(coverage_fileobj, devnull):
    import coverage
    from covimerage.coveragepy import (CoverageData, CoverageWrapper,
                                       CoverageWrapperException)

    cov_data = CoverageWrapper()
    assert cov_data.lines == {}
    assert isinstance(cov_data.data, CoverageData)

    cov_data = CoverageWrapper(data=coverage.data.CoverageData())
    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=CoverageData())

    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)
    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_accepts_data():
    from covimerage.coveragepy import CoverageData, CoverageWrapper

    data = CoverageData()
    cov = CoverageWrapper(data=data)
    assert cov.data is data
Пример #4
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