Skip to content

Commit

Permalink
Print useful error message when filename is passed to stub/apply (#88)
Browse files Browse the repository at this point in the history
Fixes #65.
  • Loading branch information
rajathagasthya authored and mpage committed Aug 20, 2018
1 parent fc361b7 commit 2272dd4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions monkeytype/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def complain_about_no_traces(args: argparse.Namespace, stderr: IO) -> None:
module, qualname = args.module_path
if qualname:
print(f'No traces found for specifier {module}:{qualname}', file=stderr)
# When there is no trace and a top level module's filename is passed, print
# a useful error message.
elif os.path.exists(module):
print(f"No traces found for {module}; did you pass a filename instead of a module name? "
f"Maybe try just '{os.path.splitext(module)[0]}'.", file=stderr)
else:
print(f'No traces found for module {module}', file=stderr)

Expand Down
18 changes: 18 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,24 @@ def test_pathlike_parameter(store_data, capsys):
assert "test/foo.py does not look like a valid Python import path" in err


def test_toplevel_filename_parameter(store_data, stdout, stderr):
filename = 'foo.py'
store, db_file = store_data
with mock.patch.dict(os.environ, {DefaultConfig.DB_PATH_VAR: db_file.name}):
orig_exists = os.path.exists

def side_effect(x):
return True if x == filename else orig_exists(x)
with mock.patch('os.path.exists', side_effect=side_effect) as mock_exists:
ret = cli.main(['stub', filename], stdout, stderr)
mock_exists.assert_called_with(filename)
err_msg = f"No traces found for {filename}; did you pass a filename instead of a module name? " \
f"Maybe try just '{os.path.splitext(filename)[0]}'.\n"
assert stderr.getvalue() == err_msg
assert stdout.getvalue() == ''
assert ret == 0


@pytest.mark.usefixtures("collector")
def test_apply_stub_init(store_data, stdout, stderr, collector):
"""Regression test for applying stubs to testmodule/__init__.py style module layout"""
Expand Down

0 comments on commit 2272dd4

Please sign in to comment.