o
    bR                     @   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  %#