B
    dE              	   @   s  d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ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ee dddZG dd dZeje	eeeeeeef dddZeje	edddZejddeeeef e
eeeddf dddZejddeeddd Zejddeedd!d"Ze ee	ee
ddf d#d$d%Z ejedd&d'Z!ejd(d)d*ddd+d,Z"ejeddd-d.Z#ejdde	d/d0d1d2Z$ejed/d3d4d5Z%ejd6d7 Z&dS )8zSphinx test fixtures for pytest    N)
namedtuple)StringIO)PIPE)AnyCallableDict	GeneratorOptionalTuple)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   xt D ]}| d| qW dS )zRegister custom markersmarkersN)DEFAULT_ENABLED_MARKERSZaddinivalue_line)configmarker r   d/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/sphinx/testing/fixtures.pypytest_configure   s    
r   session)scope)returnc               C   s   d S )Nr   r   r   r   r   rootdir   s    r   c               @   sT   e Zd ZU i Zeeeeef f ed< eeedddZ	eeee
f dddZdS )	SharedResultcache)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.store)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   r   restore/   s
    


zSharedResult.restoreN)__name__
__module____qualname__r   r   str__annotations__r   r   r$   r   r%   r   r   r   r   r   #   s   
	r   )requesttest_paramsshared_resultsphinx_test_tempdirr   r   c                s   i  i }xHt t| jdD ]0}xt|jD ]\}}| |< q.W ||j qW  fddt 	 D }	|d rd|krt
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)pargsr   r   
<listcomp>L   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   9   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,   d   s    

r,   function)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   buildernamer3   Zoutdirr   r    r!   r$   )r,   r6   rL   r-   r;   r=   r   r   r   r   app|   s    
rQ   )rQ   r   c             C   s   | j S )zJ
    Back-compatibility for testing with previous @with_app decorator
    )r   )rQ   r   r   r   r      s    r   c             C   s   | j S )zJ
    Back-compatibility for testing with previous @with_app decorator
    )r!   )rQ   r   r   r   r      s    r   )r,   monkeypatchr   c             #   sb   | ddd  g  tjdd } fdd}|V  |tjdd< xt D ]}|  qNW d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 )Nr   )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   )appsr,   r   r   make   s    

zmake_app.<locals>.make)setattrsyspathr7   cleanup)r,   rR   ZsyspathrY   r   r   )rX   r,   r   rL      s    	rL   c               C   s   t  S )N)r   r   r   r   r   r-      s    r-   moduleT)r   Zautousec               C   s   t j  d S )N)r   r   clearr   r   r   r   _shared_result_cache   s    r`   c             C   sR   t | jdd}y |r,tj|dgttd 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-V)stdoutstderrNzgraphviz "dot" is not available)getattrr   
subprocessrunr   OSErrorr@   skip)rQ   ra   r   r   r   if_graphviz_found   s    rj   z	util.path)tmpdir_factoryr   c             C   s   |   }t| S )z8
    Temporary directory wrapped with `path` class.
    )Zgetbasetempr   r\   abspath)rk   tmpdirr   r   r   r.      s    r.   )rm   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\   )rm   r   r   r   tempdir   s    rn   c           
   c   sD   zt tj} dV  W dx&t tjD ]}|| kr"tj| q"W 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   )Z
sysmodulesmodnamer   r   r   rollback_sysmodules   s    	

rq   )'__doc__rf   r[   collectionsr   ior   r   typingr   r   r   r   r	   r
   r@   Zsphinx.testingr   Zsphinx.testing.utilr   r   r   r   Zfixturer)   r   r   r6   r,   rQ   r   r   rL   r-   r`   rj   r.   rn   rq   r   r   r   r   <module>   sP    
)



