
    @e|                        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mZ ddlmZmZ ddlm Z   ej!         ej"        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ej(                  Z)d Z*d Z+d Z,d Z-d  Z. G d! de          Z/d" Z0d# Z1e2d$k    rId%d&gZ3e3D ],Z4e4ej5        v r!d'e/_6        ej5        7                    e4           -e/8                                 dS dS )(z
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Unicode   )LabAppget_app_dir)TestEnv
BrowserApp	core_modeTzStart the app in core mode.z	core-modedev_modezStart the app in dev mode.zdev-modewatchzStart the app in watch mode.zBrowserApp.app_dirzapp-dirc                   2     e Zd ZdZ fdZ fdZd Z xZS )LogErrorHandlerz.A handler that exits with 1 on a logged error.c                 n    t                                          t          j                   d| _        d S )N)levelF)super__init__loggingERRORerroredself	__class__s    8lib/python3.11/site-packages/jupyterlab/browser_check.pyr   zLogErrorHandler.__init__*   s+    w}---    c                     t          |d          r0|j        )t          |j        d         t          t          f          rd S t                                          |          S )Nexc_infor   )hasattrr%   
isinstancer
   r   r   filter)r    recordr!   s     r"   r(   zLogErrorHandler.filter.   s\     FJ''	+6?1-0ACW/XYY , Fww~~f%%%r#   c                 T    t          |j        t          j                   d| _        d S )N)fileT)printmsgsysstderrr   )r    r)   s     r"   emitzLogErrorHandler.emit;   s$    fjsz****r#   )__name__
__module____qualname____doc__r   r(   r0   __classcell__r!   s   @r"   r   r   '   sf        88    & & & & &      r#   r   c                 `    t          j                                        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	   currentspawn_callbackrun_test_async)appfuncs     r"   run_testr=   @   s*     N##NC>>>>>r#   c                   K   t                      }| j                            |           t                      }|                                 | j                            d           t          | d          r#t          dt          | j	                            }n| j
        }t          j        |          r ||          }ng| j                            d           t          j                    }t                      }|                    |||          }t          j        |g          }	 | d{V  nX# t$          $ rK}	| j                            d           | j                            t+          |	                     Y d}	~	nd}	~	ww xY w| j                            d           d}
|j        rd	}
| j                            d
           n| j                            d           | j                            d           	 | j                                         | j                                         |                                 n@# t$          $ r3}	| j                            t+          |	                     d	}
Y d}	~	nd}	~	ww xY wt5          j        d           t9          j        |
           dS # t5          j        d           t9          j        |
           w xY w)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   log
addHandlerr   startinfor&   r   r   r?   display_urlinspectiscoroutinefunctionasyncioget_event_loopr   run_in_executorwait	Exceptioncriticalerrorstrr   http_serverstopio_looptimesleepos_exit)r;   r<   handler	env_patchurltestloopexecutortaskeresults              r"   r:   r:   I   s      GGw		IOOGLL%&&& s'(( g|C,ABBCCo "4(( $tCyy=>>>%''%''##HdC88|TF##







   <===c!ff GLL!!!F )78888'(((GLL%&&&	   c!ff 	
1
 	
1
sJ   )D2 2
F<AFF?AI J0 
J)I>9J0 >JJ0 0*Kc                    K   t          j        | i | d{V }|                                 d{V \  }}|j        dk    r4t	          t          |           dz   t          |j                  z             ||fS )zRun an asynchronous commandNr   z exited with )rH   create_subprocess_execcommunicate
returncodeRuntimeErrorrO   )cmdkwargsprocstdoutr/   s        r"   run_async_processri      s      /?????????D++--------NFF!3s88o5DO8L8LLMMM6>r#   c                 X  K   t          j        t                      d          }t          j        t          j        |d                    slt          j        |          s&t	          j        t          j        |                     t          g d|           d{V  t          g d|           d{V  t          g d|           d{V  t          j        t          j        t          d          t          j        |d                     t          d	d| g|           d{V  dS )
-Run the browser test and return an exit code.browser_testnode_modulesnpminitz-ycwdNro   installzplaywright@^1.9.2npx
playwrightrt   browser-test.jsnode)
ospjoinr   existsrU   makedirsri   shutilcopyhererY   targets     r"   run_browserr      sI     Xkmm^44F:chv~6677 Uz&!! 	*K(())) 5 5 56BBBBBBBBBB G G GVTTTTTTTTTT
<<<&
I
I
IIIIIIII
K01138FDU3V3VWWW
V%6<&
I
I
IIIIIIIIIIr#   c                    t          j        t                      d          }t          j        t          j        |d                    sDt	          j        |           t          j        g d|           t          j        g d|           t          j        g d|           t          j	        t          j        t          d          t          j        |d                     t          j        dd| g|          S )	rk   rl   rm   rn   rq   rs   ru   rx   ry   )rz   r{   r   r|   rU   r}   
subprocesscallr~   r   r   
check_callr   s     r"   run_browser_syncr      s    Xkmm^44F:chv~6677 M
F---6::::???VLLLLO444&AAAA
K01138FDU3V3VWWW &*;S!AvNNNNr#   c                   z     e Zd ZdZe ZdZddiZ eddd          Zd	Z	e
ZeZ ed          Z fd
Z fdZ x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.
    Fbase_urlz/foo/z
/lab?resetTz'The default URL to redirect to from `/`)confighelpz	127.0.0.1c                     | j                             di            d| j         d         d<   d| j         d         d<   d| j         d         d<   t                                                       d S )Npage_config_dataTbrowserTestFbuildAvailableexposeAppInBrowser)settings
setdefaultr   initialize_settingsr   s    r"   r   zBrowserApp.initialize_settings   sp      !3R888;?()-8>C()*:;BF()*>?##%%%%%r#   c                     | j         rt          nd }t          j        dk    r|t          k    rt          }t          | j        |           t                                                       d S )Nc                     dS )Nr    )rY   s    r"   <lambda>z0BrowserApp.initialize_handlers.<locals>.<lambda>   s     r#   nt)	test_browserr   rU   namer   r=   	serverappr   initialize_handlers)r    r<   r!   s     r"   r   zBrowserApp.initialize_handlers   s^    "/B{{]]7d??t{22#D&&&##%%%%%r#   )r1   r2   r3   r4   r   open_browserserverapp_configr   default_urlip
test_flagsr   test_aliasesr   r   r   r   r   r5   r6   s   @r"   r   r      s          DL"G,',t:cdddK	BEG4::L& & & & && & & & & & & & &r#   c                  "    t           t          dgS )N)moduler;   )r1   r   r   r#   r"    _jupyter_server_extension_pointsr      s    
3344r#   c                      ddigS )Nr   zjupyterlab.browser_checkr   r   r#   r"   _jupyter_server_extension_pathsr      s    1233r#   __main__z--no-browser-testz--no-chrome-testF)9r4   rH   rF   r   rU   r~   r   r.   rS   concurrent.futuresr   r   rz   jupyter_server.serverappr   r   jupyter_server.utilsr   r   tornado.ioloopr	   tornado.iostreamr
   tornado.websocketr   	traitletsr   r   labappr   r   tests.test_appr   abspathdirname__file__r   dictr   r   Handlerr   r=   r:   ri   r   r   r   r   r   r1   skip_optionsoptionargvr   removelaunch_instancer   r#   r"   <module>r      s       				      



  1 1 1 1 1 1       3 3 3 3 3 3 3 3 6 6 6 6 6 6 6 6 ! ! ! ! ! ! . . . . . . 2 2 2 2 2 2 # # # # # # # # ' ' ' ' ' ' ' ' # # # # # #s{;3;x(())T%[[
(;*=>@]^
; '*d);<>Z[
: $wo68VW
7 tG}}.Y     go   2? ? ?8 8 8v  
J 
J 
J	O 	O 	O& & & & & & & &<5 5 54 4 4 z');<L $ $SX&+J#HOOF###      r#   