a
    2bK                     @   s  d 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 ddlmZmZ ddlmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZ e e!e"Z#e$eZ%dddiidfe%d< dddiidfe%d< dddiidfe%d< e$eZ&de&d< G dd dej'Z(dd Z)dd Z*d d! Z+d"d# Z,d$d% Z-G d&d deZ.d'd( Z/d)d* Z0d+d, Z1e2d-krd.d/gZ3e3D ]$Z4e4ej5v rd0e._6ej57e4 qd1ej5v rdd2l8m9Z9 d3e9_:ej57d1 d4die9_;d0e9_<e9=  ne.=  dS )5z
This module is meant to run JupyterLab in a headless browser, making sure
the application launches and starts up without errors.
    N)ThreadPoolExecutor)path)aliasesflags)pathname2urlurljoin)IOLoop)StreamClosedError)WebSocketClosedError)Bool   )LabAppget_app_dir)TestEnv
BrowserAppZ	core_modeTzStart the app in core mode.z	core-modedev_modezStart the app in dev mode.zdev-modeZwatchzStart the app in watch mode.zBrowserApp.app_dirzapp-dirc                       s4   e Zd ZdZ fddZ fddZdd Z  ZS )LogErrorHandlerz.A handler that exits with 1 on a logged error.c                    s   t  jtjd d| _d S )N)levelF)super__init__loggingZERRORerroredself	__class__ 7lib/python3.9/site-packages/jupyterlab/browser_check.pyr   2   s    zLogErrorHandler.__init__c                    s8   t |dr,|jd ur,t|jd ttfr,d S t |S )Nexc_infor   )hasattrr   
isinstancer	   r
   r   filterr   recordr   r   r   r!   6   s    (zLogErrorHandler.filterc                 C   s   t |jtjd d| _d S )N)fileT)printmsgsysstderrr   r"   r   r   r   emit?   s    zLogErrorHandler.emit)__name__
__module____qualname____doc__r   r!   r)   __classcell__r   r   r   r   r   /   s   	r   c                 C   s   t  t| | dS )a
  Synchronous entry point to run a test function.
    func is a function that accepts an app url as a parameter and returns a result.
    func can be synchronous or asynchronous.  If it is synchronous, it will be run
    in a thread, so asynchronous is preferred.
    N)r   currentZspawn_callbackrun_test_async)appfuncr   r   r   run_testD   s    r3   c              
      s  t  }| j| t }|  | jd t| drHtdt| j	}n| j
}t|rb||}n4| jd t }t }||||}t|g}z|I dH  W n@ ty }	 z(| jd | jt|	 W Y d}	~	n
d}	~	0 0 | jd d}
|jrd	}
| jd
 n| jd | jd ztz | j  | j  |  W n: ty }	 z | jt|	 d	}
W Y d}	~	n
d}	~	0 0 W td t|
 ntd t|
 0 dS )zRun a test against the application.
    func is a function that accepts an app url as a parameter and returns a result.
    func can be synchronous or asynchronous.  If it is synchronous, it will be run
    in a thread, so asynchronous is preferred.
    zRunning async testbrowser_open_filezfile:z&Using thread pool executor to run testNz!Caught exception during the test:zTest Completer   r   zExiting with 1 due to errorszExiting normallyzStopping server...   )r   logZ
addHandlerr   startinfor   r   r   r4   Zdisplay_urlinspectZiscoroutinefunctionasyncioZget_event_loopr   Zrun_in_executorwait	ExceptionZcriticalerrorstrr   Zhttp_serverstopZio_looptimesleepos_exit)r1   r2   ZhandlerZ	env_patchurltestZloopexecutorZtaskeresultr   r   r   r0   M   sL    


&



r0   c                    sT   t j| i |I dH }| I dH \}}|jdkrLtt| d t|j ||fS )zRun an asynchronous commandNr   z exited with )r:   Zcreate_subprocess_execZcommunicate
returncodeRuntimeErrorr>   )cmdkwargsprocstdoutr(   r   r   r   run_async_process   s    
rO   c                    s   t t d}t t |dsft |s:tt | tg d|dI dH  tg d|dI dH  tt t	dt |d tdd| g|dI dH  dS )	2Run the browser test and return an exit code.
    browser_testnode_modulesjlpminitz-ycwdNrT   addzplaywright@^1.9.2browser-test.jsnode)
ospjoinr   existsrB   makedirsrO   shutilcopyhererD   targetr   r   r   run_browser   s    
re   c                 C   s~   t t d}t t |dsNt| tjg d|d tjg d|d t	t t
dt |d tjdd| g|dS )rP   rQ   rR   rS   rV   rX   rZ   r[   )r\   r]   r   r^   rB   r_   
subprocesscallr`   ra   rb   Z
check_callrc   r   r   r   run_browser_sync   s    
rh   c                       sT   e Zd ZdZe ZdZddiZdZdZe	Z
eZedZ fdd	Z fd
dZ  ZS )r   zAn app the launches JupyterLab and waits for it to start up, checking for
    JS console errors, JS errors, and Python logged errors.
    FZbase_urlz/foo/z
/lab?resetz	127.0.0.1Tc                    sH   | j dt  d| j d d< d| j d d< d| j d d< t   d S )NZpage_config_dataTZbrowserTestFZbuildAvailableZexposeAppInBrowser)Zsettings
setdefaultdictr   initialize_settingsr   r   r   r   rk      s
    zBrowserApp.initialize_settingsc                    sB   | j r
tndd }tjdkr(|tkr(t}t| j| t   d S )Nc                 S   s   dS )Nr   r   )rD   r   r   r   <lambda>       z0BrowserApp.initialize_handlers.<locals>.<lambda>nt)	test_browserre   rB   namerh   r3   	serverappr   initialize_handlers)r   r2   r   r   r   rr      s
    zBrowserApp.initialize_handlers)r*   r+   r,   r-   rp   open_browserZserverapp_configdefault_urlZip
test_flagsr   test_aliasesr   r   ro   rk   rr   r.   r   r   r   r   r      s   c                   C   s   t tdgS )N)moduler1   )r*   r   r   r   r   r    _jupyter_server_extension_points   s    rx   c                 C   s8   t  }| |_|  || j || j |  d S )N)r   rq   Zload_config_fileZupdate_configZconfigZparse_command_line
extra_argsZ
initialize)rq   	extensionr   r   r   load_jupyter_server_extension   s    r{   c                   C   s
   ddigS )Nrw   jupyterlab.browser_checkr   r   r   r   r   _jupyter_server_extension_paths   s    r}   __main__z--no-browser-testz--no-chrome-testFz
--notebook)NotebookAppz/labr|   )>r-   r:   r9   r   rB   r`   rf   r'   r@   concurrent.futuresr   r   r\   Zjupyter_server.serverappr   r   Zjupyter_server.utilsr   r   Ztornado.ioloopr   Ztornado.iostreamr	   Ztornado.websocketr
   Z	traitletsr   Zlabappr   r   Ztests.test_appr   abspathdirname__file__rb   rj   ru   rv   ZHandlerr   r3   r0   rO   re   rh   r   rx   r{   r}   r*   Zskip_optionsoptionargvro   removeZnotebook.notebookappr   rt   Znbserver_extensionsrs   Zlaunch_instancer   r   r   r   <module>   sn   


	;	


