Skip to content

Commit 0a5f292

Browse files
authored
Fix --append with write_coverage (#50)
1 parent cf9fcd7 commit 0a5f292

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

covimerage/cli.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ def main(ctx, verbose, quiet, loglevel, rcfile):
3737

3838
@main.command()
3939
@click.argument('profile_file', type=click.File('r'), required=False, nargs=-1)
40-
@click.option('--data-file', required=False, show_default=True,
41-
default=DEFAULT_COVERAGE_DATA_FILE, type=click.File(mode='w'))
40+
@click.option('--data-file', required=False, type=click.Path(dir_okay=False),
41+
default=DEFAULT_COVERAGE_DATA_FILE,
42+
help=('DATA_FILE to write into. '
43+
u'[default:\xa0%s]' % DEFAULT_COVERAGE_DATA_FILE))
4244
@click.option('--source', type=click.types.Path(exists=True), help=(
4345
'Source files/dirs to include. This is necessary to include completely '
4446
'uncovered files.'), show_default=True, multiple=True)

tests/test_cli.py

+33-12
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import subprocess
44
from subprocess import call
55
import sys
6+
import tempfile
67
import time
78

89
import pytest
910

1011
from covimerage import DEFAULT_COVERAGE_DATA_FILE, cli, get_version
11-
from covimerage._compat import StringIO
1212

1313

1414
def test_dunder_main_run(capfd):
@@ -209,6 +209,29 @@ def test_cli_run_can_skip_writing_data(with_append, runner, tmpdir):
209209
assert not tmpdir.join(DEFAULT_COVERAGE_DATA_FILE).exists()
210210

211211

212+
def test_cli_write_coverage_with_append(runner, tmpdir, covdata_header):
213+
profiled_file = 'tests/test_plugin/conditional_function.vim'
214+
profiled_file_content = open(profiled_file, 'r').read()
215+
with tmpdir.as_cwd() as old_dir:
216+
profile_file = str(old_dir.join(
217+
'tests/fixtures/conditional_function.profile'))
218+
tmpdir.join(profiled_file).write(profiled_file_content, ensure=True)
219+
args = ['--append', profile_file]
220+
result = runner.invoke(cli.write_coverage, args)
221+
222+
assert result.output.splitlines() == [
223+
'Writing coverage file .coverage_covimerage.',
224+
]
225+
assert tmpdir.join(DEFAULT_COVERAGE_DATA_FILE).exists()
226+
227+
data = open('.coverage_covimerage').read()
228+
assert data.startswith(covdata_header)
229+
230+
# Not changed if appending the same.
231+
result = runner.invoke(cli.write_coverage, args)
232+
assert len(open('.coverage_covimerage').read()) == len(data)
233+
234+
212235
def test_cli_run_report_fd(capfd, tmpdir, devnull):
213236
profile_fname = 'tests/fixtures/conditional_function.profile'
214237
with open(profile_fname, 'r') as f:
@@ -328,35 +351,33 @@ def test_cli_writecoverage_without_data(runner):
328351
def test_cli_writecoverage_datafile(runner):
329352
from covimerage.coveragepy import CoverageWrapper
330353

331-
f = StringIO()
332-
result = runner.invoke(cli.main, ['write_coverage', '--data-file', f,
354+
fname = tempfile.mktemp()
355+
result = runner.invoke(cli.main, ['write_coverage', '--data-file', fname,
333356
'tests/fixtures/conditional_function.profile'])
334357
assert result.output == '\n'.join([
335-
'Writing coverage file %s.' % f,
358+
'Writing coverage file %s.' % fname,
336359
''])
337360
assert result.exit_code == 0
338361

339-
f.seek(0)
340-
cov = CoverageWrapper(data_file=f)
362+
cov = CoverageWrapper(data_file=fname)
341363
assert cov.lines == {
342364
os.path.abspath('tests/test_plugin/conditional_function.vim'): [
343365
3, 8, 9, 11, 13, 14, 15, 17, 23]}
344366

345367

346-
def test_cli_writecoverage_source(runner):
368+
def test_cli_writecoverage_source(runner, devnull):
347369
from covimerage.coveragepy import CoverageWrapper
348370

349-
f = StringIO()
371+
fname = tempfile.mktemp()
350372
result = runner.invoke(cli.main, [
351-
'write_coverage', '--data-file', f, '--source', '.',
373+
'write_coverage', '--data-file', fname, '--source', '.',
352374
'tests/fixtures/conditional_function.profile'])
353375
assert result.output == '\n'.join([
354-
'Writing coverage file %s.' % f,
376+
'Writing coverage file %s.' % fname,
355377
''])
356378
assert result.exit_code == 0
357379

358-
f.seek(0)
359-
cov = CoverageWrapper(data_file=f)
380+
cov = CoverageWrapper(data_file=fname)
360381
assert cov.lines[
361382
os.path.abspath('tests/test_plugin/conditional_function.vim')] == [
362383
3, 8, 9, 11, 13, 14, 15, 17, 23]

0 commit comments

Comments
 (0)