
    Yf                     2   d Z ddl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 ddlmZmZ ddl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 ddlmZ ddlmZ  G d dej         j!        j"                  Z# G d dej         j!        j"                  Z$dS )z0
Tests for L{twisted.application.twist._twist}.
    )stdout)AnyDictListN)IReactorCore)MemoryReactor)LogLeveljsonFileLogObserver)SignalCapturingMemoryReactor   )
ExitStatus)Runner)	DummyExit)IServiceMultiService)_twist   )TwistOptions)Twistc                   b    e Zd ZdZddZddZddZddZddZdd	Z	dd
Z
ddZddZddZdS )
TwistTestsz
    Tests for L{Twist}.
    returnNc                 .    |                                   d S N)patchInstallReactorselfs    [/var/www/html/env/lib/python3.11/site-packages/twisted/application/twist/test/test_twist.pysetUpzTwistTests.setUp   s      """""    c                 n    t                      | _        |                     t          d| j                   dS )zX
        Patch L{_twist.exit} so we can capture usage and prevent actual exits.
        exitN)r   r"   patchr   r   s    r   	patchExitzTwistTests.patchExit!   s-     KK	

6649-----r    c                      i  _         dt          dt          dt          f fd}                     t          d|           dS )zo
        Patch C{_options.installReactor} so we can capture usage and prevent
        actual installs.
        _namer   c                 8    t                      }|j        |<   |S r   )r   installedReactors)r&   r'   reactorr   s      r   installReactorz6TwistTests.patchInstallReactor.<locals>.installReactor/   s    #ooG+2D"4(Nr    r+   N)r)   r   strr   r#   )r   r+   s   ` r   r   zTwistTests.patchInstallReactor(   s_    
 ;=	l 	# 	, 	 	 	 	 	 	
 	

<!1>BBBBBr    c                 l     g  _         dt          ddf fd}                     t          d|           dS )zo
        Patch L{MultiService.startService} so we can capture usage and prevent
        actual starts.
        servicer   Nc                 <    j                             |            d S r   )serviceStartsappend)r.   r   s    r   startServicez2TwistTests.patchStartService.<locals>.startService=   s     %%g.....r    r2   )r0   r   r#   r   )r   r2   s   ` r   patchStartServicezTwistTests.patchStartService6   sW    
 .0	/( 	/t 	/ 	/ 	/ 	/ 	/ 	/ 	

<>>>>>r    c                 h    t          j        ddg          }|                     |t                     dS )zI
        L{Twist.options} given valid arguments returns options.
        twistwebN)r   optionsassertIsInstancer   )r   r7   s     r   test_optionsValidArgumentsz%TwistTests.test_optionsValidArgumentsB   s4     -% 011g|44444r    c                    |                                   t          j        ddg           |                     | j        j        t          j                   |                     | j        j	                   | 
                    | j        j	                            d                     | 
                    | j        j	                            dt                                            dS )z
        L{Twist.options} given invalid arguments exits with
        L{ExitStatus.EX_USAGE} and an error/usage message.
        r5   z--bogus-bagelszError: z

N)r$   r   r7   assertIdenticalr"   statusr   EX_USAGEassertIsNotNonemessage
assertTrue
startswithendswithr   r   s    r   test_optionsInvalidArgumentsz'TwistTests.test_optionsInvalidArgumentsJ   s    
 	w 01222TY-z/BCCCTY.///I((33	
 	
 	
 	I&&'|~~'' 	
 	
 	
 	
 	
r    c                     t          j        ddg          }t          j        |j        d         |j                  }|                     t          j        |                     dS )z:
        L{Twist.service} returns an L{IService}.
        r5   r6   N)r   r7   r.   plugins
subOptionsr@   r   
providedBy)r   r7   r.   s      r   test_servicezTwistTests.test_service^   sV     -% 011- 68JKK+G4455555r    c                    t          j        ddg          }|d         }|j        }|J t          j        |j        |         |j                  }|                                  t          j        ||           |                     | j	        |g           |                     |j
        d         d         |j        di fg           dS )	z
        L{Twist.startService} starts the service and registers a trigger to
        stop the service when the reactor shuts down.
        r5   r6   r*   N)pluginr7   beforeshutdown )r   r7   
subCommandr.   rE   rF   r3   r2   assertEqualr0   triggersstopService)r   r7   r*   rN   r.   s        r   test_startServicezTwistTests.test_startServicef   s    
 -% 011)$'
%%%-?:.&
 
 

 	   7G,,,+gY777X&z2g6I2r5R4S	
 	
 	
 	
 	
r    c           	         g |                      t          dfd           |                      t          dd            t          j        g d          }t          j        |           |                     t                    d           |                     d         t          | j        d         t          j
        t          t          	                     d
S )zi
        L{Twist.run} runs the runner with arguments corresponding to the given
        options.
        __init__c                 .                         |          S r   )r1   )r   argsargsSeens     r   <lambda>z%TwistTests.test_run.<locals>.<lambda>   s    HOOD<Q<Q r    runc                     d S r   rM   r   s    r   rX   z%TwistTests.test_run.<locals>.<lambda>   s    t r    r5   z--reactor=defaultz--log-format=jsonr6      r   defaultr*   defaultLogLevellogFilefileLogObserverFactoryN)r#   r   r   r7   rY   rO   lendictr)   r	   infor   r
   )r   twistOptionsrW   s     @r   test_runzTwistTests.test_run~   s    
 

6:'Q'Q'Q'QRRR

65"3"3444}FFF
 
 		,X***QK.y9 (':	  	
 	
 	
 	
 	
r    c           	      0   |                                   g  G fdd          }|                     t          d|           t          j        g d           |                     t          | j                  d           |                     t                    d           |                     d         j        t          | j
        d         t          j        t          t                               |                     d         j        d           dS )	zy
        L{Twist.main} runs the runner with arguments corresponding to the given
        command line arguments.
        c                   ,    e Zd Zdeddf fdZddZdS )$TwistTests.test_main.<locals>.Runnerkwargsr   Nc                 N    || _         d| _                            |            d S )Nr   )rV   runsr1   )r   rj   runnerss     r   rT   z-TwistTests.test_main.<locals>.Runner.__init__   s)    "		t$$$$$r    c                 &    | xj         dz  c_         d S )Nr\   )rl   r   s    r   rY   z(TwistTests.test_main.<locals>.Runner.run   s    		Q				r    r   N)__name__
__module____qualname__r   rT   rY   )rm   s   r   r   ri      sU        % % % % % % % %
     r    r   r[   r\   r   r]   r^   N)r3   r#   r   r   mainrO   rb   r0   rV   rc   r)   r	   rd   r   r
   rl   )r   r   rm   s     @r   	test_mainzTwistTests.test_main   s'   
 	   	 	 	 	 	 	 	 	 	 	 	

68V,,,
MMMNNNT/00!444Wq)))AJO.y9 (':	  	
 	
 	
 	!,,,,,r    ro   )rp   rq   rr   __doc__r   r$   r   r3   r9   rC   rH   rR   rf   rt   rM   r    r   r   r      s         # # # #. . . .C C C C
? 
? 
? 
?5 5 5 5
 
 
 
(6 6 6 6
 
 
 
0
 
 
 
4!- !- !- !- !- !-r    r   c                   2    e Zd ZdZddZddZddZddZdS )	TwistExitTestszr
    Tests to verify that the Twist script takes the expected actions related
    to signals and the reactor.
    r   Nc                      d _         dt          dd f fd}                     t          d|           dt          dd fd}                     t          d|           d S )	NFsigr   c                     d_         dS )z
            Fake to capture whether L{twisted.application._exitWithSignal
            was called.

            @param sig: Signal value
            @type sig: C{int}
            TN)exitWithSignalCalled)ry   r   s    r   fakeExitWithSignalz0TwistExitTests.setUp.<locals>.fakeExitWithSignal   s     )-D%%%r    _exitWithSignalr&   c                     dS )z
            Prevent Runner from adding new log observers or other
            tests outside this module will fail.

            @param _: Unused self param
            NrM   )r&   s    r   startLoggingz*TwistExitTests.setUp.<locals>.startLogging   s      r    r   )r{   intr#   r   r   )r   r|   r   s   `  r   r   zTwistExitTests.setUp   s    $)!	-C 	-D 	- 	- 	- 	- 	- 	- 	

6,.@AAA	F 	t 	 	 	 	 	

6><88888r    c                     t                      }d|_        t                      }||d<   t          |d<   t	          j        |           |                     | j                   dS )zg
        _exitWithSignal is not called if the reactor's _exitSignal attribute
        is zero.
        Nr*   ra   )r   _exitSignalr   r
   r   rY   assertFalser{   r   r*   r7   s      r   %test_twistReactorDoesntExitWithSignalz4TwistExitTests.test_twistReactorDoesntExitWithSignal   sb    
 /00"..$	,?()	'233333r    c                     t                      }t                      }||d<   t          |d<   t          j        |           |                     | j                   dS )z
        _exitWithSignal is not called if the runner's reactor does not
        implement L{twisted.internet.interfaces._ISupportsExitSignalCapturing}
        r*   ra   N)r   r   r
   r   rY   r   r{   r   s      r   $test_twistReactorHasNoExitSignalAttrz3TwistExitTests.test_twistReactorHasNoExitSignalAttr   sX    
  //..$	,?()	'233333r    c                     t                      }d|_        t                      }||d<   t          |d<   t	          j        |           |                     | j                   dS )zb
        _exitWithSignal is called if the runner's reactor exits due
        to a signal.
        r   r*   ra   N)r   r   r   r
   r   rY   r@   r{   r   s      r    test_twistReactorExitsWithSignalz/TwistExitTests.test_twistReactorExitsWithSignal   s`    
 /00..$	,?()	'122222r    ro   )rp   rq   rr   ru   r   r   r   r   rM   r    r   rw   rw      sn         
9 9 9 924 4 4 4
4 
4 
4 
43 3 3 3 3 3r    rw   )%ru   sysr   typingr   r   r   twisted.trial.unittesttwistedtwisted.internet.interfacesr   twisted.internet.testingr   twisted.loggerr	   r
   twisted.test.test_twistdr   runner._exitr   runner._runnerr   runner.test.test_runnerr   r.   r   r   r5   r   _optionsr   r   trialunittestTestCaser   rw   rM   r    r   <module>r      s          " " " " " " " " " "     4 4 4 4 4 4 2 2 2 2 2 2 8 8 8 8 8 8 8 8 A A A A A A & & & & & & $ $ $ $ $ $ 0 0 0 0 0 0 - - - - - - - -       # # # # # #      `- `- `- `- `-'0 `- `- `-FD3 D3 D3 D3 D3W]+4 D3 D3 D3 D3 D3r    