integration_test.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. from __future__ import print_function, absolute_import
  4. from __future__ import print_function, absolute_import
  5. import atexit
  6. import json
  7. import os
  8. from subprocess import Popen
  9. import sys
  10. import shutil
  11. import tempfile
  12. from tornado import gen
  13. from tornado.ioloop import IOLoop
  14. from notebook.notebookapp import NotebookApp
  15. from traitlets import Bool, Unicode
  16. HERE = os.path.dirname(__file__)
  17. DEBUG = False
  18. def create_notebook_dir():
  19. """Create a temporary directory with some file structure."""
  20. root_dir = tempfile.mkdtemp(prefix='mock_contents')
  21. os.mkdir(os.path.join(root_dir, 'src'))
  22. with open(os.path.join(root_dir, 'src', 'temp.txt'), 'w') as fid:
  23. fid.write('hello')
  24. atexit.register(lambda: shutil.rmtree(root_dir, True))
  25. return root_dir
  26. def get_command(nbapp):
  27. """Get the command to run"""
  28. terminalsAvailable = nbapp.web_app.settings['terminals_available']
  29. # Compatibility with Notebook 4.2.
  30. token = getattr(nbapp, 'token', '')
  31. cmd = ['mocha', '--timeout', '200000',
  32. '--retries', '2',
  33. 'build/integration.js',
  34. '--jupyter-config-data=./build/config.json']
  35. if DEBUG:
  36. cmd = ['devtool', '../node_modules/.bin/_mocha', '-qc'] + cmd[1:]
  37. config = dict(baseUrl=nbapp.connection_url,
  38. terminalsAvailable=str(terminalsAvailable))
  39. if token:
  40. config['token'] = nbapp.token
  41. with open('build/config.json', 'w') as fid:
  42. json.dump(config, fid)
  43. return cmd
  44. @gen.coroutine
  45. def run(cmd):
  46. """Run the cmd and exit with the return code"""
  47. yield gen.moment # sync up with the ioloop
  48. shell = os.name == 'nt'
  49. proc = Popen(cmd, shell=shell)
  50. print('\n\nRunning command: "%s"\n\n' % ' '.join(cmd))
  51. # Poll the process once per second until finished.
  52. while 1:
  53. yield gen.sleep(1)
  54. if proc.poll() is not None:
  55. break
  56. exit(proc.returncode)
  57. @gen.coroutine
  58. def exit(code):
  59. """Safely stop the app and then exit with the given code."""
  60. yield gen.moment # sync up with the ioloop
  61. IOLoop.current().stop()
  62. sys.exit(code)
  63. class TestApp(NotebookApp):
  64. """A notebook app that runs a mocha test."""
  65. open_browser = Bool(False)
  66. notebook_dir = Unicode(create_notebook_dir())
  67. def main():
  68. """Run the unit test."""
  69. app = TestApp()
  70. if app.version == '4.3.0':
  71. msg = ('Cannot run unit tests against Notebook 4.3.0. '
  72. 'Please upgrade to Notebook 4.3.1+')
  73. print(msg)
  74. sys.exit(1)
  75. app.initialize([]) # reserve sys.argv for the command
  76. cmd = get_command(app)
  77. run(cmd)
  78. try:
  79. app.start()
  80. except KeyboardInterrupt:
  81. exit(1)
  82. if __name__ == '__main__':
  83. main()