U
    n5{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                   @   sª  e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Zeeddœdd„Zddœdd„Zddœdd „Zddœd!d"„Zddœd#d$„Zeeegef dd%œd&d'„Zddœd(d)„Zddœd*d+„Zddœd,d-„Zddœd.d/„Zddœd0d1„Zddœd2d3„Zddœd4d5„Zddœd6d7„Z ddœd8d9„Z!ddœd:d;„Z"ddœd<d=„Z#dS )>ÚOptionsTestsz$
    Tests for L{TwistOptions}.
    N©Úreturnc                 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"   úO/tmp/pip-unpacked-wheel-mnhg7r0q/twisted/application/twist/test/test_options.pyÚ	patchExit$   s    zOptionsTests.patchExitc                    s4   g ˆ _ dttt tdœ‡ 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 ˆ _ ttdœ‡ 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   Ú
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   ÚEX_OKÚmessager
   r5   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)r2   r   Úopt_reactorÚassertEqualÚsetr0   r8   r5   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   Z
subCommandZparseOptionsr>   r?   r0   r5   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)r2   r   ÚassertRaisesr   r=   r5   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   r5   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_levelrE   r   rG   r5   r"   r"   r#   Útest_logLevelValidŒ   s    
zOptionsTests.test_logLevelValidc                 C   s   t ƒ }|  t|jd¡ dS )zi
        L{TwistOptions.opt_log_level} with an invalid log level name raises
        UsageError.
        ZcheeseN)r   rB   r   rH   r5   r"   r"   r#   Útest_logLevelInvalid•   s    z!OptionsTests.test_logLevelInvalid)r%   ÚexpectedStreamr   c                 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_filerE   )r!   r%   rK   r6   r"   r"   r#   Ú_testLogFilež   s    
zOptionsTests._testLogFilec                 C   s   |   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ª   s    zOptionsTests.test_logFileStdoutc                 C   s   |   dt¡ dS )zW
        L{TwistOptions.opt_log_file} given C{"+"} as a file name uses stderr.
        ú+N)rM   r   r    r"   r"   r#   Útest_logFileStderr°   s    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)rR   ÚaN)r.   r   rL   r>   r)   r5   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   r8   r   r9   r   ÚEX_IOERRZassertIsNotNoner;   r3   Ú
startswithr5   r"   r"   r#   Útest_logFileCantOpenÁ   s    
ÿÿz!OptionsTests.test_logFileCantOpen)ÚformatÚexpectedObserverFactoryr   c                 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_formatrE   r>   )r!   rX   rY   r6   r"   r"   r#   Ú_testLogFormatÔ   s    	
zOptionsTests._testLogFormatc                 C   s   |   dt¡ dS )úg
        L{TwistOptions.opt_log_format} given C{"text"} uses a
        L{textFileLogObserver}.
        ÚtextN)r]   r   r    r"   r"   r#   Útest_logFormatTextã   s    zOptionsTests.test_logFormatTextc                 C   s   |   dt¡ dS )r^   ÚjsonN)r]   r   r    r"   r"   r#   Útest_logFormatJSONê   s    zOptionsTests.test_logFormatJSONc                 C   s   t ƒ }|  t|jd¡ dS )zk
        L{TwistOptions.opt_log_format} given an invalid format name raises
        L{UsageError}.
        ZfrommageN)r   rB   r   r\   r5   r"   r"   r#   Útest_logFormatInvalidñ   s    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_   ÚquesorZ   r[   N)r.   r   r\   rL   ÚselectDefaultLogObserverrE   r   r>   r5   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 )	rd   c                   @   s   e Zd Zedœ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__Úboolrh   r"   r"   r"   r#   ÚTTYFile  s   rm   r   rN   rZ   r[   r_   N)r   r   r   rL   rf   rE   r   r>   )r!   rm   r6   r"   r"   r#   Ú+test_selectDefaultLogObserverDefaultWithTTY  s    
z8OptionsTests.test_selectDefaultLogObserverDefaultWithTTYc                 C   sD   |   ¡  tƒ }| d¡ | ¡  |  |d t¡ |  |d d¡ dS )rd   re   rZ   r[   ra   N)r.   r   rL   rf   rE   r   r>   r5   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!   r6   rp   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assertInrp   r5   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   ÚsubCommandsrq   r-   rE   r3   Úcallable)r!   r6   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%   rv   rw   rx   r"   r"   r#   Ú	<setcomp>V  s    
 z:OptionsTests.test_subCommandsIncludeWeb.<locals>.<setcomp>N)r   r?   rp   rt   r>   )r!   r6   rp   rt   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   rB   r   ZpostOptionsr5   r"   r"   r#   Útest_postOptionsNoSubCommandZ  s    z)OptionsTests.test_postOptionsNoSubCommand)$ri   rj   rk   Ú__doc__r$   r.   r2   r7   r<   r@   rA   rC   rF   rI   rJ   r-   r   rM   rO   rQ   rT   rW   r   r   r]   r`   rb   rc   rg   rn   ro   rr   rs   ry   r|   r}   r"   r"   r"   r#   r      s@   
		 þ	
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     