run-test.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. import subprocess
  8. import sys
  9. import shutil
  10. import tempfile
  11. from multiprocessing.pool import ThreadPool
  12. from tornado import ioloop
  13. from notebook.notebookapp import NotebookApp
  14. from traitlets import Bool, Unicode
  15. HERE = os.path.dirname(__file__)
  16. def create_notebook_dir():
  17. """Create a temporary directory with some file structure."""
  18. root_dir = tempfile.mkdtemp(prefix='mock_contents')
  19. os.mkdir(os.path.join(root_dir, 'src'))
  20. with open(os.path.join(root_dir, 'src', 'temp.txt'), 'w') as fid:
  21. fid.write('hello')
  22. atexit.register(lambda: shutil.rmtree(root_dir, True))
  23. return root_dir
  24. def run_task(func, args=(), kwds={}):
  25. """Run a task in a thread and exit with the return code."""
  26. loop = ioloop.IOLoop.instance()
  27. worker = ThreadPool(1)
  28. def callback(result):
  29. loop.add_callback(lambda: sys.exit(result))
  30. def start():
  31. worker.apply_async(func, args, kwds, callback)
  32. loop.call_later(1, start)
  33. def run_karma(base_url, token, terminalsAvailable):
  34. config = dict(baseUrl=base_url, token=token,
  35. terminalsAvailable=terminalsAvailable)
  36. with open(os.path.join(HERE, 'build', 'injector.js'), 'w') as fid:
  37. fid.write("""
  38. var node = document.createElement('script');
  39. node.id = 'jupyter-config-data';
  40. node.type = 'application/json';
  41. node.textContent = '%s';
  42. document.body.appendChild(node);
  43. """ % json.dumps(config))
  44. cmd = ['karma', 'start'] + ARGS
  45. shell = os.name == 'nt'
  46. return subprocess.check_call(cmd, shell=shell)
  47. class TestApp(NotebookApp):
  48. """A notebook app that runs a karma test."""
  49. open_browser = Bool(False)
  50. notebook_dir = Unicode(create_notebook_dir())
  51. allow_origin = Unicode('*')
  52. def start(self):
  53. terminals_available = self.web_app.settings['terminals_available']
  54. run_task(run_karma,
  55. args=(self.connection_url, self.token, terminals_available))
  56. super(TestApp, self).start()
  57. if __name__ == '__main__':
  58. # Reserve the command line arguments for karma.
  59. ARGS = sys.argv[1:]
  60. sys.argv = sys.argv[:1]
  61. try:
  62. TestApp.launch_instance()
  63. except KeyboardInterrupt:
  64. sys.exit(1)