run-test.py 2.8 KB

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