Skip to content

Commit f9694bb

Browse files
committed
fixup! Wrap commands in __main__ to prepend 'covimerage: ' prefix with errors.
1 parent 2c361ba commit f9694bb

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

covimerage/__main__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ def wrap_for_errormsg(f, *args, **kwargs):
44
except Exception as exc:
55
from click.exceptions import ClickException
66
if isinstance(exc, ClickException):
7-
import sys
7+
import re, sys
88
from click.utils import echo
9+
from ._compat import StringIO
910

10-
msg = 'covimerage: error: %s' % (exc.format_message(),)
11-
echo(msg, err=True)
11+
# Use `show()` to get extended message with UsageErrors.
12+
out = StringIO()
13+
exc.show(file=out)
14+
out.seek(0)
15+
msg = re.sub("^Error: ", "covimerage: error: ", out.read(), flags=re.MULTILINE)
16+
echo(msg, err=True, nl=False)
1217
sys.exit(exc.exit_code)
18+
raise
1319

1420

1521
def main():

tests/test_cli.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_cli_run_with_args_fd(capfd):
7474
lines = err.splitlines()
7575
assert lines == [
7676
"Running cmd: echo -- --no-profile %sMARKER --cmd 'profile start /doesnotexist' --cmd 'profile! file ./*' (in {})".format(os.getcwd()),
77-
'Error: The profile file (/doesnotexist) has not been created.']
77+
'covimerage: error: The profile file (/doesnotexist) has not been created.']
7878
assert ret == 1
7979

8080

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

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

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

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

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

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

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

354354

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

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

0 commit comments

Comments
 (0)