
¿«‘_62  ã               @   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 m 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 rÁ d  d	 l m Z n d  d l Z d d
 l
 m Z Gd d „  d e ƒ Z d d „  Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z d S)é    Né   )Ú
formatting)Útermui)Úutils)Ú	iteritems)ÚPY2)Ústring_types)ÚStringIO)Ú_find_binary_readerc               @   sv   e  Z d  Z d d „  Z d d „  Z d d „  Z d d d	 „ Z d d
 d „ Z d d „  Z d d „  Z	 d d „  Z
 d S)ÚEchoingStdinc             C   s   | |  _  | |  _ d  S)N)Ú_inputÚ_output)ÚselfÚinputÚoutput© r   ú./tmp/pip-build-5gj8f0j9/click/click/testing.pyÚ__init__   s    	zEchoingStdin.__init__c             C   s   t  |  j | ƒ S)N)Úgetattrr   )r   Úxr   r   r   Ú__getattr__   s    zEchoingStdin.__getattr__c             C   s   |  j  j | ƒ | S)N)r   Úwrite)r   Úrvr   r   r   Ú_echo   s    zEchoingStdin._echor   c             C   s   |  j  |  j j | ƒ ƒ S)N)r   r   Úread)r   Únr   r   r   r   #   s    zEchoingStdin.readc             C   s   |  j  |  j j | ƒ ƒ S)N)r   r   Úreadline)r   r   r   r   r   r   &   s    zEchoingStdin.readlinec                s    ‡  f d d †  ˆ  j  j ƒ  Dƒ S)Nc                s   g  |  ] } ˆ  j  | ƒ ‘ q Sr   )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>-   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__/   s    zEchoingStdin.__repr__Néÿÿÿÿr%   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r"   r$   r   r   r   r   r      s   r   c             C   s‘   t  |  d ƒ rA t r |  St |  ƒ } | d  k	 r5 | St d ƒ ‚ |  d  k rV d }  n t |  t ƒ st |  j | ƒ }  t r„ t |  ƒ St j	 |  ƒ S)Nr   z.Could not find binary reader for input stream.ó    )
Úhasattrr   r
   Ú	TypeErrorÚ
isinstanceÚbytesÚencoder	   ÚioÚBytesIO)r   Úcharsetr   r   r   r   Úmake_input_stream3   s    	
r2   c               @   sg   e  Z d  Z d Z d d d „ Z e d d „  ƒ Z e d d „  ƒ Z e d	 d
 „  ƒ Z d d „  Z	 d S)ÚResultz3Holds the captured result of an invoked CLI script.Nc             C   s:   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ d  S)N)ÚrunnerÚstdout_bytesÚstderr_bytesÚ	exit_codeÚ	exceptionÚexc_info)r   r4   r5   r6   r7   r8   r9   r   r   r   r   I   s    					zResult.__init__c             C   s   |  j  S)z(The (standard) output as unicode string.)Ústdout)r   r   r   r   r   Y   s    zResult.outputc             C   s%   |  j  j |  j j d ƒ j d d ƒ S)z&The standard output as unicode string.Úreplacez
Ú
)r5   Údecoder4   r1   r;   )r   r   r   r   r:   ^   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 capturedr;   z
r<   )r6   Ú
ValueErrorr=   r4   r1   r;   )r   r   r   r   Ústderre   s    zResult.stderrc             C   s1   d j  t |  ƒ j |  j r* t |  j ƒ n d ƒ S)Nz<{} {}>Úokay)ÚformatÚtyper&   r8   r#   )r   r   r   r   r$   n   s    zResult.__repr__)
r&   r'   r(   Ú__doc__r   Úpropertyr   r:   r?   r$   r   r   r   r   r3   F   s   	r3   c               @   s—   e  Z d  Z d Z d d d d d d „ Z d d „  Z d d	 d
 „ Z e j d d d d d „ ƒ Z	 d d d d d d d „ Z
 e j d d „  ƒ Z d S)Ú	CliRunnera™  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.  This is
                    UTF-8 by default and should not be changed currently as
                    the reporting to Click only works in Python 2 properly.
    :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
    NFTc             C   s@   | d  k r d } | |  _  | p$ i  |  _ | |  _ | |  _ d  S)Nzutf-8)r1   ÚenvÚ
echo_stdinÚ
mix_stderr)r   r1   rF   rG   rH   r   r   r   r   ‰   s    		zCliRunner.__init__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)Úname)r   Úclir   r   r   Úget_default_prog_name‘   s    zCliRunner.get_default_prog_namec             C   s&   t  |  j ƒ } | r" | j | ƒ | S)z8Returns the environment overrides for invoking a script.)ÚdictrF   Úupdate)r   Z	overridesr   r   r   r   Úmake_env˜   s    zCliRunner.make_envc             #   s  t  ˆ |  j ƒ ‰ t j } t j } t j } t j } d t _ |  j | ƒ } t	 rœ t
 ƒ  } |  j ru t ˆ | ƒ ‰ | t _ |  j s#t
 ƒ  }	 |	 t _ n‡ t j ƒ  } |  j rÀ t ˆ | ƒ ‰ t j ˆ d |  j ƒ‰ t j | d |  j ƒt _ |  j s#t j ƒ  }	 t j |	 d |  j ƒt _ |  j r8t j t _ ˆ t _ d ‡ f d d † }
 d ‡ f d d † } d d	 „  } | ‰  d d ‡  f d
 d † } t j } t j } t j } t j } |
 t _ | t _ | t _ | t _ i  } z‹ xo t | ƒ D]a \ } } t j j | ƒ | | <| d k rGy t j | =WqTt k
 rCYqTXqó| t j | <qóW| |  j oh|	 f VWd xY t | ƒ D]K \ } } | d k r¼y t j | =WqÉt k
 r¸YqÉXq~| t j | <q~W| t _ | t _ | t _ | t _ | t _ | t _ | t _ | t _ Xd S)aÙ  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.

        .. versionadded:: 4.0
           The ``color`` parameter was added.

        :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.
        éP   ÚencodingNc                sU   t  j j |  p d ƒ ˆ  j ƒ  j d ƒ } t  j j d j | ƒ ƒ t  j j ƒ  | S)NÚ z
z{}
)Úsysr:   r   r   ÚrstriprA   Úflush)ÚpromptÚval)r   r   r   Úvisible_inputÒ   s
    z*CliRunner.isolation.<locals>.visible_inputc                s?   t  j j d j |  p d ƒ ƒ t  j j ƒ  ˆ  j ƒ  j d ƒ S)Nz{}
rR   z
)rS   r:   r   rA   rU   r   rT   )rV   )r   r   r   Úhidden_inputÙ   s    z)CliRunner.isolation.<locals>.hidden_inputc             S   s9   t  j j d ƒ } |  r5 t  j j | ƒ t  j j ƒ  | S)Nr   )rS   Ústdinr   r:   r   rU   )ZechoÚcharr   r   r   Ú_getcharÞ   s
    z%CliRunner.isolation.<locals>._getcharc                s   | d  k r ˆ  S| S)Nr   )ÚstreamÚcolor)Údefault_colorr   r   Úshould_strip_ansiç   s    z.CliRunner.isolation.<locals>.should_strip_ansi)r2   r1   rS   rZ   r:   r?   r   ZFORCED_WIDTHrO   r   r	   rG   r   rH   r/   r0   ÚTextIOWrapperr   Zvisible_prompt_funcZhidden_prompt_funcr\   r   r`   r   ÚosÚenvironÚgetÚ	Exception)r   r   rF   r^   Z	old_stdinZ
old_stdoutZ
old_stderrZold_forced_widthZbytes_outputZbytes_errorrX   rY   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.isolationc          0   K   s  d } |  j  d | d | d | ƒ �³}	 d }
 d } t | t ƒ rQ t j | ƒ } y | j d ƒ } Wn! t k
 r‡ |  j | ƒ } Yn Xzy# | j d | p  f  d | | � WnÞ t	 k
 rI} zy t
 j ƒ  } | j } | d k rê d } | d k rü | }
 t | t ƒ s7t
 j j t | ƒ ƒ t
 j j d ƒ d	 } WYd d } ~ XnF t k
 rŽ} z& | sd‚  | }
 d	 } t
 j ƒ  } WYd d } ~ Xn XWd t
 j j ƒ  |	 d j ƒ  } |  j rÂd } n |	 d	 j ƒ  } XWd QRXt d
 |  d | d | d | d |
 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.

        .. versionadded:: 3.0
           The ``catch_exceptions`` parameter was added.

        .. versionchanged:: 3.0
           The result object now has an `exc_info` attribute with the
           traceback if available.

        .. versionadded:: 4.0
           The ``color`` parameter was added.

        :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.
        Nr   rF   r^   r   Ú	prog_nameÚargsr<   r   r4   r5   r6   r7   r8   r9   )rh   r,   r   ÚshlexÚsplitÚpopÚKeyErrorrL   ÚmainÚ
SystemExitrS   r9   ÚcodeÚintr:   r   Ústrre   rU   ÚgetvaluerH   r3   )r   rK   rj   r   rF   Zcatch_exceptionsr^   Úextrar9   Z
outstreamsr8   r7   ri   Úer:   r?   r   r   r   Úinvoke  sR    (!#	#		zCliRunner.invokec             c   so   t  j ƒ  } t j ƒ  } t  j | ƒ z	 | VWd t  j | ƒ y t j | ƒ Wn t t f k
 ri Yn XXd S)z‘A context manager that creates a temporary folder and changes
        the current working directory to it for isolated filesystem tests.
        N)	rb   ÚgetcwdÚtempfileÚmkdtempÚchdirÚshutilÚrmtreeÚOSErrorÚIOError)r   ÚcwdÚtr   r   r   Úisolated_filesystemo  s    	zCliRunner.isolated_filesystem)r&   r'   r(   rC   r   rL   rO   Ú
contextlibÚcontextmanagerrh   rw   r‚   r   r   r   r   rE   t   s   vUrE   )rƒ   rb   rk   r|   rS   ry   rR   r   r   r   Ú_compatr   r   r   Ú	cStringIOr	   r/   r
   Úobjectr   r2   r3   rE   r   r   r   r   Ú<module>   s&   .