Skip to content

Commit

Permalink
Wrap commands in __main__ to prepend 'covimerage: ' prefix with errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Oct 23, 2018
1 parent bb84cc9 commit 146916f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 33 deletions.
38 changes: 37 additions & 1 deletion covimerage/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
if __name__ == '__main__':
def wrap_for_errormsg(f, *args, **kwargs):
import logging
from .logger import handler

handler.setFormatter(logging.Formatter('covimerage: %(message)s'))

try:
f(*args, standalone_mode=False, **kwargs)
except Exception as exc:
from click.exceptions import ClickException
if isinstance(exc, ClickException):
import re
import sys
from click.utils import echo
from ._compat import StringIO

# 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():
from .cli import main
wrap_for_errormsg(main.main, prog_name='covimerage')


def run():
from .cli import run
wrap_for_errormsg(run.main, prog_name='covimerage-run')


if __name__ == '__main__':
main()
9 changes: 4 additions & 5 deletions covimerage/logger.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import logging
import sys

logger = logging.getLogger('covimerage')
logger.setLevel(logging.INFO)


class AlwaysStderrHandler(logging.StreamHandler):
def __init__(self, level=logging.NOTSET):
logging.Handler.__init__(self, level)
Expand All @@ -18,4 +14,7 @@ def handleError(self, record):
raise Exception('Internal logging error')


logger.addHandler(AlwaysStderrHandler())
handler = AlwaysStderrHandler()
logger = logging.getLogger('covimerage')
logger.setLevel(logging.INFO)
logger.addHandler(handler)
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ def run(self):
url='https://github.com/Vimjas/covimerage',
packages=['covimerage'],
entry_points={
'console_scripts': ['covimerage=covimerage.cli:main'],
'console_scripts': [
'covimerage=covimerage.__main__:main',
],
},
use_scm_version={
'write_to': 'covimerage/__version__.py',
Expand Down
52 changes: 26 additions & 26 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def test_dunder_main_run(capfd):
assert call([sys.executable, '-m', 'covimerage']) == 0
out, err = capfd.readouterr()
assert out.startswith('Usage: __main__')
assert out.startswith('Usage: covimerage')


def test_dunder_main_run_help(capfd):
Expand Down Expand Up @@ -67,8 +67,8 @@ def test_cli_run_with_args_fd(capfd):
out, err = capfd.readouterr()
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: Running cmd: echo -- --no-profile %sMARKER --cmd 'profile start /doesnotexist' --cmd 'profile! file ./*' (in {})".format(os.getcwd()),
'covimerage: Error: The profile file (/doesnotexist) has not been created.']
assert ret == 1


Expand Down Expand Up @@ -259,9 +259,9 @@ def test_cli_run_report_fd(capfd, tmpdir, devnull):
'----------------------------------------------------------------',
'tests/test_plugin/conditional_function.vim 13 5 62%']
assert err.splitlines() == [
'Running cmd: true (in %s)' % str(os.getcwd()),
'Parsing profile file %s.' % tmp_profile_fname,
'Writing coverage file %s.' % data_file]
'covimerage: Running cmd: true (in %s)' % str(os.getcwd()),
'covimerage: Parsing profile file %s.' % tmp_profile_fname,
'covimerage: Writing coverage file %s.' % data_file]

# Same, but to some file.
ofname = str(tmpdir.join('ofname'))
Expand All @@ -270,9 +270,9 @@ def test_cli_run_report_fd(capfd, tmpdir, devnull):
assert exit_code == 0, err
assert out == ''
assert err.splitlines() == [
'Running cmd: true (in %s)' % str(os.getcwd()),
'Parsing profile file %s.' % tmp_profile_fname,
'Writing coverage file %s.' % data_file]
'covimerage: Running cmd: true (in %s)' % str(os.getcwd()),
'covimerage: Parsing profile file %s.' % tmp_profile_fname,
'covimerage: Writing coverage file %s.' % data_file]
assert open(ofname).read().splitlines() == [
'Name Stmts Miss Cover',
'----------------------------------------------------------------',
Expand All @@ -295,14 +295,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 @@ -312,38 +312,38 @@ def test_cli_call_verbosity_fd(capfd):
out, err = capfd.readouterr()
assert out == ''
assert err.splitlines() == [
'Not writing coverage file: no data to report!',
'Error: No data to report.']
'covimerage: Not writing coverage file: no data to report!',
'covimerage: Error: No data to report.']

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

assert call(['covimerage', '-vvvv', 'write_coverage', os.devnull]) == 1
out, err = capfd.readouterr()
assert out == ''
assert err.splitlines() == [
'Parsing file: /dev/null',
'source_files: []',
'Not writing coverage file: no data to report!',
'Error: No data to report.']
'covimerage: Parsing file: /dev/null',
'covimerage: source_files: []',
'covimerage: Not writing coverage file: 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: Not writing coverage file: 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 @@ -641,7 +641,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 @@ -652,7 +652,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 146916f

Please sign in to comment.