Skip to content

Commit

Permalink
fixup! Wrap commands in __main__ to prepend 'covimerage: ' prefix wit…
Browse files Browse the repository at this point in the history
…h errors.
  • Loading branch information
blueyed committed Oct 23, 2018
1 parent 2c361ba commit f9694bb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
12 changes: 9 additions & 3 deletions covimerage/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ def wrap_for_errormsg(f, *args, **kwargs):
except Exception as exc:
from click.exceptions import ClickException
if isinstance(exc, ClickException):
import sys
import re, sys
from click.utils import echo
from ._compat import StringIO

msg = 'covimerage: error: %s' % (exc.format_message(),)
echo(msg, err=True)
# Use `show()` to get extended message with UsageErrors.
out = StringIO()
exc.show(file=out)
out.seek(0)
msg = re.sub("^Error: ", "covimerage: error: ", out.read(), flags=re.MULTILINE)
echo(msg, err=True, nl=False)
sys.exit(exc.exit_code)
raise


def main():
Expand Down
20 changes: 10 additions & 10 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_cli_run_with_args_fd(capfd):
lines = err.splitlines()
assert lines == [
"Running cmd: echo -- --no-profile %sMARKER --cmd 'profile start /doesnotexist' --cmd 'profile! file ./*' (in {})".format(os.getcwd()),
'Error: The profile file (/doesnotexist) has not been created.']
'covimerage: error: The profile file (/doesnotexist) has not been created.']
assert ret == 1


Expand Down Expand Up @@ -301,14 +301,14 @@ def test_cli_call(capfd):
# click after 6.7 (9cfea14) includes: 'Try "covimerage --help" for help.'
assert err_lines[-2:] == [
'',
'Error: No such command "file not found".']
'covimerage: error: No such command "file not found".']
assert out == ''

assert call(['covimerage', 'write_coverage', 'file not found']) == 2
out, err = capfd.readouterr()
err_lines = err.splitlines()
assert err_lines[-1] == (
'Error: Invalid value for "%s": Could not open file: file not found: No such file or directory' % (
'covimerage: error: Invalid value for "%s": Could not open file: file not found: No such file or directory' % (
"profile_file" if click.__version__ < '7.0' else "[PROFILE_FILE]...",))
assert out == ''

Expand All @@ -319,7 +319,7 @@ def test_cli_call_verbosity_fd(capfd):
assert out == ''
assert err.splitlines() == [
'Not writing coverage file: no data to report!',
'Error: No data to report.']
'covimerage: error: No data to report.']

assert call(['covimerage', '-v', 'write_coverage', os.devnull]) == 1
out, err = capfd.readouterr()
Expand All @@ -328,7 +328,7 @@ def test_cli_call_verbosity_fd(capfd):
'Parsing file: /dev/null',
'source_files: []',
'Not writing coverage file: no data to report!',
'Error: No data to report.']
'covimerage: error: No data to report.']

assert call(['covimerage', '-vvvv', 'write_coverage', os.devnull]) == 1
out, err = capfd.readouterr()
Expand All @@ -337,19 +337,19 @@ def test_cli_call_verbosity_fd(capfd):
'Parsing file: /dev/null',
'source_files: []',
'Not writing coverage file: no data to report!',
'Error: No data to report.']
'covimerage: error: No data to report.']

assert call(['covimerage', '-vq', 'write_coverage', os.devnull]) == 1
out, err = capfd.readouterr()
assert out == ''
assert err.splitlines() == [
'Not writing coverage file: no data to report!',
'Error: No data to report.']
'covimerage: error: No data to report.']

assert call(['covimerage', '-qq', 'write_coverage', os.devnull]) == 1
out, err = capfd.readouterr()
assert out == ''
assert err == 'Error: No data to report.\n'
assert err == 'covimerage: error: No data to report.\n'


def test_cli_writecoverage_without_data(runner):
Expand Down Expand Up @@ -647,7 +647,7 @@ def test_run_handles_exit_code_from_python_fd(capfd):
ret = call(['covimerage', 'run',
'python', '-c', 'print("output"); import sys; sys.exit(42)'])
out, err = capfd.readouterr()
assert 'Error: Command exited non-zero: 42.' in err.splitlines()
assert 'covimerage: error: Command exited non-zero: 42.' in err.splitlines()
assert out == 'output\n'
assert ret == 42

Expand All @@ -658,7 +658,7 @@ def test_run_handles_exit_code_from_python_pty_fd(capfd):
"import pty; pty.spawn(['/bin/sh', '-c', "
"'printf output; exit 42'])"])
out, err = capfd.readouterr()
assert ('Error: The profile file (/not/used) has not been created.' in
assert ('covimerage: error: The profile file (/not/used) has not been created.' in
err.splitlines())
assert out == 'output'
assert ret == 1
Expand Down

0 comments on commit f9694bb

Please sign in to comment.