o
    i                    @   s  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mZ d dlmZ d dlmZ d dlmZmZ d dlZd dlZd dl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" d dl#m$Z$ ddl%m&Z&m'Z' ddl(m)Z) ddl*m+Z+ e,dZ-ej./e0Z1e,dZ2e,dZ3dej4d< g dZ5drddZ6ddiddddZ7ddid d!id"d#d"id$Z8G d%d& d&eZ9d'ed(e j:fd)d*Z;d+d, Z<dsd-d.Z=d/d0 Z>dsd1d2Z?dd3d4d5Z@d6d7 ZAd8d9 ZBd:d; ZCd<d= ZDejEFd>d?gd@eGdAdBfdCeGdAdBfdDeGdAdBfdEeGdAdBfdFeGdAdBfdGeGdAdBfdHeGdAddIdIdJfdKeGdAdBfdLeGdAdBfdMeGdAdIdNfdOeGdAdBfdPeGdAdBfdQeGdAdBfdReGdAdBfdSeGdAdBfdTeGdAdBfgdUdV ZHdWdX ZIejEjJejKdYkdZd[d\d] ZLd^d_ ZMd`da ZNdbdc ZOddde ZPdfdg ZQdhdi ZRejEjJeSed  djk dkd[dldm ZTG dndo doe+ZUG dpdq dqe+ZVdS )t    N)	b64decode	b64encodeEmpty)Any)	MagicMockMock)KernelClientKernelManager)version_info)KernelSpecManager)
strip_ansi)NotebookNode)modified_env)
TraitError   )NotebookClientexecute)CellExecutionError   )NBClientTestsBasez0x[0-9a-f]{7,9}zM(<ipython-input-\d+-[0-9a-f]+>|<IPY-INPUT>) in (<module>|<cell line: \d>\(\))zU((Cell|Input) In\s?\[\d+\]|<IPY-INPUT>), (in )?(line \d|<module>|<cell line: \d>\(\))1PYDEVD_DISABLE_FILE_VALIDATION)on_cell_starton_cell_executeon_cell_completeon_cell_executedon_cell_erroron_notebook_starton_notebook_completeon_notebook_errorFc                 C   sh   |r
dd t D }ndd t D }| d ur!|d urtdt| }| D ]
\}}t||| q%||fS )Nc                 S      i | ]}|t  qS  	AsyncMock.0keyr"   r"   U/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/nbclient/tests/test_client.py
<dictcomp>;       z+get_executor_with_hooks.<locals>.<dictcomp>c                 S   r!   r"   )r   r%   r"   r"   r(   r)   =   r*   z,Cannot pass nb and executor at the same time)hook_methodsRuntimeErrorr   itemssetattr)nbexecutorasync_hookshookskvr"   r"   r(   get_executor_with_hooks9   s   r5   msg_idfake_idokstatusexecution_countparent_headercontentr:   errorexecute_replymsg_type)r=   r>   rA   headerc                   @   s   e Zd ZdS )r$   N)__name__
__module____qualname__r"   r"   r"   r(   r$   S   s    r$   objreturnc                 C   sL   zt  }W n ty   t  }t | Y nw t j|d}||  |S )N)loop)asyncioget_running_loopr,   new_event_loopset_event_loopFuture
set_result)rF   rH   futurer"   r"   r(   make_futureW   s   
rP   c              	   C   s6   zt t| ddW S  ttfy   |  Y S w )Nascii)r   r   encodedecode
ValueError	TypeError)b64_textr"   r"   r(   normalize_base64b   s
   rW   c           	   	   C   s  t | }t|d}W d   n1 sw   Y  t|}|jD ]}d|v r+|d= g |d< q"|r8d|i|}t|fi |}t 7 tj	dt
d tdd	d
 | }W d   n1 sbw   Y  W d   ||fS W d   ||fS 1 s~w   Y  ||fS )uLoads and runs a notebook, returning both the version prior to
    running it and the version after running it.

       Nr;   outputs	resourcesignoreactioncategory8024COLUMNSLINES)opennbformatreadcopydeepcopycellsr   warningscatch_warningssimplefilterFutureWarningr   r   	filenameoptsr[   finput_nbcleaned_input_nbcellr0   	output_nbr"   r"   r(   run_notebookk   s.   







rw   c                 C   s   t |  S N)rw   )argsr"   r"   r(   run_notebook_wrapper   s   rz   c           	         s   t | }t|d}W d   n1 sw   Y  t|}|jD ]}d|v r,|d= g |d< q#|r9d|i|}t|fi |}tddd | I dH }W d   ||fS 1 s]w   Y  ||fS )	rX   rY   Nr;   rZ   r[   r`   ra   rb   )	re   rf   rg   rh   ri   rj   r   r   async_executero   r"   r"   r(   async_run_notebook   s$   




r|   	reply_msgc                    sP   dt |dddid fddfdd	  fd
d}|S )aE  
    This function prepares a executor object which has a fake kernel client
    to mock the messages sent over zeromq. The mock kernel client will return
    the messages passed into this wrapper back from ``preproc.kc.iopub_channel.get_msg``
    callbacks. It also appends a kernel idle message to the end of messages.
    r7   r:   execution_stateidle)rA   r>   c                      s*   t ttd iddddpi dS )Nr6   r8   r   r9   r<   return_value)r$   rP   r   merge_dictsr"   )	parent_idr~   r"   r(   shell_channel_message_mock   s   z6prepare_cell_mocks.<locals>.shell_channel_message_mockc                      s   t fdd D dS )Nc                    s$   g | ]}t td d ii|qS )r=   r6   )rP   r   r   )r&   msg)r   r"   r(   
<listcomp>   s    zCprepare_cell_mocks.<locals>.iopub_messages_mock.<locals>.<listcomp>)side_effectr#   r"   )messagesr   r"   r(   iopub_messages_mock   s
   
z/prepare_cell_mocks.<locals>.iopub_messages_mockc                    s    t   fdd}|S )Nc                    s   t di dg d}G dd dt}tj }||}|g|j_ }tt|dt dtdttddd	|_	|_
 | |||S )
aV  
            This inner function wrapper populates the executor object with
            the fake kernel client. This client has its iopub and shell
            channels mocked so as to fake the setup handshake and return
            the messages passed into prepare_cell_mocks as the execute_cell loop
            processes them.
            "foo" = "bar"codesourcemetadata	cell_typerZ   c                   @   s   e Zd ZU eed< dS )zkprepare_cell_mocks.<locals>.prepared_wrapper.<locals>.test_mock_wrapper.<locals>.NotebookClientWithParentIDr   N)rC   rD   rE   str__annotations__r"   r"   r"   r(   NotebookClientWithParentID   s   
 r   )get_msgr   T)iopub_channelshell_channelr   is_alive)r   r   rf   v4new_notebookr/   rj   r   rP   kcr   )self	cell_mockr   r/   r0   message_mock)funcr   r   r   r"   r(   test_mock_wrapper   s    	


zGprepare_cell_mocks.<locals>.prepared_wrapper.<locals>.test_mock_wrapper)	functoolswraps)r   r   )r   r   r   )r   r(   prepared_wrapper   s   z,prepare_cell_mocks.<locals>.prepared_wrapper)listappend)r~   messages_inputr   r"   )r   r   r   r~   r   r(   prepare_cell_mocks   s   "r   c                 C   s4  t | } d| v r| d= d| v rttd| d | d< d| di v r1ttd| d d | d d< d| di v rAd| d d d< d	| di v rVt| d d	 | d d	< | di  D ]\}}t|t	rot
|| d |< q^d
| v rg }| d
 D ]}ttdt|}ttdt|}|| qz|| d
< | S )z,
    Normalizes outputs for comparison.
    r   textz	<HEXADDR>z
text/plaindata(application/vnd.jupyter.widget-view+jsonz
<MODEL_ID>model_idzimage/svg+xml	tracebackz<IPY-INPUT>)dictresubaddr_patget	xmltodictparser-   
isinstancer   rW   ipython_input_patr   ipython8_input_patr   )outputr'   valuetbliner"   r"   r(   normalize_output   s.   
r   c                 C   s   | d }|d }t |t |ksJ t||D ]6\}}|dg }|dg }ttt|}ttt|}	||	ks;J |dd }
|dd }|
|ksMJ qd S )Nrj   rZ   r;   )lenzipr   r   mapr   )expectedactualexpected_cellsactual_cellsexpected_cellactual_cellexpected_outputsactual_outputsnormalized_expected_outputsnormalized_actual_outputsexpected_execution_countactual_execution_countr"   r"   r(   assert_notebooks_equal  s   r   c                   C   s   ddt jtdiiS )zk
    Prepare a notebook resources dictionary for executing test
    notebooks in the ``files`` folder.
    r   pathfiles)osr   joincurrent_dirr"   r"   r"   r(   notebook_resources%  s   r   c                    s(   dg  fdd|   D }tj|S )NzO[IPKernelApp] WARNING | debugpy_stream undefined, debugging will not be enabledc                    s   g | ]}| vr|qS r"   r"   )r&   r   allowed_linesr"   r(   r   2      z3filter_messages_on_error_output.<locals>.<listcomp>)
splitlinesr   linesepr   )
err_outputfiltered_resultr"   r   r(   filter_messages_on_error_output-  s   r   
input_namerq   zOther Comms.ipynbpythonkernel_namezClear Output.ipynbzEmpty Cell.ipynbzFactorials.ipynbHelloWorld.ipynbzInline Image.ipynbInterrupt.ipynbT)r   timeoutinterrupt_on_timeoutallow_errorsJupyterWidgets.ipynb$Skip Exceptions with Cell Tags.ipynbSkip Exceptions.ipynb)r   r   z"Skip Execution with Cell Tag.ipynbz	SVG.ipynbzUnicode.ipynbUnicodePy3.ipynbzupdate-display-id.ipynbzCheck History in Memory.ipynbc                 C   s0   t jtd| }t||t \}}t|| dS )zHRuns a series of test notebooks and compares them to their actual outputr   N)r   r   r   r   rw   r   r   )r   rq   
input_filers   rv   r"   r"   r(   test_run_all_notebooks7  s   r   c                    s   t ddd}tjtd| t tdt|i'  fdddD }|D ]}|  q(|D ]}|jd	d
 q1W d   n1 sDw   Y  | 	 }t
|jdksVJ dS )Two notebooks should be able to be run simultaneously without problems.

    The two notebooks spawned here use the filesystem to check that the other notebook
    wrote to the filesystem.r   r   Parallel Execute {label}.ipynbr   NBEXECUTE_TEST_PARALLEL_TMPDIRc                    s(   g | ]}t jt j|d fdqS )label)targetry   )	threadingThreadrw   formatr&   r   r   rq   resr"   r(   r   b  s    z+test_parallel_notebooks.<locals>.<listcomp>ABr   r   N )r   r   r   r   r   r   r   r   start
readouterrr   err)capfdtmpdirr   threadstcapturedr"   r   r(   test_parallel_notebooksV  s    


r  ntz!warns about event loop on Windowsreasonc              	      s   t dddd}tjtd| t  tjtdd d< t = tj	dt
d	 t  tjjd
d}|t fddtdD  W d   n1 sUw   Y  W d   n1 sdw   Y  |  }t|jdksvJ dS )Ensure that when many IPython kernels are run in parallel, nothing awful happens.

    Specifically, many IPython kernels when run simultaneously would encounter errors
    due to using the same SQLite history database.
    r      r   r   r   r   r   r   r\   r]   r   )max_workersc                    s   g | ]} fqS r"   r"   r&   ir   r"   r(   r     s    z0test_many_parallel_notebooks.<locals>.<listcomp>   Nr   )r   r   r   r   r   r   build_resourcesrk   rl   rm   rn   rw   
concurrentfuturesProcessPoolExecutorr   rz   ranger   r   r   )r   r   r0   r   r"   r   r(   test_many_parallel_notebookso  s   

$
r  c                    s   t ddd}tjtd| t tdt|i  fdd}t	|  W d   n1 s3w   Y  | 
 }t|jd	ksEJ dS )
r   r   r   r   r   r   c                     s,    fdddD } t j|  I d H  d S )Nc                    s    g | ]}t  j|d qS )r   )r|   r   r   r   r"   r(   r     s    zDtest_async_parallel_notebooks.<locals>.run_tasks.<locals>.<listcomp>r   )rI   gathertasksr   r"   r(   	run_tasks  s
   z0test_async_parallel_notebooks.<locals>.run_tasksNr   )r   r   r   r   r   r   r   r   rI   runr   r   r   )r   r   r   r  r   r"   r   r(   test_async_parallel_notebooks  s   
r  c                    s   t dddd}tjtd| t  tjtdd d< t   fdd	}t	|  | 
 }t|jd
ksAJ dS )r  r   r  r  r   r   r   r   c                     s0    fddt dD } tj|  I d H  d S )Nc                    s   g | ]}t  qS r"   )r|   r	  r   r"   r(   r     r   zItest_many_async_parallel_notebooks.<locals>.run_tasks.<locals>.<listcomp>rY   )r  rI   r  r  r   r"   r(   r    s   z5test_many_async_parallel_notebooks.<locals>.run_tasksr   N)r   r   r   r   r   r   r  rw   rI   r  r   r   r   )r   r   r  r   r"   r   r(   "test_many_async_parallel_notebooks  s   
r  c                  C   s  t dd} d}tjtd|}t }t|| |\}}dd }|d d d	 d
 }||d }||d }	||d }
||d }||d d d d d }||d d d d d }tjdd}|| |k sjJ |	| |k srJ |
| |k szJ || |k sJ dS )zCompare the execution timing information stored in the cell with the
    actual time it took to run the cell. Also check for the cell timing string
    format.r   r   zSleep1s.ipynbr   c                 S   s   d}t j | |S )Nz%Y-%m-%dT%H:%M:%S.%fZ)datetimestrptime)stime_formatr"   r"   r(   get_time_from_str  s   z0test_execution_timing.<locals>.get_time_from_strrj   r   r   	executionziopub.status.busyziopub.execute_inputzshell.execute_replyziopub.status.idler   rZ   r   r      d   )millisecondsN)	r   r   r   r   r   r   rw   r  	timedelta)rq   r   r   r   rs   rv   r  execution_timingstatus_busyexecute_inputr@   status_idle
cell_startcell_enddeltar"   r"   r(   test_execution_timing  s$   
r*  c                  C   sZ   t j } t| }|  |jd usJ W d    n1 sw   Y  |jd u s+J d S rx   )rf   r   r   r   setup_kernelr   )r/   r0   r"   r"   r(   test_synchronous_setup_kernel  s   

r,  c                  C   sT   t j } t }t| |d}|  | }|d usJ |  |  |	  d S )Nkm)
rf   r   r   r
   r   start_new_kernelstart_new_kernel_clientshutdowncleanup_resourcesstop_channelsr/   r.  r0   r   r"   r"   r(   &test_startnewkernel_with_kernelmanager  s   
r5  c                  C   s~   t j } t }t| |d}| }|jg ksJ |  |jdgks%J |  |jdgks1J |  |	  |
  d S )Nr-  z#--HistoryManager.hist_file=:memory:)rf   r   r   r
   r   clientextra_argumentsr/  r1  r2  r3  r4  r"   r"   r(   *test_start_new_kernel_history_file_setting  s   
r8     zrequires client 7+c                  C   s   G dd dt } tj }t }| |_t||d}|  |js"J |j	d us)J t
t}|  W d    n1 s=w   Y  t|jjd dksNJ |jd u sUJ |j	d u s\J |jraJ d S )Nc                   @   sD   e Zd Z					ddedededededdfd	d
ZdddZdS )zLtest_start_new_kernel_client_cleans_up_kernel_on_failure.<locals>.FakeClientTshelliopubstdinhbcontrolrG   Nc                 S   s   t d)N	Any error)	Exception)r   r:  r;  r<  r=  r>  r"   r"   r(   start_channels  s   z[test_start_new_kernel_client_cleans_up_kernel_on_failure.<locals>.FakeClient.start_channelsc                 S   s   d S rx   r"   r   r"   r"   r(   r3       zZtest_start_new_kernel_client_cleans_up_kernel_on_failure.<locals>.FakeClient.stop_channels)TTTTT)rG   N)rC   rD   rE   boolrA  r3  r"   r"   r"   r(   
FakeClient  s(    

rE  r-  r   r?  )r	   rf   r   r   r
   client_factoryr   r/  
has_kernelr.  pytestraisesr@  r0  r   r   ry   r   )rE  r/   r.  r0   r   r"   r"   r(   8test_start_new_kernel_client_cleans_up_kernel_on_failure
  s   


rJ  c                   @   s   e Zd ZdZdZdd Zdd Zdd Zej	j
d	e  vd
ddd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Z dS )4TestExecutez&Contains test functions for execute.pyNc                 C   s   t tj  d S rx   )r   rf   r   r   rB  r"   r"   r(   test_constructor0  s   zTestExecute.test_constructorc                 C   s0   t j }t|dd}| }d|jv sJ d S )Nr   r   language_info)rf   r   r   r   r   r   )r   r/   r0   r"   r"   r(   test_populate_language_info3  s   
z'TestExecute.test_populate_language_infoc                 C   sB   t jtdd}|  }d|d d< t|i |\}}t|| dS )z1Can the kernel be started when the path is empty?r   r   r   r   r   N)r   r   r   r   r  rw   r   r   rp   r   rs   rv   r"   r"   r(   test_empty_path9  s
   zTestExecute.test_empty_pathpython3zrequires a python3 kernelspecr  c                 C   sz   t jtdd}|  }t|ddi|\}}t|| tt	 t|ddi|\}}W d   dS 1 s6w   Y  dS )zCan kernel in nb metadata be found when an empty string is passed?

        Note: this pattern should be discouraged in practice.
        Passing in no kernel_name to NotebookClient is recommended instead.
        r   r   r   r   N)
r   r   r   r   r  rw   r   rH  rI  r   rO  r"   r"   r(   test_empty_kernel_nameA  s   

"z"TestExecute.test_empty_kernel_namec                 C   s   t jtdd}|  }t j||d d< t|tdd|\}}| t	|d d | t	|d d	 d
 d |d d	 d
 d	 }| |d d | |d d | |d d dS )zTest disabling standard inputr   zDisable Stdin.ipynbr   r   Tr   rj   r   r   rZ   output_typer?   enameStdinNotImplementedErrorevaluezHraw_input was called, but this frontend does not support input requests.N)
r   r   r   r   r  dirnamerw   r   assertEqualr   )r   rp   r   rs   rv   r   r"   r"   r(   test_disable_stdinR  s   zTestExecute.test_disable_stdinc                 C   s   t jtdd}|  }t j||d d< tt}t	|t
dd| W d   n1 s/w   Y  | t|jjd d	 dS )
:Check that an error is raised when a computation times outr   r   r   r   r   r   Nr   zA cell timed out while it was being executed, after 1 seconds.
The message was: Cell execution timed out.
Here is a preview of the cell contents:
-------------------
while True: continue
-------------------
)r   r   r   r   r  rX  rH  rI  TimeoutErrorrw   r   rY  r   r   ry   )r   rp   r   r   r"   r"   r(   test_timeoutg  s   zTestExecute.test_timeoutc                 C   sv   t jtdd}|  }t j||d d< dd }tt t	|t
|d| W d   dS 1 s4w   Y  dS )	r[  r   r   r   r   c                 S   s   dS )N
   r"   )r   r"   r"   r(   timeout_func  rC  z3TestExecute.test_timeout_func.<locals>.timeout_func)r_  N)r   r   r   r   r  rX  rH  rI  r\  rw   r   )r   rp   r   r_  r"   r"   r(   test_timeout_funcz  s   "zTestExecute.test_timeout_funcc                 C   s   t j }t|dtd}| }d|jv sJ | ( |jd us#J |	|j
 }|d us1J d|d d v s;J W d    d S 1 sFw   Y  d S )Nr   )r   kernel_manager_classrM  namer>   )rf   r   r   r   r
   r   r   r+  r   wait_for_replykernel_info)r   r/   r0   info_msgr"   r"   r(   test_sync_kernel_manager  s   

"z$TestExecute.test_sync_kernel_managerc           	      C   s   t jtdd}t|}t|d}W d   n1 sw   Y  |  }t j||d d< t	|dd}t
t |  W d   n1 sJw   Y  | }d	d
 }||_t
ttf | \}}W d   dS 1 ssw   Y  dS )zVCheck that an error is raised when the kernel is_alive is false after a cell timed outr   r   rY   Nr   r   r   r   c                      s   dS )NFr"   r"   r"   r"   r(   r     s   z=TestExecute.test_kernel_death_after_timeout.<locals>.is_alive)r   r   r   r   re   rf   rg   r  rX  r   rH  rI  r\  r   create_kernel_managerr   r,   )	r   rp   rr   rs   r   r0   r.  r   rv   r"   r"   r(   test_kernel_death_after_timeout  s    

"z+TestExecute.test_kernel_death_after_timeoutc                 C   s   t jtdd}t|}t|d}W d   n1 sw   Y  t|}t	t
 |  W d   dS 1 s;w   Y  dS )ziCheck that an error is raised when the kernel is_alive is false during a cell
        execution.
        r   Autokill.ipynbrY   N)r   r   r   r   re   rf   rg   r   rH  rI  r,   r   r   rp   rr   rs   r0   r"   r"   r(   "test_kernel_death_during_execution  s   

"z.TestExecute.test_kernel_death_during_executionc                 C      t jtdd}|  }t j||d d< tt$}t	|t
dd| | t|jt dt|jv s7J W d   dS 1 sBw   Y  dS )	zK
        Check that conversion halts if ``allow_errors`` is False.
        r   r   r   r   FrS     # üñîçø∂éNr   r   r   r   r  rX  rH  rI  r   rw   r   assertIsInstancer   r   r   rp   r   excr"   r"   r(   test_allow_errors  s   "zTestExecute.test_allow_errorsc                 C   rl  )	z~
        Check that conversion halts if the ``force_raise_errors`` traitlet on
        NotebookClient is set to True.
        r   r   r   r   T)force_raise_errorsrm  Nrn  rp  r"   r"   r(   test_force_raise_errors  s   "z#TestExecute.test_force_raise_errorsc                 C   s   t jtdd}t|}t|d}W d    n1 sw   Y  t||  d}|j	dd |j
}|d us9J |j	dd ||j
ksFJ |j	ddd ||j
ksTJ d S )	Nr   r   rY   r[   F
cleanup_kcT)reset_kcrw  r   r   r   r   re   rf   rg   r   r  r   r   )r   rp   rr   rs   r0   r   r"   r"   r(   test_reset_kernel_client  s   
z$TestExecute.test_reset_kernel_clientc                 C   s   t jtdd}t|}t|d}W d    n1 sw   Y  t||  d}|	  |j
d u s5J |j	dd |j
d usBJ d S )Nr   r   rY   ru  Frv  ry  rj  r"   r"   r(   test_cleanup_kernel_client  s   
z&TestExecute.test_cleanup_kernel_clientc                 C   s   ddl m} tjtdd}t|}t|d}W d    n1 s#w   Y  t	
|}|jD ]}d|v r9|d= g |d< q0t||  |d}td	d
d |  W d    n1 s]w   Y  |j }|D ]\}	}
| |
d|	 d qid S )Nr   )FakeCustomKernelManagerr   r   rY   r;   rZ   )r[   ra  r`   ra   rb   r   z was called)fake_kernelmanagerr|  r   r   r   r   re   rf   rg   rh   ri   rj   r   r  r   r   expected_methodsr-   assertNotEqual)r   r|  rp   rr   rs   rt   ru   r0   r   method
call_countr"   r"   r(   test_custom_kernel_manager  s,   





z&TestExecute.test_custom_kernel_managerc           	         s   g  G  fdddt }tjt}tj|dd}t|}t|d}W d    n1 s/w   Y  t	
|}||}| } dddd	gksLJ t|| d S )
Nc                       s   e Zd Z fddZ  ZS )z@TestExecute.test_process_message_wrapper.<locals>.WrappedPreProcc                    s"   t  |||}|r| |S rx   )superprocess_messager   )r   r   ru   
cell_indexresult)	__class__rZ   r"   r(   r    s   
zPTestExecute.test_process_message_wrapper.<locals>.WrappedPreProc.process_message)rC   rD   rE   r  __classcell__r"   rZ   )r  r(   WrappedPreProc  s    r  r   r   rY   stdoutstreamzHello World
)rb  rT  r   )r   r   r   rX  __file__r   re   rf   rg   rh   ri   r   r   )	r   r  r   rp   rr   rs   originalwppexecutedr"   r  r(   test_process_message_wrapper  s   

z(TestExecute.test_process_message_wrapperc                 C   sn   t jtdd}t|}t|d}W d    n1 sw   Y  t|}t	|t j
|}t|| d S )Nr   r   rY   )r   r   r   r   re   rf   rg   rh   ri   r   rX  r   )r   rp   rr   rs   r  r  r"   r"   r(   test_execute_function/  s   

z!TestExecute.test_execute_functionc                 C   s   t jtdd}tdd}|  }t j||d d< t|||\}}dd |d	 D }d
d |D }|d d d }|D ]}	|d |	 }
d|
v sKJ d|
v sQJ d|
v sWJ q=d|v s^J d|v sdJ dS )zGRuns a test notebook with widgets and checks the widget state is saved.r   r   r   r   r   r   c                 S   s&   g | ]}|d  D ]}| di qqS )rZ   r   )r   )r&   ru   r   r"   r"   r(   r   B  s
    z,TestExecute.test_widgets.<locals>.<listcomp>rj   c                 S   s    g | ]}d |v r|d  d qS )r   r   r"   )r&   r   r"   r"   r(   r   F  s
    
widgetsz)application/vnd.jupyter.widget-state+jsonstate
model_namemodel_moduleversion_majorversion_minorN)r   r   r   r   r   r  rX  rw   )r   r   rq   r   rs   rv   output_data	model_idswdatar3   dr"   r"   r(   test_widgets:  s&   
zTestExecute.test_widgetsc                 C      t jtdd}t|}t|d}W d    n1 sw   Y  t|d\}}|  |d 	  |d 	  |d 	  |d 	  |d	 
  |d
 	  |d 	  |d 
  d S Nr   r   rY   r/   r   r   r   r   r   r   r   r    r   r   r   r   re   rf   rg   r5   r   assert_called_onceassert_not_calledr   rp   rr   rs   r0   r2   r"   r"   r(   test_execution_hookU     
zTestExecute.test_execution_hookc                 C      t jtdd}t|}t|d}W d    n1 sw   Y  t|d\}}t	t
 |  W d    n1 s=w   Y  |d   |d   |d   |d   |d	   |d
   |d   |d   d S Nr   zError.ipynbrY   r  r   r   r   r   r   r   r   r    r   r   r   r   re   rf   rg   r5   rH  rI  r   r   r  r  r  r"   r"   r(   test_error_execution_hook_errord      

z+TestExecute.test_error_execution_hook_errorc                 C   s   t jtdd}t|}t|d}W d    n1 sw   Y  t|d\}}t	t
 |  W d    n1 s=w   Y  |d   |d   |d   |d   |d	   |d
   |d   |d   d S )Nr   ri  rY   r  r   r   r   r   r   r   r   r    )r   r   r   r   re   rf   rg   r5   rH  rI  r,   r   r  r  r  r"   r"   r(   test_error_notebook_hookt  r  z$TestExecute.test_error_notebook_hookc                 C   r  r  r  r  r"   r"   r(   test_async_execution_hook  r  z%TestExecute.test_async_execution_hookc                 C   r  r  r  r  r"   r"   r(   test_error_async_execution_hook  r  z+TestExecute.test_error_async_execution_hook)!rC   rD   rE   __doc__maxDiffrL  rN  rP  rH  markxfailr   find_kernel_specsrR  rZ  r]  r`  rf  rh  rk  rr  rt  rz  r{  r  r  r  r  r  r  r  r  r  r"   r"   r"   r(   rK  +  s<    
rK  c                
   @   sj  e Zd ZdZe dd Zedddiddid	d
dddd Zedddiddiddd Zedddid	d
dddddidddddd Ze dd Z	edddid	d
dddddidddddd Z
edddii ddd Zedddid	d
dddddiddddd d! Zedddid	d
ddd"dd"ii dd#d$ Zedddid	d
ddd"dd"id%d&idd'd( Zedddid	d
ddd"dd"id%d&iddddiddddd)d* Zedddid	d
ddd"dd"id%d&idd+dd+id,d-id
did.dd/d0 Zedddid1d2idd3d4 Zedddid1d2idd5d6 Zedddid2d	d
d7dd8d9 Zed:dd:id;d<d
diid=dd>d? Zed:dd:id@gd;d
didAggdBd=dCdDdE Zed:dd:id@gd;d
didAggdBd=dCd:dd:id@gd;dFdGidHggdBd=dCdIdJ Zed:dd:id;d
did=ddKdL ZedMddMid,d-id
did2dNddOdP ZedMddMidQd;id,d-id
did2dRddSdT ZedUddUid,d-id
did.ddVdW ZedUddUidQd;id,d-id
didXddYdZ ZedUddUidQd;id,d-id
didXddUddUidQd[id\d]id
d^idXddUddUidQd;id_d`idGdadbdXddcdd Zed+dd+id,d-id
did.ddedf ZedUddUidQd;id_d`idGdadbdXdd+dd+idQdgid_d`idGdadbdXddhdi ZedUddUidQd;id,d-id
didXdd+dd+idQd;id_d`idGdadbdXddjdk Zedlddlid
ddmgdnddodp Z edlddlid
ddmgdnddddiddliddqdrds Z!edlddlid
ddmgdnddddiddtiddqdudv Z"edddiddliddqdwdx Z#edddidldydzddqd{d| Z$edddiddliddqd}d~ Z%edddiddliddqdd Z&edddiddliddqdd Z'e dd Z(edlddlid
ddmgdnddddiddliddqdd Z)edddiddliddqdd Z*e dd Z+edlddlid
ddmgdnddddiddliddqdd Z,dS )TestRunCellz7Contains test functions for NotebookClient.execute_cellc                 C   ,   | |d |jdksJ |jg ksJ d S )Nr   r   execute_cellr  rZ   r   r0   r   r   r"   r"   r(   test_idle_message  s   zTestRunCell.test_idle_messager  rA   r@   r6   wrong_parentr  foo)rb  r   )rA   rB   r=   r>   c                 C   r  Nr   r   r  r  r"   r"   r(   test_message_for_wrong_parent     	z)TestRunCell.test_message_for_wrong_parentr:   r   busy)rA   rB   r>   c                 C   r  r  r  r  r"   r"   r(   test_busy_message  s   zTestRunCell.test_busy_messagestderrbarc                 C   s   dd }||j j_d|_tt ||d W d    n1 s"w   Y  |jdks.J | 	|j
dddd	dd
dd	g d S )Nc                    s   t | I d H  trx   )rI   sleepr   r   r"   r"   r(   r     s   z5TestRunCell.test_deadline_exec_reply.<locals>.get_msgr   r   r  r  r  r  rT  rb  r   r  r  )r   r   r   r   rH  rI  r\  r  r  assertListEqualrZ   )r   r0   r   r   r   r"   r"   r(   test_deadline_exec_reply  s   


z$TestRunCell.test_deadline_exec_replyc                 C   sJ   t  |_d|_tt ||d W d    d S 1 sw   Y  d S )NTr   )r   r   raise_on_iopub_timeoutrH  rI  r\  r  r  r"   r"   r(   test_deadline_iopub  s
   "zTestRunCell.test_deadline_iopubc                 C   s   dd }|t |jd d |_ttdd|jiid|jj_d|_t	
t ||d W d    n1 s8w   Y  |jd	ksDJ | |jd
dddd
dddg d S )Nc                 s   s    | E d H  	 t  V  qrx   r   )r   r"   r"   r(   message_seq  s
   
z=TestRunCell.test_eventual_deadline_iopub.<locals>.message_seqr=   r6   r   Tr   r  r  r  r  r  r  r  )r   r   r   rP   r   r   r   r   r  rH  rI  r\  r  r  r  rZ   )r   r0   r   r   r  r"   r"   r(   test_eventual_deadline_iopub  s    

z(TestRunCell.test_eventual_deadline_iopubr%  c                 C   r  r  r  r  r"   r"   r(   test_execute_input_message  s   z&TestRunCell.test_execute_input_messagec                 C   s@   | |d |jdksJ | |jddddddddg d S )	Nr   r  r  r  r  r  r  r  )r  r  r  rZ   r  r"   r"   r(   test_stream_messages'  s   

z TestRunCell.test_stream_messagesclear_outputc                 C   r  )Nr   r  r  r  r"   r"   r(   test_clear_output_message@  r  z%TestRunCell.test_clear_output_messagewaitTc                 C   sB   | |d |jdksJ | |j |jddddgksJ d S )Nr   r  r  r  r  r  )r  r  
assertTrueclear_before_next_outputrZ   r  r"   r"   r(   test_clear_output_wait_messageO  s   z*TestRunCell.test_clear_output_wait_messagec                 C   s@   | |d |jdksJ |jrJ |jddddgksJ d S )Nr   rY   r  r  r  r  r  r  r  rZ   r  r"   r"   r(   +test_clear_output_wait_then_message_messaged     
z7TestRunCell.test_clear_output_wait_then_message_messageupdate_display_datametafoometabar)r   r   c                 C   s@   | |d |jdksJ |jsJ |jddddgksJ d S )Nr   rY   r  r  r  r  r  r  r"   r"   r(   2test_clear_output_wait_then_update_display_message~  r  z>TestRunCell.test_clear_output_wait_then_update_display_messager;   *   c                 C   s:   | |d |jdksJ |jdksJ |jg ksJ d S )Nr   r   r  r  r  r;   rZ   r  r"   r"   r(   test_execution_count_message  s   z(TestRunCell.test_execution_count_messagec                 C   s>   |j |ddd |jdksJ |jdksJ |jg ksJ d S )Nr      )r;   r   r  r  r"   r"   r(   0test_execution_count_message_ignored_on_override  s   z<TestRunCell.test_execution_count_message_ignored_on_override)r;   rb  r   c                 C   sD   | |d |jdksJ |jdksJ |jddddgks J d S )Nr   r   r  r  r  r  r  r  r  r"   r"   r(   (test_execution_count_with_stream_message  s   z4TestRunCell.test_execution_count_with_stream_messagecommfoobarr  )comm_idr   c                 C   sL   | |d |jdksJ | |jdddii |jrJ |jg ks$J d S )Nr   r   r  r  r  )r  r  rY  widget_statewidget_buffersrZ   r  r"   r"   r(   test_widget_comm_message  s
   
z$TestRunCell.test_widget_comm_messages   123r   )r  buffer_paths)rA   rB   buffersr>   c                 C   sb   | |d |jdksJ |jdddiiksJ |jddddd	gd
iiks(J |jg ks/J d S )Nr   r   r  r  r  r   MTIzbase64r   r   encodingr   r  r  r  r  rZ   r  r"   r"   r(   &test_widget_comm_buffer_message_single  s   
z2TestRunCell.test_widget_comm_buffer_message_singlefoo2bar2path2c                 C   sp   | |d |jdksJ |jddddiksJ |jdddd	gd
dddgd
diks/J |jg ks6J d S )Nr   r  r  r  r  )r  r  r  r  r   r  r  )r  )r  r  r  r"   r"   r(    test_widget_comm_buffer_messages  s   
z,TestRunCell.test_widget_comm_buffer_messagesc                 C   s@   | |d |jdksJ |jrJ |jrJ |jg ksJ d S r  r  r  r"   r"   r(   test_unknown_comm_message  s
   

z%TestRunCell.test_unknown_comm_messageexecute_result)r   r   r;   c                 C   sX   | |d |jdksJ |jdksJ |jdddiddidd	gks%J |jr*J d S )
Nr   r   r  r  r  r  r  r  rT  r   r   r;   r  r  r;   rZ   _display_id_mapr  r"   r"   r(   test_execute_result_message#  s   
	z'TestRunCell.test_execute_result_message
display_id)	transientr   r   r;   c                 C   s\   | |d |jdksJ |jdksJ |jdddiddidd	gks%J d
|jv s,J d S )Nr   r   r  r  r  r  r  r  r  r  r  r  r"   r"   r(   (test_execute_result_with_display_message?  s   
z4TestRunCell.test_execute_result_with_display_messagedisplay_datac                 C   sH   | |d |jdksJ |jdddiddidgksJ |jr"J d S )	Nr   r   r  r  r  r  r  rT  r   r   r  r  rZ   r  r  r"   r"   r(   $test_display_data_without_id_message[  s   
z0TestRunCell.test_display_data_without_id_message)r  r   r   c                 C   sL   | |d |jdksJ |jdddiddidgksJ d	|jv s$J d S )
Nr   r   r  r  r  r  r  r  r  r  r  r"   r"   r(   test_display_data_messageq  s   
z%TestRunCell.test_display_data_messagefoobar_othermetafoo_othermetabar_other	bar_othermetafoo2metabar2	foobarbazr  bazc                 C   st   | |d |jdksJ |jdddidddd	dd
diddid	dddidddd	gks1J d|jv s8J d S )Nr   rY   r  r  r  r  r  r	  r  r  r  r  r  r  r  r  r"   r"   r(   !test_display_data_same_id_message  s"   
z-TestRunCell.test_display_data_same_id_messagec                 C   s6   | |d |jdksJ |jg ksJ |jrJ d S r  r  r  r"   r"   r(   +test_update_display_data_without_id_message  s   z7TestRunCell.test_update_display_data_without_id_messagefoobar2c                 C   N   | |d |jdksJ |jdddidddd	gksJ d
|jv s%J d S Nr   r  r  r  r  r  r  r	  r  r  r  r  r"   r"   r(   ,test_update_display_data_mismatch_id_message     
z8TestRunCell.test_update_display_data_mismatch_id_messagec                 C   r  r  r  r  r"   r"   r(    test_update_display_data_message  r  z,TestRunCell.test_update_display_data_messager?   Boom)rU  rW  r   c                 C   :   | |d |jdksJ |jddddgdgksJ d S Nr   r   r?   r  r  r  )rT  rU  rW  r   r  r  r"   r"   r(   test_error_message  s
   zTestRunCell.test_error_messager}   c                 C   sd   |  t ||d W d    n1 sw   Y  |jdks"J |jddddgdgks0J d S r  )assertRaisesr   r  r  rZ   r  r"   r"   r(   $test_error_and_error_status_messages#  s   z0TestRunCell.test_error_and_error_status_messagesr8   c                 C   r  r  r  r  r"   r"   r(   test_error_message_only;  s
   z#TestRunCell.test_error_message_onlyc                 C   s2   d|_ ||d |jdksJ |jg ksJ d S )NTr   r   )r   r  r  rZ   r  r"   r"   r(   rr  S  s   	zTestRunCell.test_allow_errorsNotImplementedError)r:   rU  c                 C   s4   dg|_ ||d |jdksJ |jg ksJ d S )Nr  r   r   )allow_error_namesr  r  rZ   r  r"   r"   r(   test_allow_error_namese  s   	z"TestRunCell.test_allow_error_namesc                 C   s8   dg|j d< ||d |jdksJ |jg ksJ d S )Nzraises-exceptiontagsr   r   )r   r  r  rZ   r  r"   r"   r(   test_raises_exception_tagw  s   	z%TestRunCell.test_raises_exception_tagc                 C   <   t di dg d}||d |jdksJ |jg ksJ d S )Nr   rawr   r   r   r  r  rZ   r  r"   r"   r(   test_non_code_cell  s   	zTestRunCell.test_non_code_cellc                 C   r  )Nz     r   r   r   r!  r  r"   r"   r(   test_no_source  s   	zTestRunCell.test_no_sourcec                 C   s   t |d\}}||d |d j|dd |d j|dd |d j|dd |d j|dtd |d	   |d
   |d   |d   d S Nr0   r   r   ru   r  r   r   r   ru   r  r@   r   r   r   r    r5   r  assert_called_once_withEXECUTE_REPLY_OKr  r   r0   r   r   r2   r"   r"   r(   test_cell_hooks  s   zTestRunCell.test_cell_hooksc                 C   s   t |d\}}| t ||d W d    n1 sw   Y  |d j|dd |d j|dd |d j|dd |d j|dtd |d	 j|dtd |d
   |d   |d   d S r$  r5   r  r   r  r)  EXECUTE_REPLY_ERRORr  r+  r"   r"   r(   test_error_cell_hooks  s    z!TestRunCell.test_error_cell_hooksc                 C   s   t di dg d}t|d\}}||d |d j|dd |d   |d	   |d
   |d   |d   |d   |d   d S )Nr   r   r   r%  r   r   r&  r   r   r   r   r   r   r    )r   r5   r  r)  r  r+  r"   r"   r(   test_non_code_cell_hooks  s   	z$TestRunCell.test_non_code_cell_hooksc                 C   s   t |dd\}}||d |d j|dd |d j|dd |d j|dd |d j|dtd	 |d
   |d   |d   |d   d S NT)r0   r1   r   r   r&  r   r   r   r'  r   r   r   r    r(  r+  r"   r"   r(   test_async_cell_hooks  s   z!TestRunCell.test_async_cell_hooksc                 C   s   t |dd\}}| t ||d W d    n1 sw   Y  |d j|dd |d j|dd |d j|dd |d j|dtd	 |d
 j|dtd	 |d   |d   |d   d S r1  r-  r+  r"   r"   r(   test_error_async_cell_hooks  s    z'TestRunCell.test_error_async_cell_hooksN)-rC   rD   rE   r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  rr  r  r  r"  r#  r,  r/  r0  r2  r3  r"   r"   r"   r(   r    s   









	
	
	


















	




	














r  )NNFrx   )WrI   concurrent.futuresr  rh   r  r   r   r   r   rk   r  r   r   queuer   typingr   unittest.mockr   r   rf   rH  r   jupyter_clientr	   r
   jupyter_client._versionr   jupyter_client.kernelspecr   nbconvert.filtersr   r   testpathr   	traitletsr   r   r   r   
exceptionsr   baser   compiler   r   rX  r  r   r   r   environr+   r5   r*  r.  r$   rM   rP   rW   rw   rz   r|   r   r   r   r   r   r  parametrizer   r   r  skipifrb  r  r  r  r*  r,  r5  r8  intrJ  rK  r  r"   r"   r"   r(   <module>   s    



	
M




   {