o
    ¯b¯R  ã                   @   sN  d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	m
Z
 ddlmZmZmZmZ ddlmZ G dd	„ d	eƒZG d
d„ ded�Zdd„ ZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ de
jƒZG dd„ de
jƒZG dd„ deƒZdd„ ZG dd„ de
jƒZG dd „ d e
jƒZ G d!d"„ d"e
jƒZ!G d#d$„ d$eƒZ"d%Z#d&Z$dS )'z*
Test cases for twisted.python._shellcomp
é    N)ÚBytesIO)ÚListÚOptional)Ú
_shellcompÚreflectÚusage)ÚCompleteFilesÚCompleteListÚ	CompleterÚCompletions)Úunittestc                   @   ó   e Zd ZdZdd„ ZdS )ÚZshScriptTestMetaz*
    Metaclass of ZshScriptTestMixin.
    c                 C   sH   dd„ }d|v r|d D ]\}}|||ƒ}||d| < qt  | |||¡S )Nc                    s   ‡ ‡fdd„}|S )Nc                    s   t | ˆ ˆƒS )N)Útest_genZshFunction©Úself©ÚcmdNameÚoptionsFQPN© úD/usr/lib/python3/dist-packages/twisted/python/test/test_shellcomp.pyÚrunTest   s   z<ZshScriptTestMeta.__new__.<locals>.makeTest.<locals>.runTestr   )r   r   r   r   r   r   ÚmakeTest   s   z+ZshScriptTestMeta.__new__.<locals>.makeTestÚgenerateForÚtest_genZshFunction_)ÚtypeÚ__new__)ÚclsÚnameÚbasesÚattrsr   r   r   Útestr   r   r   r      s   
zZshScriptTestMeta.__new__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZdS )Ú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%   r   r   r   r   r&   (   s    r&   )Ú	metaclassc           
      C   sŠ  t ƒ }|  tjd|¡ zt |¡ƒ }W n ty' } zt d|f ¡‚d}~ww z	| 	g d¢¡ W n t
yD } zt d|f¡‚d}~w tyL   Y nw |  d¡ | d¡ |  dt| d¡ƒ¡ | d¡ | ¡  t|d	ƒr¿|jD ]K\}}}}	z| 	|d
ddg¡ W n t
y— } zt d|f¡‚d}~w tyŸ   Y nw |  d¡ | d¡ |  dt| d¡ƒ¡ | d¡ | ¡  qs|  ¡  dS )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.
    Ú_shellCompFilez0Couldn't import or instantiate Options class: %sN©Ú ú--_shell-completionúzsh:2z&ImportError calling parseOptions(): %szSystemExit not raisedr   é   ÚsubCommandsr*   r+   zzsh:3z4ImportError calling parseOptions() on subcommand: %s)r   Úpatchr   ÚOptionsr   ÚnamedAnyÚ	Exceptionr   ÚSkipTestÚparseOptionsÚImportErrorÚ
SystemExitÚfailÚseekÚassertEqualÚlenÚreadÚtruncateÚhasattrr.   ÚflushWarnings)
r   r   r   Ú
outputFileÚoÚeÚcmdÚshortÚparserÚdocr   r   r   r   ;   sR   ÿ€ÿ€ÿ



ÿ€ÿ



r   c                   @   sx   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚZshTestsz'
    Tests for zsh completion code
    c                 C   s¤   t ƒ }t |dtƒ ¡}tjj ¡ }| t jj¡ |  	|j|¡ |  	|j
ttjj
ƒ¡ |  	|jtjj¡ tjj ¡ }| t jj¡ |  	|j|¡ |  	|jtjj¡ dS )zi
        Are `compData' attributes you can place on Options classes
        picked up correctly?
        ÚaceN)ÚFighterAceExtendedOptionsr   ÚZshArgumentsGeneratorr   ÚFighterAceOptionsÚcompDataÚdescriptionsÚcopyÚupdater9   ÚmultiUseÚsetÚmutuallyExclusiveÚ
optActionsÚextraActions)r   ÚoptsÚagrL   rR   r   r   r   Útest_accumulateMetadata„   s   
ÿz ZshTests.test_accumulateMetadatac                 C   s\   G dd„ dt ƒ}|ƒ }t |dtƒ ¡}h d£h d£h d£h d£h d£d	œ}|  |j|¡ d
S )z²
        Exercise a corner-case of ZshArgumentsGenerator.makeExcludesDict()
        where the long option name already exists in the `excludes` dict being
        built.
        c                   @   s&   e Zd Zg d¢gZeg d¢gd�ZdS )zGZshTests.test_mutuallyExclusiveCornerCase.<locals>.OddFighterAceOptions)ÚanatraNz0Select the Anatra DS as your dogfighter aircraft)rW   ÚfokkerÚalbatrosÚspadÚbristol©rQ   N)r"   r#   r$   ÚoptFlagsr   rK   r   r   r   r   ÚOddFighterAceOptions¢   s    ÿ
ÿr^   rG   >   ÚbÚfÚsrZ   rW   rX   r[   >   Úar_   r`   ra   rZ   rX   r[   rY   >   rb   r`   ra   rZ   rW   rX   rY   >   rb   r_   ra   rZ   rW   r[   rY   >   rb   r_   r`   rW   rX   r[   rY   )rY   rW   r[   rX   rZ   N)rH   r   rI   r   r9   Úexcludes)r   r^   rT   rU   Úexpectedr   r   r   Ú test_mutuallyExclusiveCornerCase›   s   ûz)ZshTests.test_mutuallyExclusiveCornerCasec                 C   sR   t ƒ }t |dtƒ ¡}|  d|j¡ |  d|j¡ |  d|j¡ |  d|j¡ dS )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)
        rG   ÚnocrashÚ
difficultyN)rH   r   rI   r   ÚassertInÚflagNameToDefinitionÚallOptionsNameToDefinitionÚparamNameToDefinition)r   rT   rU   r   r   r   Ú test_accumulateAdditionalOptionsº   s   z)ZshTests.test_accumulateAdditionalOptionsc                 C   sT   G dd„ dt ƒ}|  ttj|ƒ dtƒ ¡ G dd„ dt ƒ}|  ttj|ƒ dtƒ ¡ dS )z\
        Using a parameter/flag name that doesn't exist
        will raise an error
        c                   @   s   e Zd Zeddid�ZdS )z0ZshTests.test_verifyZshNames.<locals>.TmpOptionsÚdetaillN)rR   ©r"   r#   r$   r   rK   r   r   r   r   Ú
TmpOptionsÏ   s    ro   rG   c                   @   s   e Zd Zedgd�ZdS )z1ZshTests.test_verifyZshNames.<locals>.TmpOptions2)ÚfooÚbarr\   Nrn   r   r   r   r   ÚTmpOptions2×   s    rr   N)rH   ÚassertRaisesÚ
ValueErrorr   rI   r   )r   ro   rr   r   r   r   Útest_verifyZshNamesÉ   s   ÿûzZshTests.test_verifyZshNamesc                 C   óP   t ƒ }|  tjd|¡ |  tdg d¢¡ tƒ }|  t|j¡ |  	t
| ¡ ¡ dS )zt
        Generate a completion function, and test the textual output
        against a known correct output
        r(   Úargv)Úsillyr*   r+   r,   N)r   r/   r   r0   ÚsysÚSimpleProgOptionsrs   r6   r4   r9   ÚtestOutput1Úgetvalue©r   r?   rT   r   r   r   Útest_zshCodeä   ó   zZshTests.test_zshCodec                 C   rv   )z…
        Generate a completion function with subcommands,
        and test the textual output against a known correct output
        r(   rw   )Úsilly2r*   r+   r,   N)r   r/   r   r0   ry   ÚSimpleProgWithSubcommandsrs   r6   r4   r9   ÚtestOutput2r|   r}   r   r   r   Útest_zshCodeWithSubsð   r   zZshTests.test_zshCodeWithSubsc                 C   óT   t ƒ }|  tjd|¡ tƒ }|  t|jg d¢¡ | d¡ |  	dt
| d¡ƒ¡ dS )zx
        Completion still happens even if a command-line is given
        that would normally throw UsageError.
        r(   )ú--fokkerÚserverú--unknown-optionz--unknown-option2r+   úzsh:5r   r-   N©r   r/   r   r0   rJ   rs   r6   r4   r8   r9   r:   r;   r}   r   r   r   Útest_incompleteCommandLineü   s   ý
z#ZshTests.test_incompleteCommandLinec                 C   sf   t ƒ }|  tjd|¡ tƒ }|  t|jg d¢¡ | d¡ |  	dt
| d¡ƒ¡ | d¡ | ¡  dS )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(   )r…   r‡   r†   ú--list-serverr+   rˆ   r   r-   N)r   r/   r   r0   rJ   rs   r6   r4   r8   r9   r:   r;   r<   r}   r   r   r   Ú test_incompleteCommandLine_case2  s   ý

z)ZshTests.test_incompleteCommandLine_case2c                 C   r„   )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(   )r…   zunknown-subcommandr‹   r+   zzsh:4r   r-   Nr‰   r}   r   r   r   Ú test_incompleteCommandLine_case34  s   ý
z)ZshTests.test_incompleteCommandLine_case3c                 C   r„   )z�
        Ensure the optimization which skips building the subcommand list
        under certain conditions isn't broken.
        r(   )z--albar+   r,   r   r-   Nr‰   r}   r   r   r   Útest_skipSubcommandListO  s   ÿ
z ZshTests.test_skipSubcommandListc                 C   s.   t ƒ }t |dd¡}| d¡}|  |d¡ dS )zY
        Test corner case fetching an option description from a method docstring
        rG   Nrx   )rJ   r   rI   ÚgetDescriptionr9   )r   rT   ÚargGenÚdescrr   r   r   Útest_poorlyDescribedOptMethod_  s   
z&ZshTests.test_poorlyDescribedOptMethodc                 C   sD   G dd„ dt jƒ}tƒ }|ƒ }|  |d|¡ |  t|jg d¢¡ dS )zu
        A C{Completer} with repeat=True may only be used as the
        last item in the extraActions list.
        c                   @   s(   e Zd Zejejdd�e ¡ gd�ZdS )z2ZshTests.test_brokenActions.<locals>.BrokenActionsT)Úrepeat)rS   N)r"   r#   r$   r   r   r
   rK   r   r   r   r   ÚBrokenActionsr  s    
ÿr”   r(   r)   N)r   r0   r   r/   rs   rt   r4   )r   r”   r?   rT   r   r   r   Útest_brokenActionsl  s   ÿzZshTests.test_brokenActionsc                 C   sN   G dd„ dt jƒ}|ƒ }t |dd¡}|  | d¡d¡ |  | d¡d¡ dS )	z~
        opt_* methods on Options classes should not override the
        data provided in optFlags or optParameters.
        c                   @   s0   e Zd Zg d¢gZg d¢gZdd„ Zdd„ ZdS )z5ZshTests.test_optMethodsDontOverride.<locals>.Options)Úflagr`   úA flag)ÚparamÚpNúA paramc                 S   ó   dS ©zjunk descriptionNr   r   r   r   r   Úopt_flagˆ  ó    z>ZshTests.test_optMethodsDontOverride.<locals>.Options.opt_flagc                 S   r›   rœ   r   )r   r˜   r   r   r   Ú	opt_param‹  rž   z?ZshTests.test_optMethodsDontOverride.<locals>.Options.opt_paramN)r"   r#   r$   r]   ÚoptParametersr�   rŸ   r   r   r   r   r0   „  s
    

r0   rG   Nr–   r—   r˜   rš   )r   r0   r   rI   r9   r�   )r   r0   rT   r�   r   r   r   Útest_optMethodsDontOverride~  s
   
z$ZshTests.test_optMethodsDontOverrideN)r"   r#   r$   r%   rV   re   rl   ru   r~   rƒ   rŠ   rŒ   r�   rŽ   r’   r•   r¡   r   r   r   r   rF      s    rF   c                   @   s   e Zd Zdd„ ZdS )ÚEscapeTestsc                 C   s2   t j}d}|  ||ƒd¡ d}|  ||ƒd¡ dS )z5
        Verify _shellcomp.escape() function
        ú$z'$'zA--'$"\`--Bz"A--'\$\"\\\`--B"N)r   Úescaper9   )r   Úescr!   r   r   r   Útest_escape–  s
   zEscapeTests.test_escapeN)r"   r#   r$   r¦   r   r   r   r   r¢   •  s    r¢   c                   @   r   )ÚCompleterNotImplementedTestszª
    Test that using an unknown shell constant with SubcommandAction
    raises NotImplementedError

    The other Completer() subclasses are tested in test_usage.py
    c                 C   s   t  ¡ }|  t|jdd¡ dS )zM
        Using an unknown shellType should raise NotImplementedError
        NÚbad_shell_type)r   ÚSubcommandActionrs   ÚNotImplementedErrorÚ
_shellCode)r   Úactionr   r   r   Útest_unknownShell«  s   
ÿz.CompleterNotImplementedTests.test_unknownShellN)r"   r#   r$   r%   r­   r   r   r   r   r§   £  s    r§   c                   @   s$   e Zd ZdZg d¢gZg d¢gZdS )ÚFighterAceServerOptionsz4
    Options for FighterAce 'server' subcommand
    )zlist-serverNz3List this server with the online FighterAce network)zpackets-per-secondNz+Number of update packets to send per secondÚ20N)r"   r#   r$   r%   r]   r    r   r   r   r   r®   ¶  s    ÿÿr®   c                   @   sÀ   e Zd ZU dZg d¢g d¢g d¢g d¢g d¢g d¢g d¢gZeeee   ed	< g d
¢g d¢gZ	eeee   ed< dde
dggZedddœdgg d¢gdedgƒiedd�gd�Zdd„ ZdS )rJ   zB
    Command-line options for an imaginary `Fighter Ace` game
    )rX   r`   z2Select the Fokker Dr.I as your dogfighter aircraft)rY   rb   z5Select the Albatros D-III as your dogfighter aircraft)rZ   ra   z1Select the SPAD S.VII as your dogfighter aircraft)r[   r_   z4Select the Bristol Scout as your dogfighter aircraft)Úphysicsr™   z$Enable secret Twisted physics engine)ÚjamÚjz6Enable a small chance that your machine guns will jam!©ÚverboseÚvz1Verbose logging (may be specified more than once)r]   )z
pilot-nameNzWhat's your name, Ace?zManfred von Richthofen)ÚdetailÚdz*Select the level of rendering detail (1-5)Ú3r    r†   NzStart FighterAce game-server.zTwisted-PhysicszRendering detail level)r°   r¶   r´   )rX   rY   rZ   r[   r¶   Ú12345zsaved game file to load©r‘   ©rL   rO   rQ   rR   rS   c                 C   r›   )ú Nr   r   r   r   r   Ú	opt_sillyè  rž   zFighterAceOptions.opt_silly)r"   r#   r$   r%   r]   r   r   ÚstrÚ__annotations__r    r®   r.   r   r	   r   rK   r½   r   r   r   r   rJ   È  s.   
 ùþ
ÿ
ûrJ   c                   @   sN   e Zd ZdZg d¢gZg d¢gZeddidedd�id	�Zd
d„ Z	dd„ Z
dS )rH   zÃ
    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)Ú5rÀ   zCan't stall your planerÁ   zPhysics reality levelrº   )rL   rR   c                 C   r›   )z8
        Select that you can't crash your plane
        Nr   r   r   r   r   Úopt_nocrashþ  rž   z%FighterAceExtendedOptions.opt_nocrashc                 C   r›   )z+
        How tough are you? (1-10)
        Nr   )r   rg   r   r   r   Úopt_difficulty  rž   z(FighterAceExtendedOptions.opt_difficultyN)r"   r#   r$   r%   r]   r    r   r
   rK   rÃ   rÄ   r   r   r   r   rH   í  s    
ÿþrH   c                   C   s   t g d¢dd�S )N)Ú1Ú2r¸   zAccuracy'`?rº   )r	   r   r   r   r   Ú_accuracyAction	  s   rÇ   c                   @   sx   e Zd ZdZg d¢g d¢g d¢gZg d¢g d¢gZeddd	œd
gddggeg d¢dd�edœe	dd�gd�Z
dd„ ZdS )rz   z>
    Command-line options for a `Silly` imaginary program
    )ÚcolorÚczTurn on color output)ÚgrayÚgzTurn on gray-scale outputr³   )ÚoptimizationNrÂ   z&Select the level of optimization (1-5))Úaccuracyrb   r¸   z"Select the level of accuracy (1-3)zColor onzOptimization level)rÈ   rÌ   r´   rÈ   rÊ   )rÅ   rÆ   r¸   Ú4rÂ   zOptimization?rº   )rÌ   rÍ   zoutput filer»   c                 C   r›   )zM
        usage.Options does not recognize single-letter opt_ methods
        Nr   r   r   r   r   Úopt_X+  rž   zSimpleProgOptions.opt_XN)r"   r#   r$   r%   r]   r    r   r	   rÇ   r   rK   rÏ   r   r   r   r   rz     s*    ýþÿü
örz   c                   @   ó   e Zd Zg d¢gZdS )ÚSimpleProgSub1)úsub-optra   zSub Opt OneN©r"   r#   r$   r]   r   r   r   r   rÑ   1  ó    rÑ   c                   @   rÐ   )ÚSimpleProgSub2)rÒ   ra   zSub Opt TwoNrÓ   r   r   r   r   rÕ   5  rÔ   rÕ   c                   @   sF   e Zd ZdgddggZdgddgg d¢gZdd	ed
gdd	edggZd	S )r�   zsome-optionzother-optionr@   z
some-paramzother-paramr™   )zanother-paramÚPzYet Another ParamÚsub1NzSub Command 1Úsub2zSub Command 2)r"   r#   r$   r]   r    rÑ   rÕ   r.   r   r   r   r   r�   9  s    ý

þr�   sº  #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%   ry   Úior   Útypingr   r   Útwisted.pythonr   r   r   Útwisted.python.usager   r	   r
   r   Útwisted.trialr   r   r   r&   r   ÚTestCaserF   r¢   r§   r0   r®   rJ   rH   rÇ   rz   rÑ   rÕ   r�   r{   r‚   r   r   r   r   Ú<module>   s2   D  %#