
    Fe                        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mZmZ ddlZddlmZmZ erddlmZ dd	lmZ d
dgZd Z ej0                  d      d"d       Z G d d      Z ej0                         	 	 	 	 	 	 d#d       Z edd      Z ej0                         d$d       Z ej0                         	 	 	 	 d%d       Z ej0                         d&d       Z ej0                         d&d       Z  ej0                         d'd       Z! ej0                         d(d       Z" ej0                  dd      d)d       Z# ej0                         d*d       Z$ ej0                  d      d+d        Z% ej0                         d!        Z&y),zSphinx test fixtures for pytest    )annotationsN)
namedtuple)StringIO)TYPE_CHECKINGAnyCallable)SphinxTestApp#SphinxTestAppWrapperForSkipBuilding)	Generator)Pathzsphinx(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                >    t         D ]  }| j                  d|        y)zRegister custom markersmarkersN)DEFAULT_ENABLED_MARKERSaddinivalue_line)configmarkers     9/usr/lib/python3/dist-packages/sphinx/testing/fixtures.pypytest_configurer      s!    ) 3	623    session)scopec                      y N r   r   r   rootdirr   #   s    r   c                  ,    e Zd ZU i Zded<   ddZddZy)SharedResultzdict[str, dict[str, str]]cachec                    || j                   v ry |j                  j                         |j                  j                         d}|| j                   |<   y )Nstatuswarning)r   _statusgetvalue_warning)selfkeyapp_datas       r   storezSharedResult.store+   sG    $**ll++-}}--/
 

3r   c                z    || j                   vri S | j                   |   }t        |d         t        |d         dS )Nr!   r"   r    )r   r   )r&   r'   r)   s      r   restorezSharedResult.restore4   sB    djj Izz#tH~.Y0
 	
r   N)r'   strr(   r	   returnr   )r'   r-   r.   zdict[str, StringIO])__name__
__module____qualname__r   __annotations__r*   r,   r   r   r   r   r   (   s    ')E$)
r   r   _app_paramsc                   i }i }t        t        | j                  j                  d                  D ]?  }t	        |j
                        D ]
  \  }}	|	||<    |j                  |j                         A t        |j                               D cg c]  }||   	 }
}|d   rHd|v rd}t        j                  |      |d   |d<   |j                  |d         }|j                  |       |j                  dd      }||j                  d|      z  x|d<   }|r.|j                         s|d|z   z  }t!        j"                  ||       t%        |
|      S c c}w )zp
    Parameters that are specified by 'pytest.mark.sphinx' for
    sphinx.application.Sphinx initialization
    sphinxshared_resultsrcdirz:You can not specify shared_result and srcdir in same time.testrootrootztest-)reversedlistnodeiter_markers	enumerateargsupdatekwargssortedkeyspytest	Exceptionr,   popgetexistsshutilcopytreer3   )requesttest_paramsr6   sphinx_test_tempdirr   pargsrA   infoiar?   msgr,   r8   r7   testroot_paths                   r   
app_paramsrT   >   sV    EF gll77ABC #dii( 	DAqE!H	dkk"#
 %UZZ\23E!H3D3 ?#vNC""3''&7x''O(DEg zz*f-H 3fjj86T TTF8v v}}7X#56v.tV$$+ 4s   Ezargs,kwargsc                    | j                   j                  d      }|r|j                  ni }ddi}|j                  |       |d   r*t	        |d   t
              sd}t        j                  |      |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.
    rL   r6   Nz?You can only provide a string type of value for "shared_result")r<   get_closest_markerrA   r@   
isinstancer-   rD   rE   )rK   envrA   resultrR   s        r   rL   rL   l   sp     ,,
)
)-
8CSZZBFF MM&oz&2I3'OOs##Mr   c              #    K   |\  }} ||i |}| t        d|j                  dd             t        d|j                  j                         t        d|j                         t        d|j
                         t        dd|j                  j                         z          t        d	d|j                  j                         z          | d
   r|j                  | d
   |       yyw)z9
    Provides the 'sphinx.application.Sphinx' object
    z# testroot:r8   r9   z
# builder:z	# srcdir:z	# outdir:z	# status:
z
# warning:r6   N)
printrG   buildernamer7   outdirr#   r$   r%   r*   )rL   rT   make_appr6   r?   rA   r(   s          r   appra      s      LD&T$V$D
J	-J78	,))*	+t{{#	+t{{#	+tdll33556	,t}}55778?#K8$? $s   C$C&c                    | j                   S zJ
    Back-compatibility for testing with previous @with_app decorator
    )r#   ra   s    r   r!   r!      s    
 ;;r   c                    | j                   S rc   )r%   rd   s    r   r"   r"      s    
 <<r   c              #      K   g t         j                  dd } fd}| |t         j                  dd t              D ]  }|j                           yw)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.
    Nc                     t               t               }}|j                  d|       |j                  d|       t        | i |}j                  |       d   rt	        |      }|S )Nr!   r"   r6   )r   
setdefaultr	   appendr
   )r?   rA   r!   r"   r(   appsrL   s        r   makezmake_app.<locals>.make   sb    "*hj(F+)W-!4262D'6t<Dr   )syspathr:   cleanup)rL   monkeypatchsyspathrk   r(   rj   s   `    @r   r`   r`      sP      DhhqkG JCHHQK s   AAc                     t               S r   )r   r   r   r   r6   r6      s
    >r   moduleT)r   autousec                 @    t         j                  j                          y r   )r   r   clearr   r   r   _shared_result_cacherv      s    r   c                    t        | j                  dd      }	 |rt        j                  |dgd       y	 t        j                  d       y# t        $ r Y !w xY w)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OSErrorrD   skip)ra   rx   s     r   if_graphviz_foundr      s\     3::~r:LNNL$/E  KK12  s   A 	AAc                "    | j                         S )zTemporary directory.)getbasetemp)tmp_path_factorys    r   rM   rM      s     ''))r   c               #  Z  K   	 t        t        j                        } d t        t        j                        D ]&  }|| vst        j                  j                  |       ( y# t        t        j                        D ]&  }| vst        j                  j                  |       ( w xY w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;   rl   modulesrF   )
sysmodulesmodnames     r   rollback_sysmodulesr      s|     )#++&
CKK( 	)Gj((	)tCKK( 	)Gj((	)s'   B+A$  B+"B+$!B("B((B+)r.   z
str | None)rK   r   rL   dictr6   r   rM   r-   r   r-   r.   r3   )rK   r   r.   r   )
rL   r   rT   ztuple[dict, dict]r`   r   r6   r   r.   z$Generator[SphinxTestApp, None, None])ra   r	   r.   r   )rL   r   ro   r   r.   zGenerator[Callable, None, None])r.   r   )r.   None)ra   r	   r.   r   )r   r   r.   r   )'__doc__
__future__r   rI   r|   rl   collectionsr   ior   typingr   r   r   rD   sphinx.testing.utilr	   r
   collections.abcr   pathlibr   r   r   fixturer   r   rT   r3   rL   ra   r!   r"   r`   r6   rv   r   rM   r   r   r   r   <module>r      s   % "   
 "  / /  R)	` 7 3 i  !
 
, '%$''%25'%:E'% '%T 6  . @#@(L@ @(      2   h- . 3 3  i * !*
 ) )r   