
    c"                         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
  G d dej                        Z G d dej                  ej                        Z G d	 d
ej                        Zd Zd Zy)z Common utilities used in testing    N)content)optionsc                   (     e Zd ZdZd Z fdZ xZS )DiveDirzaDive into given directory and return back on cleanup.

    :ivar path: The target directory.
    c                     || _         y N)path)selfr	   s     0/usr/lib/python3/dist-packages/pbr/tests/base.py__init__zDiveDir.__init__=   s	    	    c                     t         t        |           | j                  t        j
                  t	        j                                t	        j
                  | j                         y r   )superr   setUp
addCleanuposchdirgetcwdr	   )r
   	__class__s    r   r   zDiveDir.setUp@   s8    gt"$"))+.
r   __name__
__module____qualname____doc__r   r   __classcell__r   s   @r   r   r   7   s    
 r   r   c                   >     e Zd Z fdZd Zd Zd Zg ddfdZ xZS )BaseTestCasec                    t         t        |           t        j                  j                  dd      }	 t        |      }|dkD  r&| j                  t        j                  |d             t        j                  j                  d      t        j                  v rS| j                  t        j                  d            j                  }| j                  t        j                   d	|             t        j                  j                  d
      t        j                  v rS| j                  t        j                  d            j                  }| j                  t        j                   d|             | j                  t        j"                  d            | _        | j                  t        j&                                | j                  t        j(                                | j                  t        j"                                | j                  t        j*                  dd             | j                  t        j,                               j.                  | _        t        j.                  j3                  | j0                  d      | _        t7        j8                  t        j.                  j3                  t        j.                  j;                  t<              d      | j4                         | j?                  t        j@                  t        jB                                t        j@                  | j4                         | j?                  | jD                         tG        | dd      s| j                  t        j*                  d             t        j.                  j3                  | j4                  d      }tI        |d      5 }|jK                         }d d d         jL                  dd      }tI        |d      5 }|jO                  |       d d d        y y # t        $ r t        d       d}Y w xY w# 1 sw Y   axY w# 1 sw Y   y xY w)NOS_TEST_TIMEOUT   z=OS_TEST_TIMEOUT set to invalid value defaulting to no timeoutr   T)gentleOS_STDOUT_CAPTUREstdoutz
sys.stdoutOS_STDERR_CAPTUREstderrz
sys.stderrpbrPBR_VERSIONz0.0testpackagepreversionedz	setup.cfgrtzversion = 0.1.dev wt)(r   r   r   r   environgetint
ValueErrorprint
useFixturefixturesTimeoutr   TRUE_VALUESStringStreamstreamMonkeyPatch
FakeLoggerlog_fixtureTempHomeDirNestedTempfileEnvironmentVariableTempDirr	   temp_dirjoinpackage_dirshutilcopytreedirname__file__r   r   r   _discard_testpackagegetattropenreadreplacewrite)r
   test_timeoutr$   r&   setup_cfg_pathcfgr   r   s          r   r   zBaseTestCase.setUpH   s   lD')zz~~&7<	|,L !OOH,,\$GH::>>-.'2E2EE__X%:%:8%DELLFOOH00vFG::>>-.'2E2EE__X%:%:8%DELLFOOH00vFG??&( 	,,.///12++-. 	44]EJK(8(8(:;@@77<<}ERWW__X%>N((	*"))+.
!!"112 t^T2OOH88GHWW\\$*:*:KHNnd+ %s((*%%goo&:C@Gnd+ #s		'"# # 3K  	 . /L		P% %# #s)   P 9P*1P6P'&P'*P36P?c                     t        t        j                        D ]*  }|dk(  s|j                  d      st        j                  |= , y )Npbr_testpackagezpbr_testpackage.)listsysmodules
startswith)r
   ks     r   rG   z!BaseTestCase._discard_testpackage{   s=     ckk" 	#A&&LL!34KKN	#r   c                 *     | j                   d|fi |S )Nr'   _run_cmdr
   argskwargss      r   run_pbrzBaseTestCase.run_pbr   s    t}}UD3F33r   c                 L     | j                   t        j                  d|z   fi |S )N)zsetup.py)rY   rS   
executablerZ   s      r   	run_setupzBaseTestCase.run_setup   s#    t}}S^^]T-ALVLLr   TNc                     |xs | j                   }t        |gt        |      z   |      }|d   r|st        d|d   z        |S )af  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.
        )cwd   zCommand failed retcode=%s)rB   rY   rR   	Exception)r
   cmdr[   
allow_failrb   results         r   rY   zBaseTestCase._run_cmd   sN     %T%%3%$t*,#6!9Z7&)CDDr   )	r   r   r   r   rG   r]   r`   rY   r   r   s   @r   r   r   F   s'    1#f#4M "$$ r   r   c                   ,     e Zd ZdZ fdZ fdZ xZS )CapturedSubprocessa)  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                    t         t        |           || _        || _        || _        t        j                  | j
                  d<   t        j                  | j
                  d<   t        j                  | j
                  d<   y)zCreate 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&   stdinr$   N)r   ri   r   labelr[   r\   
subprocessPIPE)r
   rl   r[   r\   r   s       r   r   zCapturedSubprocess.__init__   s^     	 $02
	 *H)G *Hr   c                    t         t        |           t        j                  | j
                  i | j                  }|j                         \  }}|j                  dd      | _	        |j                  dd      | _
        | j                  | j                  dz   t        j                  | j                               | j                  | j                  dz   t        j                  | j                               |j                  | _        |j                  r2t!        d| j
                  d| j                  d|j                        | j#                  t$        | d       | j#                  t$        | d	       | j#                  t$        | d
       y )Nzutf-8rK   z-stdoutz-stderrzFailed process args=z	, kwargs=z, returncode=outerr
returncode)r   ri   r   rm   Popenr[   r\   communicatedecoderp   rq   	addDetailrl   r   text_contentrr   AssertionErrorr   delattr)r
   procrp   rq   r   s       r   r   zCapturedSubprocess.setUp   s    $-/:dkk:##%S::gy1::gy1tzzI-w/C/CDHH/MNtzzI-w/C/CDHH/MN//?? IIt{{DOO=> > 	u-u-|4r   r   r   s   @r   ri   ri      s    05 5r   ri   c                    t        ddj                  |       z         t        j                  | t        j                  t        j                  t        j                  |      }t        d |j                         D              }t        d       t        |d          t        d       t        |d          ||j                  fz   S )	zRun 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)
    z
Running %s )rk   r$   r&   rb   c              3   Z   K   | ]#  }|j                  d       j                          % yw)latin1N)ru   strip).0ss     r   	<genexpr>z_run_cmd.<locals>.<genexpr>   s"     H1AHHX&,,.Hs   )+zSTDOUT:r   zSTDERR:   )r2   rA   rm   rs   rn   tuplert   rr   )r[   rb   pstreamss       r   rY   rY      s     
,$
'(JOOJOOC	)A HHHG	)	'!*	)	'!*&&r   c                  X    t        g dd        t        g dd        t        g dd        y )N)gitconfig--globalz
user.emailexample@example.com)r   r   r   z	user.namezOpenStack Developer)r   r   r   zuser.signingkeyr   rX    r   r   _config_gitr      s2    J I 	 !%'r   )r   r   rC   rm   rS   r4   testresources	testtoolsr   r'   r   Fixturer   TestCaseResourcedTestCaser   ri   rY   r   r   r   r   <module>r      sr   P ' 	   
     h Q9%%}'F'F Qh)5)) )5X'&	'r   