ó
[³XMc           @   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 m Z d d l m	 Z	 d d l
 m Z m Z m Z m Z d e	 j f d „  ƒ  YZ d e	 j f d	 „  ƒ  YZ d S(
   sD   
Test running processes with the APIs in L{twisted.internet.utils}.
iÿÿÿÿN(   t   platform(   t   unittest(   t   errort   reactort   utilst
   interfacest   ProcessUtilsTestsc           B   sã   e  Z d  Z e j e d ƒ d k r- d Z n  d Z d Z	 e
 j Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e j ƒ  r™ d	 e _ n  d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   st   
    Test running a process using L{getProcessOutput}, L{getProcessValue}, and
    L{getProcessOutputAndValue}.
    s)   reactor doesn't implement IReactorProcessc         C   sU   |  j  ƒ  } t | d ƒ } | j t j j | ƒ t j ƒ | j ƒ  t j j | ƒ S(   sj   
        Write the given list of lines to a text file and return the absolute
        path to it.
        t   wt(	   t   mktempt   filet   writet   ost   linesept   joint   closet   patht   abspath(   t   selft   sourceLinest   scriptt
   scriptFile(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   makeSourceFile   s
     
c         C   sI   |  j  d d d d g ƒ } t j |  j d | g ƒ } | j |  j d ƒ S(   s™   
        L{getProcessOutput} returns a L{Deferred} which fires with the complete
        output of the process it runs after that process exits.
        s
   import syss   for s in 'hello world\n':s       sys.stdout.write(s)s       sys.stdout.flush()s   -us   hello world
(   R   R   t   getProcessOutputt   exet   addCallbackt   assertEquals(   R   R   t   d(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   test_output(   s    c            sb   ˆ  j  d d g ƒ } t j ˆ  j d | g ƒ } ˆ  j | t ƒ } ‡  f d †  } | j | ƒ | S(   s—   
        The L{Deferred} returned by L{getProcessOutput} is fired with an
        L{IOError} L{Failure} if the child process writes to stderr.
        s
   import syss!   sys.stderr.write("hello world\n")s   -uc            s   ˆ  j  |  j t j ƒ S(   N(   t   assertFailuret   processEndedR   t   ProcessDone(   t   err(   R   (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   cbFailedC   s    (   R   R   R   R   R   t   IOErrorR   (   R   R   R   R    (    (   R   s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   test_outputWithErrorIgnored6   s    c         C   sR   |  j  d d d d d g ƒ } t j |  j d | g d t ƒ} | j |  j d ƒ S(	   sÒ   
        If a C{True} value is supplied for the C{errortoo} parameter to
        L{getProcessOutput}, the returned L{Deferred} fires with the child's
        stderr output as well as its stdout output.
        s
   import syss   sys.stdout.write("foo")s   sys.stdout.flush()s   sys.stderr.write("foo")s   sys.stderr.flush()s   -ut   errortoot   foofoo(   R   R   R   R   t   TrueR   t   assertEqual(   R   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   test_outputWithErrorCollectedI   s    !c         C   s@   |  j  d g ƒ } t j |  j d | g ƒ } | j |  j d ƒ S(   s|   
        The L{Deferred} returned by L{getProcessValue} is fired with the exit
        status of the child process.
        s   raise SystemExit(1)s   -ui   (   R   R   t   getProcessValueR   R   R&   (   R   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt
   test_value\   s    c            s[   t  j } ˆ  j d d d d g ƒ } ‡  f d †  } t j ˆ  j d | g ƒ } | j | ƒ S(   s  
        The L{Deferred} returned by L{getProcessOutputAndValue} fires with a
        three-tuple, the elements of which give the data written to the child's
        stdout, the data written to the child's stderr, and the exit status of
        the child.
        s
   import syss"   sys.stdout.write('hello world!\n')s$   sys.stderr.write('goodbye world!\n')s   sys.exit(1)c            sJ   |  \ } } } ˆ  j  | d ƒ ˆ  j  | d t j ƒ ˆ  j  | d ƒ d  S(   Ns   hello world!
s   goodbye world!i   (   R   R   R   (   t   .0t   outR   t   code(   R   (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   gotOutputAndValuev   s    s   -u(   t   syst
   executableR   R   t   getProcessOutputAndValueR   R   (   R   R   R   R-   R   (    (   R   s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   test_outputAndValueg   s    	c            sj   ˆ  j  d d d d d d g ƒ } ‡  f d †  } t j ˆ  j d | g ƒ } ˆ  j | t ƒ } | j | ƒ S(	   s  
        If the child process exits because of a signal, the L{Deferred}
        returned by L{getProcessOutputAndValue} fires a L{Failure} of a tuple
        containing the the child's stdout, stderr, and the signal which caused
        it to exit.
        s   import sys, os, signals"   sys.stdout.write('stdout bytes\n')s"   sys.stderr.write('stderr bytes\n')s   sys.stdout.flush()s   sys.stderr.flush()s$   os.kill(os.getpid(), signal.SIGKILL)c            sF   |  \ } } } ˆ  j  | d ƒ ˆ  j  | d ƒ ˆ  j  | t j ƒ d  S(   Ns   stdout bytes
s   stderr bytes
(   R   t   signalt   SIGKILL(   R*   R+   R   t   sig(   R   (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyR-      s    s   -u(   R   R   R0   R   R   t   tupleR   (   R   R   R-   R   (    (   R   s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   test_outputSignal~   s    
s"   Windows doesn't have real signals.c         C   sl   t  j j |  j ƒ  ƒ } t  j | ƒ |  j d d g ƒ } | |  j d | g d | ƒ} | j | | ƒ | S(   Ns   import os, syss   sys.stdout.write(os.getcwd())s   -uR   (   R   R   R   R   t   makedirsR   R   R   (   R   t   utilFunct   checkt   dirR   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt	   _pathTest   s    c         C   s   |  j  t j |  j ƒ S(   s   
        L{getProcessOutput} runs the given command with the working directory
        given by the C{path} parameter.
        (   R;   R   R   R&   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   test_getProcessOutputPath¨   s    c            s"   ‡  f d †  } ˆ  j  t j | ƒ S(   s~   
        L{getProcessValue} runs the given command with the working directory
        given by the C{path} parameter.
        c            s   ˆ  j  |  d ƒ d  S(   Ni    (   R&   (   t   resultt   ignored(   R   (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyR9   µ   s    (   R;   R   R(   (   R   R9   (    (   R   s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   test_getProcessValuePath°   s    c            s"   ‡  f d †  } ˆ  j  t j | ƒ S(   s‡   
        L{getProcessOutputAndValue} runs the given command with the working
        directory given by the C{path} parameter.
        c            s3   |  \ } } } ˆ  j  | | ƒ ˆ  j  | d ƒ d  S(   Ni    (   R&   (   R*   R:   R+   R   t   status(   R   (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyR9   ¿   s    (   R;   R   R0   (   R   R9   (    (   R   s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt!   test_getProcessOutputAndValuePathº   s    c         C   sÑ   t  j j |  j ƒ  ƒ } t  j | ƒ |  j d d | f d g ƒ } |  j t  j t  j ƒ  ƒ t  j | ƒ |  j t  j	 | t
 j t  j
 d ƒ j ƒ ƒ t  j	 | d ƒ | |  j d | g ƒ } | j | | ƒ | S(   Ns   import os, sys, stats   os.chmod(%r, stat.S_IXUSR)s   sys.stdout.write(os.getcwd())t   .i    s   -u(   R   R   R   R   R7   R   t
   addCleanupt   chdirt   getcwdt   chmodt   statt   S_IMODEt   st_modeR   R   (   R   R8   R9   R:   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   _defaultPathTestÅ   s    
%c         C   s   |  j  t j |  j ƒ S(   s  
        If no value is supplied for the C{path} parameter, L{getProcessOutput}
        runs the given command in the same working directory as the parent
        process and succeeds even if the current working directory is not
        accessible.
        (   RJ   R   R   R&   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt    test_getProcessOutputDefaultPathâ   s    c            s"   ‡  f d †  } ˆ  j  t j | ƒ S(   s   
        If no value is supplied for the C{path} parameter, L{getProcessValue}
        runs the given command in the same working directory as the parent
        process and succeeds even if the current working directory is not
        accessible.
        c            s   ˆ  j  |  d ƒ d  S(   Ni    (   R&   (   R=   R>   (   R   (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyR9   ó   s    (   RJ   R   R(   (   R   R9   (    (   R   s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   test_getProcessValueDefaultPathì   s    c            s"   ‡  f d †  } ˆ  j  t j | ƒ S(   s	  
        If no value is supplied for the C{path} parameter,
        L{getProcessOutputAndValue} runs the given command in the same working
        directory as the parent process and succeeds even if the current
        working directory is not accessible.
        c            s3   |  \ } } } ˆ  j  | | ƒ ˆ  j  | d ƒ d  S(   Ni    (   R&   (   R*   R:   R+   R   R@   (   R   (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyR9   ÿ   s    (   RJ   R   R0   (   R   R9   (    (   R   s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt(   test_getProcessOutputAndValueDefaultPathø   s    N(   t   __name__t
   __module__t   __doc__R   t   IReactorProcessR   t   Nonet   skipt   outputt   valueR.   R/   R   R   R   R"   R'   R)   R1   R6   R    t	   isWindowsR;   R<   R?   RA   RJ   RK   RL   RM   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyR      s.   												
			
	t   WarningSuppressionc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s%   g  |  _  t  j |  _ |  j t  _ d  S(   N(   t   warningst   showwarningt   originalshow(   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   setUp  s    	c         C   s   |  j  t _ d  S(   N(   RZ   RX   RY   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   tearDown  s    c         O   s   |  j  j | | f ƒ d  S(   N(   RX   t   append(   R   t   at   kw(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyRY     s    c         C   s—   d „  } t  j | d	 t d d ƒ f ƒ } | d ƒ |  j t |  j ƒ d ƒ | d ƒ |  j t |  j ƒ d ƒ | d ƒ |  j t |  j ƒ d ƒ d  S(
   Nc         S   s   t  j |  ƒ d  S(   N(   RX   t   warn(   t   msg(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   f  s    t   ignoret   messages   This is messages   Sanity check messagei   s   Unignored messagei   (   s   ignore(   R   t   suppressWarningst   dictR   t   lenRX   (   R   Rb   t   g(    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   testSuppressWarnings  s    	!


(   RN   RO   R[   R\   RY   Ri   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyRW     s   			(   RP   RX   R   RG   R.   R2   t   twisted.python.runtimeR    t   twisted.trialR   t   twisted.internetR   R   R   R   t   TestCaseR   RW   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/test/test_iutils.pyt   <module>   s   <"ø