forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.py
115 lines (83 loc) · 2.79 KB
/
integration_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import print_function, absolute_import
from __future__ import print_function, absolute_import
import atexit
import json
import os
from subprocess import Popen
import sys
import shutil
import tempfile
from tornado import gen
from tornado.ioloop import IOLoop
from notebook.notebookapp import NotebookApp
from traitlets import Bool, Unicode
HERE = os.path.dirname(__file__)
DEBUG = False
def create_notebook_dir():
"""Create a temporary directory with some file structure."""
root_dir = tempfile.mkdtemp(prefix='mock_contents')
os.mkdir(os.path.join(root_dir, 'src'))
with open(os.path.join(root_dir, 'src', 'temp.txt'), 'w') as fid:
fid.write('hello')
atexit.register(lambda: shutil.rmtree(root_dir, True))
return root_dir
def get_command(nbapp):
"""Get the command to run"""
terminalsAvailable = nbapp.web_app.settings['terminals_available']
# Compatibility with Notebook 4.2.
token = getattr(nbapp, 'token', '')
cmd = ['mocha', '--timeout', '200000',
'--retries', '2',
'build/integration.js',
'--jupyter-config-data=./build/config.json']
if DEBUG:
cmd = ['devtool', '../node_modules/.bin/_mocha', '-qc'] + cmd[1:]
config = dict(baseUrl=nbapp.connection_url,
terminalsAvailable=str(terminalsAvailable))
if token:
config['token'] = nbapp.token
with open('build/config.json', 'w') as fid:
json.dump(config, fid)
return cmd
@gen.coroutine
def run(cmd):
"""Run the cmd and exit with the return code"""
yield gen.moment # sync up with the ioloop
shell = os.name == 'nt'
proc = Popen(cmd, shell=shell)
print('\n\nRunning command: "%s"\n\n' % ' '.join(cmd))
# Poll the process once per second until finished.
while 1:
yield gen.sleep(1)
if proc.poll() is not None:
break
exit(proc.returncode)
@gen.coroutine
def exit(code):
"""Safely stop the app and then exit with the given code."""
yield gen.moment # sync up with the ioloop
IOLoop.current().stop()
sys.exit(code)
class TestApp(NotebookApp):
"""A notebook app that runs a mocha test."""
open_browser = Bool(False)
notebook_dir = Unicode(create_notebook_dir())
def main():
"""Run the unit test."""
app = TestApp()
if app.version == '4.3.0':
msg = ('Cannot run unit tests against Notebook 4.3.0. '
'Please upgrade to Notebook 4.3.1+')
print(msg)
sys.exit(1)
app.initialize([]) # reserve sys.argv for the command
cmd = get_command(app)
run(cmd)
try:
app.start()
except KeyboardInterrupt:
exit(1)
if __name__ == '__main__':
main()