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_apython_server(capfd, event_loop, monkeypatch):
def run_forever(self, orig=InteractiveEventLoop.run_forever):
if self.console_server is not None:
self.call_later(0, self.stop)
return orig(self)
with patch('aioconsole.InteractiveEventLoop.run_forever', run_forever):
with pytest.raises(SystemExit):
apython.run_apython(['--serve=:0'])
out, err = capfd.readouterr()
assert out.startswith('The console is being served on')
assert err == ''
tempfd.write(startupfile.encode())
tempfd.flush()
test_vectors = (
('print(foo)\n', '', '>>> 1\n'),
('print(hehe())\n', '', '>>> 42\n'),
('print(r)\n', '', '>>> 1.0\n'),
('pprint({1:2})\n', '{1: 2}\n', '>>> >>> \n'),
)
inputstr = ''.join([tv[0] for tv in test_vectors])
outstr = ''.join([tv[1] for tv in test_vectors])
errstr = 'test\n' + ''.join([tv[2] for tv in test_vectors])
with patch('sys.stdin', new=io.StringIO(inputstr)):
with pytest.raises(SystemExit):
apython.run_apython(['--banner=test'] + use_readline)
out, err = capfd.readouterr()
assert out == outstr
assert err == errstr
def test_apython_with_stdout_logs(capfd, use_readline):
with patch('sys.stdin', new=io.StringIO(
'import sys; sys.stdout.write("logging") or 7\n')):
with pytest.raises(SystemExit):
apython.run_apython(['--banner=test'] + use_readline)
out, err = capfd.readouterr()
assert out == 'logging'
assert err == 'test\n>>> 7\n>>> \n'
def test_apython_with_ainput(capfd, use_readline):
input_string = "{} ainput()\nhello\n".format(
'await' if compat.PY35 else 'yield from')
with patch('sys.stdin', new=io.StringIO(input_string)):
with pytest.raises(SystemExit):
apython.run_apython(['--banner=test'] + use_readline)
out, err = capfd.readouterr()
assert out == ''
assert err == "test\n>>> 'hello'\n>>> \n"
def test_apython_non_existing_module(capfd):
with pytest.raises(SystemExit):
apython.run_apython(['-m', 'idontexist'])
out, err = capfd.readouterr()
assert out == ''
assert "No module named idontexist" in err
def test_apython_non_existing_file(capfd):
with pytest.raises(SystemExit):
apython.run_apython(['idontexist.py'])
out, err = capfd.readouterr()
assert out == ''
assert "No such file or directory: 'idontexist.py'" in err
def test_apython_with_prompt_control(capfd):
with patch('sys.stdin', new=io.StringIO('1+1\n')):
with pytest.raises(SystemExit):
apython.run_apython(
['--banner=test', '--prompt-control=â–²', '--no-readline'])
out, err = capfd.readouterr()
assert out == ''
assert err == 'test\nâ–²>>> â–²2\nâ–²>>> â–²\n'
def test_basic_apython_usage_with_sys_argv(capfd, use_readline):
with patch('sys.argv', new=[
'path.py', '--banner=test'] + use_readline):
with patch('sys.stdin', new=io.StringIO('1+1\n')):
with pytest.raises(SystemExit):
apython.run_apython()
out, err = capfd.readouterr()
assert out == ''
assert err == 'test\n>>> 2\n>>> \n'
def test_apython_with_prompt_control_and_ainput(capfd):
input_string = "{} ainput()\nhello\n".format(
'await' if compat.PY35 else 'yield from')
with patch('sys.stdin', new=io.StringIO(input_string)):
with pytest.raises(SystemExit):
apython.run_apython(
['--no-readline', '--banner=test', '--prompt-control=â–²'])
out, err = capfd.readouterr()
assert out == ''
assert err == "test\n▲>>> ▲▲▲'hello'\n▲>>> ▲\n"
def test_basic_apython_usage(capfd, use_readline):
with patch('sys.stdin', new=io.StringIO('1+1\n')):
with pytest.raises(SystemExit):
apython.run_apython(['--banner=test'] + use_readline)
out, err = capfd.readouterr()
assert out == ''
assert err == 'test\n>>> 2\n>>> \n'