ó
£„öUc           @   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   (    (    s2   /usr/lib/python2.7/dist-packages/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   (    (    s2   /usr/lib/python2.7/dist-packages/pbr/tests/base.pyR   @   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s2   /usr/lib/python2.7/dist-packages/pbr/tests/base.pyR   7   s   	t   BaseTestCasec           B   s5   e  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    (    (    s2   /usr/lib/python2.7/dist-packages/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(    (    s2   /usr/lib/python2.7/dist-packages/pbr/tests/base.pyR5   {   s    c         O   s   |  j  t j d | |  S(   Ns   setup.py(   s   setup.py(   t   _run_cmdR@   t
   executable(   R   t   argst   kwargs(    (    s2   /usr/lib/python2.7/dist-packages/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   cmdRF   t
   allow_failRI   t   result(    (    s2   /usr/lib/python2.7/dist-packages/pbr/tests/base.pyRD   †   s
    
N(   R   R   R   R5   RH   R#   t   NoneRD   (    (    (    s2   /usr/lib/python2.7/dist-packages/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   RO   R   t   labelRF   RG   t
   subprocesst   PIPE(   R   RQ   RF   RG   (    (    s2   /usr/lib/python2.7/dist-packages/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   RO   R   RR   t   PopenRF   RG   t   communicatet   decodeRT   RU   t	   addDetailRQ   R    t   text_contentRV   t   AssertionErrorR   t   delattr(   R   t   procRT   RU   (    (    s2   /usr/lib/python2.7/dist-packages/pbr/tests/base.pyR   ±   s    ##	(   R   R   R   R   R   (    (    (    s2   /usr/lib/python2.7/dist-packages/pbr/tests/base.pyRO   —   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)
    RP   R   R   RI   c         s   s$   |  ] } | j  d  ƒ j ƒ  Vq d S(   t   latin1N(   RY   t   strip(   t   .0t   s(    (    s2   /usr/lib/python2.7/dist-packages/pbr/tests/base.pys	   <genexpr>Ë   s    (   RR   RW   RS   t   tupleRX   RV   (   RF   RI   t   pt   streamst   stream_content(    (    s2   /usr/lib/python2.7/dist-packages/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   RN   (    (    (    s2   /usr/lib/python2.7/dist-packages/pbr/tests/base.pyt   _config_gitÑ   s    (   R   R	   R1   RR   R@   R!   t   testresourcest	   testtoolsR    R   R   t   FixtureR   t   TestCaset   ResourcedTestCaseR   RO   RD   Ri   (    (    (    s2   /usr/lib/python2.7/dist-packages/pbr/tests/base.pyt   <module>(   s   Q*	