Skip to content

Commit

Permalink
Fix --append with write_coverage (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed authored Jul 21, 2018
1 parent cf9fcd7 commit 0a5f292
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
6 changes: 4 additions & 2 deletions covimerage/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ def main(ctx, verbose, quiet, loglevel, rcfile):

@main.command()
@click.argument('profile_file', type=click.File('r'), required=False, nargs=-1)
@click.option('--data-file', required=False, show_default=True,
default=DEFAULT_COVERAGE_DATA_FILE, type=click.File(mode='w'))
@click.option('--data-file', required=False, type=click.Path(dir_okay=False),
default=DEFAULT_COVERAGE_DATA_FILE,
help=('DATA_FILE to write into. '
u'[default:\xa0%s]' % DEFAULT_COVERAGE_DATA_FILE))
@click.option('--source', type=click.types.Path(exists=True), help=(
'Source files/dirs to include. This is necessary to include completely '
'uncovered files.'), show_default=True, multiple=True)
Expand Down
45 changes: 33 additions & 12 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import subprocess
from subprocess import call
import sys
import tempfile
import time

import pytest

from covimerage import DEFAULT_COVERAGE_DATA_FILE, cli, get_version
from covimerage._compat import StringIO


def test_dunder_main_run(capfd):
Expand Down Expand Up @@ -209,6 +209,29 @@ def test_cli_run_can_skip_writing_data(with_append, runner, tmpdir):
assert not tmpdir.join(DEFAULT_COVERAGE_DATA_FILE).exists()


def test_cli_write_coverage_with_append(runner, tmpdir, covdata_header):
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)
args = ['--append', profile_file]
result = runner.invoke(cli.write_coverage, args)

assert result.output.splitlines() == [
'Writing coverage file .coverage_covimerage.',
]
assert tmpdir.join(DEFAULT_COVERAGE_DATA_FILE).exists()

data = open('.coverage_covimerage').read()
assert data.startswith(covdata_header)

# Not changed if appending the same.
result = runner.invoke(cli.write_coverage, args)
assert len(open('.coverage_covimerage').read()) == len(data)


def test_cli_run_report_fd(capfd, tmpdir, devnull):
profile_fname = 'tests/fixtures/conditional_function.profile'
with open(profile_fname, 'r') as f:
Expand Down Expand Up @@ -328,35 +351,33 @@ def test_cli_writecoverage_without_data(runner):
def test_cli_writecoverage_datafile(runner):
from covimerage.coveragepy import CoverageWrapper

f = StringIO()
result = runner.invoke(cli.main, ['write_coverage', '--data-file', f,
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.' % f,
'Writing coverage file %s.' % fname,
''])
assert result.exit_code == 0

f.seek(0)
cov = CoverageWrapper(data_file=f)
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]}


def test_cli_writecoverage_source(runner):
def test_cli_writecoverage_source(runner, devnull):
from covimerage.coveragepy import CoverageWrapper

f = StringIO()
fname = tempfile.mktemp()
result = runner.invoke(cli.main, [
'write_coverage', '--data-file', f, '--source', '.',
'write_coverage', '--data-file', fname, '--source', '.',
'tests/fixtures/conditional_function.profile'])
assert result.output == '\n'.join([
'Writing coverage file %s.' % f,
'Writing coverage file %s.' % fname,
''])
assert result.exit_code == 0

f.seek(0)
cov = CoverageWrapper(data_file=f)
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]
Expand Down

0 comments on commit 0a5f292

Please sign in to comment.