
_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__Nr%   )__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)zGiven 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)zA 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&   .