o
    Zh-                  
   @   s\  d Z ddlZddlZddlmZ ddlZddlm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 dd	l	mZ dd
l	mZ ddl	mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl m!Z! ddl m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z* e*ee+e,f   Z-ed Z.eej/G dd  d Z0d!ee+ fd"d#Z1d$ed!dfd%d&Z2d'ed!dfd(d)Z3e!d*d+d,e"d!e0fd-d.Z4d,e"d/e0d!efd0d1Z5e!d,e"d.e0d!eeddf fd2d3Z6d4ee7ef fd5d6Z8ed7d7d8d9e&d!ede(e(f fd:d;Z9dS )<z>Support for providing temporary directories to test functions.    N)Path)rmtree)Any)Dict)final)	Generator)Literal)Optional)Union   )cleanup_dead_symlinks)LOCK_TIMEOUT)make_numbered_dir)make_numbered_dir_with_cleanup)rm_rf)get_user_id)Config)ExitCode)hookimpl)Parser)check_ispytest)fixture)FixtureRequest)MonkeyPatch)Item)
TestReport)StashKeyallfailednonec                   @   s   e Zd ZU dZee ed< eed< ee ed< eed< e	ed< 	ddd	d
ee dede	dee de
ddfddZedd	dede
dd fddZdedefddZddede
defddZdefddZdS ) TempPathFactoryzFactory for temporary directories under the common base temp directory.

    The base directory can be configured using the ``--basetemp`` option.
    _given_basetemp_trace	_basetemp_retention_count_retention_policyNF	_ispytestgiven_basetempretention_countretention_policybasetempr(   returnc                C   sJ   t | |d u rd | _nttjt|| _|| _|| _|| _	|| _
d S N)r   r"   r   ospathabspathstrr#   r%   r&   r$   )selfr)   r*   r+   tracer,   r(    r5   E/var/www/html/lang_env/lib/python3.10/site-packages/_pytest/tmpdir.py__init__7   s   

zTempPathFactory.__init__configc                C   sn   t | t|d}|dk rtd| d|d}|dvr(td| d| |jj|jd||d	d
S )zTCreate a factory according to pytest configuration.

        :meta private:
        tmp_path_retention_countr   z6tmp_path_retention_count must be >= 0. Current input: .tmp_path_retention_policyr   zKtmp_path_retention_policy must be either all, failed, none. Current input: ZtmpdirT)r)   r4   r*   r+   r(   )r   intZgetini
ValueErroroptionr,   r4   get)clsr8   r(   countpolicyr5   r5   r6   from_configN   s$   



zTempPathFactory.from_configbasenamec                 C   s8   t j|}|  |  j|  krt| d|S )Nz& is not a normalized and relative path)r/   r0   normpathgetbasetempresolveparentr=   )r3   rD   r5   r5   r6   _ensure_relative_to_basetempn   s   z,TempPathFactory._ensure_relative_to_basetempTnumberedc                 C   sN   |  |}|s|  |}|jdd |S t|  |dd}| d| |S )a  Create a new temporary directory managed by the factory.

        :param basename:
            Directory base name, must be a relative path.

        :param numbered:
            If ``True``, ensure the directory is unique by adding a numbered
            suffix greater than any existing one: ``basename="foo-"`` and ``numbered=True``
            means that this function will create directories named ``"foo-0"``,
            ``"foo-1"``, ``"foo-2"`` and so on.

        :returns:
            The path to the new directory.
          mode)rootprefixrM   mktemp)rI   rF   joinpathmkdirr   r#   )r3   rD   rJ   pr5   r5   r6   rP   t   s   
zTempPathFactory.mktempc           	      C   sZ  | j dur| j S | jdur#| j}| rt| |jdd | }nwtjd}t	|p/t
  }t p7d}|d| }z	|jddd W n ty^   |d	}|jddd Y nw t }|dur| }|j|krwtd
| d|jd@ dkrt||jd@  | j}| jdkrd}td||tdd}|dusJ ||| _ | d| |S )zReturn the base temporary directory, creating it if needed.

        :returns:
            The base temporary directory.
        NrK   rL   ZPYTEST_DEBUG_TEMPROOTunknownz
pytest-of-T)rM   exist_okzpytest-of-unknownzThe temporary directory z: is not owned by the current user. Fix this and try again.?   r   ir    zpytest-)rO   rN   keepZlock_timeoutrM   znew basetemp)r$   r"   existsr   rR   rG   r/   environr?   r   tempfile
gettempdirget_userrQ   OSErrorr   statst_uidst_modechmodr%   r&   r   r   r#   )	r3   r,   Zfrom_envZtemprootuserrootdiruidZrootdir_statrW   r5   r5   r6   rF      sR   








zTempPathFactory.getbasetempr.   )T)__name__
__module____qualname____doc__r	   r   __annotations__r   r<   RetentionTypeboolr7   classmethodr   rC   r2   rI   rP   rF   r5   r5   r5   r6   r!   (   sF   
 	
r!   r-   c               
   C   s.   z	ddl } |  W S  tttfy   Y dS w )zlReturn the current user name, or None if getuser() does not work
    in the current environment (see #1010).r   N)getpassgetuserImportErrorr]   KeyError)rm   r5   r5   r6   r\      s   
r\   r8   c                 C   s6   t  }| |j tj| dd}|j| d|dd dS )a  Create a TempPathFactory and attach it to the config object.

    This is to comply with existing plugins which expect the handler to be
    available at pytest_configure time, but ideally should be moved entirely
    to the tmp_path_factory session fixture.
    Tr'   _tmp_path_factoryF)ZraisingN)r   Zadd_cleanupZundor!   rC   setattr)r8   mprq   r5   r5   r6   pytest_configure   s   rt   parserc                 C   s$   | j dddd | j dddd d S )Nr9   zfHow many sessions should we keep the `tmp_path` directories, according to `tmp_path_retention_policy`.   )helpdefaultr;   zvControls which directories created by the `tmp_path` fixture are kept around, based on test outcome. (all/failed/none)r   )Zaddini)ru   r5   r5   r6   pytest_addoption   s   
ry   session)scoperequestc                 C   s   | j jS )zGReturn a :class:`pytest.TempPathFactory` instance for the test session.)r8   rq   )r|   r5   r5   r6   tmp_path_factory   s   r}   factoryc                 C   s4   | j j}tdd|}d}|d | }|j|ddS )Nz[\W]_   T)rJ   )nodenameresubrP   )r|   r~   r   ZMAXVALr5   r5   r6   _mk_tmp   s
   r   c                 c   s\    t | |}|V  | jjj}|j}| jjt }|dkr'|ddr't	|dd | jjt= dS )ag  Return a temporary directory path object which is unique to each test
    function invocation, created as a sub directory of the base temporary
    directory.

    By default, a new base temporary directory is created each test session,
    and old bases are removed after 3 sessions, to aid in debugging.
    This behavior can be configured with :confval:`tmp_path_retention_count` and
    :confval:`tmp_path_retention_policy`.
    If ``--basetemp`` is used then it is cleared each session. See
    :ref:`temporary directory location and retention`.

    The returned object is a :class:`pathlib.Path` object.
    r   callTignore_errorsN)
r   rz   r8   rq   r&   r   stashtmppath_result_keyr?   r   )r|   r}   r0   rB   Zresult_dictr5   r5   r6   tmp_path   s   

r   
exitstatusc                 C   sf   | j j}|j}|du rdS |j}|dkr'|dkr'|jdu r'| r't|dd | r1t| dS dS )zAfter each session, remove base directory if all the tests passed,
    the policy is "failed", and the basetemp is not specified by a user.
    Nr   r   Tr   )r8   rq   r$   r&   r"   is_dirr   r   )rz   r   r}   r,   rB   r5   r5   r6   pytest_sessionfinish   s   
r   T)wrapperZtryfirstitemc                 c   s4    d V }|j d usJ i }|j| jt||j < |S r.   )whenZpassedr   
setdefaultr   )r   r   repemptyr5   r5   r6   pytest_runtest_makereport9  s   r   ):rh   dataclassesr/   pathlibr   r   shutilr   rZ   typingr   r   r   r   r   r	   r
   r   r   r   r   r   Z_pytest.compatr   Z_pytest.configr   r   r   Z_pytest.config.argparsingr   Z_pytest.deprecatedr   Z_pytest.fixturesr   r   Z_pytest.monkeypatchr   Z_pytest.nodesr   Z_pytest.reportsr   Z_pytest.stashr   r2   rk   r   rj   	dataclassr!   r\   rt   ry   r}   r   r   r<   r   r   r5   r5   r5   r6   <module>   st     
