Skip to content

Commit

Permalink
Display retype errors (#52)
Browse files Browse the repository at this point in the history
* Display retype errors

Catch retype failures and print the error so the user has a better
idea of what went wrong.

* Update changelog
  • Loading branch information
mpage committed Jan 10, 2018
1 parent 1f7b427 commit fb16e35
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
master
------

* Display retype errors when stub application fails. Merge of #42, fixes #49.

* Add ``monkeytype run -m`` for running a module as a script. Merge of
#41. Thanks Simon Gomizelj.

Expand Down
15 changes: 13 additions & 2 deletions monkeytype/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def get_stub(args: argparse.Namespace, stdout: IO, stderr: IO) -> Optional[Stub]
return stubs.get(module, None)


class HandlerError(Exception):
pass


def apply_stub_handler(args: argparse.Namespace, stdout: IO, stderr: IO) -> None:
stub = get_stub(args, stdout, stderr)
if stub is None:
Expand All @@ -121,7 +125,10 @@ def apply_stub_handler(args: argparse.Namespace, stdout: IO, stderr: IO) -> None
'--target-dir ' + src_dir,
src_path
])
subprocess.run(cmd, shell=True, check=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
try:
subprocess.run(cmd, shell=True, check=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
except subprocess.CalledProcessError as cpe:
raise HandlerError(f"Failed applying stub with retype:\n{cpe.stdout.decode('utf-8')}")


def print_stub_handler(args: argparse.Namespace, stdout: IO, stderr: IO) -> None:
Expand Down Expand Up @@ -274,7 +281,11 @@ def main(argv: List[str], stdout: IO, stderr: IO) -> int:
return 1

with args.config.cli_context(args.command):
handler(args, stdout, stderr)
try:
handler(args, stdout, stderr)
except HandlerError as err:
print(f"ERROR: {err}", file=stderr)
return 1

return 0

Expand Down
19 changes: 19 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import pytest
import sqlite3
import subprocess
import tempfile
from typing import Iterator

Expand Down Expand Up @@ -117,6 +118,24 @@ def test_display_sample_count_from_cli(store_data, stdout, stderr):
assert ret == 0


def test_retype_failure(store_data, stdout, stderr):
store, db_file = store_data
traces = [
CallTrace(func, {'a': int, 'b': str}, NoneType),
CallTrace(func2, {'a': int, 'b': int}, NoneType),
]
store.add(traces)
msg = "this is a test"
err = subprocess.CalledProcessError(returncode=100, cmd='retype')
err.stdout = msg.encode()
with mock.patch.dict(os.environ, {DefaultConfig.DB_PATH_VAR: db_file.name}):
with mock.patch('subprocess.run', side_effect=err):
ret = cli.main(['apply', func.__module__], stdout, stderr)
assert stdout.getvalue() == ""
assert stderr.getvalue() == f"ERROR: Failed applying stub with retype:\n{msg}\n"
assert ret == 1


def test_cli_context_manager_activated(capsys, stdout, stderr):
ret = cli.main(['-c', f'{__name__}:LoudContextConfig()', 'stub', 'some.module'], stdout, stderr)
out, err = capsys.readouterr()
Expand Down

0 comments on commit fb16e35

Please sign in to comment.