
2\c           @@ s:  d  Z  d d l m Z m Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m Z m Z m Z y d d l m Z Wn! e k
 r d d l m Z n Xd d l m Z d d l m Z m Z d d	 l m Z d d
 l  m! Z! y d d l m" Z" d   Z# Wn0 e k
 rd e$ f d     YZ" d   Z# n Xd Z% i  Z& e' e d   e& d <e' e d   e& d <e' e d   e& d <d e f d     YZ( d e) f d     YZ* d   Z+ d   Z, d e* f d     YZ- d   Z. e/ d  Z0 d   Z1 d   Z2 e j3 d  d!  Z4 e4 j5 d" d# d$ d% d& e4 j5 d' d( d) d% d* e4 j5 d+ d% d, e4 j5 d- d. d# d/ d0 d d1 d2 d3 e7 d% d4 e4 j5 d5 d( d) d% d6 e4 j5 d7 d1 d8 d% d9 d:   Z8 d;   Z9 e: d< k r6e9   n  d S(=   s   Notebook Javascript Test Controller

This module runs one or more subprocesses which will actually run the Javascript
test suite.
i    (   t   absolute_importt   print_functionN(   t   BytesIO(   t   Threadt   Lockt   Event(   t   patch(   t   jupyter_runtime_dir(   t   bytes_to_strt   which(   t   get_sys_info(   t   TemporaryDirectory(   t   TimeoutExpiredc         C@ s   |  j  |  S(   N(   t   wait(   t   pt   timeout(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt
   popen_wait'   s    R   c           B@ s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyR   *   s   c         C@ sc   xA t  t d |   D]) } |  j   d k	 r3 d St j d  q W|  j   d k r_ t  n  d S(   s$   backport of Popen.wait from Python 3i
   Ng?(   t   ranget   intt   pollt   Nonet   timet   sleepR   (   R   R   t   i(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyR   ,   s    i
   t   casperjst	   phantomjst   slimerjst   StreamCapturerc           B@ sM   e  Z e Z e Z e d   Z d   Z d   Z d   Z	 d   Z
 d   Z RS(   c         C@ se   t  t |   j   | |  _ g  |  _ t   |  _ t j   \ |  _	 |  _
 t   |  _ t   |  _ d  S(   N(   t   superR   t   __init__t   echot   streamsR   t   buffert   ost   pipet   readfdt   writefdR   t   buffer_lockR   t   stop(   t   selfR    (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyR   ?   s    		c         C@ s   t  |  _ xj |  j j   su t j |  j d  } |  j  |  j j	 |  Wd  QX|  j
 r t j j	 t |   q q Wt j |  j  t j |  j  d  S(   Ni   (   t   Truet   startedR(   t   is_setR#   t   readR%   R'   R"   t   writeR    t   syst   stdoutR   t   closeR&   (   R)   t   chunk(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   runH   s    	
	c         C@ s4   |  j  % |  j j d  |  j j d  Wd  QXd  S(   Ni    (   R'   R"   t   truncatet   seek(   R)   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   reset_bufferV   s    
c         C@ s!   |  j   |  j j   SWd  QXd  S(   N(   R'   R"   t   getvalue(   R)   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt
   get_buffer[   s    
c         C@ s   |  j  s |  j   n  d  S(   N(   R+   t   start(   R)   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   ensure_started_   s    	c         C@ s;   |  j  s d S|  j j   t j |  j d  |  j   d S(   s   Safely stop the thread.Ns    (   R+   R(   t   setR#   R.   R&   t   join(   R)   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   haltc   s
    	(   R   R   R*   t   daemont   FalseR+   R   R3   R6   R8   R:   R=   (    (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyR   <   s   					t   TestControllerc           B@ s}   e  Z d  Z d Z d Z d Z d Z d Z d Z	 d   Z
 d   Z e e d  Z d   Z d   Z d   Z d   Z e Z RS(	   s   Run tests in a subprocess
    c         C@ s   g  |  _  i  |  _ g  |  _ d  S(   N(   t   cmdt   envt   dirs(   R)   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyR   }   s    		c         C@ s   d S(   s   Create temporary directories etc.
        
        This is only called when we know the test group will be run. Things
        created here may be cleaned up by self.cleanup().
        N(    (   R)   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   setup   s    c         C@ s   t  j j   } | j |  j  | r. t } n  t d |  |  _ } | j   | r^ | j	 n d  } | rs t j n d  } t j |  j d | d | d | |  _ d  S(   NR    R0   t   stderrRB   (   R#   t   environt   copyt   updateRB   R*   R   t   stdout_capturerR9   R&   R   t
   subprocesst   STDOUTt   PopenRA   t   process(   R)   t   buffer_outputt   capture_outputRB   t   cR0   RE   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   launch   s    	
c         C@ s6   |  j  j   |  j j   |  j j   |  _ |  j  j S(   N(   RM   R   RI   R=   R8   R0   t
   returncode(   R)   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyR      s    c         C@ s   d S(   s^  Print extra information about this test run.
        
        If we're running in parallel and showing the concise view, this is only
        called if the test group fails. Otherwise, it's called before the test
        group is started.
        
        The base implementation does nothing, but it can be overridden by
        subclasses.
        N(    (   R)   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   print_extra_info   s    
c         C@ s   |  j  } | d k s' | j   d k	 r+ d Sy t d | j  | j   Wn n; Xx7 t d  D]) } | j   d k r t j d  qa Pqa W| j   d k r t d  n  d S(   s2   Cleanup on exit by killing any leftover processes.Ns   Cleaning up stale PID: %di
   g?s+   ... failed. Manual cleanup may be required.(	   RM   R   R   t   printt   pidt   killR   R   R   (   R)   t   subpR   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   cleanup_process   s    	c         C@ s,   |  j    x |  j D] } | j   q Wd S(   sD   Kill process if it's still alive, and clean up temporary directoriesN(   RX   RC   t   cleanup(   R)   t   td(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyRY      s    
N(   R   R   t   __doc__R   t   sectionRA   RB   RC   RM   R0   R   RD   R?   RQ   R   RS   RX   RY   t   __del__(    (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyR@   m   s   						c          C@ s1   d d  l  j }  t j j t j j |  j  d  S(   Ni    t    (   t   notebook.testst   testsR#   t   pathR<   t   dirnamet   __file__(   t   t(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   get_js_test_dir   s    c          C@ sN   d d  l  }  t   } |  j  | d  } g  | D] } t j j | |  ^ q/ S(   Ni    s   [!_]*/(   t   globRe   R#   Ra   t   relpath(   Rf   t   test_dirt   all_subdirst   x(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   all_js_groups   s    	t   JSControllerc           B@ s   e  Z d  Z d g Z e d d d  Z d   Z d   Z d   Z	 d   Z
 d   Z e d	    Z d
   Z d   Z d   Z d   Z d   Z RS(   s   Run CasperJS tests R   R   c         C@ s   t  j |   | |  _ | |  _ | |  _ | |  _ d |  _ t j d d t j	 |  _
 t   } d t j j | d  } t j j | |  j  } d d | | d |  j g |  _ d	 S(
   s   Create new test runner.s   /a@b/s   ^FAIL.*t   flagss   --includes=s   util.jsR   t   tests   --engine=%sN(   R@   R   t   engineR\   t   xunitt   urlt   base_urlt   ret   compilet	   MULTILINEt   slimer_failureRe   R#   Ra   R<   RA   (   R)   R\   Rp   Ro   Rq   t   js_test_dirt   includest
   test_cases(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyR      s    						c         C@ s  t    |  _ t    |  _ t    |  _ t    |  _ i |  j j d 6|  j j d 6|  j j d 6|  _ |  j j |  j  |  j j |  j  |  j j |  j  |  j j |  j  t	 j
 t	 j j |  j j t	 j j d d    t	 j
 t	 j j |  j j t	 j j d d    |  j r$|  j   n  |  j ry t j |  j  j d k } Wn t } n X| r||  j j d	 |  j  qt d
 |  j   nT d |  _ |  j   |  j r|  j j d |  j |  j f  n t j d d g |  _ d  S(   Nt   HOMEt   JUPYTER_CONFIG_DIRt
   IPYTHONDIRu
   sub ∂ir1u   sub ∂ir 1au
   sub ∂ir2u   sub ∂ir 1bi   s   --url=%ss   Could not reach "%s".i    s   --url=http://localhost:%i%ss   -cs   raise SystemExit(1)(   R   t   ipydirt
   config_dirt   nbdirt   homet   nameRB   RC   t   appendR#   t   makedirsRa   R<   Rp   t	   add_xunitRq   t   requestst   gett   status_codeR?   RA   t	   Exceptiont   server_portt   _init_serverRr   R/   t
   executable(   R)   t   alive(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyRD      s:    11		
	
	#c         C@ s=   t  j j |  j j d d  d  } |  j j d |  d  S(   Nt   /t   .s
   .xunit.xmls
   --xunit=%s(   R#   Ra   t   abspathR\   t   replaceRA   R   (   R)   t
   xunit_file(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyR     s    %c         C@ sL   |  j  d k r/ | r/ t t |   j d t  St t |   j d |  Sd  S(   NR   RO   RN   (   Ro   R   Rl   RQ   R*   (   R)   RN   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyRQ     s    c         O@ sg   t  t |   j | |   } |  j d k r_ t |  j  } | d k rI | St |  j j |   S| Sd S(   s#   Wait for the JSController to finishR   i    N(	   R   Rl   R   Ro   R   R0   t   boolRv   t   search(   R)   t   pargst   kwargst   retR0   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyR      s    c         C@ s   t  d |  j j  d  S(   Ns(   Running tests with notebook directory %r(   RT   R   R   (   R)   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyRS   .  s    c         C@ s'   t  d   |  j |  j g D  } | S(   Nc         s@ s   |  ] } t  | Vq d  S(   N(   t   have(   t   .0t   a(    (    s.   lib/python2.7/site-packages/notebook/jstest.pys	   <genexpr>3  s    (   t   allt   requirementsRo   (   R)   t
   should_run(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   will_run1  s    #c      
   C@ s%  t  j d d d d |  j j d d |  j g |  _ } t  j j d  rW | j d  n  t	   |  _
 } | j   t j j   } | j |  j  t j | d	 | j d
 t j d |  j j d | |  _ t j d i |  j j d 6  t   } Wd QXt j j | d |  j j  |  _ |  j   d S(   s/   Start the notebook server in a separate processs   -mt   notebooks   --no-browsers   --notebook-dirs   --NotebookApp.token=s   --NotebookApp.base_url=%st   linuxs   --KernelManager.transport=ipcR0   RE   t   cwdRB   s
   os.environRz   Ns   nbserver-%i.json(   R/   R   R   R   Rr   t   server_commandt   platformt
   startswithR   R   t   stream_capturerR9   R#   RF   RG   RH   RB   RJ   RL   R&   RK   t   serverR   t   dictR   R   Ra   R<   RU   t   server_info_filet   _wait_for_server(   R)   t   commandRP   RB   t   runtime_dir(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyR   6  s,    
		 c         C@ s   x~ t  d  D]p } |  j j   d k	 r2 |  j   St j j |  j  rp y |  j	   Wn t
 k
 rh qp Xd Sn  t j d  q Wt d |  j d t j d S(   s0   Wait 30 seconds for the notebook server to starti,  Ng?s+   Notebook server-info file never arrived: %st   file(   R   R   R   R   t   _failed_to_startR#   Ra   t   existsR   t   _load_server_infot
   ValueErrorR   R   RT   R/   RE   (   R)   R   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyR   T  s    
c         C@ sR   |  j  j   j d d  } t d d t j t |  j  t | d t j d S(   s"   Notebook server exited prematurelys   utf-8R   s   Notebook failed to start: R   N(   R   R8   t   decodeRT   R/   RE   R   (   R)   t   captured(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyR   g  s    c         C@ s8   t  |  j   } t j |  } Wd QX| d |  _ d S(   s7   Notebook server started, load connection info from JSONNt   port(   t   openR   t   jsont   loadR   (   R)   t   ft   info(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyR   n  s    c         C@ s   t  |  d  r y |  j j   Wn t k
 r3 n Xy t |  j t  WnI t k
 r y$ t d d t j	 |  j j
   Wq t k
 r q Xn Xy t |  j t  Wn+ t k
 r t d |  j d t j	 n X|  j j   n  t j |   d  S(   NR   s0   Failed to terminate notebook server, killing it.R   s"   Notebook server still running (%s)(   t   hasattrR   t	   terminatet   OSErrorR   t   NOTEBOOK_SHUTDOWN_TIMEOUTR   RT   R/   RE   RV   R   R   R=   R@   RY   (   R)   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyRY   t  s*    	
N(   R   R   R[   R   R*   R   R   RD   R   RQ   R   RS   t   propertyR   R   R   R   R   RY   (    (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyRl      s   		)									c   	   
   C@ s   |  j  } | s t   } n  |  j r* d n d } g  | D]* } t | d |  j d | d |  j ^ q7 } | } g  | D] } | j rt | ^ qt } g  | D] } | j s | ^ q } | | f S(   sV   Returns two lists of TestController instances, those to run, and those
    not to run.R   R   Rp   Ro   Rq   (   t
   testgroupsRk   R   Rl   Rp   Rq   R   (	   t   optionsR   Ro   R   t   c_jst   controllersRP   t   to_runt   not_run(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   prepare_controllers  s    	7""c         C@ s   z y| y1 |  j    | s& |  j   n  |  j d |  Wn. t k
 rg d d l } | j   |  d f SX|  j   } |  | f SWn t k
 r |  t j	 f SXWd |  j
   Xd S(   s  Setup and run a test controller.
    
    If buffer_output is True, no output is displayed, to avoid it appearing
    interleaved. In this case, the caller is responsible for displaying test
    output on failure.
    
    Returns
    -------
    controller : TestController
      The same controller as passed in, as a convenience for using map() type
      APIs.
    exitcode : int
      The exit code of the test subprocess. Non-zero indicates failure.
    RN   i    Ni   (   RD   RS   RQ   R   t	   tracebackt	   print_excR   t   KeyboardInterruptt   signalt   SIGINTRY   (   t
   controllerRN   R   t   exitcode(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   do_run  s      

c    	      @ sy  t    }  g      f d   } | d |  d j d d   | d |  d  | d |  d	  t d
     D  } g    D]$ \ } } d j | | d | ^ qz   g  } g  } x= t j   D]/ \ } } | r | j |  q | j |  q W| r.  j d  | j     j d d j |  d  n  | rl  j d  | j     j d d j |  d  n  d j    S(   s@   Return a string with a summary report of test-related variables.c         @ s     j  |  | f  d  S(   N(   R   (   R   t   value(   t   out(    s.   lib/python2.7/site-packages/notebook/jstest.pyt   _add  s    s   Python versiont   sys_versions   
R^   s   sys.executablet   sys_executablet   PlatformR   c         s@ s!   |  ] \ } } t  |  Vq d  S(   N(   t   len(   R   t   nt   v(    (    s.   lib/python2.7/site-packages/notebook/jstest.pys	   <genexpr>  s    s   {:<{width}}: {}
t   widths-   
Tools and libraries available at test time:
s      t    s1   
Tools and libraries NOT available at test time:
(	   R
   R   t   maxt   formatR   t   itemsR   t   sortR<   (	   t   infR   R   R   R   t   availt	   not_availt   kt   is_avail(    (   R   s.   lib/python2.7/site-packages/notebook/jstest.pyt   report  s.    	1
!
!c         C@ s  t  |   \ } } d d d  } g  } t j   } t   |  j d k r xU| D]v } t d | j  t j j   t | d t	 \ } } | r | j
 |  | t j k r t d  Pq n  t   qP Wn y t j j |  j  } x | j t |  D] \ } } | d k rd	 n d
 }	 t | d | j |	   | r | j   t t | j   | j
 |  | t j k rt d  Pqq q WWn t k
 rd SXx( | D]  } t | d | j d   qWt j   }
 |
 | } t |  } t |  } t d d  t d  t t    d | } t d d d | sSt d | |  nf g  | D] } | j ^ qZ} t d j | | d j |   |  t   t d  t d |  t   | rt j d  n  d S(   s  Run the entire Javascript test suite.
    
    This function constructs TestControllers and runs them in subprocesses.

    Parameters
    ----------

    All parameters are passed as attributes of the options object.

    testgroups : list of str
      Run only these sections of the test suite. If empty, run all the available
      sections.

    fast : int or None
      Run the test suite in parallel, using n simultaneous processes. If None
      is passed, one process is used per CPU core. Default 1 (i.e. sequential)

    inc_slow : bool
      Include slow tests. By default, these tests aren't run.

    slimerjs : bool
      Use slimerjs if it's installed instead of phantomjs for casperjs tests.

    url : unicode
      Address:port to use when running the JS tests.

    xunit : bool
      Produce Xunit XML output. This is written to multiple foo.xunit.xml files.

    extra_args : list
      Extra arguments to pass to the test subprocesses, e.g. '-v'
    iF   t   -c         S@ s2   |  d 7}  d | j  | t |   |  } |  | S(   NR   (   t   rjustR   (   t   ltextt   rtextR   t   fill(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   justify  s    
 i   s   Test group:RN   t   Interruptedi    t   OKt   FAILEDs   Test group: Ns   NOT RUNt   _s?   Test suite completed for system with the following information:s   Took %.3fs.s   Status: t   endR^   s   OK (%d test groups).s-   ERROR - {} out of {} test groups failed ({}).s   , s"   You may wish to rerun these, with:s     python -m notebook.jstest(   R   R   RT   t   fastR\   R/   R0   t   flushR   R?   R   R   R   t   multiprocessingt   poolt
   ThreadPoolt   imap_unorderedRS   R   R   R   R   R   R<   t   exit(   R   R   R   R   t   failedt   t_startR   t   resR   t
   res_stringt   t_endt   t_testst   nrunnerst   nfailt   tookRP   t   failed_sections(    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   run_jstestall  sh    !






t   descriptions%   Run Jupyter Notebook Javascript testsR   t   nargst   *t   helps9   Run specified groups of tests. If omitted, run all tests.s
   --slimerjst   actiont
   store_truesG   Use slimerjs if it's installed instead of phantomjs for casperjs tests.s   --urls   URL to use for the JS tests.s   -js   --fastt   ?t   constt   defaulti   t   typesl   Run test sections in parallel. This starts as many processes as you have cores, or you can specify a number.s   --xunits   Produce Xunit XML resultss   --subproc-streamst   capturesk   What to do with stdout/stderr from subprocesses. 'capture' (default), 'show' and 'discard' are the options.c          C@ s   t  j g   }  g  |  _ |  S(   sg   Get an argparse Namespace object with the default arguments, to pass to
    :func:`run_iptestall`.
    (   t	   argparsert
   parse_argst
   extra_args(   R   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   default_optionsa  s    	c          C@ s   y t  j j d  }  Wn$ t k
 r< t  j d } g  } n" Xt  j d |  !} t  j |  d } t j |  } | | _ t |  d  S(   Ns   --i   (   R/   t   argvt   indexR   R	  R
  R  R   (   t   ixt   to_parseR  R   (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   maini  s    
	t   __main__(;   R[   t
   __future__R    R   t   argparseR   t   multiprocessing.poolR   R#   Rs   R   R   R/   RJ   R   t   ioR   t	   threadingR   R   R   t   unittest.mockR   t   ImportErrort   mockt   jupyter_core.pathsR   t   ipython_genutils.py3compatR   R	   t   notebook._sysinfoR
   t   ipython_genutils.tempdirR   R   R   R   R   R   R   R   t   objectR@   Re   Rk   Rl   R   R*   R   R   R   t   ArgumentParserR	  t   add_argumentR   R   R  R  R   (    (    (    s.   lib/python2.7/site-packages/notebook/jstest.pyt   <module>   sr   	1^			"	#	i'		