o
    Òó·i¸8  ã                   @   sd  d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZmZmZmZmZ ddlmZmZmZ ddlZddlmZ dd	lmZmZmZ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* ddl%m+Z+ G dd„ dej,j-j.ƒZ/edd�G dd„ de+ƒƒZ0G dd„ de)ƒZ1G dd„ dƒZ2G dd„ dƒZ3G dd„ dƒZ4G dd „ d ƒZ5dS )!z2
Tests for L{twisted.application.runner._runner}.
é    N)ÚStringIO)ÚSIGTERM)ÚTracebackType)	ÚAnyÚIterableÚListÚOptionalÚTextIOÚTupleÚTypeÚUnionÚcast)ÚFactoryÚattribÚattrs)ÚMemoryReactor)ÚFileLogObserverÚFilteringLogObserverÚILogObserverÚLogBeginnerÚLogLevelÚLogLevelFilterPredicateÚLogPublisher)ÚFilePathé   )Ú_runneré   )Ú
ExitStatus)ÚNonePIDFileÚPIDFile)ÚRunnerc                   @   sÞ   e Zd ZdZd)dee dee fdd„Zd*dd„Z	d*d	d
„Z
d*dd„Zd*dd„Zd*dd„Zd*dd„Zd*dd„Zd*dd„Zd*dd„Zd*dd„Zd*dd„Zd*dd„Zd*dd „Zd*d!d"„Zd*d#d$„Zd)d%ed&ee ddfd'd(„ZdS )+ÚRunnerTestsz
    Tests for L{Runner}.
    NÚcontentÚreturnc                 C   s"   t |  ¡ ƒ}|d ur| |¡ |S ©N)r   ÚmktempZ
setContent)Úselfr"   ÚfilePath© r(   úqC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/application/runner/test/test_runner.pyr'   '   s   
zRunnerTests.filePathc                 C   s¶   t ƒ | _tƒ | _|  td| j¡ |  td| j¡ d| _| j› d� ¡ | _t	ƒ | _
t	ƒ | _t| j
| jƒ| _tƒ | _tƒ | _t| j| jj| j| jƒ| _|  td| j¡ |  td| j¡ d S )NÚexitÚkilli9  Ú
ÚstderrÚglobalLogBeginner)Ú	DummyExitr*   Ú	DummyKillr+   Úpatchr   ÚpidÚencodeÚpidFileContentr   Ústdoutr-   ÚDummyStandardIOZstdioÚDummyWarningsModuleÚwarningsr   ZglobalLogPublisherr   r.   ©r&   r(   r(   r)   ÚsetUp-   s&   üzRunnerTests.setUpc                 C   s*   t tƒ d�}| ¡  |  |jg d¢¡ dS )zD
        L{Runner.run} calls the expected methods in order.
        ©Úreactor)ÚkillIfRequestedÚstartLoggingÚstartReactorÚreactorExitedN)ÚDummyRunnerr   ÚrunÚassertEqualÚcalledMethods©r&   Úrunnerr(   r(   r)   Útest_runInOrderO   s   þzRunnerTests.test_runInOrderc                 C   sP   t ƒ }ttƒ |d�}|  |j¡ |  |j¡ | ¡  |  |j¡ |  |j¡ dS )z;
        L{Runner.run} uses the provided PID file.
        ©r<   ÚpidFileN)ÚDummyPIDFiler    r   ÚassertFalseÚenteredÚexitedrB   Ú
assertTrue©r&   rI   rF   r(   r(   r)   Útest_runUsesPIDFile`   s   zRunnerTests.test_runUsesPIDFilec                 C   sV   t |  | j¡ƒ}dd„ |_ttƒ |d�}| ¡  |  | jj	t
j¡ |  | jjd¡ dS )z²
        L{Runner.run} exits with L{ExitStatus.EX_USAGE} and the expected
        message if a process is already running that corresponds to the given
        PID file.
        c                   S   ó   dS ©NTr(   r(   r(   r(   r)   Ú<lambda>w   ó    z4RunnerTests.test_runAlreadyRunning.<locals>.<lambda>rH   zAlready running.N)r   r'   r4   Z	isRunningr    r   rB   rC   r*   Ústatusr   Z	EX_CONFIGÚmessagerO   r(   r(   r)   Útest_runAlreadyRunningp   s   
z"RunnerTests.test_runAlreadyRunningc                 C   s6   t tƒ d�}| ¡  |  | jjg ¡ |  | jj¡ dS )z‚
        L{Runner.killIfRequested} when C{kill} is false doesn't exit and
        doesn't indiscriminately murder anyone.
        r;   N)	r    r   r=   rC   r+   ÚcallsrK   r*   rM   rE   r(   r(   r)   Útest_killNotRequested   s   z!RunnerTests.test_killNotRequestedc                 C   sL   t tƒ dd�}| ¡  |  | jjg ¡ |  | jjtj	¡ |  | jj
d¡ dS )zÞ
        L{Runner.killIfRequested} when C{kill} is true but C{pidFile} is
        L{nonePIDFile} exits with L{ExitStatus.EX_USAGE} and the expected
        message; and also doesn't indiscriminately murder anyone.
        T)r<   r+   zNo PID file specified.N)r    r   r=   rC   r+   rX   r*   rU   r   ZEX_USAGErV   rE   r(   r(   r)   Ú test_killRequestedWithoutPIDFileŠ   s
   z,RunnerTests.test_killRequestedWithoutPIDFilec                 C   sf   t |  | j¡ƒ}ttƒ d|d�}| ¡  |  | jj| j	t
fg¡ |  | jjtj¡ |  | jjd¡ dS )z—
        L{Runner.killIfRequested} when C{kill} is true and given a C{pidFile}
        performs a targeted killing of the appropriate process.
        T©r<   r+   rI   N)r   r'   r4   r    r   r=   rC   r+   rX   r2   r   r*   rU   r   ZEX_OKÚassertIdenticalrV   rO   r(   r(   r)   Útest_killRequestedWithPIDFile—   s   z)RunnerTests.test_killRequestedWithPIDFilec                 C   s`   t |  d¡ƒ}dtfdd„}||_ttƒ d|d�}| ¡  |  | jj	t
j¡ |  | jjd¡ dS )z•
        L{Runner.killIfRequested} when C{kill} is true and given a C{pidFile}
        that it can't read exits with L{ExitStatus.EX_IOERR}.
        Nr#   c                   S   s   t tjdƒ‚)NzPermission denied)ÚOSErrorÚerrnoÚEACCESr(   r(   r(   r)   Úread«   s   z?RunnerTests.test_killRequestedWithPIDFileCantRead.<locals>.readTr[   zUnable to read PID file.)r   r'   Úintra   r    r   r=   rC   r*   rU   r   ZEX_IOERRrV   )r&   rI   ra   rF   r(   r(   r)   Ú%test_killRequestedWithPIDFileCantRead¤   s   z1RunnerTests.test_killRequestedWithPIDFileCantReadc                 C   óL   t |  d¡ƒ}ttƒ d|d�}| ¡  |  | jjtj	¡ |  | jj
d¡ dS )z˜
        L{Runner.killIfRequested} when C{kill} is true and given a C{pidFile}
        containing no value exits with L{ExitStatus.EX_DATAERR}.
        ó    Tr[   úInvalid PID file.N©r   r'   r    r   r=   rC   r*   rU   r   Z
EX_DATAERRrV   rO   r(   r(   r)   Ú"test_killRequestedWithPIDFileEmpty¶   ó
   z.RunnerTests.test_killRequestedWithPIDFileEmptyc                 C   rd   )z£
        L{Runner.killIfRequested} when C{kill} is true and given a C{pidFile}
        containing a non-integer value exits with L{ExitStatus.EX_DATAERR}.
        s    ** totally not a number, dude **Tr[   rf   Nrg   rO   r(   r(   r)   Ú%test_killRequestedWithPIDFileNotAnIntÂ   ri   z1RunnerTests.test_killRequestedWithPIDFileNotAnIntc                    s  t ƒ }G ‡ fdd„dƒ‰ |  tdˆ ƒ ¡ G ‡fdd„dtƒ‰|  tdˆ¡ G ‡fdd„dtƒ‰ttƒ tj|ˆd	�}| 	¡  |  
tˆ jƒd
¡ |  ˆ jd t¡ |  
tˆjƒd
¡ |  ˆjd t¡ |  ˆjd jtj¡ tˆˆjƒ}|  |ˆ¡ |  |j|¡ dS )zÛ
        L{Runner.startLogging} sets up a filtering observer with a log level
        predicate set to the given log level that contains a file observer of
        the given type which writes to the given file.
        c                       s8   e Zd ZU g Zee ed< dee ddf‡ fdd„ZdS )z2RunnerTests.test_startLogging.<locals>.LogBeginnerÚ	observersr#   Nc                    s   t |ƒˆ _d S r$   )Úlistrk   )r&   rk   ©r   r(   r)   ÚbeginLoggingToÜ   ó   zARunnerTests.test_startLogging.<locals>.LogBeginner.beginLoggingTo)	Ú__name__Ú
__module__Ú__qualname__rk   r   r   Ú__annotations__r   rn   r(   rm   r(   r)   r   Ù   s   
 r   r.   c                	       s^   e Zd ZU dZee ed< g Zee	 ed< e
edd„ ƒfdedee	 deddf‡ fdd	„ZdS )
z?RunnerTests.test_startLogging.<locals>.MockFilteringLogObserverNÚobserverÚ
predicatesc                 S   s   d S r$   r(   )Úeventr(   r(   r)   rS   ë   rT   zHRunnerTests.test_startLogging.<locals>.MockFilteringLogObserver.<lambda>ÚnegativeObserverr#   c                    s$   |ˆ _ t|ƒˆ _t | |||¡ d S r$   )rt   rl   ru   r   Ú__init__)r&   rt   ru   rw   ©ÚMockFilteringLogObserverr(   r)   rx   ç   s
   
ÿzHRunnerTests.test_startLogging.<locals>.MockFilteringLogObserver.__init__)rp   rq   rr   rt   r   r   rs   ru   r   r   r   r   rx   r(   ry   r(   r)   rz   ã   s   
 üþýüûrz   r   c                       s4   e Zd ZU dZee ed< deddf‡ fdd„ZdS )z:RunnerTests.test_startLogging.<locals>.MockFileLogObserverNÚoutFiler#   c                    s   |ˆ _ t | |t¡ d S r$   )r{   r   rx   Ústr)r&   r{   ©ÚMockFileLogObserverr(   r)   rx   ú   s   zCRunnerTests.test_startLogging.<locals>.MockFileLogObserver.__init__)rp   rq   rr   r{   r   r	   rs   rx   r(   r}   r(   r)   r~   ÷   s   
 r~   )r<   ÚdefaultLogLevelÚlogFileZfileLogObserverFactoryé   r   N)r   r1   r   r   r   r    r   r   Úcriticalr>   rC   Úlenrk   ZassertIsInstanceru   r   r\   r   r   rt   r{   )r&   r€   rF   rt   r(   )r   r~   rz   r)   Útest_startLoggingÎ   s2   ü
ÿÿzRunnerTests.test_startLoggingc                 C   s(   t ƒ }t|d�}| ¡  |  |j¡ dS )ze
        L{Runner.startReactor} with the C{reactor} argument runs the given
        reactor.
        r;   N)r   r    r?   rN   ZhasRun)r&   r<   rF   r(   r(   r)   Útest_startReactorWithReactor  s   
z(RunnerTests.test_startReactorWithReactorc                 C   s   |   dd¡ dS )z�
        L{Runner.startReactor} ensures that C{whenRunning} is called with
        C{whenRunningArguments} when the reactor is running.
        ÚwhenRunningr?   N©Ú	_testHookr9   r(   r(   r)   Útest_startReactorWhenRunning&  s   z(RunnerTests.test_startReactorWhenRunningc                 C   ó   |   d¡ dS )zb
        L{Runner.whenRunning} calls C{whenRunning} with
        C{whenRunningArguments}.
        r†   Nr‡   r9   r(   r(   r)   Útest_whenRunningWithArguments-  ó   z)RunnerTests.test_whenRunningWithArgumentsc                 C   rŠ   )zf
        L{Runner.whenRunning} calls C{reactorExited} with
        C{reactorExitedArguments}.
        r@   Nr‡   r9   r(   r(   r)   Útest_reactorExitedWithArguments4  rŒ   z+RunnerTests.test_reactorExitedWithArgumentsÚ
methodNameÚ
callerNamec                    s˜   |du r|}t tƒ tƒ tƒ d�}g ‰ dtddf‡ fdd„}|||› d�| ¡ i}tddtƒ i|¤Ž}t||ƒ}|ƒ  |  tˆ ƒd	¡ |  ˆ d
 |¡ dS )aÊ  
        Verify that the named hook is run with the expected arguments as
        specified by the arguments used to create the L{Runner}, when the
        specified caller is invoked.

        @param methodName: The name of the hook to verify.

        @param callerName: The name of the method that is expected to cause the
            hook to be called.
            If C{None}, use the L{Runner} method with the same name as the
            hook.
        N)ÚaÚbÚcÚ	argumentsr#   c                     s   ˆ   | ¡ d S r$   )Úappend)r“   ©ZargumentsSeenr(   r)   ÚhookN  ro   z#RunnerTests._testHook.<locals>.hookÚ	Argumentsr<   r�   r   r(   )ÚdictÚobjectÚcopyr    r   ÚgetattrrC   rƒ   )r&   rŽ   r�   r“   r–   ZrunnerArgumentsrF   Z
hookCallerr(   r•   r)   rˆ   ;  s"   þÿÿ
zRunnerTests._testHookr$   ©r#   N)rp   rq   rr   Ú__doc__r   Úbytesr   r|   r'   r:   rG   rP   rW   rY   rZ   r]   rc   rh   rj   r„   r…   r‰   r‹   r�   rˆ   r(   r(   r(   r)   r!   "   s&    

"









M


 r!   T)Úfrozenc                   @   sL   e Zd ZdZeee eeƒd�Z	ddd„Z
ddd„Zdd	d
„Zddd„ZdS )rA   zg
    Stub for L{Runner}.

    Keep track of calls to some methods without actually doing anything.
    )ÚtypeÚdefaultr#   Nc                 C   ó   | j  d¡ d S )Nr=   ©rD   r”   r9   r(   r(   r)   r=   j  ó   zDummyRunner.killIfRequestedc                 C   r¢   )Nr>   r£   r9   r(   r(   r)   r>   m  r¤   zDummyRunner.startLoggingc                 C   r¢   )Nr?   r£   r9   r(   r(   r)   r?   p  r¤   zDummyRunner.startReactorc                 C   r¢   )Nr@   r£   r9   r(   r(   r)   r@   s  r¤   zDummyRunner.reactorExitedrœ   )rp   rq   rr   r�   r   r   r|   r   rl   rD   r=   r>   r?   r@   r(   r(   r(   r)   rA   `  s    


rA   c                   @   sN   e Zd ZdZddd„Zddd„Zdeee  d	ee d
ee	 ddfdd„Z
dS )rJ   z]
    Stub for L{PIDFile}.

    Tracks context manager entry/exit without doing anything.
    r#   Nc                 C   s   t  | ¡ d| _d| _d S ©NF)r   rx   rL   rM   r9   r(   r(   r)   rx   ~  s   

zDummyPIDFile.__init__c                 C   s
   d| _ | S rR   )rL   r9   r(   r(   r)   Ú	__enter__„  s   zDummyPIDFile.__enter__ÚexcTypeÚexcValueÚ	tracebackc                 C   ó
   d| _ d S rR   ©rM   )r&   r§   r¨   r©   r(   r(   r)   Ú__exit__ˆ  s   
zDummyPIDFile.__exit__rœ   )r#   rJ   )rp   rq   rr   r�   rx   r¦   r   r   ÚBaseExceptionr   r¬   r(   r(   r(   r)   rJ   w  s    


þýüûrJ   c                   @   s@   e Zd ZdZd
dd„Z	ddeeef dee	 ddfdd	„Z
dS )r/   zy
    Stub for L{_exit.exit} that remembers whether it's been called and, if it has,
    what arguments it was given.
    r#   Nc                 C   rª   r¥   r«   r9   r(   r(   r)   rx   —  ó   
zDummyExit.__init__rU   rV   c                 C   s    | j rJ ‚|| _|| _d| _ d S rR   )rM   rU   rV   )r&   rU   rV   r(   r(   r)   Ú__call__š  s   

zDummyExit.__call__rœ   r$   )rp   rq   rr   r�   rx   r   rb   r   r   r|   r¯   r(   r(   r(   r)   r/   ‘  s    
ÿ
ÿÿþr/   c                   @   s0   e Zd ZdZd
dd„Zdededdfdd	„ZdS )r0   zv
    Stub for L{os.kill} that remembers whether it's been called and, if it has,
    what arguments it was given.
    r#   Nc                 C   s
   g | _ d S r$   )rX   r9   r(   r(   r)   rx   ª  r®   zDummyKill.__init__r2   Úsigc                 C   s   | j  ||f¡ d S r$   )rX   r”   )r&   r2   r°   r(   r(   r)   r¯   ­  s   zDummyKill.__call__rœ   )rp   rq   rr   r�   rx   rb   r¯   r(   r(   r(   r)   r0   ¤  s    
r0   c                   @   ó&   e Zd ZdZdededdfdd„ZdS )r6   zR
    Stub for L{sys} which provides L{StringIO} streams as stdout and stderr.
    r5   r-   r#   Nc                 C   s   || _ || _d S r$   )r5   r-   )r&   r5   r-   r(   r(   r)   rx   ¶  s   
zDummyStandardIO.__init__)rp   rq   rr   r�   r	   rx   r(   r(   r(   r)   r6   ±  ó    r6   c                   @   r±   )r7   zV
    Stub for L{warnings} which provides a C{showwarning} method that is a no-op.
    ÚargsÚkwargsr#   Nc                  O   rQ   )z\
        Do nothing.

        @param args: ignored.
        @param kwargs: ignored.
        Nr(   )r³   r´   r(   r(   r)   ÚshowwarningÀ  rT   zDummyWarningsModule.showwarning)rp   rq   rr   r�   r   rµ   r(   r(   r(   r)   r7   »  r²   r7   )6r�   r_   Úior   Úsignalr   Útypesr   Útypingr   r   r   r   r	   r
   r   r   r   Úattrr   r   r   Ztwisted.trial.unittestZtwistedZtwisted.internet.testingr   Ztwisted.loggerr   r   r   r   r   r   r   Ztwisted.python.filepathr   rF   r   Ú_exitr   Z_pidfiler   r   r    ZtrialZunittestZTestCaser!   rA   rJ   r/   r0   r6   r7   r(   r(   r(   r)   Ú<module>   s2   ,$	  @
