B
    Wc                 @   sB  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	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	jddG dd dZee dddZeddddZeddeedddZeeedd d!Zeeeed"d#d$Z dS )%z>Support for providing temporary directories to test functions.    N)Path)Optional   )LOCK_TIMEOUT)make_numbered_dir)make_numbered_dir_with_cleanup)rm_rf)final)Config)check_ispytest)fixture)FixtureRequest)MonkeyPatchF)Zinitc               @   s   e Zd ZdZejee dZe Z	ejee dZ
dddee ee eddddZeddeed d	d
dZeedddZdeeedddZed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.
    )typeNF)	_ispytest)given_basetempbasetempr   returnc            C   s>   t | |d krd | _nttjt|| _|| _|| _d S )N)	r   _given_basetempr   ospathabspathstr_trace	_basetemp)selfr   tracer   r    r   -lib/python3.7/site-packages/_pytest/tmpdir.py__init__#   s    zTempPathFactory.__init__)configr   r   c            C   s"   t | | |jj|jdddS )zTCreate a factory according to pytest configuration.

        :meta private:
        ZtmpdirT)r   r   r   )r   Zoptionr   r   get)clsr!   r   r   r   r   from_config6   s
    
zTempPathFactory.from_config)basenamer   c             C   s8   t j|}|  |  j|  kr4t| d|S )Nz& is not a normalized and relative path)r   r   normpathgetbasetempresolveparent
ValueError)r   r%   r   r   r   _ensure_relative_to_basetempH   s    z,TempPathFactory._ensure_relative_to_basetempT)r%   numberedr   c             C   sL   |  |}|s*|  |}|jdd n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.
        i  )mode)rootprefixr-   mktemp)r+   r'   joinpathmkdirr   r   )r   r%   r,   pr   r   r   r0   N   s    
zTempPathFactory.mktemp)r   c             C   s^  | j dk	r| j S | jdk	rF| j}| r0t| |jdd | }ntjd}t	|p^t
  }t pnd}|d| }y|jddd W n, tk
r   |d	}|jddd Y nX tjd
kr$t }| }|dkr$|j|krtd| d|jd@ dkr$t||jd@  td|dtdd}|dk	sHt||| _ | d| |S )zReturn the base temporary directory, creating it if needed.

        :returns:
            The base temporary directory.
        Ni  )r-   ZPYTEST_DEBUG_TEMPROOTunknownz
pytest-of-T)r-   exist_okzpytest-of-unknownZwin32zThe temporary directory z: is not owned by the current user. Fix this and try again.?   r   izpytest-   )r/   r.   ZkeepZlock_timeoutr-   znew basetemp)r   r   existsr   r2   r(   r   environr"   r   tempfileZ
gettempdirget_userr1   OSErrorsysplatformgetuidstatst_uidst_modechmodr   r   AssertionErrorr   )r   r   Zfrom_envZtemprootuserZrootdirZuidZrootdir_statr   r   r   r'   f   sH    





zTempPathFactory.getbasetemp)N)T)__name__
__module____qualname____doc__attrZibr   r   r   r   r   boolr    classmethodr
   r$   r   r+   r0   r'   r   r   r   r   r      s   
r   )r   c           	   C   s.   yddl } |  S  ttfk
r(   dS X dS )zlReturn the current user name, or None if getuser() does not work
    in the current environment (see #1010).r   N)getpassZgetuserImportErrorKeyError)rN   r   r   r   r<      s
    r<   )r!   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.
    T)r   _tmp_path_factoryF)ZraisingN)r   Zadd_cleanupZundor   r$   setattr)r!   ZmprQ   r   r   r   pytest_configure   s    rS   Zsession)Zscope)requestr   c             C   s   | j jS )zGReturn a :class:`pytest.TempPathFactory` instance for the test session.)r!   rQ   )rT   r   r   r   tmp_path_factory   s    rU   )rT   factoryr   c             C   s4   | j j}tdd|}d}|d | }|j|ddS )Nz[\W]_   T)r,   )Znodenameresubr0   )rT   rV   rY   ZMAXVALr   r   r   _mk_tmp   s
    r\   )rT   rU   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.

    By default, a new base temporary directory is created each test session,
    and old bases are removed after 3 sessions, to aid in debugging. 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\   )rT   rU   r   r   r   tmp_path   s    r]   )!rJ   r   rZ   r>   r;   Zpathlibr   typingr   rK   r   r   r   r   Z_pytest.compatr	   Z_pytest.configr
   Z_pytest.deprecatedr   Z_pytest.fixturesr   r   Z_pytest.monkeypatchr   sr   r   r<   rS   rU   r\   r]   r   r   r   r   <module>   s6    