o
    Ni                     @  s  d Z ddlm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 ddlZddlmZmZ erHddlmZmZ dd	lmZ dd
l
mZ ddgZdBddZejdddCddZG dd dZejdDd#d$Zed"d%ZejdEd&dZejdFd*d+ZejdGd.d/ZejdGd0d1Z ejdHd3d'Z!ejdId4dZ"ejd5d6d7dJd8d9Z#ejdKd:d;Z$ejdddLd>dZ%ejdMd@dAZ&dS )NzSphinx test fixtures for pytest    )annotationsN)
namedtuple)StringIO)TYPE_CHECKING)SphinxTestApp#SphinxTestAppWrapperForSkipBuilding)CallableIterator)Path)Anyzsphinx(buildername="html", *, testroot="root", srcdir=None, confoverrides=None, freshenv=False, warningiserror=False, tags=None, verbosity=0, parallel=0, builddir=None, docutils_conf=None): arguments to initialize the sphinx test application.z0test_params(shared_result=...): test parameters.configpytest.ConfigreturnNonec                 C  s   t D ]}| d| qdS )zRegister custom markersmarkersN)DEFAULT_ENABLED_MARKERSaddinivalue_line)r   marker r   K/home/ubuntu/.local/lib/python3.10/site-packages/sphinx/testing/fixtures.pypytest_configure&   s   r   session)scopePath | Nonec                   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
ZdddZdS )SharedResultzdict[str, dict[str, str]]cachekeystrapp_r   r   r   c                 C  s2   || j v rd S |j |j d}|| j |< d S )Nstatuswarning)r   r"   getvaluer#   )selfr   r    datar   r   r   store4   s   
zSharedResult.storedict[str, StringIO]c                 C  s2   || j vri S | j | }t|d t|d dS )Nr"   r#   r!   )r   r   )r%   r   r&   r   r   r   restore=   s   



zSharedResult.restoreN)r   r   r    r   r   r   )r   r   r   r(   )__name__
__module____qualname__r   __annotations__r'   r)   r   r   r   r   r   1   s   
 
	r   requestr   test_paramsdict[str, Any]shared_resultsphinx_test_tempdirr   r
   _app_paramsc                   s   i  i }t t| jdD ]} tt|jO  ||j q fddt	 
 D }|d rNd|v r<d}t| |d |d< ||d }	||	 |dd}
||d|
  |d< }|rr| sr|d	|
  }t|| t||S )
zp
    Parameters that are specified by 'pytest.mark.sphinx' for
    sphinx.application.Sphinx initialization
    sphinxc                   s   g | ]} | qS r   r   ).0ipargsr   r   
<listcomp>]   s    zapp_params.<locals>.<listcomp>r1   srcdirz:You can not specify shared_result and srcdir in same time.testrootrootztest-)reversedlistnodeiter_markersdict	enumerateargsupdatekwargssortedkeyspytestfailr)   popgetexistsshutilcopytreer3   )r.   r/   r1   r2   r   rE   inforC   msgr)   r;   r:   testroot_pathr   r7   r   
app_paramsG   s&   


rR   zargs,kwargsc                 C  sT   | j d}|r|jni }ddi}|| |d r(t|d ts(d}t||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/   r1   Nz?You can only provide a string type of value for "shared_result")r?   get_closest_markerrE   rD   
isinstancer   rH   	Exception)r.   envrE   resultrP   r   r   r   r/   x   s   

make_appCallable[[], SphinxTestApp]Iterator[SphinxTestApp]c                 c  s    |\}}||i |}|V  t d|dd t d|jj t d|j t d|j t dd|j   t d	d|j   | d
 rM|	| d
 | dS dS )z9
    Provides the 'sphinx.application.Sphinx' object
    z# testroot:r;   r<   z
# builder:z	# srcdir:z	# outdir:z	# status:
z
# warning:r1   N)
printrK   buildernamer:   outdirr"   r$   r#   r'   )r/   rR   rX   r1   rC   rE   r    r   r   r   app   s   
r`   r   r   c                 C     | j S zJ
    Back-compatibility for testing with previous @with_app decorator
    )r"   r`   r   r   r   r"         r"   c                 C  ra   rb   )r#   rc   r   r   r   r#      rd   r#   %Iterator[Callable[[], SphinxTestApp]]c                 #  sN    g  t j }d	 fdd}|V  |t jdd< t D ]}|  q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.
    rC   r   rE   r   r   c                    sZ   t  t  }}|d| |d| d rt| i |}nt| i |} | |S )Nr"   r#   r1   )r   
setdefaultr   r   append)rC   rE   r"   r#   r    appsr/   r   r   make   s   
zmake_app.<locals>.makeN)rC   r   rE   r   r   r   )syspathcopyr=   cleanup)r/   syspathrj   r    r   rh   r   rX      s   

c                   C  s   t  S r   )r   r   r   r   r   r1      s   moduleT)r   autousec                   C  s   t j  d S r   )r   r   clearr   r   r   r   _shared_result_cache   s   rs   c                 C  sR   t | jdd}z|rtj|dgddd W dS W n	 ty!   Y nw 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-VTF)capture_outputcheckNzgraphviz "dot" is not available)getattrr   
subprocessrunOSErrorrH   skip)r`   rt   r   r   r   if_graphviz_found   s   r}   tmp_path_factorypytest.TempPathFactoryc                 C  s   |   S )zTemporary directory.)getbasetemp)r~   r   r   r   r2      s   Iterator[None]c               
   c  sd    t tj} zdV  W t tjD ]}|| vrtj| qdS t tjD ]}|| vr0tj| q$w )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)r>   rk   modulesrJ   )
sysmodulesmodnamer   r   r   rollback_sysmodules   s   
	r   )r   r   r   r   )r   r   )r.   r   r/   r0   r1   r   r2   r   r   r
   r   r3   )r.   r   r   r0   )
r/   r0   rR   r3   rX   rY   r1   r   r   rZ   )r`   r   r   r   )r/   r0   r   re   )r   r   )r   r   )r`   r   r   r   )r~   r   r   r
   )r   r   )'__doc__
__future__r   rM   ry   rk   collectionsr   ior   typingr   rH   sphinx.testing.utilr   r   collections.abcr   r	   pathlibr
   r   r   r   fixturer   r   rR   r3   r/   r`   r"   r#   rX   r1   rs   r}   r2   r   r   r   r   r   <module>   sX    


-
