Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run: fix --append with --data-file #45

Merged
merged 4 commits into from
Jul 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions covimerage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,8 @@ def merge_lines(line1, line2):
return lines

def _get_coveragepy_data(self):
if self.append_to:
fname, fobj, _ = get_fname_and_fobj_and_str(self.append_to)
if fobj or (fname and os.path.exists(fname)):
data = CoverageData(data_file=self.append_to)
else:
data = CoverageData()
if self.append_to and os.path.exists(self.append_to):
data = CoverageData(data_file=self.append_to)
else:
data = CoverageData()

Expand Down
6 changes: 2 additions & 4 deletions covimerage/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def write_coverage(profile_file, data_file, source):
type=click.Path(dir_okay=False), help=(
'File name for the PROFILE_FILE file. '
'By default a temporary file is used.'))
@click.option('--data-file', required=False, type=click.File('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('--append', is_flag=True, default=False, show_default=True,
Expand Down Expand Up @@ -135,9 +136,6 @@ def run(ctx, args, wrap_profile, profile_file, write_data, data_file,
p = Profile(profile_file)
p.parse()

if (write_data or append) and not data_file:
data_file = DEFAULT_COVERAGE_DATA_FILE

if append:
m = MergedProfiles([p], source=source, append_to=data_file)
else:
Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def devnull():
yield f


@pytest.fixture
@pytest.fixture(scope='session')
def covdata_header():
return "!coverage.py: This is a private format, don't read it directly!"


@pytest.fixture(scope='session')
def covdata_empty():
return "!coverage.py: This is a private format, don't read it directly!{}"
59 changes: 35 additions & 24 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,16 @@ def test_cli_run_args(runner, mocker, devnull, tmpdir):
'Error: Command exited non-zero: 3.']

# Write data with non-sources only.
f = StringIO()
m.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', '--data-file', f,
'--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 == [
Expand All @@ -139,8 +139,6 @@ def test_cli_run_args(runner, mocker, devnull, tmpdir):
'Ignoring non-source: %s' % str(tmpdir.join(
'tests/test_plugin/conditional_function.vim')),
'Not writing coverage file: no data to report!']
f.seek(0)
assert f.read() == ''

profiled_file = 'tests/test_plugin/conditional_function.vim'
profiled_file_content = open(profiled_file, 'r').read()
Expand All @@ -153,23 +151,25 @@ def test_cli_run_args(runner, mocker, devnull, tmpdir):
result = runner.invoke(cli.run, [
'--no-wrap-profile', '--no-report',
'--profile-file', profile_file,
'--write-data', '--data-file', f,
'--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 %r.' % f]
f.seek(0)
'Writing coverage file %s.' % DEFAULT_COVERAGE_DATA_FILE]
assert result.exit_code == 0

m.exit_code == 0
from covimerage.coveragepy import CoverageWrapper
cov = CoverageWrapper(data_file=f)
expected = {
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
assert cov.lines == expected_cov_lines


@pytest.mark.parametrize('with_append', (True, False))
Expand Down Expand Up @@ -616,28 +616,41 @@ def test_run_append_with_empty_data(runner, tmpdir):
with tmpdir.as_cwd() as old_dir:
profile_file = str(old_dir.join(
'tests/fixtures/conditional_function.profile'))
data_file = StringIO()
data_file = '.covimerage_covimerage'
with open(data_file, 'w') as f:
f.write('')
result = runner.invoke(cli.run, [
'--append', '--no-wrap-profile', '--profile-file', profile_file,
'--data-file', data_file, 'printf', '--', '--headless'])
assert result.output.splitlines() == [
'Running cmd: printf -- --headless (in %s)' % str(tmpdir),
'Parsing profile file %s.' % profile_file,
'Error: Coverage could not read data_file: %r (CoverageException("Doesn\'t seem to be a coverage.py data file",))' % data_file,
'Error: Coverage could not read data_file: %s '
'(CoverageException("Couldn\'t read data from \'%s\': '
'CoverageException: Doesn\'t seem to be a coverage.py data file",))' % (
data_file, data_file),
]
assert result.exit_code == 1


def test_run_append_with_data(runner, tmpdir):
@pytest.mark.parametrize('with_data_file', (True, False))
def test_run_append_with_data(with_data_file, runner, tmpdir, covdata_header):
profiled_file = 'tests/test_plugin/conditional_function.vim'
profiled_file_content = open(profiled_file, 'r').read()
tmpdir.join(profiled_file).write(profiled_file_content, ensure=True)

def run_args(profile_file):
args = []
if with_data_file:
args += ['--data-file', DEFAULT_COVERAGE_DATA_FILE]
return args + [
'--append', '--no-wrap-profile', '--profile-file', profile_file,
'printf', '--', '--headless']

with tmpdir.as_cwd() as old_dir:
profile_file = str(old_dir.join(
'tests/fixtures/conditional_function.profile'))
result = runner.invoke(cli.run, [
'--append', '--no-wrap-profile', '--profile-file', profile_file,
'printf', '--', '--headless'])
result = runner.invoke(cli.run, run_args(profile_file))
assert result.output.splitlines() == [
'Running cmd: printf -- --headless (in %s)' % str(tmpdir),
'Parsing profile file %s.' % profile_file,
Expand All @@ -647,10 +660,10 @@ def test_run_append_with_data(runner, tmpdir):
'tests/test_plugin/conditional_function.vim 13 5 62%']
assert result.exit_code == 0

assert open('.coverage.covimerage').read().startswith(covdata_header)

# The same again.
result = runner.invoke(cli.run, [
'--append', '--no-wrap-profile', '--profile-file', profile_file,
'printf', '--', '--headless'])
result = runner.invoke(cli.run, run_args(profile_file))
assert result.output.splitlines() == [
'Running cmd: printf -- --headless (in %s)' % str(tmpdir),
'Parsing profile file %s.' % profile_file,
Expand All @@ -668,9 +681,7 @@ def test_run_append_with_data(runner, tmpdir):
profile_file = str(old_dir.join(
'tests/fixtures/merged_conditionals-0.profile'))
tmpdir.join(profiled_file).write(profiled_file_content, ensure=True)
result = runner.invoke(cli.run, [
'--append', '--no-wrap-profile', '--profile-file', profile_file,
'printf', '--', '--headless'])
result = runner.invoke(cli.run, run_args(profile_file))
assert result.output.splitlines() == [
'Running cmd: printf -- --headless (in %s)' % str(tmpdir),
'Parsing profile file %s.' % profile_file,
Expand Down