o
    Ú­j¼-  ã                
   @   sV  d Z ddl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	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r`ddlmZ ed 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!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  ƒ Z0e!ej1G dd „ d ƒƒƒZ2d!ee. fd"d#„Z3d$e$d!dfd%d&„Z4d'ed!dfd(d)„Z5e*d*d+�d,e+d!e2fd-d.„ƒZ6d,e+d/e2d!efd0d1„Z7e*d,e+d.e2d!eeddf fd2d3„ƒZ8d4ee9e%f fd5d6„Z:e&d7d7d8�d9efd:d;„ƒZ;dS )<z>Support for providing temporary directories to test functions.é    N)ÚPath)Úrmtree)ÚAny)ÚDict)Ú	Generator)ÚOptional)ÚTYPE_CHECKING)ÚUnion)ÚItem)ÚCollectReport)ÚStashKey)ÚLiteral©ÚallÚfailedÚnone)ÚParseré   )ÚLOCK_TIMEOUT)Úmake_numbered_dir)Úmake_numbered_dir_with_cleanup)Úrm_rf)Úcleanup_dead_symlinks)ÚfinalÚget_user_id)ÚConfig)ÚExitCode)Úhookimpl)Úcheck_ispytest)Úfixture)ÚFixtureRequest)ÚMonkeyPatchc                   @   sÎ   e Zd ZU dZee ed< eed< ee ed< eed< ded< 	dd	d
œdee dedd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 )!ÚTempPathFactoryz—Factory 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ÚRetentionTypeÚ_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*   © r7   úK/var/www/html/CropPilot/venv/lib/python3.10/site-packages/_pytest/tmpdir.pyÚ__init__;   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: ÚtmpdirT)r+   r6   r,   r-   r*   )r   ÚintÚgetiniÚ
ValueErrorÚoptionr.   r6   Úget)Úclsr:   r*   ÚcountÚpolicyr7   r7   r8   Úfrom_configR   s$   
ÿ

ÿ
ûzTempPathFactory.from_configÚbasenamec                 C   s8   t j |¡}|  ¡ |  ¡ j|  ¡ krt|› d�ƒ‚|S )Nz& is not a normalized and relative path)r1   r2   ÚnormpathÚgetbasetempÚresolveÚparentrA   )r5   rH   r7   r7   r8   Ú_ensure_relative_to_basetempr   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ÚprefixrQ   Úmktemp)rM   rJ   ÚjoinpathÚmkdirr   r$   )r5   rH   rN   Úpr7   r7   r8   rT   x   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r‡t ||jd@ ¡ | j}| jdkr‘d}td||tdd�}|dus¢J |ƒ‚|| _ |  d|¡ |S )z�Return the base temporary directory, creating it if needed.

        :returns:
            The base temporary directory.
        NrO   rP   ÚPYTEST_DEBUG_TEMPROOTÚunknownz
pytest-of-T)rQ   Úexist_okzpytest-of-unknownzThe temporary directory z: is not owned by the current user. Fix this and try again.é?   r   iÀÿÿÿr   zpytest-)rS   rR   ÚkeepÚlock_timeoutrQ   znew basetemp)r%   r#   Úexistsr   rV   rK   r1   ÚenvironrC   r   ÚtempfileÚ
gettempdirÚget_userrU   ÚOSErrorr   ÚstatÚst_uidÚst_modeÚchmodr&   r(   r   r   r$   )	r5   r.   Úfrom_envÚtemprootÚuserÚrootdirÚuidÚrootdir_statr\   r7   r7   r8   rJ   �   sR   




ý


ÿ
ûzTempPathFactory.getbasetempr0   )T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__annotations__r   r?   Úboolr9   Úclassmethodr   rG   r4   rM   rT   rJ   r7   r7   r7   r8   r"   ,   sF   
 úøþýüúø	
÷üþüûr"   r/   c               	   C   s,   z	ddl } |  ¡ W S  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ÚImportErrorÚKeyError)ru   r7   r7   r8   rb   Ì   s   
ÿrb   r:   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)ÚraisingN)r!   Úadd_cleanupÚundor"   rG   Úsetattr)r:   Úmpry   r7   r7   r8   Úpytest_configureØ   s   r   Úparserc                 C   s$   | j dddd� | j dddd� d S )Nr;   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   )Úaddini)r€   r7   r7   r8   Úpytest_addoptionå   s   ý
ür…   Úsession)ÚscopeÚrequestc                 C   s   | j jS )zGReturn a :class:`pytest.TempPathFactory` instance for the test session.)r:   ry   )rˆ   r7   r7   r8   Útmp_path_factoryô   s   r‰   Úfactoryc                 C   s4   | j j}t dd|¡}d}|d |… }|j|dd�S )Nz[\W]Ú_é   T)rN   )ÚnodeÚnameÚreÚsubrT   )rˆ   rŠ   rŽ   ÚMAXVALr7   r7   r8   Ú_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 )aU  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:`base
    temporary directory`.

    The returned object is a :class:`pathlib.Path` object.
    r   ÚcallT©Úignore_errorsN)
r’   r†   r:   ry   r(   r�   ÚstashÚtmppath_result_keyrC   r   )rˆ   r‰   r2   rF   Úresult_dictr7   r7   r8   Ú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 )z�After 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”   )r:   ry   r%   r(   r#   Úis_dirr   r   )r†   rš   r‰   r.   rF   r7   r7   r8   Úpytest_sessionfinish%  s   
ÿrœ   T)ÚtryfirstÚhookwrapperÚitemc                 c   s.   � d V }|  ¡ }i }|j| j t|¡|j< d S r0   )Ú
get_resultÚpassedr–   Ú
setdefaultr—   Úwhen)rŸ   r“   ÚoutcomeÚresultÚemptyr7   r7   r8   Úpytest_runtest_makereport>  s
   €r§   )<rq   Údataclassesr1   r�   r`   Úpathlibr   Úshutilr   Útypingr   r   r   r   r   r	   Ú_pytest.nodesr
   Ú_pytest.reportsr   Ú_pytest.stashr   Útyping_extensionsr   r'   Ú_pytest.config.argparsingr   r   r   r   r   r   Ú_pytest.compatr   r   Ú_pytest.configr   r   r   Ú_pytest.deprecatedr   Ú_pytest.fixturesr   r    Ú_pytest.monkeypatchr!   r4   rs   r—   Ú	dataclassr"   rb   r   r…   r‰   r’   r™   r?   rœ   r§   r7   r7   r7   r8   Ú<module>   sn     ÿÿþ!
