3
¦Yði¿>  ã               @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dl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 ejrŽddlmZ G d	d
„ d
ƒZe jeje ejd dœdd„ƒZG dd„ dejƒZejejeeejf  eejdœdd„Z G dd„ dƒZ!G dd„ dƒZ"dS )é    N)ÚTracebackTypeé   )Ú
formatting)Útermui)Úutils)Ú_find_binary_reader)ÚBaseCommandc               @   s°   e Zd Zejejddœdd„Zeejdœdd„Ze	e	dœd	d
„Z
dee	dœdd„Zdee	dœdd„Zdee	dœdd„Zeje	 dœdd„Zeje	 dœdd„Zedœdd„ZdS ) ÚEchoingStdinN)ÚinputÚoutputÚreturnc             C   s   || _ || _d| _d S )NF)Ú_inputÚ_outputÚ_paused)Úselfr
   r   © r   ú./tmp/pip-build-8om4l6ph/click/click/testing.pyÚ__init__   s    zEchoingStdin.__init__)Úxr   c             C   s   t | j|ƒS )N)Úgetattrr   )r   r   r   r   r   Ú__getattr__   s    zEchoingStdin.__getattr__)Úrvr   c             C   s   | j s| jj|ƒ |S )N)r   r   Úwrite)r   r   r   r   r   Ú_echo   s    zEchoingStdin._echor   )Únr   c             C   s   | j | jj|ƒƒS )N)r   r   Úread)r   r   r   r   r   r   #   s    zEchoingStdin.readc             C   s   | j | jj|ƒƒS )N)r   r   Úread1)r   r   r   r   r   r   &   s    zEchoingStdin.read1c             C   s   | j | jj|ƒƒS )N)r   r   Úreadline)r   r   r   r   r   r   )   s    zEchoingStdin.readline)r   c                s   ‡ fdd„ˆ j jƒ D ƒS )Nc                s   g | ]}ˆ j |ƒ‘qS r   )r   )Ú.0r   )r   r   r   ú
<listcomp>-   s    z*EchoingStdin.readlines.<locals>.<listcomp>)r   Ú	readlines)r   r   )r   r   r    ,   s    zEchoingStdin.readlinesc                s   t ‡ fdd„ˆ jD ƒƒS )Nc             3   s   | ]}ˆ j |ƒV  qd S )N)r   )r   r   )r   r   r   ú	<genexpr>0   s    z(EchoingStdin.__iter__.<locals>.<genexpr>)Úiterr   )r   r   )r   r   Ú__iter__/   s    zEchoingStdin.__iter__c             C   s
   t | jƒS )N)Úreprr   )r   r   r   r   Ú__repr__2   s    zEchoingStdin.__repr__éÿÿÿÿ)r&   r&   )r&   r&   )r&   )Ú__name__Ú
__module__Ú__qualname__ÚtÚBinaryIOr   ÚstrÚAnyr   Úbytesr   Úintr   r   r   ÚListr    ÚIteratorr#   r%   r   r   r   r   r	      s   r	   )Ústreamr   c             c   s&   | d krd V  nd| _ d V  d| _ d S )NTF)r   )r2   r   r   r   Ú_pause_echo6   s
    r3   c                   sR   e Zd Zejeeejddœ‡ fdd„Zeedœdd„ƒZ	eedœdd	„ƒZ
‡  ZS )
Ú_NamedTextIOWrapperN)ÚbufferÚnameÚmodeÚkwargsr   c                s    t ƒ j|f|Ž || _|| _d S )N)Úsuperr   Ú_nameÚ_mode)r   r5   r6   r7   r8   )Ú	__class__r   r   r   A   s    z_NamedTextIOWrapper.__init__)r   c             C   s   | j S )N)r:   )r   r   r   r   r6   H   s    z_NamedTextIOWrapper.namec             C   s   | j S )N)r;   )r   r   r   r   r7   L   s    z_NamedTextIOWrapper.mode)r'   r(   r)   r*   r+   r,   r-   r   Úpropertyr6   r7   Ú__classcell__r   r   )r<   r   r4   @   s   r4   )r
   Úcharsetr   c             C   sd   t | dƒr0ttjtj| ƒƒ}|d k	r(|S tdƒ‚| d kr>d} nt| tƒrR| j|ƒ} t	j
tjt| ƒƒS )Nr   z.Could not find binary reader for input stream.ó    )Úhasattrr   r*   ÚcastÚIOÚ	TypeErrorÚ
isinstancer,   ÚencodeÚioÚBytesIOr.   )r
   r?   r   r   r   r   Úmake_input_streamQ   s    


rI   c               @   s–   e Zd ZdZddeeje ejeeje	 ejej
eje	 e	ef  dœdd„Zeedœdd	„ƒZeedœd
d„ƒZeedœdd„ƒZedœdd„ZdS )ÚResultz3Holds the captured result of an invoked CLI script.NÚ	CliRunner)ÚrunnerÚstdout_bytesÚstderr_bytesÚreturn_valueÚ	exit_codeÚ	exceptionÚexc_infoc             C   s.   || _ || _|| _|| _|| _|| _|| _d S )N)rL   rM   rN   rO   rP   rQ   rR   )r   rL   rM   rN   rO   rP   rQ   rR   r   r   r   r   h   s    zResult.__init__)r   c             C   s   | j S )z(The (standard) output as unicode string.)Ústdout)r   r   r   r   r   …   s    zResult.outputc             C   s   | j j| jjdƒjddƒS )z&The standard output as unicode string.Úreplacez
Ú
)rM   ÚdecoderL   r?   rT   )r   r   r   r   rS   Š   s    zResult.stdoutc             C   s,   | j dkrtdƒ‚| j j| jjdƒjddƒS )z%The standard error as unicode string.Nzstderr not separately capturedrT   z
rU   )rN   Ú
ValueErrorrV   rL   r?   rT   )r   r   r   r   Ústderr‘   s    
zResult.stderrc             C   s,   | j rt| j ƒnd}dt| ƒj› d|› d�S )NÚokayú<ú ú>)rQ   r$   Útyper'   )r   Zexc_strr   r   r   r%   š   s    zResult.__repr__)N)r'   r(   r)   Ú__doc__r.   r*   ÚOptionalr-   r/   ÚBaseExceptionÚTupleÚTyper   r   r=   r,   r   rS   rX   r%   r   r   r   r   rJ   e   s   BrJ   c            
   @   sv  e Zd ZdZdeejejeeje f  eeddœdd„Z	d	ed
œdd„Z
dejejeeje f  ejeeje f dœdd„Zejdejejeeejf  ejejeeje f  eejejejejej f  dœdd„ƒZdd	ejejeeje f  ejejeeejf  ejejeeje f  eeejedœdd„Zejdejejeejf  eje dœdd„ƒZdS )rK   aø  The CLI runner provides functionality to invoke a Click command line
    script for unittesting purposes in a isolated environment.  This only
    works in single-threaded systems without any concurrency as it changes the
    global interpreter state.

    :param charset: the character set for the input and output data.
    :param env: a dictionary with environment variables for overriding.
    :param echo_stdin: if this is set to `True`, then reading from stdin writes
                       to stdout.  This is useful for showing examples in
                       some circumstances.  Note that regular prompts
                       will automatically echo the input.
    :param mix_stderr: if this is set to `False`, then stdout and stderr are
                       preserved as independent streams.  This is useful for
                       Unix-philosophy apps that have predictable stdout and
                       noisy stderr, such that each may be measured
                       independently
    úutf-8NFT)r?   ÚenvÚ
echo_stdinÚ
mix_stderrr   c             C   s    || _ |pi | _|| _|| _d S )N)r?   rd   re   rf   )r   r?   rd   re   rf   r   r   r   r   ²   s    
zCliRunner.__init__r   )Úclir   c             C   s
   |j pdS )zžGiven a command object it will return the default program name
        for it.  The default is the `name` attribute or ``"root"`` if not
        set.
        Úroot)r6   )r   rg   r   r   r   Úget_default_prog_name¾   s    zCliRunner.get_default_prog_name)Ú	overridesr   c             C   s   t | jƒ}|r|j|ƒ |S )z8Returns the environment overrides for invoking a script.)Údictrd   Úupdate)r   rj   r   r   r   r   Úmake_envÅ   s    

zCliRunner.make_env)r
   rd   Úcolorr   c             #   s€  t || jƒ}d}tj}tj}tj}tj}	dt_| j|ƒ}t	j
ƒ }
| jr^tjtjt||
ƒƒ }}t|| jddd� t_‰| jr‚dˆ_t|
| jddd�t_d}| jrªtjt_nt	j
ƒ }t|| jd	dd
d�t_t|ƒdtjt tdœ‡fdd„ƒ}t|ƒdtjt tdœ‡fdd„ƒ}t|ƒttdœdd„ƒ}|‰ dtjtj tjt tdœ‡ fdd„}tj}tj}tj}tj}|t_|t_|t_|t_i }zpx`|jƒ D ]T\}}tj j!|ƒ||< |dk�rÚytj |= W n t"k
�rÖ   Y nX n
|tj |< �q’W |
|fV  W dxP|jƒ D ]D\}}|dk�r:ytj |= W n t"k
�r6   Y nX n
|tj |< �qW |t_|t_|t_|t_|t_|t_|t_|	t_X dS )an  A context manager that sets up the isolation for invoking of a
        command line tool.  This sets up stdin with the given input data
        and `os.environ` with the overrides from the given dictionary.
        This also rebinds some internals in Click to be mocked (like the
        prompt functionality).

        This is automatically done in the :meth:`invoke` method.

        :param input: the input stream to put into sys.stdin.
        :param env: the environment overrides as dictionary.
        :param color: whether the output should contain color codes. The
                      application can still override this explicitly.

        .. versionchanged:: 8.0
            ``stderr`` is opened with ``errors="backslashreplace"``
            instead of the default ``"strict"``.

        .. versionchanged:: 4.0
            Added the ``color`` parameter.
        NéP   z<stdin>Úr)Úencodingr6   r7   r   z<stdout>Úwz<stderr>Úbackslashreplace)rq   r6   r7   Úerrors)Úpromptr   c                s>   t jj| pdƒ ˆ jƒ jdƒ}t jj|› d�ƒ t jjƒ  |S )NÚ z
rU   )ÚsysrS   r   r   ÚrstripÚflush)ru   Úval)Ú
text_inputr   r   Úvisible_input  s
    
z*CliRunner.isolation.<locals>.visible_inputc                s.   t jj| pd› d�ƒ t jjƒ  ˆ jƒ jdƒS )Nrv   rU   z
)rw   rS   r   ry   r   rx   )ru   )r{   r   r   Úhidden_input  s    
z)CliRunner.isolation.<locals>.hidden_input)Úechor   c             S   s*   t jjdƒ}| rt jj|ƒ t jjƒ  |S )Nr   )rw   Ústdinr   rS   r   ry   )r~   Úcharr   r   r   Ú_getchar#  s
    
z%CliRunner.isolation.<locals>._getchar)r2   rn   r   c                s   |d krˆ  S | S )Nr   )r2   rn   )Údefault_colorr   r   Úshould_strip_ansi/  s    z.CliRunner.isolation.<locals>.should_strip_ansi)N)N)NN)#rI   r?   rw   r   rS   rX   r   ZFORCED_WIDTHrm   rG   rH   re   r*   rB   r+   r	   r4   Ú_CHUNK_SIZErf   r3   r_   r,   ÚboolrC   r   Zvisible_prompt_funcZhidden_prompt_funcr�   r   rƒ   ÚitemsÚosÚenvironÚgetÚ	Exception)r   r
   rd   rn   Zbytes_inputZ
echo_inputZ	old_stdinZ
old_stdoutZ
old_stderrZold_forced_widthZbytes_outputZbytes_errorr|   r}   r�   rƒ   Zold_visible_prompt_funcZold_hidden_prompt_funcZold__getchar_funcZold_should_strip_ansiZold_envÚkeyÚvaluer   )r‚   r{   r   Ú	isolationÎ   sŽ    


	

zCliRunner.isolation)rg   Úargsr
   rd   Úcatch_exceptionsrn   Úextrar   c          ,   K   s¬  d}| j |||d���z}	d}
d}d}t|tƒr8tj|ƒ}y|jdƒ}W n tk
rd   | j|ƒ}Y nX zòy|jf |pvf |dœ|—Ž}
W nÎ t	k
�r } zvt
jƒ }tjtjtjttjf  |jƒ}|dkrÎd}|dkrÚ|}t|tƒ�st
jjt|ƒƒ t
jjdƒ d}|}W Y dd}~X n< tk
�rT } z|�s4‚ |}d}t
jƒ }W Y dd}~X nX W dt
jjƒ  |	d jƒ }| j�r~d}n|	d jƒ }X W dQ R X t| |||
|||d�S )	aÚ  Invokes a command in an isolated environment.  The arguments are
        forwarded directly to the command line script, the `extra` keyword
        arguments are passed to the :meth:`~clickpkg.Command.main` function of
        the command.

        This returns a :class:`Result` object.

        :param cli: the command to invoke
        :param args: the arguments to invoke. It may be given as an iterable
                     or a string. When given as string it will be interpreted
                     as a Unix shell command. More details at
                     :func:`shlex.split`.
        :param input: the input data for `sys.stdin`.
        :param env: the environment overrides.
        :param catch_exceptions: Whether to catch any other exceptions than
                                 ``SystemExit``.
        :param extra: the keyword arguments to pass to :meth:`main`.
        :param color: whether the output should contain color codes. The
                      application can still override this explicitly.

        .. versionchanged:: 8.0
            The result object has the ``return_value`` attribute with
            the value returned from the invoked command.

        .. versionchanged:: 4.0
            Added the ``color`` parameter.

        .. versionchanged:: 3.0
            Added the ``catch_exceptions`` parameter.

        .. versionchanged:: 3.0
            The result object has the ``exc_info`` attribute with the
            traceback if available.
        N)r
   rd   rn   r   Ú	prog_name)rŽ   r‘   rU   r   )rL   rM   rN   rO   rP   rQ   rR   )r�   rE   r,   ÚshlexÚsplitÚpopÚKeyErrorri   ÚmainÚ
SystemExitrw   rR   r*   rB   r_   ÚUnionr/   r-   ÚcoderS   r   rŠ   ry   Úgetvaluerf   rJ   )r   rg   rŽ   r
   rd   r�   rn   r�   rR   Z
outstreamsrO   rQ   rP   r‘   ÚeZe_coderS   rX   r   r   r   Úinvoke]  sX    ,

 
zCliRunner.invoke)Útemp_dirr   c             c   sn   t jƒ }tj|d�}t j|ƒ ztjt|ƒV  W dt j|ƒ |dkrhytj	|ƒ W n t
k
rf   Y nX X dS )aã  A context manager that creates a temporary directory and
        changes the current working directory to it. This isolates tests
        that affect the contents of the CWD to prevent them from
        interfering with each other.

        :param temp_dir: Create the temporary directory under this
            directory. If given, the created directory is not removed
            when exiting.

        .. versionchanged:: 8.0
            Added the ``temp_dir`` parameter.
        )ÚdirN)r‡   ÚgetcwdÚtempfileÚmkdtempÚchdirr*   rB   r,   ÚshutilÚrmtreeÚOSError)r   r�   ÚcwdÚdtr   r   r   Úisolated_filesystemÂ  s    

zCliRunner.isolated_filesystem)rc   NFT)N)NNF)NNNTF)N)r'   r(   r)   r^   r,   r*   r_   ÚMappingr…   r   ri   rm   Ú
contextlibÚcontextmanagerr˜   r.   rC   r1   ra   rG   rH   r�   ÚSequencer-   rJ   rœ   r‡   ÚPathLiker¨   r   r   r   r   rK   Ÿ   s2         2*     N\rK   )#rª   rG   r‡   r’   r£   rw   r    Útypingr*   Útypesr   rv   r   r   r   Ú_compatr   ZTYPE_CHECKINGÚcorer   r	   r«   r_   r1   r3   ÚTextIOWrapperr4   r˜   r,   r.   rC   r+   rI   rJ   rK   r   r   r   r   Ú<module>   s,   "	: