Ϫf,.dZddlmZmZddlmZmZmZmZm Z m Z ddl Z ddl mZddlmZddlmZddlmZdd lmZmZmZmZdd lmZd d lmZd d lmZd dl m!Z!d dl"m#Z#d dl$m%Z%ddl%m&Z&Gdde jNjPjRZ*y)z2 Tests for L{twisted.application.twist._options}. )stderrstdout)CallableDictListOptionalTextIOTupleN)version)reactor) IReactorCore) MemoryReactor)FileLogObserverLogLeveljsonFileLogObservertextFileLogObserver) UsageError) NoSuchReactor) ExitStatus) DummyExit) ServiceMaker)_options) TwistOptionsceZdZdZd$dZd$dZd$dZd$dZd$dZd$d Z d$d Z d$d Z d$d Z d$d Z d$dZdededdfdZd$dZd$dZd$dZd$dZdedeegefddfdZd$dZd$dZd$dZd$dZd$dZd$dZd$dZd$d Z d$d!Z!d$d"Z"d$d#Z#y)% OptionsTestsz$ Tests for L{TwistOptions}. returnNcdt|_|jtd|jy)zX Patch L{_twist.exit} so we can capture usage and prevent actual exits. exitN)rr patchrselfs M/usr/lib/python3/dist-packages/twisted/application/twist/test/test_options.py patchExitzOptionsTests.patchExit$s!K  8VTYY/cg_ddtdttdtffd }j t d|y)zZ Patch L{_options.open} so we can capture usage and prevent actual opens. Nnamemodercn|dk(r tdd|jj||ftS)N nocanopen)OSErroropenedappendNotImplemented)r(r)r#s r$fakeOpenz(OptionsTests.patchOpen..fakeOpen1s6{"dD$// KK  d| ,! !r&openFile)N)r-strrr r!r)r#r0s` r$ patchOpenzOptionsTests.patchOpen+s<8:  "3 "hsm "v " 8Z2r&cfi_dtdtffd }jtd|y)zo Patch C{_options.installReactor} so we can capture usage and prevent actual installs. r(rcX|dk7r tt}|j|<|S)Nfusion)rrinstalledReactors)r(r r#s r$installReactorz8OptionsTests.patchInstallReactor..installReactorAs/x#o%#oG+2D " "4 (Nr&r8N)r7r2r r!r)r#r8s` r$patchInstallReactorz OptionsTests.patchInstallReactor:s3 ;=    8-~>r&ctt}|j|jjdy)z@ L{TwistOptions.getSynopsis} appends arguments. z plugin [plugin_options]N)r assertTrue getSynopsisendswithr#optionss r$ test_synopsiszOptionsTests.test_synopsisKs,. ++-667QRSr&c|jt}|j|j|jj t j|j|jjty)zl L{TwistOptions.opt_version} exits with L{ExitStatus.EX_OK} and prints the version. N) r%r opt_version assertEqualsr statusrEX_OKmessager r>s r$ test_versionzOptionsTests.test_versionSs[ . $))**J,<,<= $))++W5r&c|jt}|jd|jt |j dh|j |ddy)zo L{TwistOptions.installReactor} installs the chosen reactor and sets the reactor name. r6 reactorNameN)r9r opt_reactor assertEqualsetr7rCr>s r$ test_reactorzOptionsTests.test_reactor`sW   ".H% T334xjA '-0(;r&c|jt}d|_|jdg|j t |j dhy)z L{TwistOptions.installReactor} installs the chosen reactor after the command line options have been parsed. ztest-subcommandz--reactor=fusionr6N)r9r subCommand parseOptionsrKrLr7r>s r$test_installCorrectReactorz'OptionsTests.test_installCorrectReactormsN   "..012 T334xjAr&cz|jt}|jt|jdy)zn L{TwistOptions.installReactor} raises UsageError if an unknown reactor is specified. coalN)r9r assertRaisesrrJr>s r$test_installReactorBogusz%OptionsTests.test_installReactorBoguszs/   ". *g&9&96Br&cbt}|jt|jdy)z L{TwistOptions.installReactor} returns the currently installed reactor when the default reactor name is specified. defaultN)rassertIdenticalr r8r>s r$test_installReactorDefaultz'OptionsTests.test_installReactorDefaults& . Wg&<&s r$test_logLevelValidzOptionsTests.test_logLevelValids2.f% WZ0(--@r&cZt}|jt|jdy)zi L{TwistOptions.opt_log_level} with an invalid log level name raises UsageError. cheeseN)rrTrr]r>s r$test_logLevelInvalidz!OptionsTests.test_logLevelInvalids# . *g&;&;XFr&r(expectedStreamcdt}|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(rbr?s r$ _testLogFilezOptionsTests._testLogFiles..T" WY/@r&c0|jdty)zW L{TwistOptions.opt_log_file} given C{"-"} as a file name uses stdout. -N)rfrr"s r$test_logFileStdoutzOptionsTests.test_logFileStdout #v&r&c0|jdty)zW L{TwistOptions.opt_log_file} given C{"+"} as a file name uses stderr. +N)rfrr"s r$test_logFileStderrzOptionsTests.test_logFileStderrrjr&c|jt}|jd|jdg|jy)zX L{TwistOptions.opt_log_file} opens the given file name in append mode. mylog)roaN)r3rrerKr-r>s r$test_logFileNamedzOptionsTests.test_logFileNameds: .W% .)4;;7r&c|j|jt}|jd|j |j j tj|j|j j|j|j jjdy)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%r3rrerCr rDrEX_IOERRassertIsNotNonerFr; startswithr>s r$test_logFileCantOpenz!OptionsTests.test_logFileCantOpens  .[) $))**J,?,?@ TYY../  II   ( (7  r&formatexpectedObserverFactoryct}|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)ropt_log_formatrXrK)r#rwrxr?s r$_testLogFormatzOptionsTests._testLogFormatsE.v& W%=>@WX -v6r&c0|jdty)g L{TwistOptions.opt_log_format} given C{"text"} uses a L{textFileLogObserver}. textN)r}rr"s r$test_logFormatTextzOptionsTests.test_logFormatText F$78r&c0|jdty)rjsonN)r}rr"s r$test_logFormatJSONzOptionsTests.test_logFormatJSONrr&cZt}|jt|jdy)zk L{TwistOptions.opt_log_format} given an invalid format name raises L{UsageError}. frommageN)rrTrr|r>s r$test_logFormatInvalidz"OptionsTests.test_logFormatInvalids# . *g&<&s r$'test_selectDefaultLogObserverNoOverridez4OptionsTests.test_selectDefaultLogObserverNoOverridesk .v&W%((* W%=>@ST -v6r&cGdd}|jtd|t}|jd|j |j |dt |j|ddy) rceZdZdefdZy)IOptionsTests.test_selectDefaultLogObserverDefaultWithTTY..TTYFilercy)NTr"s r$isattyzPOptionsTests.test_selectDefaultLogObserverDefaultWithTTY..TTYFile.isattysr&N)__name__ __module__ __qualname__boolrrr&r$TTYFilers  r&rrrhrzr{rN)r!rrrerrXrrK)r#rr?s r$+test_selectDefaultLogObserverDefaultWithTTYz8OptionsTests.test_selectDefaultLogObserverDefaultWithTTY sm    8Xwy1.S!((* W%=>@ST -v6r&c|jt}|jd|j|j |dt |j |ddy)rrrzr{rN)r3rrerrXrrKr>s r$.test_selectDefaultLogObserverDefaultWithoutTTYz;OptionsTests.test_selectDefaultLogObserverDefaultWithoutTTYs[ .W%((* W%=>@ST -v6r&ct}|j}|D]1}|j|t|j||t3y)zM L{TwistOptions.plugins} is a mapping of available plug-ins. N)rpluginsassertIsInstancer2r)r#r?rr(s r$test_pluginsTypezOptionsTests.test_pluginsType-sG.// ?D  ! !$ ,  ! !'$- > ?r&cPt}|jd|jy)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)rassertInrr>s r$test_pluginsIncludeWebz#OptionsTests.test_pluginsIncludeWeb8s . eW__-r&ct}|jD]_\}}}}|j|t|j |d|j t ||j|tay)z~ L{TwistOptions.subCommands} is an iterable of tuples as expected by L{twisted.python.usage.Options}. N)r subCommandsrr2rXr;callable)r#r?r(shortcutparserdocs r$test_subCommandsTypez!OptionsTests.test_subCommandsTypeBsj .+2+>+> , 'D(FC  ! !$ ,  4 0 OOHV, -  ! !#s +  ,r&c t}t|j}|jDchc] \}}}}| }}}}}|j ||ycc}}}}w)zW L{TwistOptions.subCommands} includes a sub-command for every plug-in. N)rrLrrrK)r#r?rr(rrrrs r$test_subCommandsIncludeWebz'OptionsTests.test_subCommandsIncludeWebOsT.goo&?F?R?RSS ;htS S g.TsA cx|jt}|jt|jy)zd L{TwistOptions.postOptions} raises L{UsageError} is it has no sub-command. N)r9rrTr postOptionsr>s r$test_postOptionsNoSubCommandz)OptionsTests.test_postOptionsNoSubCommandZs-   ". *g&9&9:r&)rN)$rrr__doc__r%r3r9r@rGrMrQrUrYr^rar2r rfrirmrqrvrrr}rrrrrrrrrrrrr&r$rrs0 3?"T 6 < BCIAG A Af A A' ' 8 & 7 74rsc@@%$42 ,%&0##D;7==))22D;r&