# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from __future__ import print_function, absolute_import

import atexit
import sys
import os
import shutil
import tempfile

from tornado.ioloop import IOLoop
from tornado.process import Subprocess
from notebook.notebookapp import NotebookApp
from traitlets import Bool, Unicode


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 run_command(cmd):
    """Run a task in a thread and exit with the return code."""
    shell = os.name == 'nt'
    p = Subprocess(cmd, shell=shell)
    print('\n\nRunning command: "%s"\n\n' % ' '.join(cmd))
    p.set_exit_callback(sys.exit)


def get_command(nbapp):
    """Get the command to run"""
    terminalsAvailable = nbapp.web_app.settings['terminals_available']
    cmd = ['mocha', '--timeout', '20000',
           '--retries', '2',
           'build/integration.js',
           '--baseUrl=%s' % nbapp.connection_url,
           '--terminalsAvailable=%s' % terminalsAvailable]
    if nbapp.token:
        cmd.append('--token=%s' % nbapp.token)
    return cmd


class TestApp(NotebookApp):
    """A notebook app that runs a mocha test."""

    open_browser = Bool(False)
    notebook_dir = Unicode(create_notebook_dir())

    def start(self):
        cmd = get_command(self)
        IOLoop.current().add_callback(run_command, cmd)
        super(TestApp, self).start()


if __name__ == '__main__':
    try:
        TestApp.launch_instance()
    except KeyboardInterrupt:
        sys.exit(1)

@@ -1117,16 +1117,95 @@
 lable'%5D%0A
+    # Compatibility with Notebook 4.2.%0A    token = getattr(nbapp, 'token', '')%0A
     cmd 
@@ -1238,16 +1238,16 @@
 20000',%0A
-
         
@@ -1465,22 +1465,16 @@
 n=%25s' %25 
-nbapp.
 token)%0A 
@@ -1649,16 +1649,16 @@
 dir())%0A%0A
-
     def 
@@ -1670,16 +1670,309 @@
 (self):%0A
+        # Cannot run against Notebook 4.3.0 due to auth incompatibilities.%0A        if self.version == '4.3.0':%0A            msg = ('Cannot run unit tests against Notebook 4.3.0.  '%0A                   'Please upgrade to Notebook 4.3.1+')%0A            self.log.error(msg)%0A            sys.exit(1)%0A%0A
         

Add compatibility with Notebook 4.2 and add an error message for Notebook 4.3.0