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" in err assert "idontexist.py" in err
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'
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"
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_apython_with_ainput(capfd, use_readline): input_string = "await ainput()\nhello\n" 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_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_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_with_prompt_control_and_ainput(capfd): input_string = "await ainput()\nhello\n" 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_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_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 == ''
def test_apython_pythonstartup(capfd, use_readline, monkeypatch, tempfd): monkeypatch.setenv('PYTHONSTARTUP', tempfd.name) 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_pythonstartup(capfd, use_readline, monkeypatch, tempfd): monkeypatch.setenv("PYTHONSTARTUP", tempfd.name) 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_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