run-test.py 2.4 KB

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