
    Vh,                     .   d Z ddlmZmZ ddlmZmZmZ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 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! ddl"m#Z# ddl$m%Z% ddl%m&Z&  G d dejN                  jP                  jR                        Z*y)z2
Tests for L{twisted.application.twist._options}.
    )stderrstdout)CallableDictListOptionalTextIOTupleN)version)reactor)IReactorCore)MemoryReactor)FileLogObserverLogLeveljsonFileLogObservertextFileLogObserver)
UsageError   )NoSuchReactor)
ExitStatus)	DummyExit)ServiceMaker)_options   )TwistOptionsc                      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$dZdededdfdZd$dZd$dZd$dZd$dZdedeegef   ddf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$d#Z#y)%OptionsTestsz$
    Tests for L{TwistOptions}.
    returnNc                 d    t               | _        | j                  t        d| j                         y)zX
        Patch L{_twist.exit} so we can capture usage and prevent actual exits.
        exitN)r   r    patchr   selfs    [/home/dcms/DCMS/lib/python3.12/site-packages/twisted/application/twist/test/test_options.py	patchExitzOptionsTests.patchExit$   s!     K	

8VTYY/    c                      g  _         ddt        dt        t           dt        f fd} j	                  t
        d|       y)zZ
        Patch L{_options.open} so we can capture usage and prevent actual opens.
        Nnamemoder   c                 n    | dk(  rt        d d |       j                  j                  | |f       t        S )N	nocanopen)OSErroropenedappendNotImplemented)r(   r)   r#   s     r$   fakeOpenz(OptionsTests.patchOpen.<locals>.fakeOpen1   s6    {"dD$//KKd|,!!r&   openFile)N)r-   strr   r	   r!   r   )r#   r0   s   ` r$   	patchOpenzOptionsTests.patchOpen+   s<     8:	"3 	"hsm 	"v 	" 	

8Z2r&   c                 f     i  _         dt        dt        f fd} j                  t        d|       y)zo
        Patch C{_options.installReactor} so we can capture usage and prevent
        actual installs.
        r(   r   c                 X    | dk7  r
t               t               }|j                  | <   |S )Nfusion)r   r   installedReactors)r(   r   r#   s     r$   installReactorz8OptionsTests.patchInstallReactor.<locals>.installReactorA   s/    x#o%#oG+2D""4(Nr&   r8   N)r7   r2   r   r!   r   )r#   r8   s   ` r$   patchInstallReactorz OptionsTests.patchInstallReactor:   s3    
 ;=	 	 	 	

8-~>r&   c                 t    t               }| j                  |j                         j                  d             y)z@
        L{TwistOptions.getSynopsis} appends arguments.
        z plugin [plugin_options]N)r   
assertTruegetSynopsisendswithr#   optionss     r$   test_synopsiszOptionsTests.test_synopsisK   s,     .++-667QRSr&   c                    | j                          t               }|j                          | j                  | j                  j
                  t        j                         | j                  | j                  j                  t               y)zl
        L{TwistOptions.opt_version} exits with L{ExitStatus.EX_OK} and prints
        the version.
        N)
r%   r   opt_versionassertEqualsr    statusr   EX_OKmessager   r>   s     r$   test_versionzOptionsTests.test_versionS   s[    
 	.$))**J,<,<=$))++W5r&   c                     | j                          t               }|j                  d       | j                  t	        | j
                        dh       | j                  |d   d       y)zo
        L{TwistOptions.installReactor} installs the chosen reactor and sets
        the reactor name.
        r6   reactorNameN)r9   r   opt_reactorassertEqualsetr7   rC   r>   s     r$   test_reactorzOptionsTests.test_reactor`   sW    
 	  ".H%T334xjA'-0(;r&   c                     | j                          t               }d|_        |j                  dg       | j	                  t        | j                        dh       y)z
        L{TwistOptions.installReactor} installs the chosen reactor after the
        command line options have been parsed.
        ztest-subcommandz--reactor=fusionr6   N)r9   r   
subCommandparseOptionsrK   rL   r7   r>   s     r$   test_installCorrectReactorz'OptionsTests.test_installCorrectReactorm   sN    
 	  "..012T334xjAr&   c                 z    | j                          t               }| j                  t        |j                  d       y)zn
        L{TwistOptions.installReactor} raises UsageError if an unknown reactor
        is specified.
        coalN)r9   r   assertRaisesr   rJ   r>   s     r$   test_installReactorBogusz%OptionsTests.test_installReactorBogusz   s/    
 	  ".*g&9&96Br&   c                 b    t               }| j                  t        |j                  d             y)z
        L{TwistOptions.installReactor} returns the currently installed reactor
        when the default reactor name is specified.
        defaultN)r   assertIdenticalr   r8   r>   s     r$   test_installReactorDefaultz'OptionsTests.test_installReactorDefault   s&    
 .Wg&<&<Y&GHr&   c                     t               }|j                  d       | j                  |d   t        j                         y)zQ
        L{TwistOptions.opt_log_level} sets the corresponding log level.
        warnlogLevelN)r   opt_log_levelrX   r   r[   r>   s     r$   test_logLevelValidzOptionsTests.test_logLevelValid   s2     .f%WZ0(--@r&   c                 Z    t               }| j                  t        |j                  d       y)zi
        L{TwistOptions.opt_log_level} with an invalid log level name raises
        UsageError.
        cheeseN)r   rT   r   r]   r>   s     r$   test_logLevelInvalidz!OptionsTests.test_logLevelInvalid   s#    
 .*g&;&;XFr&   r(   expectedStreamc                 d    t               }|j                  |       | j                  |d   |       y)z
        Set log file name and check the selected output stream.

        @param name: The name of the file.
        @param expectedStream: The expected stream.
        logFileN)r   opt_log_filerX   )r#   r(   rb   r?   s       r$   _testLogFilezOptionsTests._testLogFile   s.     .T"WY/@r&   c                 0    | j                  dt               y)zW
        L{TwistOptions.opt_log_file} given C{"-"} as a file name uses stdout.
        -N)rf   r   r"   s    r$   test_logFileStdoutzOptionsTests.test_logFileStdout        	#v&r&   c                 0    | j                  dt               y)zW
        L{TwistOptions.opt_log_file} given C{"+"} as a file name uses stderr.
        +N)rf   r   r"   s    r$   test_logFileStderrzOptionsTests.test_logFileStderr   rj   r&   c                     | j                          t               }|j                  d       | j                  dg| j                         y)zX
        L{TwistOptions.opt_log_file} opens the given file name in append mode.
        mylog)ro   aN)r3   r   re   rK   r-   r>   s     r$   test_logFileNamedzOptionsTests.test_logFileNamed   s:     	.W%.)4;;7r&   c                    | j                          | j                          t               }|j                  d       | j	                  | j
                  j                  t        j                         | j                  | j
                  j                         | j                  | j
                  j                  j                  d             y)z
        L{TwistOptions.opt_log_file} exits with L{ExitStatus.EX_IOERR} if
        unable to open the log file due to an L{EnvironmentError}.
        r+   z%Unable to open log file 'nocanopen': N)r%   r3   r   re   rC   r    rD   r   EX_IOERRassertIsNotNonerF   r;   
startswithr>   s     r$   test_logFileCantOpenz!OptionsTests.test_logFileCantOpen   s    
 	.[)$))**J,?,?@TYY../II((7	
r&   formatexpectedObserverFactoryc                     t               }|j                  |       | j                  |d   |       | j                  |d   |       y)z
        Set log file format and check the selected observer factory.

        @param format: The format of the file.
        @param expectedObserverFactory: The expected observer factory.
        fileLogObserverFactory	logFormatN)r   opt_log_formatrX   rK   )r#   rw   rx   r?   s       r$   _testLogFormatzOptionsTests._testLogFormat   sE     .v&W%=>@WX-v6r&   c                 0    | j                  dt               y)g
        L{TwistOptions.opt_log_format} given C{"text"} uses a
        L{textFileLogObserver}.
        textN)r}   r   r"   s    r$   test_logFormatTextzOptionsTests.test_logFormatText       
 	F$78r&   c                 0    | j                  dt               y)r   jsonN)r}   r   r"   s    r$   test_logFormatJSONzOptionsTests.test_logFormatJSON   r   r&   c                 Z    t               }| j                  t        |j                  d       y)zk
        L{TwistOptions.opt_log_format} given an invalid format name raises
        L{UsageError}.
        frommageN)r   rT   r   r|   r>   s     r$   test_logFormatInvalidz"OptionsTests.test_logFormatInvalid   s#    
 .*g&<&<jIr&   c                     | j                          t               }|j                  d       |j                  d       |j	                          | j                  |d   t               | j                  |d   d       y)r
        L{TwistOptions.selectDefaultLogObserver} will not override an already
        selected observer.
        r   quesorz   r{   N)r3   r   r|   re   selectDefaultLogObserverrX   r   rK   r>   s     r$   'test_selectDefaultLogObserverNoOverridez4OptionsTests.test_selectDefaultLogObserverNoOverride   sk    
 	.v&W%((* 	W%=>@ST-v6r&   c                     G d d      }| j                  t        d |              t               }|j                  d       |j	                          | j                  |d   t               | j                  |d   d       y)	r   c                       e Zd ZdefdZy)IOptionsTests.test_selectDefaultLogObserverDefaultWithTTY.<locals>.TTYFiler   c                      y)NT r"   s    r$   isattyzPOptionsTests.test_selectDefaultLogObserverDefaultWithTTY.<locals>.TTYFile.isatty  s    r&   N)__name__
__module____qualname__boolr   r   r&   r$   TTYFiler     s     r&   r   r   rh   rz   r{   r   N)r!   r   r   re   r   rX   r   rK   )r#   r   r?   s      r$   +test_selectDefaultLogObserverDefaultWithTTYz8OptionsTests.test_selectDefaultLogObserverDefaultWithTTY  sm    	 	
 	

8Xwy1.S!((*W%=>@ST-v6r&   c                     | j                          t               }|j                  d       |j                          | j	                  |d   t
               | j                  |d   d       y)r   r   rz   r{   r   N)r3   r   re   r   rX   r   rK   r>   s     r$   .test_selectDefaultLogObserverDefaultWithoutTTYz;OptionsTests.test_selectDefaultLogObserverDefaultWithoutTTY  s[    
 	.W%((*W%=>@ST-v6r&   c                     t               }|j                  }|D ]1  }| j                  |t               | j                  ||   t               3 y)zM
        L{TwistOptions.plugins} is a mapping of available plug-ins.
        N)r   pluginsassertIsInstancer2   r   )r#   r?   r   r(   s       r$   test_pluginsTypezOptionsTests.test_pluginsType-  sG     .// 	?D!!$,!!'$->	?r&   c                 P    t               }| j                  d|j                         y)z
        L{TwistOptions.plugins} includes a C{"web"} plug-in.
        This is an attempt to verify that something we expect to be in the list
        is in there without enumerating all of the built-in plug-ins.
        webN)r   assertInr   r>   s     r$   test_pluginsIncludeWebz#OptionsTests.test_pluginsIncludeWeb8  s     .eW__-r&   c                     t               }|j                  D ]_  \  }}}}| j                  |t               | j	                  |d       | j                  t        |             | j                  |t               a y)z~
        L{TwistOptions.subCommands} is an iterable of tuples as expected by
        L{twisted.python.usage.Options}.
        N)r   subCommandsr   r2   rX   r;   callable)r#   r?   r(   shortcutparserdocs         r$   test_subCommandsTypez!OptionsTests.test_subCommandsTypeB  sj    
 .+2+>+> 	,'D(FC!!$,  40OOHV,-!!#s+		,r&   c           
          t               }t        |j                        }|j                  D ch c]	  \  }}}}| }}}}}| j	                  ||       yc c}}}}w )zW
        L{TwistOptions.subCommands} includes a sub-command for every plug-in.
        N)r   rL   r   r   rK   )r#   r?   r   r(   r   r   r   r   s           r$   test_subCommandsIncludeWebz'OptionsTests.test_subCommandsIncludeWebO  sT     .goo&?F?R?RSS ;htSSg. Ts   A
c                 x    | j                          t               }| j                  t        |j                         y)zd
        L{TwistOptions.postOptions} raises L{UsageError} is it has no
        sub-command.
        N)r9   r   rT   r   postOptionsr>   s     r$   test_postOptionsNoSubCommandz)OptionsTests.test_postOptionsNoSubCommandZ  s-    
 	  ".*g&9&9:r&   )r   N)$r   r   r   __doc__r%   r3   r9   r@   rG   rM   rQ   rU   rY   r^   ra   r2   r	   rf   ri   rm   rq   rv   r   r   r}   r   r   r   r   r   r   r   r   r   r   r   r   r&   r$   r   r      s    03?"T6<BCIAG
A 
Af 
A 
A''	8
&774<fX=V4W7	799J7"7(7	?.,	/	;r&   r   )+r   sysr   r   typingr   r   r   r   r	   r
   twisted.trial.unittesttwistedtwisted.copyrightr   twisted.internetr   twisted.internet.interfacesr   twisted.internet.testingr   twisted.loggerr   r   r   r   twisted.python.usager   reactorsr   runner._exitr   runner.test.test_runnerr   servicer   twistr   r   trialunittestTestCaser   r   r&   r$   <module>r      sc     @ @  % $ 4 2  , % & 0 #  #D;7==))22 D;r&   