U
    \ShT                     @  s  d Z ddlm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mZ ddlZddlmZ ddlmZmZ d	d
gZdd ZejddddddZG dd dZe dddddddddZe dddddZejddddd dd!d"d#d$Zejddd%d&d'd(d)Zejddd%d&d'd*d+Ze ddd,d-d.d/Ze ddd0d1Zejd2d3d4d5dd6d7Ze d%d5d'd8d9Z ejdddd:d;d<d=Z!e dd:d>d?d@Z"e dAdB Z#dS )CzSphinx test fixtures for pytest    )annotationsN)
namedtuple)StringIO)AnyCallable	Generator)util)SphinxTestApp#SphinxTestAppWrapperForSkipBuildingzsphinx(builder, testroot=None, freshenv=False, confoverrides=None, tags=None, docutilsconf=None, parallel=0): arguments to initialize the sphinx test application.z0test_params(shared_result=...): test parameters.c                 C  s   t D ]}| d| qdS )zRegister custom markersmarkersN)DEFAULT_ENABLED_MARKERSZaddinivalue_line)configmarker r   J/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/testing/fixtures.pypytest_configure   s    r   session)scopez
str | None)returnc                   C  s   d S Nr   r   r   r   r   rootdir   s    r   c                   @  s<   e Zd ZU i Zded< ddddddZdd	d
ddZdS )SharedResultzdict[str, dict[str, str]]cachestrr	   r   )keyapp_r   c                 C  s2   || j krd S |j |j d}|| j |< d S )Nstatuswarning)r   _statusgetvalue_warning)selfr   r   datar   r   r   store'   s    
zSharedResult.storezdict[str, StringIO])r   r   c                 C  s2   || j kri S | j | }t|d t|d dS )Nr   r   r   )r   r   )r"   r   r#   r   r   r   restore0   s    



zSharedResult.restoreN)__name__
__module____qualname__r   __annotations__r$   r%   r   r   r   r   r   $   s   
	r   r   dictr   ztuple[dict, dict])requesttest_paramsshared_resultsphinx_test_tempdirr   r   c                   s   i  i }t t| jdD ],}t|jD ]\}}| |< q*||j q fddt 	 D }	|d rd|kr~t
d|d |d< ||d }
||
 |dd}||d|  |d< }|r| s|d	|  }|| td
d|	|S )zp
    Parameters that are specified by 'pytest.mark.sphinx' for
    sphinx.application.Sphinx initialization
    Zsphinxc                   s   g | ]} | qS r   r   ).0iZpargsr   r   
<listcomp>M   s     zapp_params.<locals>.<listcomp>r-   srcdirz:You can not specify shared_result and srcdir in same time.testrootrootztest-
app_paramszargs,kwargs)reversedlistnodeZiter_markers	enumerateargsupdatekwargssortedkeyspytest	Exceptionr%   popgetexistscopytreer   )r+   r,   r-   r.   r   r=   infor0   ar;   r%   r4   r3   Ztestroot_pathr   r1   r   r6   :   s&    




r6   )r+   r   c                 C  sP   | j d}|r|jni }ddi}|| |d rLt|d tsLtd|S )au  
    Test parameters that are specified by 'pytest.mark.test_params'

    :param Union[str] shared_result:
       If the value is provided, app._status and app._warning objects will be
       shared in the parametrized test functions and/or test functions that
       have same 'shared_result' value.
       **NOTE**: You can not specify both shared_result and srcdir.
    r,   r-   Nz@You can only provide a string type of value for "shared_result" )r9   Zget_closest_markerr=   r<   
isinstancer   r@   rA   )r+   envr=   resultr   r   r   r,   e   s     

r,   functionr   z$Generator[SphinxTestApp, None, None])r,   r6   make_appr-   r   c                 c  s   |\}}|||}|V  t d|dd t d|jj t d|j t d|j t dd|j   t d	d|j   | d
 r|	| d
 | dS )z9
    Provides the 'sphinx.application.Sphinx' object
    z# testroot:r4   r5   z
# builder:z	# srcdir:z	# outdir:z	# status:
z
# warning:r-   N)
printrC   Zbuildernamer3   Zoutdirr   r    r!   r$   )r,   r6   rL   r-   r;   r=   r   r   r   r   app}   s    
rP   r	   r   )rP   r   c                 C  s   | j S zJ
    Back-compatibility for testing with previous @with_app decorator
    )r   rP   r   r   r   r      s    r   c                 C  s   | j S rQ   )r!   rR   r   r   r   r      s    r   zGenerator[Callable, None, None])r,   monkeypatchr   c                 #  s^   | ddd  g  tjdd } fdd}|V  |tjdd< t D ]}|  qLdS )z
    Provides make_app function to initialize SphinxTestApp instance.
    if you want to initialize 'app' in your test function. please use this
    instead of using SphinxTestApp class directory.
    zsphinx.application.abspathc                 S  s   | S r   r   )xr   r   r   <lambda>       zmake_app.<locals>.<lambda>Nc                    sN   t  t   }}|d| |d| t| |} | d rJt|}|S )Nr   r   r-   )r   
setdefaultr	   appendr
   )r;   r=   r   r   r   Zappsr,   r   r   make   s    

zmake_app.<locals>.make)setattrsyspathr7   cleanup)r,   rS   ZsyspathrZ   r   r   rY   r   rL      s    	rL   c                   C  s   t  S r   )r   r   r   r   r   r-      s    r-   moduleT)r   ZautouseNonec                   C  s   t j  d S r   )r   r   clearr   r   r   r   _shared_result_cache   s    rb   c                 C  sR   t | jdd}z |r,tj|dgdd W dS W n tk
rB   Y nX td dS )zt
    The test will be skipped when using 'if_graphviz_found' fixture and graphviz
    dot command is not found.
    graphviz_dot z-VT)capture_outputNzgraphviz "dot" is not available)getattrr   
subprocessrunOSErrorr@   skip)rP   rc   r   r   r   if_graphviz_found   s    
rk   z	util.path)tmpdir_factoryr   c                 C  s   |   }t| S )z8
    Temporary directory wrapped with `path` class.
    )Zgetbasetempr   r]   abspath)rl   tmpdirr   r   r   r.      s    r.   )rn   r   c                 C  s
   t | S )z
    Temporary directory wrapped with `path` class.
    This fixture is for back-compatibility with old test implementation.
    )r   r]   )rn   r   r   r   tempdir   s    ro   c               
   c  s@   zt tj}dV  W 5 t tjD ]} | |kr tj|  q X dS )z
    Rollback sys.modules to its value before testing to unload modules
    during tests.

    For example, used in test_ext_autosummary.py to permit unloading the
    target module to clear its cache.
    N)r8   r\   modulesrB   )modnameZ
sysmodulesr   r   r   rollback_sysmodules   s    	

rr   )$__doc__
__future__r   rg   r\   collectionsr   ior   typingr   r   r   r@   Zsphinx.testingr   Zsphinx.testing.utilr	   r
   r   r   Zfixturer   r   r6   r,   rP   r   r   rL   r-   rb   rk   r.   ro   rr   r   r   r   r   <module>   sP   	
*



