o
    n~b-                     @   s  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mZm	Z	m
Z
mZ d dlZd dlmZmZ d dlmZ ddlmZmZmZmZmZmZmZmZ zd dlZW n ey[   dZY nw zd dlZW n eym   dZY nw eeeef gee f Zdd	 Z d
d Z!ej"dd Z#ej"dd Z$ej%dd Z&ej%d0ddZ'dd Z(dd Z)dd Z*ej"dd Z+ej"dd Z,ej"dd  Zej"d!d" Z-ej"d#d$ Z.ej"d%d& Z/ej"d'd( Z0ej"d)d* Z1ej"de j2d+e	eddf fd,d-Z3ej"d.d/ Z4dS )1    N)Callable)Any	Awaitabler   Dict	GeneratorOptionalUnion)PY_37isasyncgenfunction)Application   )BaseTestServerRawTestServer
TestClient
TestServerloop_contextsetup_test_loopteardown_test_loopunused_portc                 C   s:   | j ddddd | j dddd	d | j d
dddd d S )N--aiohttp-fast
store_trueFz*run tests faster by disabling extra checks)actiondefaulthelpz--aiohttp-loopstorepyloopz:run tests with specific loop: pyloop, uvloop, tokio or all--aiohttp-enable-loop-debugzenable event loop debug mode)Z	addoption)parser r   @/usr/local/lib/python3.10/dist-packages/aiohttp/pytest_plugin.pypytest_addoption$   s$   
r    c                    sb   | j  t r
dn
t rdndS dd| jvr$|  jd7  _d fdd}|| _ dS )zjSet up pytest fixture.

    Allow fixtures to be coroutines. Run coroutine fixtures in an event loop.
    TFNrequestr!   c                     sz   |d }r	|d= d|j vrtd|d r3| i | fdd}||   S  | i |S )Nr!   loopz^Asynchronous fixtures must depend on the 'loop' fixture or be used in tests depending from it.c                      s&   z   W S  ty   Y d S w N)run_until_complete	__anext__StopAsyncIterationr   _loopgenr   r   	finalizerd   s
   z8pytest_fixture_setup.<locals>.wrapper.<locals>.finalizer)fixturenames	ExceptionZgetfixturevalueZaddfinalizerr%   r&   )argskwargsr!   r+   funcZis_async_genZstrip_requestr(   r   wrapperO   s   


z%pytest_fixture_setup.<locals>.wrapper)r1   r
   asyncioiscoroutinefunctionargnames)Z
fixturedefr2   r   r0   r   pytest_fixture_setup9   s   


 r6   c                 C      | j dS )z--fast config optionr   config	getoptionr"   r   r   r   fastr      r;   c                 C   r7   )z!--enable-loop-debug config optionr   r8   r"   r   r   r   
loop_debugx   r<   r=   c               	   c   sx    t jdd+} dV  dd | D }|r*tdt|t|dkr"dnd	d
|W d   dS 1 s5w   Y  dS )zContext manager which checks for RuntimeWarnings.

    This exists specifically to
    avoid "coroutine 'X' was never awaited" warnings being missed.

    If RuntimeWarnings occur in the context a RuntimeError is raised.
    T)recordNc                 S   s"   g | ]}|j tkrd j|dqS )z#{w.filename}:{w.lineno}:{w.message})w)categoryRuntimeWarningformat).0r?   r   r   r   
<listcomp>   s    
z,_runtime_warning_context.<locals>.<listcomp>z{} Runtime Warning{},
{}r    s
)warningscatch_warningsRuntimeErrorrB   lenjoin)	_warningsrwr   r   r   _runtime_warning_context~   s   	 "rO   Fc                 c   s,    | r| V  dS t  } | V  t| |d dS )zPassthrough loop context.

    Sets up and tears down a loop unless one is passed in via the loop
    argument when it's passed straight through.
    r;   N)r   r   )r#   r;   r   r   r   _passthrough_loop_context   s   
rQ   c                 C   s,   |  |rt|rt| ||S dS dS )z%Fix pytest collecting for coroutines.N)Zfuncnamefilterr3   r4   listZ_genfunctions)	collectornameobjr   r   r   pytest_pycollect_makeitem   s   rV   c              	      s    j d}t jrc jdp jdd}t = t||d} fdd j	j
D }| jd	i | W d   n1 sDw   Y  W d   dS W d   dS 1 s\w   Y  dS dS )
zBRun coroutines in an event loop instead of a normal function call.r   proactor_loopr#   NrP   c                    s   i | ]}| j | qS r   )funcargs)rC   arg
pyfuncitemr   r   
<dictcomp>   s    
z&pytest_pyfunc_call.<locals>.<dictcomp>Tr   )r9   r:   r3   r4   functionrX   getrO   rQ   Z_fixtureinfor5   r%   rU   )r[   r;   Zexisting_loopr)   Ztestargsr   rZ   r   pytest_pyfunc_call   s,   


r_   c                 C   s   d| j vrd S | jjj}dtji}td urtj|d< td ur#tj|d< |dkr)d}i }|	dD ]&}|
d }|d	}||vrP|rOtd
|t| f q0|| ||< q0| jdt| t| d d S )Nloop_factoryr   uvlooptokioallzpyloop,uvloop?,tokio?,?z ?z&Unknown loop '%s', available loops: %s)ids)r,   r9   optionZaiohttp_loopr3   DefaultEventLoopPolicyra   ZEventLoopPolicyrb   splitendswithstrip
ValueErrorrR   keysZparametrizevalues)ZmetafuncZloopsZavail_factoriesZ	factoriesrT   requiredr   r   r   pytest_generate_tests   s4   






rp   c                 c   s`    |  }t | t|d}|r|d t | |V  W d   dS 1 s)w   Y  dS )z%Return an instance of the event loop.rP   TN)r3   set_event_loop_policyr   	set_debugset_event_loop)r`   r;   r=   policyr)   r   r   r   r#      s   


"r#   c                  c   sj    t st } tj| _n	t } t|  t| j}t	| |V  W d    d S 1 s.w   Y  d S r$   )
r	   r3   get_event_loop_policyZProactorEventLoop_loop_factoryZWindowsProactorEventLoopPolicyrq   r   new_event_looprs   )rt   r)   r   r   r   rW      s   


"rW   c                 C      t jdtdd | S )Nz3Deprecated, use aiohttp_unused_port fixture instead   
stacklevelrH   warnDeprecationWarning)aiohttp_unused_portr   r   r   r         r   c                   C   s   t S )z1Return a port that is unused on the current host.)_unused_portr   r   r   r   r     s   r   c                 #   >    g dd fdd
}|V  dfdd}  |  dS )	z^Factory to create a TestServer instance, given an app.

    aiohttp_server(app, **kwargs)
    Nportc                   6   t | |d}|jdd i|I d H  | |S Nr   r#   r   )r   start_serverappend)Zappr   r/   serverr#   serversr   r   go  
   
zaiohttp_server.<locals>.goreturnc                      $    r    I d H   sd S d S r$   popcloser   r   r   r   finalize     z aiohttp_server.<locals>.finalizer   Nr%   r#   r   r   r   r   r   aiohttp_server     r   c                 C   rx   )Nz.Deprecated, use aiohttp_server fixture insteadry   rz   r|   )r   r   r   r   test_server"  r   r   c                 #   r   )	zpFactory to create a RawTestServer instance, given a web handler.

    aiohttp_raw_server(handler, **kwargs)
    Nr   c                   r   r   )r   r   r   )handlerr   r/   r   r   r   r   r   4  r   zaiohttp_raw_server.<locals>.gor   c                      r   r$   r   r   r   r   r   r   <  r   z$aiohttp_raw_server.<locals>.finalizer   r   r   r   r   r   aiohttp_raw_server,  r   r   c                 C   rx   )Nz2Deprecated, use aiohttp_raw_server fixture insteadry   rz   r|   )r   r   r   r   raw_test_serverC  s   r   r   c                 #   sh    g  dddt ttf dtdttttf  dtdtf
 fdd	}|V  d fd
d}|  dS )zFactory to create a TestClient instance.

    aiohttp_client(app, **kwargs)
    aiohttp_client(server, **kwargs)
    aiohttp_client(raw_server, **kwargs)
    N)server_kwargs__paramr.   r   r/   r   c                   s   t | trt | ttfs| g|R i |} i }n|r!J dt | tr?|p)i }t| fdi|}t|fdi|}nt | trOt| fdi|}ntdt|  | I d H   	| |S )Nzargs should be emptyr#   zUnknown argument type: %r)

isinstancer   r   r   r   r   rl   typer   r   )r   r   r.   r/   r   clientclientsr#   r   r   r   [  s"   


zaiohttp_client.<locals>.goc                      r   r$   r   r   )r   r   r   r   y  r   z aiohttp_client.<locals>.finalizer   )	r   r   r   r   r   r   strr   r%   r   r   r   r   aiohttp_clientO  s"   

r   c                 C   rx   )Nz.Deprecated, use aiohttp_client fixture insteadry   rz   r|   )r   r   r   r   test_client  r   r   )F)5r3   
contextlibrH   collections.abcr   typingr   r   r   r   r   r   ZpytestZaiohttp.helpersr	   r
   Zaiohttp.webr   Z
test_utilsr   r   r   r   r   r   r   r   r   ra   ImportErrorrb   ZAiohttpClientr    r6   Zfixturer;   r=   contextmanagerrO   rQ   rV   r_   rp   r#   rW   r   r   r   r   r   AbstractEventLoopr   r   r   r   r   r   <module>   sr    $(9


"


	


	

0