Debian Codegraph @

jupyter_console/tests/test_console.py

raw
1 """Tests for two-process terminal frontend"""
2
3 # Copyright (c) Jupyter Development Team.
4 # Distributed under the terms of the Modified BSD License.
5
6 import os
7 import shutil
8 import sys
9 import tempfile
10 from subprocess import check_output
11
12 from flaky import flaky
13 import pytest
14
15 from traitlets.tests.utils import check_help_all_output
16
17
18 should_skip = sys.platform == "win32" or sys.version_info < (3,8) or sys.version_info[:2] == (3, 10) # noqa
19
20
21 @flaky
22 @pytest.mark.skipif(should_skip, reason="not supported")
23 def test_console_starts():
24 """test that `jupyter console` starts a terminal"""
25 p, pexpect, t = start_console()
26 p.sendline("5")
27 p.expect([r"Out\[\d+\]: 5", pexpect.EOF], timeout=t)
28 p.expect([r"In \[\d+\]", pexpect.EOF], timeout=t)
29 stop_console(p, pexpect, t)
30
31 def test_help_output():
32 """jupyter console --help-all works"""
33 check_help_all_output('jupyter_console')
34
35
36 @flaky
37 @pytest.mark.skipif(should_skip, reason="not supported")
38 def test_display_text():
39 "Ensure display protocol plain/text key is supported"
40 # equivalent of:
41 #
42 # x = %lsmagic
43 # from IPython.display import display; display(x);
44 p, pexpect, t = start_console()
45 p.sendline('x = %lsmagic')
46 p.expect(r'In \[\d+\]', timeout=t)
47 p.sendline('from IPython.display import display; display(x);')
48 p.expect(r'Available line magics:', timeout=t)
49 p.expect(r'In \[\d+\]', timeout=t)
50 stop_console(p, pexpect, t)
51
52 def stop_console(p, pexpect, t):
53 "Stop a running `jupyter console` running via pexpect"
54 # send ctrl-D;ctrl-D to exit
55 p.sendeof()
56 p.sendeof()
57 p.expect([pexpect.EOF, pexpect.TIMEOUT], timeout=t)
58 if p.isalive():
59 p.terminate()
60
61
62 def start_console():
63 "Start `jupyter console` using pexpect"
64 import pexpect
65
66 args = ['-m', 'jupyter_console', '--colors=NoColor']
67 cmd = sys.executable
68 env = os.environ.copy()
69 env["JUPYTER_CONSOLE_TEST"] = "1"
70 env["PROMPT_TOOLKIT_NO_CPR"] = "1"
71
72 try:
73 p = pexpect.spawn(cmd, args=args, env=env)
74 except IOError:
75 pytest.skip("Couldn't find command %s" % cmd)
76
77 # timeout after two minutes
78 t = 120
79 p.expect(r"In \[\d+\]", timeout=t)
80 return p, pexpect, t
81
82
83 def test_multiprocessing():
84 p, pexpect, t = start_console()
85 p.sendline('')
86
87
88 def test_generate_config():
89 """jupyter console --generate-config works"""
90 td = tempfile.mkdtemp()
91 try:
92 check_output([sys.executable, '-m', 'jupyter_console', '--generate-config'],
93 env={'JUPYTER_CONFIG_DIR': td},
94 )
95 assert os.path.isfile(os.path.join(td, 'jupyter_console_config.py'))
96 finally:
97 shutil.rmtree(td)