ó
X¾÷Xc           @   sá   d  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 e j f d „  ƒ  YZ d e j e j f d „  ƒ  YZ d	 e j f d
 „  ƒ  YZ d „  Z d „  Z d S(   s    Common utilities used in testingiÿÿÿÿN(   t   content(   t   optionst   DiveDirc           B   s    e  Z d  Z d „  Z d „  Z RS(   sa   Dive into given directory and return back on cleanup.

    :ivar path: The target directory.
    c         C   s   | |  _  d  S(   N(   t   path(   t   selfR   (    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyt   __init__=   s    c         C   s@   t  t |  ƒ j ƒ  |  j t j t j ƒ  ƒ t j |  j ƒ d  S(   N(   t   superR   t   setUpt
   addCleanupt   ost   chdirt   getcwdR   (   R   (    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyR   @   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyR   7   s   	t   BaseTestCasec           B   s>   e  Z d  „  Z d „  Z d „  Z d „  Z g  e d d „ Z RS(   c         C   sÒ  t  t |  ƒ j ƒ  t j j d d ƒ } y t | ƒ } Wn t k
 rV d GHd } n X| d k r‚ |  j t	 j
 | d t ƒƒ n  t j j d ƒ t j k rÔ |  j t	 j d ƒ ƒ j } |  j t	 j d | ƒ ƒ n  t j j d	 ƒ t j k r&|  j t	 j d
 ƒ ƒ j } |  j t	 j d | ƒ ƒ n  |  j t	 j d ƒ ƒ |  _ |  j t	 j ƒ  ƒ |  j t	 j ƒ  ƒ |  j t	 j ƒ  ƒ |  j t	 j d d ƒ ƒ |  j t	 j ƒ  ƒ j |  _ t j j |  j d ƒ |  _ t j t j j t j j t ƒ d ƒ |  j ƒ |  j t j  t j! ƒ  ƒ t j  |  j ƒ |  j |  j" ƒ t# |  d t ƒ sÎ|  j t	 j d ƒ ƒ t j j |  j d ƒ } t$ | d ƒ  } | j% ƒ  } Wd  QX| j& d d ƒ } t$ | d ƒ  } | j' | ƒ Wd  QXn  d  S(   Nt   OS_TEST_TIMEOUTi   s=   OS_TEST_TIMEOUT set to invalid value defaulting to no timeouti    t   gentlet   OS_STDOUT_CAPTUREt   stdouts
   sys.stdoutt   OS_STDERR_CAPTUREt   stderrs
   sys.stderrt   pbrt   PBR_VERSIONs   0.0t   testpackaget   preversioneds	   setup.cfgt   rtu   version = 0.1.devu    t   wt((   R   R   R   R	   t   environt   gett   intt
   ValueErrort
   useFixturet   fixturest   Timeoutt   TrueR   t   TRUE_VALUESt   StringStreamt   streamt   MonkeyPatcht
   FakeLoggert   log_fixturet   TempHomeDirt   NestedTempfilet   EnvironmentVariablet   TempDirR   t   temp_dirt   joint   package_dirt   shutilt   copytreet   dirnamet   __file__R   R
   R   t   _discard_testpackaget   getattrt   opent   readt   replacet   write(   R   t   test_timeoutR   R   t   setup_cfg_patht   cfgR    (    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyR   H   sH    
$
c         C   sF   x? t  t j ƒ D]. } | d k s1 | j d ƒ r t j | =q q Wd  S(   Nt   pbr_testpackages   pbr_testpackage.(   t   listt   syst   modulest
   startswith(   R   t   k(    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyR5   {   s    c         O   s   |  j  d | |  S(   NR   (   t   _run_cmd(   R   t   argst   kwargs(    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyt   run_pbrƒ   s    c         O   s   |  j  t j d | |  S(   Ns   setup.py(   s   setup.py(   RD   R@   t
   executable(   R   RE   RF   (    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyt	   run_setup†   s    c         C   sZ   | p |  j  } t | g t | ƒ d | ƒ} | d rV | rV t d | d ƒ ‚ n  | S(   sf  Run a command in the root of the test working copy.

        Runs a command, with the given argument list, in the root of the test
        working copy--returns the stdout and stderr streams and the exit code
        from the subprocess.

        :param cwd: If falsy run within the test package dir, otherwise run
            within the named path.
        t   cwdi   s   Command failed retcode=%s(   R0   RD   R?   t	   Exception(   R   t   cmdRE   t
   allow_failRJ   t   result(    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyRD   ‰   s
    
N(	   R   R   R   R5   RG   RI   R#   t   NoneRD   (    (    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyR   F   s
   	3			t   CapturedSubprocessc           B   s    e  Z d  Z d „  Z d „  Z RS(   s)  Run a process and capture its output.

    :attr stdout: The output (a string).
    :attr stderr: The standard error (a string).
    :attr returncode: The return code of the process.

    Note that stdout and stderr are decoded from the bytestrings subprocess
    returns using error=replace.
    c         O   sb   t  t |  ƒ j ƒ  | |  _ | |  _ | |  _ t j |  j d <t j |  j d <t j |  j d <d S(   sÞ   Create a CapturedSubprocess.

        :param label: A label for the subprocess in the test log. E.g. 'foo'.
        :param *args: The *args to pass to Popen.
        :param **kwargs: The **kwargs to pass to Popen.
        R   t   stdinR   N(   R   RP   R   t   labelRE   RF   t
   subprocesst   PIPE(   R   RR   RE   RF   (    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyR   ¥   s    			c         C   s  t  t |  ƒ j ƒ  t j |  j |  j Ž  } | j ƒ  \ } } | j d d ƒ |  _	 | j d d ƒ |  _
 |  j |  j d t j |  j	 ƒ ƒ |  j |  j d t j |  j
 ƒ ƒ | j |  _ | j rØ t d | j ƒ ‚ n  |  j t |  d ƒ |  j t |  d ƒ |  j t |  d ƒ d  S(	   Ns   utf-8R9   s   -stdouts   -stderrs   Failed process %st   outt   errt
   returncode(   R   RP   R   RS   t   PopenRE   RF   t   communicatet   decodeRU   RV   t	   addDetailRR   R    t   text_contentRW   t   AssertionErrorR   t   delattr(   R   t   procRU   RV   (    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyR   ´   s    ##	(   R   R   R   R   R   (    (    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyRP   š   s   		c      
   C   sp   t  j |  d t  j d t  j d t  j d | ƒ} t d „  | j ƒ  Dƒ ƒ } x | D] } | GHqS W| | j f S(   s¾   Run the command args in cwd.

    :param args: The command to run e.g. ['git', 'status']
    :param cwd: The directory to run the comamnd in.
    :return: ((stdout, stderr), returncode)
    RQ   R   R   RJ   c         s   s$   |  ] } | j  d  ƒ j ƒ  Vq d S(   t   latin1N(   RZ   t   strip(   t   .0t   s(    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pys	   <genexpr>Î   s    (   RS   RX   RT   t   tupleRY   RW   (   RE   RJ   t   pt   streamst   stream_content(    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyRD   Ä   s    	c           C   sX   t  d d d d d g d  ƒ t  d d d d d g d  ƒ t  d d d d d g d  ƒ d  S(	   Nt   gitt   configs   --globals
   user.emails   example@example.coms	   user.names   OpenStack Developers   user.signingkey(   RD   RO   (    (    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyt   _config_gitÔ   s    (   R   R	   R1   RS   R@   R!   t   testresourcest	   testtoolsR    R   R   t   FixtureR   t   TestCaset   ResourcedTestCaseR   RP   RD   Rj   (    (    (    s+   /tmp/pip-build-h1VYrz/pbr/pbr/tests/base.pyt   <module>(   s   T*	