run-test.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. import argparse
  4. import subprocess
  5. import sys
  6. import os
  7. import re
  8. import json
  9. import shutil
  10. import threading
  11. import tempfile
  12. # Set up the file structure
  13. root_dir = tempfile.mkdtemp(prefix='mock_contents')
  14. os.mkdir(os.path.join(root_dir, 'src'))
  15. with open(os.path.join(root_dir, 'src', 'temp.txt'), 'w') as fid:
  16. fid.write('hello')
  17. HERE = os.path.dirname(__file__)
  18. shell = (sys.platform == 'win32')
  19. def start_notebook():
  20. nb_command = [sys.executable, '-m', 'notebook', root_dir, '--no-browser',
  21. # FIXME: allow-origin=* only required for notebook < 4.3
  22. '--NotebookApp.allow_origin="*"',
  23. # disable user password:
  24. '--NotebookApp.password=',
  25. # disable token:
  26. '--NotebookApp.token=']
  27. nb_server = subprocess.Popen(nb_command, shell=shell,
  28. stderr=subprocess.STDOUT,
  29. stdout=subprocess.PIPE)
  30. # wait for notebook server to start up
  31. while 1:
  32. line = nb_server.stdout.readline().decode('utf-8').strip()
  33. if not line:
  34. continue
  35. print(line)
  36. if 'Jupyter Notebook is running at:' in line:
  37. base_url = re.search(r'(http[^\?]+)', line).groups()[0]
  38. break
  39. while 1:
  40. line = nb_server.stdout.readline().decode('utf-8').strip()
  41. if not line:
  42. continue
  43. print(line)
  44. if 'Control-C' in line:
  45. break
  46. def print_thread():
  47. while 1:
  48. line = nb_server.stdout.readline().decode('utf-8').strip()
  49. if not line:
  50. continue
  51. print(line)
  52. thread = threading.Thread(target=print_thread)
  53. thread.setDaemon(True)
  54. thread.start()
  55. return nb_server, base_url
  56. def run_karma(base_url):
  57. config = dict(baseUrl=base_url,
  58. terminalsAvailable="True")
  59. with open(os.path.join(HERE, 'build', 'injector.js'), 'w') as fid:
  60. fid.write("""
  61. var node = document.createElement('script');
  62. node.id = 'jupyter-config-data';
  63. node.type = 'application/json';
  64. node.textContent = '%s';
  65. document.body.appendChild(node);
  66. """ % json.dumps(config))
  67. cmd = ['karma', 'start'] + sys.argv[1:]
  68. return subprocess.check_call(cmd, shell=shell, stderr=subprocess.STDOUT)
  69. if __name__ == '__main__':
  70. nb_server, base_url = start_notebook()
  71. try:
  72. resp = run_karma(base_url)
  73. except (subprocess.CalledProcessError, KeyboardInterrupt):
  74. resp = 1
  75. finally:
  76. nb_server.kill()
  77. shutil.rmtree(root_dir, True)
  78. sys.exit(resp)