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

[WIP] Wrap commands in __main__ to prepend 'covimerage: ' prefix #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 32 additions & 1 deletion covimerage/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
if __name__ == '__main__':
def wrap_for_prefix(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_prefix(main.main, prog_name='covimerage')


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

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


class AlwaysStderrHandler(logging.StreamHandler):
def __init__(self, level=logging.NOTSET):
Expand All @@ -18,4 +15,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
11 changes: 9 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
from covimerage._compat import FileNotFoundError, StringIO


def test_main_import():
from covimerage import __main__ # noqa: F401
def test_main_wrap_for_prefix():
from covimerage.__main__ import wrap_for_prefix

def f(**kwargs):
raise ValueError("custom")

with pytest.raises(ValueError) as excinfo:
wrap_for_prefix(f)
assert excinfo.value.args[0] == "custom"


def test_profile_repr_lines():
Expand Down