test_console.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """Tests for two-process terminal frontend"""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import os
  5. import shutil
  6. import sys
  7. import tempfile
  8. from subprocess import check_output
  9. from nose import SkipTest
  10. from traitlets.tests.utils import check_help_all_output
  11. from ipython_genutils.testing import decorators as dec
  12. @dec.skip_win32
  13. def test_console_starts():
  14. """test that `jupyter console` starts a terminal"""
  15. p, pexpect, t = start_console()
  16. p.sendline('5')
  17. idx = p.expect([r'Out\[\d+\]: 5', pexpect.EOF], timeout=t)
  18. idx = p.expect([r'In \[\d+\]', pexpect.EOF], timeout=t)
  19. stop_console(p, pexpect, t)
  20. def test_help_output():
  21. """jupyter console --help-all works"""
  22. check_help_all_output('jupyter_console')
  23. def test_display_text():
  24. "Ensure display protocol plain/text key is supported"
  25. # equivalent of:
  26. #
  27. # x = %lsmagic
  28. # from IPython.display import display; display(x);
  29. p, pexpect, t = start_console()
  30. p.sendline('x = %lsmagic')
  31. p.expect(r'In \[\d+\]', timeout=t)
  32. p.sendline('from IPython.display import display; display(x);')
  33. p.expect(r'Available line magics:', timeout=t)
  34. p.expect(r'In \[\d+\]', timeout=t)
  35. stop_console(p, pexpect, t)
  36. def stop_console(p, pexpect, t):
  37. "Stop a running `jupyter console` running via pexpect"
  38. # send ctrl-D;ctrl-D to exit
  39. p.sendeof()
  40. p.sendeof()
  41. p.expect([pexpect.EOF, pexpect.TIMEOUT], timeout=t)
  42. if p.isalive():
  43. p.terminate()
  44. def start_console():
  45. "Start `jupyter console` using pexpect"
  46. import pexpect
  47. args = ['-m', 'jupyter_console', '--colors=NoColor']
  48. cmd = sys.executable
  49. env = os.environ.copy()
  50. env['JUPYTER_CONSOLE_TEST'] = '1'
  51. try:
  52. p = pexpect.spawn(cmd, args=args, env=env)
  53. except IOError:
  54. raise SkipTest("Couldn't find command %s" % cmd)
  55. # timeout after one minute
  56. t = 60
  57. idx = p.expect(r'In \[\d+\]', timeout=t)
  58. return p, pexpect, t
  59. def test_generate_config():
  60. """jupyter console --generate-config works"""
  61. td = tempfile.mkdtemp()
  62. try:
  63. check_output([sys.executable, '-m', 'jupyter_console', '--generate-config'],
  64. env={'JUPYTER_CONFIG_DIR': td},
  65. )
  66. assert os.path.isfile(os.path.join(td, 'jupyter_console_config.py'))
  67. finally:
  68. shutil.rmtree(td)