Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_apply_stub_init(store, db_file, stdout, stderr, collector):
"""Regression test for applying stubs to testmodule/__init__.py style module layout"""
with trace_calls(collector):
func_foo()
store.add(collector.traces)
with mock.patch.dict(os.environ, {DefaultConfig.DB_PATH_VAR: db_file.name}):
ret = cli.main(['apply', Foo.__module__], stdout, stderr)
assert ret == 0
assert 'warning:' not in stdout.getvalue()
def test_quiet_failed_traces(store, db_file, stdout, stderr):
traces = [
CallTrace(func, {'a': int, 'b': str}, NoneType),
CallTrace(func2, {'a': int, 'b': int}, NoneType),
]
store.add(traces)
with mock.patch("monkeytype.encoding.CallTraceRow.to_trace", side_effect=MonkeyTypeError("the-trace")):
ret = cli.main(['stub', func.__module__], stdout, stderr)
assert "2 traces failed to decode" in stderr.getvalue()
assert ret == 0
def test_pathlike_parameter(store, db_file, capsys, stdout, stderr):
with mock.patch.dict(os.environ, {DefaultConfig.DB_PATH_VAR: db_file.name}):
with pytest.raises(SystemExit):
cli.main(['stub', 'test/foo.py:bar'], stdout, stderr)
out, err = capsys.readouterr()
assert "test/foo.py does not look like a valid Python import path" in err
def test_verbose_failed_traces(store, db_file, stdout, stderr):
traces = [
CallTrace(func, {'a': int, 'b': str}, NoneType),
CallTrace(func2, {'a': int, 'b': int}, NoneType),
]
store.add(traces)
with mock.patch("monkeytype.encoding.CallTraceRow.to_trace", side_effect=MonkeyTypeError("the-trace")):
ret = cli.main(['-v', 'stub', func.__module__], stdout, stderr)
assert "WARNING: Failed decoding trace: the-trace" in stderr.getvalue()
assert ret == 0
def test_display_list_of_modules_no_modules(store, db_file, stdout, stderr):
with mock.patch.dict(os.environ, {DefaultConfig.DB_PATH_VAR: db_file.name}):
ret = cli.main(['list-modules'], stdout, stderr)
expected = ""
assert stderr.getvalue() == expected
expected = "\n"
assert stdout.getvalue() == expected
assert ret == 0
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()
assert out == "IN SETUP: stub\nIN TEARDOWN: stub\n"
assert err == ""
assert ret == 0
def test_no_traces(store, db_file, stdout, stderr, arg, error):
with mock.patch.dict(os.environ, {DefaultConfig.DB_PATH_VAR: db_file.name}):
ret = cli.main(['stub', arg], stdout, stderr)
assert stderr.getvalue() == error
assert stdout.getvalue() == ''
assert ret == 0
def test_generate_stub(store, db_file, stdout, stderr):
traces = [
CallTrace(func, {'a': int, 'b': str}, NoneType),
CallTrace(func2, {'a': int, 'b': int}, NoneType),
]
store.add(traces)
ret = cli.main(['stub', func.__module__], stdout, stderr)
expected = """def func(a: int, b: str) -> None: ...
def func2(a: int, b: int) -> None: ...
"""
assert stdout.getvalue() == expected
assert stderr.getvalue() == ''
assert ret == 0
def test_display_sample_count_from_cli(store, db_file, stdout, stderr):
traces = [
CallTrace(func, {'a': int, 'b': str}, NoneType),
CallTrace(func2, {'a': int, 'b': int}, NoneType),
]
store.add(traces)
with mock.patch.dict(os.environ, {DefaultConfig.DB_PATH_VAR: db_file.name}):
ret = cli.main(['stub', func.__module__, '--sample-count'], stdout, stderr)
expected = """Annotation for tests.test_cli.func based on 1 call trace(s).
Annotation for tests.test_cli.func2 based on 1 call trace(s).
"""
assert stderr.getvalue() == expected
assert ret == 0
def test_toplevel_filename_parameter(store, db_file, stdout, stderr):
filename = 'foo.py'
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