Ë
    oj@5  ã                   óÊ   — 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	m
Z
mZmZ ddlmZ ddlmZ ddlmZ ddlmZmZ  G d	„ d
e«      Z G d„ de«      Z G d„ de«      Zy)zD
Test running processes with the APIs in L{twisted.internet.utils}.
é    N)ÚskipIf)ÚerrorÚ
interfacesÚreactorÚutils)ÚDeferred)Úplatform)ÚSuppressedWarningsTests)ÚSynchronousTestCaseÚTestCasec                   óð   — e Zd ZdZ ej
                  ed«      €dZdZdZ	e
j                  Zd„ Zd„ Zd„ Zd„ Zd„ Zd	„ Z e ej*                  «       d
«      d„ «       Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zy)ÚProcessUtilsTestszt
    Test running a process using L{getProcessOutput}, L{getProcessValue}, and
    L{getProcessOutputAndValue}.
    Nz)reactor doesn't implement IReactorProcessc                 ó   — | j                  «       }t        |d«      5 }|j                  t        j                  j                  |«      t        j                  z   «       ddd«       t        j                  j                  |«      S # 1 sw Y   Œ(xY w)zj
        Write the given list of lines to a text file and return the absolute
        path to it.
        ÚwtN)ÚmktempÚopenÚwriteÚosÚlinesepÚjoinÚpathÚabspath)ÚselfÚsourceLinesÚscriptÚ
scriptFiles       ú:/usr/lib/python3/dist-packages/twisted/test/test_iutils.pyÚmakeSourceFilez ProcessUtilsTests.makeSourceFile$   sk   € ð
 —‘“ˆÜ�&˜$Óð 	H :Ø×ÑœRŸZ™ZŸ_™_¨[Ó9¼B¿J¹JÑFÔG÷	Hä�w‰w�‰˜vÓ&Ð&÷	Hð 	Hús   �A BÂBc                 ó¤   — | j                  g d¢«      }t        j                  | j                  d|g«      }|j	                  | j
                  d«      S )z™
        L{getProcessOutput} returns a L{Deferred} which fires with the complete
        output of the process it runs after that process exits.
        )ú
import syszfor s in b'hello world\n':z    s = bytes([s])z    sys.stdout.buffer.write(s)z    sys.stdout.flush()ú-us   hello world
©r   r   ÚgetProcessOutputÚexeÚaddCallbackÚassertEqual©r   r   Úds      r   Útest_outputzProcessUtilsTests.test_output.   sN   € ð
 ×(Ñ(òó
ˆ
ô ×"Ñ" 4§8¡8¨d°JÐ-?Ó@ˆØ�}‰}˜T×-Ñ-Ð/?Ó@Ð@ó    c                 óÊ   ‡ — ‰ j                  ddg«      }t        j                  ‰ j                  d|g«      }‰ j	                  |t
        «      }ˆ fd„}|j                  |«       |S )z—
        The L{Deferred} returned by L{getProcessOutput} is fired with an
        L{IOError} L{Failure} if the child process writes to stderr.
        r    z!sys.stderr.write("hello world\n")r!   c                 óX   •— ‰j                  | j                  t        j                  «      S ©N)ÚassertFailureÚprocessEndedr   ÚProcessDone)Úerrr   s    €r   ÚcbFailedz?ProcessUtilsTests.test_outputWithErrorIgnored.<locals>.cbFailedL   s"   ø€ Ø×%Ñ% c×&6Ñ&6¼×8IÑ8IÓJÐJr*   )r   r   r#   r$   r.   ÚIOErrorr%   )r   r   r(   r2   s   `   r   Útest_outputWithErrorIgnoredz-ProcessUtilsTests.test_outputWithErrorIgnored?   sf   ø€ ð ×(Ñ(ØÐ?Ð@ó
ˆ
ô ×"Ñ" 4§8¡8¨d°JÐ-?Ó@ˆØ×Ñ˜q¤'Ó*ˆô	Kð 	
�‰�hÔØˆr*   c                 ó¨   — | j                  g d¢«      }t        j                  | j                  d|gd¬«      }|j	                  | j
                  d«      S )zÒ
        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.
        )r    zsys.stdout.write("foo")úsys.stdout.flush()zsys.stderr.write("foo")úsys.stderr.flush()r!   T)Úerrortoos   foofoor"   r'   s      r   Útest_outputWithErrorCollectedz/ProcessUtilsTests.test_outputWithErrorCollectedR   sO   € ð ×(Ñ(òó

ˆ
ô ×"Ñ" 4§8¡8¨d°JÐ-?È$ÔOˆØ�}‰}˜T×-Ñ-¨yÓ9Ð9r*   c                 ó¢   — | j                  dg«      }t        j                  | j                  d|g«      }|j	                  | j
                  d«      S )z|
        The L{Deferred} returned by L{getProcessValue} is fired with the exit
        status of the child process.
        zraise SystemExit(1)r!   é   )r   r   ÚgetProcessValuer$   r%   r&   r'   s      r   Ú
test_valuezProcessUtilsTests.test_valueg   sJ   € ð
 ×(Ñ(Ð*?Ð)@ÓAˆ
ä×!Ñ! $§(¡(¨T°:Ð,>Ó?ˆØ�}‰}˜T×-Ñ-¨qÓ1Ð1r*   c                 óš   ‡ — ‰ j                  g d¢«      }ˆ fd„}t        j                  ‰ j                  d|g«      }|j	                  |«      S )a  
        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.
        )r    z*sys.stdout.buffer.write(b'hello world!\n')z,sys.stderr.buffer.write(b'goodbye world!\n')zsys.exit(1)c                 ó~   •— | \  }}}‰j                  |d«       ‰j                  |d«       ‰j                  |d«       y )Ns   hello world!
s   goodbye world!
r;   ©r&   )Úout_err_codeÚoutr1   Úcoder   s       €r   ÚgotOutputAndValuez@ProcessUtilsTests.test_outputAndValue.<locals>.gotOutputAndValue�   s@   ø€ Ø)‰NˆC��dØ×Ñ˜SÐ"3Ô4Ø×Ñ˜SÐ"5Ô6Ø×Ñ˜T 1Õ%r*   r!   ©r   r   ÚgetProcessOutputAndValuer$   r%   ©r   r   rD   r(   s   `   r   Útest_outputAndValuez%ProcessUtilsTests.test_outputAndValueq   sL   ø€ ð ×(Ñ(òó
ˆ
ô	&ô ×*Ñ*¨4¯8©8°d¸JÐ5GÓHˆØ�}‰}Ð.Ó/Ð/r*   z"Windows doesn't have real signals.c                 óÆ   ‡ — ‰ j                  g d¢«      }ˆ fd„}t        j                  ‰ j                  d|g«      }‰ j	                  |t
        «      }|j                  |«      S )zþ
        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 child's stdout, stderr, and the signal which caused
        it to exit.
        )zimport sys, os, signalz"sys.stdout.write('stdout bytes\n')z"sys.stderr.write('stderr bytes\n')r6   r7   z$os.kill(os.getpid(), signal.SIGKILL)c                 óš   •— | \  }}}‰j                  |d«       ‰j                  |d«       ‰j                  |t        j                  «       y )Ns   stdout bytes
s   stderr bytes
)r&   ÚsignalÚSIGKILL)Úout_err_sigrB   r1   Úsigr   s       €r   rD   z>ProcessUtilsTests.test_outputSignal.<locals>.gotOutputAndValue    sD   ø€ Ø'‰MˆC��cØ×Ñ˜SÐ"3Ô4Ø×Ñ˜SÐ"3Ô4Ø×Ñ˜S¤&§.¡.Õ1r*   r!   )r   r   rF   r$   r.   Útupler%   rG   s   `   r   Útest_outputSignalz#ProcessUtilsTests.test_outputSignalŠ   s^   ø€ ð ×(Ñ(òó	
ˆ
ô	2ô ×*Ñ*¨4¯8©8°d¸JÐ5GÓHˆØ×Ñ˜q¤%Ó(ˆØ�}‰}Ð.Ó/Ð/r*   c                 óD  — t         j                  j                  | j                  «       «      }t        j                  |«       | j                  ddg«      } || j                  d|g|¬«      }|j                  ||j                  t        j                  «       «      «       |S )Núimport os, syszsys.stdout.write(os.getcwd())r!   )r   )r   r   r   r   Úmakedirsr   r$   r%   ÚencodeÚsysÚgetfilesystemencoding)r   ÚutilFuncÚcheckÚdirr   r(   s         r   Ú	_pathTestzProcessUtilsTests._pathTestª   s}   € Ü�g‰g�o‰o˜dŸk™k›mÓ,ˆÜ
�‰�CÔØ×(Ñ(ØÐ>Ð?ó
ˆ
ñ �T—X‘X  jÐ1¸Ô<ˆØ	�‰�e˜SŸZ™Z¬×(AÑ(AÓ(CÓDÔEØˆr*   c                 óV   — | j                  t        j                  | j                  «      S )z
        L{getProcessOutput} runs the given command with the working directory
        given by the C{path} parameter.
        )rZ   r   r#   r&   ©r   s    r   Útest_getProcessOutputPathz+ProcessUtilsTests.test_getProcessOutputPath´   s!   € ð
 �~‰~œe×4Ñ4°d×6FÑ6FÓGÐGr*   c                 óN   ‡ — ˆ fd„}‰ j                  t        j                  |«      S )z~
        L{getProcessValue} runs the given command with the working directory
        given by the C{path} parameter.
        c                 ó*   •— ‰j                  | d«       y ©Nr   r@   ©ÚresultÚignoredr   s     €r   rX   z9ProcessUtilsTests.test_getProcessValuePath.<locals>.checkÁ   ó   ø€ Ø×Ñ˜V QÕ'r*   )rZ   r   r<   ©r   rX   s   ` r   Útest_getProcessValuePathz*ProcessUtilsTests.test_getProcessValuePath»   s!   ø€ ô	(ð �~‰~œe×3Ñ3°UÓ;Ð;r*   c                 óN   ‡ — ˆ fd„}‰ j                  t        j                  |«      S )z‡
        L{getProcessOutputAndValue} runs the given command with the working
        directory given by the C{path} parameter.
        c                 óZ   •— | \  }}}‰j                  ||«       ‰j                  |d«       y r`   r@   ©Úout_err_statusrY   rB   r1   Ústatusr   s        €r   rX   zBProcessUtilsTests.test_getProcessOutputAndValuePath.<locals>.checkÌ   ó/   ø€ Ø-ÑˆC��fØ×Ñ˜S #Ô&Ø×Ñ˜V QÕ'r*   )rZ   r   rF   re   s   ` r   Ú!test_getProcessOutputAndValuePathz3ProcessUtilsTests.test_getProcessOutputAndValuePathÆ   s!   ø€ ô	(ð
 �~‰~œe×<Ñ<¸eÓDÐDr*   c                 óÞ  — t         j                  j                  | j                  «       «      }t        j                  |«       | j                  g d¢«      }| j                  t         j                  t        j                  «       «       t        j                  |«       t        j                  t        j                  d«      j                  «      }t        j                  |t        j                  t        j                  z  «       | j                  t         j                  ||«        || j                  d|g«      }|j!                  ||j#                  t%        j&                  «       «      «       |S )N)rR   zcdir = os.getcwd()zsys.stdout.write(cdir)ú.r!   )r   r   r   r   rS   r   Ú
addCleanupÚchdirÚgetcwdÚstatÚS_IMODEÚst_modeÚchmodÚS_IXUSRÚS_IRUSRr$   r%   rT   rU   rV   )r   rW   rX   rY   r   ÚoriginalModer(   s          r   Ú_defaultPathTestz"ProcessUtilsTests._defaultPathTestÓ   sé   € ä�g‰g�o‰o˜dŸk™k›mÓ,ˆÜ
�‰�CÔà×(Ñ(ÚNó
ˆ
ð
 	�‰œŸ™¤"§)¡)£+Ô.Ü
�‰�Œô —|‘|¤B§G¡G¨C£L×$8Ñ$8Ó9ˆô
 	�‰�”d—l‘l¤T§\¡\Ñ1Ô2ð
 	�‰œŸ™ # |Ô4á�T—X‘X  jÐ1Ó2ˆØ	�‰�e˜SŸZ™Z¬×(AÑ(AÓ(CÓDÔEØˆr*   c                 óV   — | j                  t        j                  | j                  «      S )a  
        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.
        )rz   r   r#   r&   r\   s    r   Ú test_getProcessOutputDefaultPathz2ProcessUtilsTests.test_getProcessOutputDefaultPathñ   s#   € ð ×$Ñ$¤U×%;Ñ%;¸T×=MÑ=MÓNÐNr*   c                 óN   ‡ — ˆ fd„}‰ j                  t        j                  |«      S )a   
        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                 ó*   •— ‰j                  | d«       y r`   r@   ra   s     €r   rX   z@ProcessUtilsTests.test_getProcessValueDefaultPath.<locals>.check  rd   r*   )rz   r   r<   re   s   ` r   Útest_getProcessValueDefaultPathz1ProcessUtilsTests.test_getProcessValueDefaultPathú   s#   ø€ ô	(ð ×$Ñ$¤U×%:Ñ%:¸EÓBÐBr*   c                 óN   ‡ — ˆ fd„}‰ j                  t        j                  |«      S )a	  
        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                 óZ   •— | \  }}}‰j                  ||«       ‰j                  |d«       y r`   r@   ri   s        €r   rX   zIProcessUtilsTests.test_getProcessOutputAndValueDefaultPath.<locals>.check  rl   r*   )rz   r   rF   re   s   ` r   Ú(test_getProcessOutputAndValueDefaultPathz:ProcessUtilsTests.test_getProcessOutputAndValueDefaultPath  s#   ø€ ô	(ð
 ×$Ñ$¤U×%CÑ%CÀUÓKÐKr*   c                 óª   ‡ ‡— ‰ j                  ddg«      }dŠt        j                  ‰ j                  d|g‰¬«      }ˆ ˆfd„}|j	                  |«       |S )z„
        Standard input can be made available to the child process by passing
        bytes for the `stdinBytes` parameter.
        r    z"sys.stdout.write(sys.stdin.read())s   These are the bytes to see.r!   )Ú
stdinBytesc                 óZ   •— | \  }}}‰j                  ‰|«       ‰j                  d|«       y r`   )ÚassertInr&   )rA   rB   r1   rC   r   r„   s       €€r   rD   zPProcessUtilsTests.test_get_processOutputAndValueStdin.<locals>.gotOutputAndValue(  s.   ø€ Ø)‰NˆC��dð �M‰M˜* cÔ*Ø×Ñ˜Q Õ%r*   rE   )r   r   r(   rD   r„   s   `   @r   Ú#test_get_processOutputAndValueStdinz5ProcessUtilsTests.test_get_processOutputAndValueStdin  sb   ù€ ð
 ×(Ñ(àØ4ðó
ˆ
ð 4ˆ
Ü×*Ñ*Ø�H‰HØ�:ÐØ!ô
ˆõ	&ð 	
�‰Ð'Ô(Øˆr*   ) Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚIReactorProcessr   ÚskipÚoutputÚvaluerU   Ú
executabler$   r   r)   r4   r9   r=   rH   r   r	   Ú	isWindowsrP   rZ   r]   rf   rm   rz   r|   r   r‚   r‡   © r*   r   r   r      s°   „ ñð
 "€z×!Ñ! '¨4Ó0Ð8Ø:ˆà€FØ€EØ
�.‰.€Cò'òAò"ò&:ò*2ò0ñ2 ÐˆH×ÑÓ Ð"FÓGñ0ó Hð0ò>òHò	<òEòò<OòCòLór*   r   c                   ó   — e Zd ZdZd„ Zy)ÚSuppressWarningsTestsz.
    Tests for L{utils.suppressWarnings}.
    c                 ó^  ‡— g Šˆfd„}| j                  t        d|«       d„ }t        j                  |dt	        d¬«      f«      } |d«       | j                  t        ‰«      d«        |d«       | j                  t        ‰«      d«        |d	«       | j                  t        ‰«      d
«       y)zs
        L{utils.suppressWarnings} decorates a function so that the given
        warnings are suppressed.
        c                 ó,   •— ‰j                  ||f«       y r-   )Úappend)r   ÚaÚkwrb   s      €r   Úshowwarningz@SuppressWarningsTests.test_suppressWarnings.<locals>.showwarning@  s   ø€ Ø�M‰M˜1˜b˜'Õ"r*   rš   c                 ó.   — t        j                  | «       y r-   )ÚwarningsÚwarn)Úmsgs    r   Úfz6SuppressWarningsTests.test_suppressWarnings.<locals>.fE  s   € Ü�M‰M˜#Õr*   )ÚignorezThis is message)ÚmessagezSanity check messager;   zUnignored messageé   N)Úpatchrœ   r   ÚsuppressWarningsÚdictr&   Úlen)r   rš   rŸ   Úgrb   s       @r   Útest_suppressWarningsz+SuppressWarningsTests.test_suppressWarnings9  s¢   ø€ ð
 ˆô	#ð 	�
‰
”8˜]¨KÔ8ò	ô ×"Ñ" 1 {´DÐARÔ4SÐ&TÓUˆñ 	
Ð
 Ô!Ø×Ñœ˜V› aÔ(ñ 	
Ð
ÔØ×Ñœ˜V› aÔ(ñ 	
Ð
ÔØ×Ñœ˜V› aÕ(r*   N)rˆ   r‰   rŠ   r‹   r¨   r’   r*   r   r”   r”   4  s   „ ñó)r*   r”   c                   ó@   — e Zd ZdZ eej                  «      Zd„ Zd„ Zy)ÚDeferredSuppressedWarningsTestsz`
    Tests for L{utils.runWithWarningsSuppressed}, the version that supports
    Deferreds.
    c                 ó8  ‡— di fdi fg}t        «       Š| j                  |ˆfd„«       t        j                  d«       ‰j	                  d«       t        j                  d«       | j                  dg| j                  «       D �cg c]  }|d   ‘Œ	 c}«       yc c}w )	z²
        If the function called by L{utils.runWithWarningsSuppressed} returns a
        C{Deferred}, the warning filters aren't removed until the Deferred
        fires.
        ©r    z.*foo.*©r    z.*bar.*c                  ó   •— ‰ S r-   r’   ©rb   s   €r   ú<lambda>zGDeferredSuppressedWarningsTests.test_deferredCallback.<locals>.<lambda>l  s   ø€ ¸€ r*   ú
ignore fooé   úignore foo 2r¡   N)r   ÚrunWithWarningsSuppressedrœ   r�   Úcallbackr&   ÚflushWarnings)r   ÚfiltersÚwrb   s      @r   Útest_deferredCallbackz5DeferredSuppressedWarningsTests.test_deferredCallbackd  sƒ   ø€ ð *¨2Ð.Ð1FÈÐ0KÐLˆÜ“ˆØ×&Ñ& w³Ô?Ü�‰�lÔ#Ø�‰˜ÔÜ�‰�nÔ%Ø×Ñ˜.Ð)À$×BTÑBTÓBVÖ+W¸Q¨A¨i«LÒ+WÕXùÒ+Ws   ÂB
c                 ól  ‡— di fdi fg}t        «       Š| j                  |ˆfd„«      }t        j                  d«       ‰j	                  t        «       «       |j                  d„ «       t        j                  d«       | j                  dg| j                  «       D �cg c]  }|d   ‘Œ	 c}«       yc c}w )	zÂ
        If the function called by L{utils.runWithWarningsSuppressed} returns a
        C{Deferred}, the warning filters aren't removed until the Deferred
        fires with an errback.
        r¬   r­   c                  ó   •— ‰ S r-   r’   r¯   s   €r   r°   zFDeferredSuppressedWarningsTests.test_deferredErrback.<locals>.<lambda>z  s   ø€ ¸F€ r*   r±   c                 ó,   — | j                  t        «      S r-   )ÚtrapÚZeroDivisionError)rŸ   s    r   r°   zFDeferredSuppressedWarningsTests.test_deferredErrback.<locals>.<lambda>}  s   € ˜qŸv™vÔ&7Ó8€ r*   r³   r¡   N)	r   r´   rœ   r�   Úerrbackr¾   Ú
addErrbackr&   r¶   )r   r·   r(   r¸   rb   s       @r   Útest_deferredErrbackz4DeferredSuppressedWarningsTests.test_deferredErrbackr  s–   ø€ ð *¨2Ð.Ð1FÈÐ0KÐLˆÜ“ˆØ×*Ñ*¨7³NÓCˆÜ�‰�lÔ#Ø�‰Ô(Ó*Ô+Ø	�‰Ñ8Ô9Ü�‰�nÔ%Ø×Ñ˜.Ð)À$×BTÑBTÓBVÖ+W¸Q¨A¨i«LÒ+WÕXùÒ+Ws   ÂB1
N)	rˆ   r‰   rŠ   r‹   Ústaticmethodr   r´   r¹   rÁ   r’   r*   r   rª   rª   Z  s&   „ ññ !-¨U×-LÑ-LÓ MÐòYóYr*   rª   )r‹   r   rK   rs   rU   rœ   Úunittestr   Útwisted.internetr   r   r   r   Útwisted.internet.deferr   Útwisted.python.runtimer	   Útwisted.python.test.test_utilr
   Útwisted.trial.unittestr   r   r   r”   rª   r’   r*   r   ú<module>rÉ      s\   ðñó
 
Û Û Û 
Û Ý ç >Ó >Ý +Ý +Ý Aß @ôZ˜ô Zôz#)Ð/ô #)ôL%YÐ&=õ %Yr*   