o
    >h,                     @   s   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 dej'j(j)Z*dS )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                   @   sJ  e Zd Z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ededdfddZd@ddZd@d d!Zd@d"d#Zd@d$d%Zd&ed'eegef ddfd(d)Zd@d*d+Zd@d,d-Zd@d.d/Zd@d0d1Zd@d2d3Zd@d4d5Zd@d6d7Zd@d8d9Z d@d:d;Z!d@d<d=Z"d@d>d?Z#dS )AOptionsTestsz$
    Tests for L{TwistOptions}.
    returnNc                 C   s   t  | _| td| j dS )zX
        Patch L{_twist.exit} so we can capture usage and prevent actual exits.
        exitN)r   r   patchr   self r!   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/application/twist/test/test_options.py	patchExit$   s   zOptionsTests.patchExitc                    s8   g  _ ddtdtt dtf fdd} td| dS )	zZ
        Patch L{_options.open} so we can capture usage and prevent actual opens.
        Nnamemoder   c                    s(   | dkr
t d d |  j| |f tS )N	nocanopen)OSErroropenedappendNotImplemented)r$   r%   r   r!   r"   fakeOpen1   s   z(OptionsTests.patchOpen.<locals>.fakeOpenopenFile)N)r(   strr   r   r   r   )r    r+   r!   r   r"   	patchOpen+   s    zOptionsTests.patchOpenc                    s.   i  _ dtdtf fdd} td| dS )zo
        Patch C{_options.installReactor} so we can capture usage and prevent
        actual installs.
        r$   r   c                    s"   | dkrt  t }| j| < |S )Nfusion)r   r   installedReactors)r$   r   r   r!   r"   installReactorA   s
   
z8OptionsTests.patchInstallReactor.<locals>.installReactorr1   N)r0   r-   r   r   r   )r    r1   r!   r   r"   patchInstallReactor:   s   z OptionsTests.patchInstallReactorc                 C   s   t  }| | d dS )z@
        L{TwistOptions.getSynopsis} appends arguments.
        z plugin [plugin_options]N)r   
assertTruegetSynopsisendswithr    optionsr!   r!   r"   test_synopsisK   s   zOptionsTests.test_synopsisc                 C   s<   |    t }|  | | jjtj | | jjt	 dS )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
   r6   r!   r!   r"   test_versionS   s
   zOptionsTests.test_versionc                 C   s@   |    t }|d | t| jdh | |d d dS )zo
        L{TwistOptions.installReactor} installs the chosen reactor and sets
        the reactor name.
        r/   reactorNameN)r2   r   opt_reactorassertEqualsetr0   r:   r6   r!   r!   r"   test_reactor`   s
   
zOptionsTests.test_reactorc                 C   s8   |    t }d|_|dg | t| jdh dS )z
        L{TwistOptions.installReactor} installs the chosen reactor after the
        command line options have been parsed.
        ztest-subcommandz--reactor=fusionr/   N)r2   r   
subCommandparseOptionsrA   rB   r0   r6   r!   r!   r"   test_installCorrectReactorm   s
   z'OptionsTests.test_installCorrectReactorc                 C   s"   |    t }| t|jd dS )zn
        L{TwistOptions.installReactor} raises UsageError if an unknown reactor
        is specified.
        coalN)r2   r   assertRaisesr   r@   r6   r!   r!   r"   test_installReactorBogusz   s   z%OptionsTests.test_installReactorBogusc                 C   s   t  }| t|d dS )z
        L{TwistOptions.installReactor} returns the currently installed reactor
        when the default reactor name is specified.
        defaultN)r   assertIdenticalr   r1   r6   r!   r!   r"   test_installReactorDefault   s   z'OptionsTests.test_installReactorDefaultc                 C   s&   t  }|d | |d tj dS )zQ
        L{TwistOptions.opt_log_level} sets the corresponding log level.
        warnlogLevelN)r   opt_log_levelrK   r   rM   r6   r!   r!   r"   test_logLevelValid   s   
zOptionsTests.test_logLevelValidc                 C      t  }| t|jd dS )zi
        L{TwistOptions.opt_log_level} with an invalid log level name raises
        UsageError.
        cheeseN)r   rH   r   rO   r6   r!   r!   r"   test_logLevelInvalid      z!OptionsTests.test_logLevelInvalidr$   expectedStreamc                 C   s$   t  }|| | |d | dS )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_filerK   )r    r$   rU   r7   r!   r!   r"   _testLogFile   s   
zOptionsTests._testLogFilec                 C      |  dt dS )zW
        L{TwistOptions.opt_log_file} given C{"-"} as a file name uses stdout.
        -N)rX   r   r   r!   r!   r"   test_logFileStdout      zOptionsTests.test_logFileStdoutc                 C   rY   )zW
        L{TwistOptions.opt_log_file} given C{"+"} as a file name uses stderr.
        +N)rX   r   r   r!   r!   r"   test_logFileStderr   r\   zOptionsTests.test_logFileStderrc                 C   s,   |    t }|d | dg| j dS )zX
        L{TwistOptions.opt_log_file} opens the given file name in append mode.
        mylog)r_   aN)r.   r   rW   rA   r(   r6   r!   r!   r"   test_logFileNamed   s   
zOptionsTests.test_logFileNamedc                 C   sX   |    |   t }|d | | jjtj | 	| jj
 | | jj
d dS )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#   r.   r   rW   r:   r   r;   r   EX_IOERRassertIsNotNoner=   r3   
startswithr6   r!   r!   r"   test_logFileCantOpen   s   
z!OptionsTests.test_logFileCantOpenformatexpectedObserverFactoryc                 C   s4   t  }|| | |d | | |d | dS )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_formatrK   rA   )r    rf   rg   r7   r!   r!   r"   _testLogFormat   s   	
zOptionsTests._testLogFormatc                 C   rY   )g
        L{TwistOptions.opt_log_format} given C{"text"} uses a
        L{textFileLogObserver}.
        textN)rk   r   r   r!   r!   r"   test_logFormatText      zOptionsTests.test_logFormatTextc                 C   rY   )rl   jsonN)rk   r   r   r!   r!   r"   test_logFormatJSON   ro   zOptionsTests.test_logFormatJSONc                 C   rQ   )zk
        L{TwistOptions.opt_log_format} given an invalid format name raises
        L{UsageError}.
        frommageN)r   rH   r   rj   r6   r!   r!   r"   test_logFormatInvalid   rT   z"OptionsTests.test_logFormatInvalidc                 C   sN   |    t }|d |d |  | |d t | |d d dS )r
        L{TwistOptions.selectDefaultLogObserver} will not override an already
        selected observer.
        rm   quesorh   ri   N)r.   r   rj   rW   selectDefaultLogObserverrK   r   rA   r6   r!   r!   r"   'test_selectDefaultLogObserverNoOverride   s   

z4OptionsTests.test_selectDefaultLogObserverNoOverridec                 C   sZ   G dd d}|  td|  t }|d |  | |d t | |d d dS )	rt   c                   @   s   e Zd ZdefddZdS )zIOptionsTests.test_selectDefaultLogObserverDefaultWithTTY.<locals>.TTYFiler   c                 S   s   dS )NTr!   r   r!   r!   r"   isatty  s   zPOptionsTests.test_selectDefaultLogObserverDefaultWithTTY.<locals>.TTYFile.isattyN)__name__
__module____qualname__boolrx   r!   r!   r!   r"   TTYFile  s    r}   r   rZ   rh   ri   rm   N)r   r   r   rW   rv   rK   r   rA   )r    r}   r7   r!   r!   r"   +test_selectDefaultLogObserverDefaultWithTTY  s   
z8OptionsTests.test_selectDefaultLogObserverDefaultWithTTYc                 C   sD   |    t }|d |  | |d t | |d d dS )rt   ru   rh   ri   rp   N)r.   r   rW   rv   rK   r   rA   r6   r!   r!   r"   .test_selectDefaultLogObserverDefaultWithoutTTY  s   
z;OptionsTests.test_selectDefaultLogObserverDefaultWithoutTTYc                 C   s6   t  }|j}|D ]}| |t | || t qdS )zM
        L{TwistOptions.plugins} is a mapping of available plug-ins.
        N)r   pluginsassertIsInstancer-   r   )r    r7   r   r$   r!   r!   r"   test_pluginsType-  s   zOptionsTests.test_pluginsTypec                 C   s   t  }| d|j dS )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   r6   r!   r!   r"   test_pluginsIncludeWeb8  s   z#OptionsTests.test_pluginsIncludeWebc                 C   sP   t  }|jD ]\}}}}| |t | |d | t| | |t qdS )z~
        L{TwistOptions.subCommands} is an iterable of tuples as expected by
        L{twisted.python.usage.Options}.
        N)r   subCommandsr   r-   rK   r3   callable)r    r7   r$   shortcutparserdocr!   r!   r"   test_subCommandsTypeB  s   z!OptionsTests.test_subCommandsTypec                 C   s0   t  }t|j}dd |jD }| || dS )zW
        L{TwistOptions.subCommands} includes a sub-command for every plug-in.
        c                 S   s   h | ]\}}}}|qS r!   r!   ).0r$   r   r   r   r!   r!   r"   	<setcomp>V  s    z:OptionsTests.test_subCommandsIncludeWeb.<locals>.<setcomp>N)r   rB   r   r   rA   )r    r7   r   r   r!   r!   r"   test_subCommandsIncludeWebO  s   
z'OptionsTests.test_subCommandsIncludeWebc                 C   s    |    t }| t|j dS )zd
        L{TwistOptions.postOptions} raises L{UsageError} is it has no
        sub-command.
        N)r2   r   rH   r   postOptionsr6   r!   r!   r"   test_postOptionsNoSubCommandZ  s   z)OptionsTests.test_postOptionsNoSubCommand)r   N)$ry   rz   r{   __doc__r#   r.   r2   r8   r>   rC   rF   rI   rL   rP   rS   r-   r   rX   r[   r^   ra   re   r   r   rk   rn   rq   rs   rw   r~   r   r   r   r   r   r   r!   r!   r!   r"   r      sH    











		








	






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!   r"   <module>   s"    