ϪfR"dZddlZddlmZddlmZmZddlmZm Z m Z ddl m Z m Z mZmZddlmZGdd eZGd d e Zd ZGddej,ZGddej,ZGddej,ZGdde j4ZGdde j4ZGddeZdZGdde j4ZGdde j4Z Gdd e j4Z!Gd!d"eZ"d#Z#d$Z$y)%z* Test cases for twisted.python._shellcomp N)BytesIO)ListOptional) _shellcompreflectusage) CompleteFiles CompleteList Completer Completions)unittestceZdZdZdZy)ZshScriptTestMetaz* Metaclass of ZshScriptTestMixin. c|d}d|vr|dD]\}}|||}||d|z<tj||||S)Ncfd}|S)Nct|S)N)test_genZshFunction)selfcmdName optionsFQPNs D/usr/lib/python3/dist-packages/twisted/python/test/test_shellcomp.pyrunTestz.makeTest..runTests*4+FF)rrrs`` rmakeTestz+ZshScriptTestMeta.__new__..makeTests GNr generateFortest_genZshFunction_)type__new__)clsnamebasesattrsrrrtests rrzZshScriptTestMeta.__new__s]  E !(-m(< ?$5:>,w67 ?||Cue44rN)__name__ __module__ __qualname____doc__rrrrrrs 5rrceZdZdZy)ZshScriptTestMixina Integration test helper to show that C{usage.Options} classes can have zsh completion functions generated for them without raising errors. In your subclasses set a class variable like so:: # | cmd name | Fully Qualified Python Name of Options class | # generateFor = [('conch', 'twisted.conch.scripts.conch.ClientOptions'), ('twistd', 'twisted.scripts.twistd.ServerOptions'), ] Each package that contains Twisted scripts should contain one TestCase subclass which also inherits from this mixin, and contains a C{generateFor} list appropriate for the scripts in that package. N)r%r&r'r(rrrr*r*(srr*) metaclassc t}|jtjd| t j |} |jgd|jd|jd|jdt|j!d|jd|j#t%|d r|j&D]\}}}} |j|d d d g|jd|jd|jdt|j!d|jd|j#|j)y#t $r}tjd|d}~wwxYw#t$r}tjd|fd}~wt$rYjwxYw#t$r}tjd |fd}~wt$rYwxYw)aI Generate completion functions for given twisted command - no errors should be raised @type cmdName: C{str} @param cmdName: The name of the command-line utility e.g. 'twistd' @type optionsFQPN: C{str} @param optionsFQPN: The Fully Qualified Python Name of the C{Options} class to be tested. _shellCompFilez.Couldn't import or instantiate Options class: N--_shell-completionzsh:2zSystemExit not raisedz&ImportError calling parseOptions(): %sr subCommandsr/r0zzsh:3z4ImportError calling parseOptions() on subcommand: %s)rpatchrOptionsrnamedAny Exceptionr SkipTest parseOptionsfail ImportError SystemExitseek assertEquallenreadtruncatehasattrr3 flushWarnings) rrr outputFileoecmdshortparserdocs rrr;sJJJu}}. ; )G  [ ) + + ;< )*OOAQJOOA./0OOAq- '(}} " #C 3R)>HI 12 OOA    QJOOA$6 7 8 OOA     !% ", ]  EF H    P H1$OO     ''MPQt  sSE>F')G> F$FF$' G0G GG H #G:: H H c^eZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZy)ZshTestsz' Tests for zsh completion code cTt}tj|dt}tj j j}|jtj j |j|j ||j|jttj j|j|jtj jtj jj}|jtj j|j|j||j|jtj jy)zi Are `compData' attributes you can place on Options classes picked up correctly? aceN)FighterAceExtendedOptionsrZshArgumentsGeneratorrFighterAceOptionscompData descriptionscopyupdater>multiUsesetmutuallyExclusive optActions extraActions)roptsagrSrYs rtest_accumulateMetadataz ZshTests.test_accumulateMetadatas# )*  - -dE79 E(11>>CCE 5>>KKL ,7 c*;*D*D*M*M&NO   "3"<"<"N"N '//::??A 3<<GGH  3 *;*D*D*Q*QRrcGddt}|}tj|dt}hdhdhdhdhdd }|j |j |y ) z Exercise a corner-case of ZshArgumentsGenerator.makeExcludesDict() where the long option name already exists in the `excludes` dict being built. c.eZdZgdgZegdgZy)GZshTests.test_mutuallyExclusiveCornerCase..OddFighterAceOptions)anatraNz0Select the Anatra DS as your dogfighter aircraft)rafokkeralbatrosspadbristolrXN)r%r&r'optFlagsr rRrrrOddFighterAceOptionsr`s! UH##V"WHrrhrN>bfsrdrarbre>arirjrkrdrbrerc>rlrjrkrdrarbrc>rlrirkrdrarerc>rlrirjrarbrerc)rcrarerbrdN)rOrrPrr>excludes)rrhr[r\expecteds r test_mutuallyExclusiveCornerCasez)ZshTests.test_mutuallyExclusiveCornerCases^ #< $%  - -dE79 EOSNNN   h/rc6t}tj|dt}|j d|j |j d|j |j d|j|j d|j y)z We pick up options that are only defined by having an appropriately named method on your Options class, e.g. def opt_foo(self, foo) rNnocrash difficultyN)rOrrPrassertInflagNameToDefinitionallOptionsNameToDefinitionparamNameToDefinition)rr[r\s r test_accumulateAdditionalOptionsz)ZshTests.test_accumulateAdditionalOptionssr )*  - -dE79 E i!8!89 i!>!>? lB$<$<= lB$A$ABrcGddt}|jttj|dt Gddt}|jttj|dt y)z\ Using a parameter/flag name that doesn't exist will raise an error c"eZdZeddiZy)0ZshTests.test_verifyZshNames..TmpOptionsdetaillN)rYr%r&r'r rRrrr TmpOptionsrzs"y$.?@Hrr}rNc eZdZedgZy)1ZshTests.test_verifyZshNames..TmpOptions2)foobarrfNr|rrr TmpOptions2rs#n5EFHrrN)rO assertRaises ValueErrorrrPr)rr}rs rtest_verifyZshNameszZshTests.test_verifyZshNamessn  A2 A   88*,wy  G3 G    , , M  I  rc(t}|jtjd||jtdgdt }|j t|j|jt|jy)zt Generate a completion function, and test the textual output against a known correct output r-argv)sillyr/r0r1N) rr4rr5sysSimpleProgOptionsrr<r9r> testOutput1getvaluerrDr[s r test_zshCodezZshTests.test_zshCodesh Y  5=="2J? 3 MN " *d&7&78 j&9&9&; testOutput2rrs rtest_zshCodeWithSubszZshTests.test_zshCodeWithSubssh Y  5=="2J? 3 NO(* *d&7&78 j&9&9&;r?r@rs rtest_incompleteCommandLinez#ZshTests.test_incompleteCommandLiness Y  5=="2J? "         C  234rclt}|jtjd|t }|j t |jgd|jd|jdt|jd|jd|jy)z Completion still happens even if a command-line is given that would normally throw UsageError. The existence of --unknown-option prior to the subcommand will break subcommand detection... but we complete anyway r-)rrr --list-serverr0rrr2N) rr4rr5rQrr<r9r=r>r?r@rArs r test_incompleteCommandLine_case2z)ZshTests.test_incompleteCommandLine_case2sY  5=="2J? "         C  234rc*t}|jtjd|t }|j t |jgd|jd|jdt|jdy)z Completion still happens even if a command-line is given that would normally throw UsageError. Break subcommand detection in a different way by providing an invalid subcommand name. r-)rzunknown-subcommandrr0zzsh:4rr2Nrrs r test_incompleteCommandLine_case3z)ZshTests.test_incompleteCommandLine_case34ssY  5=="2J? "         C  234rc*t}|jtjd|t }|j t |jgd|jd|jdt|jdy)z Ensure the optimization which skips building the subcommand list under certain conditions isn't broken. r-)z--albar0r1rr2Nrrs rtest_skipSubcommandListz ZshTests.test_skipSubcommandListOsp Y  5=="2J? "  ))+U   C  234rct}tj|dd}|jd}|j |dy)zY Test corner case fetching an option description from a method docstring rNNr)rQrrPgetDescriptionr>)rr[argGendescrs rtest_poorlyDescribedOptMethodz&ZshTests.test_poorlyDescribedOptMethod_sB!"11$tD%%g. (rcGddtj}t}|}|j|d||j t |j gdy)zu A C{Completer} with repeat=True may only be used as the last item in the extraActions list. cveZdZejej dej gZy)2ZshTests.test_brokenActions..BrokenActionsT)repeat)rZN)r%r&r'rr r rRrrr BrokenActionsrrs0(u((-eooT:OEOO.Options)flagrjA flag)parampNA paramcyzjunk descriptionNrrs ropt_flagz>ZshTests.test_optMethodsDontOverride..Options.opt_flagrcyrr)rrs r opt_paramz?ZshTests.test_optMethodsDontOverride..Options.opt_paramrrN)r%r&r'rg optParametersrrrrrr5rs/0H<=M ' 'rr5rNNrrrr)rr5rrPr>r)rr5r[rs rtest_optMethodsDontOverridez$ZshTests.test_optMethodsDontOverride~se  'emm 'y11$tD ..v6A ..w7CrN)r%r&r'r(r]rorwrrrrrrrrrrrrrrLrLsKS.0> C 6 = =52>565 ) $DrrLceZdZdZy) EscapeTestsctj}d}|j||dd}|j||dy)z5 Verify _shellcomp.escape() function $z'$'z A--'$"\`--Bz"A--'\$\"\\\`--B"N)rescaper>)rescr$s r test_escapezEscapeTests.test_escapesB TE* T$=>rN)r%r&r'rrrrrrs ?rrceZdZdZdZy)CompleterNotImplementedTestsz Test that using an unknown shell constant with SubcommandAction raises NotImplementedError The other Completer() subclasses are tested in test_usage.py cptj}|jt|jddy)zM Using an unknown shellType should raise NotImplementedError Nbad_shell_type)rSubcommandActionrNotImplementedError _shellCode)ractions rtest_unknownShellz.CompleterNotImplementedTests.test_unknownShells0,,.  !2!2D:J rN)r%r&r'r(rrrrrrs  rrc$eZdZdZgdgZgdgZy)FighterAceServerOptionsz4 Options for FighterAce 'server' subcommand )z list-serverNz3List this server with the online FighterAce network)zpackets-per-secondNz+Number of update packets to send per second20N)r%r&r'r(rgrrrrrrs  UH Mrrc eZdZUdZgdgdgdgdgdgdgdgZeeeeed <gd gd gZ eeeeed <d de dggZ e ddddggdgde dgiedgZdZy)rQzB Command-line options for an imaginary `Fighter Ace` game )rbrjz2Select the Fokker Dr.I as your dogfighter aircraft)rcrlz5Select the Albatros D-III as your dogfighter aircraft)rdrkz1Select the SPAD S.VII as your dogfighter aircraft)reriz4Select the Bristol Scout as your dogfighter aircraft)physicsrz$Enable secret Twisted physics engine)jamjz6Enable a small chance that your machine guns will jam!verbosevz1Verbose logging (may be specified more than once)rg)z pilot-nameNzWhat's your name, Ace?zManfred von Richthofen)detaildz*Select the level of rendering detail (1-5)3rrNzStart FighterAce game-server.zTwisted-PhysicszRendering detail level)rrr)rbrcrdrer12345zsaved game file to loadrrSrVrXrYrZcy) Nrrs r opt_sillyzFighterAceOptions.opt_sillyrr)r%r&r'r(rgrrstr__annotations__rrr3r r r rRrrrrrQrQs NRJP@NM+Hd4 &' QJ0M4Xc]+, 402QRK!2>VWDEl,?+@AB#*CDE HrrQcZeZdZdZgdgZgdgZeddidedi Zd Z d Z y ) rOz Extend the options and zsh metadata provided by FighterAceOptions. _shellcomp must accumulate options and metadata from all classes in the hiearchy so this is important to test. ) no-stallsNz+Turn off the ability to stall your aircraft) reality-levelNz)Select the level of physics reality (1-5)5rzCan't stall your planerzPhysics reality levelr)rSrYcy)z8 Select that you can't crash your plane Nrrs r opt_nocrashz%FighterAceExtendedOptions.opt_nocrashrrcy)z+ How tough are you? (1-10) Nr)rrrs ropt_difficultyz(FighterAceExtendedOptions.opt_difficultyrrN) r%r&r'r(rgrr r rRrrrrrrOrOsI SSHQM!#;<#Y5L%MNH   rrOc tgddS)N)12rz Accuracy'`?r)r rrr_accuracyActionr s } ==rc eZdZdZgdgdgdgZgdgdgZeddd d gd d ggegd dede dgZ dZ y)rz> Command-line options for a `Silly` imaginary program )colorczTurn on color output)graygzTurn on gray-scale outputr) optimizationNrz&Select the level of optimization (1-5))accuracyrlrz"Select the level of accuracy (1-3)zColor onzOptimization level)rrrrr)rrr4rz Optimization?r)rrz output filercy)zM usage.Options does not recognize single-letter opt_ methods Nrrs ropt_XzSimpleProgOptions.opt_X+rrN) r%r&r'r(rgrr r rr rRrrrrrrsw /2MH NDM );OP#V,-()(  $-89 H rrceZdZgdgZy)SimpleProgSub1)sub-optrkz Sub Opt OneNr%r&r'rgrrrrr1 /0HrrceZdZgdgZy)SimpleProgSub2)rrkz Sub Opt TwoNrrrrrr5rrrcFeZdZdgddggZdgddggdgZdd ed gd d ed ggZy ) rz some-optionz other-optionrEz some-paramz other-paramr)z another-paramPzYet Another Paramsub1Nz Sub Command 1sub2z Sub Command 2)r%r&r'rgrrrr3rrrrr9sM.#!67H  3M ~7 ~7Krrs#compdef silly _arguments -s -A "-*" \ ':output file (*):_files -g "*"' \ "(--accuracy)-a[Select the level of accuracy (1-3)]:Accuracy'\`?:(1 2 3)" \ "(-a)--accuracy=[Select the level of accuracy (1-3)]:Accuracy'\`?:(1 2 3)" \ '(--color --gray -g)-c[Color on]' \ '(--gray -c -g)--color[Color on]' \ '(--color --gray -c)-g[Turn on gray-scale output]' \ '(--color -c -g)--gray[Turn on gray-scale output]' \ '--help[Display this help and exit.]' \ '--optimization=[Optimization level]:Optimization?:(1 2 3 4 5)' \ '*-v[Verbose logging (may be specified more than once)]' \ '*--verbose[Verbose logging (may be specified more than once)]' \ '--version[Display Twisted version and exit.]' \ && return 0 s#compdef silly2 _arguments -s -A "-*" \ '*::subcmd:->subcmd' \ ':output file (*):_files -g "*"' \ "(--accuracy)-a[Select the level of accuracy (1-3)]:Accuracy'\`?:(1 2 3)" \ "(-a)--accuracy=[Select the level of accuracy (1-3)]:Accuracy'\`?:(1 2 3)" \ '(--another-param)-P[another-param]:another-param:_files' \ '(-P)--another-param=[another-param]:another-param:_files' \ '(--color --gray -g)-c[Color on]' \ '(--gray -c -g)--color[Color on]' \ '(--color --gray -c)-g[Turn on gray-scale output]' \ '(--color -c -g)--gray[Turn on gray-scale output]' \ '--help[Display this help and exit.]' \ '--optimization=[Optimization level]:Optimization?:(1 2 3 4 5)' \ '(--other-option)-o[other-option]' \ '(-o)--other-option[other-option]' \ '(--other-param)-p[other-param]:other-param:_files' \ '(-p)--other-param=[other-param]:other-param:_files' \ '--some-option[some-option]' \ '--some-param=[some-param]:some-param:_files' \ '*-v[Verbose logging (may be specified more than once)]' \ '*--verbose[Verbose logging (may be specified more than once)]' \ '--version[Display Twisted version and exit.]' \ && return 0 local _zsh_subcmds_array _zsh_subcmds_array=( "sub1:Sub Command 1" "sub2:Sub Command 2" ) _describe "sub-command" _zsh_subcmds_array )%r(riortypingrrtwisted.pythonrrrtwisted.python.usager r r r twisted.trialr rrr*rTestCaserLrrr5rrQrOrrrrrrrrrrr s  !55TT"55,#4&AHSDx  SDl ?(## ? 8#4#4 &emm$" "J 1 8>    F1U]]11U]]1  1  &  r