B
    ܇\J                 @   s<  d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddl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 e	jG dd deZe	jG dd deZdd Zdd Zejdddd Zejdddd Zdd Zejdd Zejdd  Z dS )!zA support for providing temporary directories to test functions.      )absolute_import)division)print_functionN   )ensure_reset_dir)LOCK_TIMEOUT)make_numbered_dir)make_numbered_dir_with_cleanup)Path)MonkeyPatchc               @   sZ   e Zd ZdZejejdd dZe Z	ejddZ
edd Zdd
dZ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.c             C   s   t tjt| S )N)r
   ospathabspathsixZ	text_type)p r   -lib/python3.7/site-packages/_pytest/tmpdir.py<lambda>#   s    zTempPathFactory.<lambda>)Z	converterN)defaultc             C   s   | |j j|jddS )z7
        :param config: a pytest configuration
        tmpdir)Zgiven_basetemptrace)Zoptionbasetempr   get)clsconfigr   r   r   from_config)   s    zTempPathFactory.from_configTc             C   s<   |s|   |}|  nt|   |d}| d| |S )z2makes a temporary directory managed by the factory)rootprefixmktemp)getbasetempjoinpathmkdirr   _trace)selfbasenamenumberedr   r   r   r   r   2   s    
zTempPathFactory.mktempc             C   s   | j dkr| jdk	r,| j}t| | }nVtjd}t|pDt	  }t
 pTd}|d|}|jdd td|dtd	}|dk	st| | _ }| d
| |S | j S dS )z" return base temporary directory. NZPYTEST_DEBUG_TEMPROOTunknownzpytest-of-{}T)exist_okzpytest-   )r   r   ZkeepZlock_timeoutznew basetemp)	_basetemp_given_basetempr   resolver   environr   r
   tempfileZ
gettempdirget_userr!   formatr"   r	   r   AssertionErrorr#   )r$   r   Zfrom_envZtemprootuserZrootdirtr   r   r   r    <   s"    




zTempPathFactory.getbasetemp)T)__name__
__module____qualname____doc__attribZ
convertersZoptionalr+   r#   r*   classmethodr   r   r    r   r   r   r   r      s   	

r   c               @   s4   e Zd ZdZe ZdddZdddZdd	 Z	d
S )TempdirFactoryzs
    backward comptibility wrapper that implements
    :class:``py.path.local`` for :class:``TempPathFactory``
    r   c             C   s,   ddl m} tj|dd |  j||dS )a*   (deprecated) return temporary directory path with
            the given string as the trailing part.  It is usually
            better to use the 'tmpdir' function argument which
            provides an empty unique-per-test-invocation directory
            and is guaranteed to be empty.
        r   )PYTEST_ENSURETEMP   )
stacklevel)dir)Z
deprecatedr<   warningswarnr    Zensure)r$   stringr?   r<   r   r   r   
ensuretemp_   s    zTempdirFactory.ensuretempTc             C   s   t j| j|| S )zCreate a subdirectory of the base temporary directory and return it.
        If ``numbered``, ensure the directory is unique by adding a number
        prefix greater than any existing one.
        )pyr   local_tmppath_factoryr   r,   )r$   r%   r&   r   r   r   r   l   s    zTempdirFactory.mktempc             C   s   t j| j  S )z<backward compat wrapper for ``_tmppath_factory.getbasetemp``)rD   r   rE   rF   r    r,   )r$   r   r   r   r    s   s    zTempdirFactory.getbasetempN)r   )T)
r4   r5   r6   r7   r8   r9   rF   rC   r   r    r   r   r   r   r;   V   s
   

r;   c           	   C   s.   ddl } y|  S  ttfk
r(   dS X dS )zqReturn the current user name, or None if getuser() does not work
    in the current environment (see #1010).
    r   N)getpassZgetuserImportErrorKeyError)rG   r   r   r   r/   x   s
    r/   c             C   sb   t  }t| }t|}| j|j |j| d|dd |j| d|dd |jtd|j	dd dS )a  Create a TempdirFactory 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 tmpdir_factory session fixture.
    _tmp_path_factoryF)Zraising_tmpdirhandlerrC   N)
r   r   r   r;   Z_cleanupappendZundosetattrpytestrC   )r   ZmpZtmppath_handlerr3   r   r   r   pytest_configure   s    
rO   Zsession)Zscopec             C   s   | j jS )zSReturn a :class:`_pytest.tmpdir.TempdirFactory` instance for the test session.
    )r   rK   )requestr   r   r   tmpdir_factory   s    rQ   c             C   s   | j jS )zTReturn a :class:`_pytest.tmpdir.TempPathFactory` instance for the test session.
    )r   rJ   )rP   r   r   r   tmp_path_factory   s    rR   c             C   s4   | j j}tdd|}d}|d | }|j|ddS )Nz[\W]_   T)r&   )Znodenameresubr   )rP   factoryrU   ZMAXVALr   r   r   _mk_tmp   s
    rY   c             C   s   t j| S )a+  Return a temporary directory path object
    which is unique to each test function invocation,
    created as a sub directory of the base temporary
    directory.  The returned object is a `py.path.local`_
    path object.

    .. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html
    )rD   r   rE   )tmp_pathr   r   r   r      s    
r   c             C   s
   t | |S )a#  Return a temporary directory path object
    which is unique to each test function invocation,
    created as a sub directory of the base temporary
    directory.  The returned object is a :class:`pathlib.Path`
    object.

    .. note::

        in python < 3.6 this is a pathlib2.Path
    )rY   )rP   rR   r   r   r   rZ      s    rZ   )!r7   Z
__future__r   r   r   r   rV   r.   r@   r8   rD   r   rN   Zpathlibr   r   r   r	   r
   Z_pytest.monkeypatchr   sobjectr   r;   r/   rO   ZfixturerQ   rR   rY   r   rZ   r   r   r   r   <module>   s8   =!