o
    Òó·i¨,  ã                   @   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!   úqC:\Users\psant\Downloads\PAF backend\school\venv\Lib\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>.fakeOpenZ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>.installReactorr0   N)r/   r,   r   r   r   )r    r0   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   Ú
assertTrueZ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   Zopt_versionÚassertEqualsr   Ústatusr   ZEX_OKÚmessager
   r4   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.   ZreactorNameN)r1   r   Úopt_reactorÚassertEqualÚsetr/   r7   r4   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)r1   r   Z
subCommandZparseOptionsr<   r=   r/   r4   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.
        ZcoalN)r1   r   ÚassertRaisesr   r;   r4   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   r0   r4   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.
        ÚwarnZlogLevelN)r   Úopt_log_levelrC   r   rE   r4   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.
        ZcheeseN)r   r@   r   rF   r4   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.
        ZlogFileN)r   Úopt_log_filerC   )r    r$   rK   r5   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)rM   r   r   r!   r!   r"   Útest_logFileStdoutª   ó   zOptionsTests.test_logFileStdoutc                 C   rN   )zW
        L{TwistOptions.opt_log_file} given C{"+"} as a file name uses stderr.
        ú+N)rM   r   r   r!   r!   r"   Útest_logFileStderr°   rQ   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)rT   ÚaN)r-   r   rL   r<   r(   r4   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   rL   r7   r   r8   r   ZEX_IOERRZassertIsNotNoner9   r2   Ú
startswithr4   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_formatrC   r<   )r    rY   rZ   r5   r!   r!   r"   Ú_testLogFormatÔ   s   	
zOptionsTests._testLogFormatc                 C   rN   )úg
        L{TwistOptions.opt_log_format} given C{"text"} uses a
        L{textFileLogObserver}.
        ÚtextN)r^   r   r   r!   r!   r"   Útest_logFormatTextã   ó   zOptionsTests.test_logFormatTextc                 C   rN   )r_   ÚjsonN)r^   r   r   r!   r!   r"   Útest_logFormatJSONê   rb   zOptionsTests.test_logFormatJSONc                 C   rH   )zk
        L{TwistOptions.opt_log_format} given an invalid format name raises
        L{UsageError}.
        ZfrommageN)r   r@   r   r]   r4   r!   r!   r"   Útest_logFormatInvalidñ   rJ   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.
        r`   Úquesor[   r\   N)r-   r   r]   rL   ÚselectDefaultLogObserverrC   r   r<   r4   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 )	rf   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__Úboolrj   r!   r!   r!   r"   ÚTTYFile  s    ro   r   rO   r[   r\   r`   N)r   r   r   rL   rh   rC   r   r<   )r    ro   r5   r!   r!   r"   Ú+test_selectDefaultLogObserverDefaultWithTTY  s   
z8OptionsTests.test_selectDefaultLogObserverDefaultWithTTYc                 C   sD   |   ¡  tƒ }| d¡ | ¡  |  |d t¡ |  |d d¡ dS )rf   rg   r[   r\   rc   N)r-   r   rL   rh   rC   r   r<   r4   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    r5   rr   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.
        ZwebN)r   ZassertInrr   r4   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   ÚsubCommandsrs   r,   rC   r2   Úcallable)r    r5   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$   rx   ry   rz   r!   r!   r"   Ú	<setcomp>V  s    z:OptionsTests.test_subCommandsIncludeWeb.<locals>.<setcomp>N)r   r=   rr   rv   r<   )r    r5   rr   rv   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)r1   r   r@   r   ZpostOptionsr4   r!   r!   r"   Útest_postOptionsNoSubCommandZ  s   z)OptionsTests.test_postOptionsNoSubCommand)r   N)$rk   rl   rm   Ú__doc__r#   r-   r1   r6   r:   r>   r?   rA   rD   rG   rI   r,   r   rM   rP   rS   rV   rX   r   r   r^   ra   rd   re   ri   rp   rq   rt   ru   r{   r~   r   r!   r!   r!   r"   r      sH    











		



ÿÿ
þ



	






r   )+r€   Úsysr   r   Útypingr   r   r   r   r   r	   Ztwisted.trial.unittestZtwistedZtwisted.copyrightr
   Ztwisted.internetr   Ztwisted.internet.interfacesr   Ztwisted.internet.testingr   Ztwisted.loggerr   r   r   r   Ztwisted.python.usager   Zreactorsr   Zrunner._exitr   Zrunner.test.test_runnerr   Zservicer   Ztwistr   r   ZtrialZunittestZTestCaser   r!   r!   r!   r"   Ú<module>   s"    