U
e([V�*�@s�ddlZddlZddlZddlZddlZddlmZmZeje �
dd�dZer`ddlm
Z
nddlZddlmZGdd�de�Zd d
�ZGdd�de�ZGd
d�de�ZdS)�N�)� iteritems�PY2�.)�StringIO)�_find_binary_readerc@sPeZdZdd�Zdd�Zdd�Zddd �Zdd
d�Zdd
�Zdd�Z dd�Z
dS)�EchoingStdincCs||_||_dS�N)�_input�_output)�self�input�output�r�/testing.py�__init__szEchoingStdin.__init__cCst|j|�Sr )�getattrr
)r�xrrr�__getattr__szEchoingStdin.__getattr__cCs|j�|�|Sr )r�write)r�rvrrr�_echo!szEchoingStdin._echo���cCs|�|j�|��Sr )rr
�read�r�nrrrr%szEchoingStdin.readcCs|�|j�|��Sr )rr
�readlinerrrrr(szEchoingStdin.readlinecs�fdd��j��D�S)Ncsg|]}��|��qSr�r��.0r�rrr�
<listcomp>,sz*EchoingStdin.readlines.<locals>.<listcomp>)r
� readlinesr rr rr"+szEchoingStdin.readlinescst�fdd��jD��S)Nc3s|]}��|�VqdSr rrr rr� <genexpr>/sz(EchoingStdin.__iter__.<locals>.<genexpr>)�iterr
r rr r�__iter__.szEchoingStdin.__iter__cCs
t|j�Sr )�reprr
r rrr�__repr__1szEchoingStdin.__repr__N)r)r)�__name__�
__module__�__qualname__rrrrrr"r%r'rrrrrs
rcCsft|d�r.tr|St|�}|dk r&|Std��|dkr<d}nt|t�sP|�|�}tr\t|�St� |�S)Nrz.Could not find binary reader for input stream.�)
�hasattrrr� TypeError�
isinstance�bytes�encoder�io�BytesIO)r
�charsetrrrr�make_input_stream5s
r4c@s.eZdZdZd dd�Zedd��Zdd�ZdS)
�Resultz3Holds the captured result of an invoked CLI script.NcCs"||_||_||_||_||_dSr ��runner�output_bytes� exit_code� exception�exc_info)rr7r8r9r:r;rrrrKs
zResult.__init__cCs|j�|jjd��dd�S)zThe output as unicode string.�replace�
�
)r8�decoder7r3r<r rrrrXs�z
Result.outputcCsd|jrt|j�pdfS)Nz<Result %s>Zokay)r:r&r rrrr'^s�zResult.__repr__)N)r(r)r*�__doc__r�propertyrr'rrrrr5Hs�
r5c@sTeZdZdZddd�Zdd�Zddd �Zejdd
d��Z dd
d�Z
ejdd��ZdS)� CliRunneraIThe 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.
NFcCs&|dkrd}||_|pi|_||_dS)Nzutf-8)r3�env�
echo_stdin)rr3rCrDrrrrts
zCliRunner.__init__cCs
|jpdS)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�clirrr�get_default_prog_name{szCliRunner.get_default_prog_namecCst|j�}|r|�|�|S)z8Returns the environment overrides for invoking a script.)�dictrC�update)rZ overridesrrrr�make_env�s
zCliRunner.make_envc#st�|j��tj}tj}tj}tjj}dtj_|� |�}t
r`t�t_t_}|jr�t
�|��n@t��}|jrxt
�|��tj�|jd��tj||jd�t_t_�t_d�fdd� } d
�fdd� }
dd �}|�d�fd
d� }tjj}
tjj}tjj}tjj}| tj_|
tj_|tj_|tj_i}zht|�D]T\}}tj�|�||<|dk�rrztj|=Wntk
�rnYnXn
|tj|<�q*|VW5t|�D]D\}}|dk�r�ztj|=Wntk
�r�YnXn
|tj|<�q�|t_|t_|t_|
tj_|tj_|tj_|tj_|tj_XdS)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)�encodingNcs<tj�|pd�����d�}tj�|d�tj��|S)N�r=r>)�sys�stdoutrr�rstrip�flush)�prompt�val�r
rr�
visible_input�s
z*CliRunner.isolation.<locals>.visible_inputcs,tj�|pdd�tj������d�S)NrNr>r=)rOrPrrRrrQ)rSrUrr�hidden_input�s
z)CliRunner.isolation.<locals>.hidden_inputcSs*tj�d�}|r&tj�|�tj��|S)Nr)rO�stdinrrPrrR)Zecho�charrrr�_getchar�s
z%CliRunner.isolation.<locals>._getcharcs|dkr�S|Sr r)�stream�color)�
default_colorrr�should_strip_ansi�sz.CliRunner.isolation.<locals>.should_strip_ansi)N)N)NN)r4r3rOrXrP�stderr�clickpkgZ
formattingZFORCED_WIDTHrKrrrDrr1r2�
TextIOWrapperZtermuiZvisible_prompt_funcZhidden_prompt_funcrZZutilsr^r�os�environ� Exception�get)rr
rCr\Z old_stdinZ
old_stdoutZ
old_stderrZold_forced_widthZbytes_outputrVrWrZr^Zold_visible_prompt_funcZold_hidden_prompt_funcZold__getchar_funcZold_should_strip_ansiZold_env�key�valuer)r]r
r� isolation�sv
�
zCliRunner.isolationTcKsd}|j|||d���} d}
d}z�z$|jf|p.d|�|�d�|��Wn�tk
r�}
zJ|
jdkrd|
}
t� �}|
j}t
|t�s�tj�t
|��tj�d�d}W5d}
~
XYn8tk
r�}
z|s|
}
d}t� �}W5d}
~
XYnXW5tj��| ��}XW5QRXt||||
|d �S)
aeInvokes 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
: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.
N)r
rCr\rr)�argsZ prog_namer>rrr6)rhrOrPrR�getvalue�mainrH�
SystemExit�coder;r.�intr�strrdr5)rrGrir
rCZcatch_exceptionsr\Zextrar;�outr:r9r�errr�invoke�sB��
�zCliRunner.invokeccs^t��}t��}t�|�z
|VW5t�|�zt�|�Wnttfk
rVYnXXdS)z�A context manager that creates a temporary folder and changes
the current working directory to it for isolated filesystem tests.
N) rb�getcwd�tempfileZmkdtemp�chdir�shutilZrmtree�OSError�IOError)r�cwd�trrr�isolated_filesystem3s
zCliRunner.isolated_filesystem)NNF)N)NNF)NNNTF)r(r)r*r@rrHrK�
contextlib�contextmanagerrhrrr{rrrrrBds
i�
@rB)rbrOrvrtr|Z_compatrr�modulesr(�rsplitr`Z cStringIOrr1r�objectrr4r5rBrrrr�<module>s |