run-test.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. foo='bar')
  24. print('\n\nNotebook config:')
  25. print(json.dumps(config))
  26. with open(os.path.join(HERE, 'build', 'injector.js'), 'w') as fid:
  27. fid.write("""
  28. var node = document.createElement('script');
  29. node.id = 'jupyter-config-data';
  30. node.type = 'application/json';
  31. node.textContent = '%s';
  32. document.body.appendChild(node);
  33. """ % json.dumps(config))
  34. return ['karma', 'start'] + sys.argv[1:]
  35. def create_notebook_dir():
  36. """Create a temporary directory with some file structure."""
  37. root_dir = tempfile.mkdtemp(prefix='mock_contents')
  38. os.mkdir(os.path.join(root_dir, 'src'))
  39. with open(os.path.join(root_dir, 'src', 'temp.txt'), 'w') as fid:
  40. fid.write('hello')
  41. atexit.register(lambda: shutil.rmtree(root_dir, True))
  42. return root_dir
  43. @gen.coroutine
  44. def run(cmd):
  45. """Run the cmd and exit with the return code"""
  46. yield gen.moment # sync up with the ioloop
  47. shell = os.name == 'nt'
  48. proc = Popen(cmd, shell=shell)
  49. print('\n\nRunning command: "%s"\n\n' % ' '.join(cmd))
  50. # Poll the process once per second until finished.
  51. while 1:
  52. yield gen.sleep(1)
  53. if proc.poll() is not None:
  54. break
  55. exit(proc.returncode)
  56. @gen.coroutine
  57. def exit(code):
  58. """Safely stop the app and then exit with the given code."""
  59. yield gen.moment # sync up with the ioloop
  60. IOLoop.current().stop()
  61. sys.exit(code)
  62. class TestApp(NotebookApp):
  63. """A notebook app that supports a unit test."""
  64. open_browser = Bool(False)
  65. notebook_dir = Unicode(create_notebook_dir())
  66. allow_origin = Unicode('*')
  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()