B x `62@sddlZddlZddlZddlZddlZddlZddlmZddlmZddlm Z ddl m Z ddl m Z ddl m Z e rdd lmZnddlZdd l mZGd d d eZd dZGdddeZGdddeZdS)N) formatting)termui)utils) iteritems)PY2) string_types)StringIO)_find_binary_readerc@sPeZdZddZddZddZddd Zdd d Zd d ZddZ ddZ dS) EchoingStdincCs||_||_dS)N)_input_output)selfinputoutputr1/tmp/pip-unpacked-wheel-za9e8r29/click/testing.py__init__szEchoingStdin.__init__cCs t|j|S)N)getattrr )rxrrr __getattr__szEchoingStdin.__getattr__cCs|j||S)N)r write)rrvrrr_echos zEchoingStdin._echocCs||j|S)N)rr read)rnrrrr#szEchoingStdin.readcCs||j|S)N)rr readline)rrrrrr&szEchoingStdin.readlinecsfddjDS)Ncsg|]}|qSr)r).0r)rrr *sz*EchoingStdin.readlines..)r readlines)rr)rrr )szEchoingStdin.readlinescstfddjDS)Nc3s|]}|VqdS)N)r)rr)rrr -sz(EchoingStdin.__iter__..)iterr )rr)rr__iter__,szEchoingStdin.__iter__cCs t|jS)N)reprr )rrrr__repr__/szEchoingStdin.__repr__N)r)r) __name__ __module__ __qualname__rrrrrr r#r%rrrrr s  r cCsft|dr.tr|St|}|dk r&|Std|dkrokay)formattyper&r8r$)rrrrr%nszResult.__repr__)N) r&r'r(__doc__rpropertyrr:r?r%rrrrr3Fs     r3c@sTeZdZdZdddZddZdd d Zejdd d Z dd dZ ejddZ dS) CliRunneraThe 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 NFTcCs,|dkr d}||_|pi|_||_||_dS)Nzutf-8)r1env echo_stdin mix_stderr)rr1rFrGrHrrrrs  zCliRunner.__init__cCs |jpdS)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)rclirrrget_default_prog_nameszCliRunner.get_default_prog_namecCst|j}|r|||S)z8Returns the environment overrides for invoking a script.)dictrFupdate)rZ overridesrrrrmake_envs  zCliRunner.make_envc#sDt|jtj}tj}tj}tj}dt_||}t rht }|j rNt ||t_|j st } | t_nZt}|j rt |tj|jdtj||jdt_|j st} tj| |jdt_|j rtjt_t_d fdd } d fdd } dd } |dfd d } tj}tj}tj}tj}| t_| t_| t_| t_i}zzx`t|D]T\}}tj|||<|dkry tj|=Wntk rYnXn |tj|<qLW||j o| fVWdxPt|D]D\}}|dkry tj|=Wntk rYnXn |tj|<qW|t_|t_|t_|t_|t_|t_|t_|t_XdS)aA 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|p dd}tjd|tj|S)Nz z{} )sysr:rrrstriprAflush)promptval)rrr visible_inputs  z*CliRunner.isolation..visible_inputcs.tjd|pdtjdS)Nz{} rRz )rSr:rrArUrrT)rV)rrr hidden_inputs z)CliRunner.isolation..hidden_inputcSs*tjd}|r&tj|tj|S)Nr)rSstdinrr:rrU)Zechocharrrr_getchars    z%CliRunner.isolation.._getcharcs|dkr S| S)Nr)streamcolor) default_colorrrshould_strip_ansisz.CliRunner.isolation..should_strip_ansi)N)N)NN)r2r1rSrZr:r?rZ FORCED_WIDTHrOrr rGr rHr/r0 TextIOWrapperrZvisible_prompt_funcZhidden_prompt_funcr\rr`rosenvironget Exception)rrrFr^Z old_stdinZ old_stdoutZ old_stderrZold_forced_widthZ bytes_outputZ bytes_errorrXrYr\r`Zold_visible_prompt_funcZold_hidden_prompt_funcZold__getchar_funcZold_should_strip_ansiZold_envkeyvaluer)r_rr isolations        zCliRunner.isolationc Ksd}|j|||dT} d} d} t|tr4t|}y|d} Wntk r`||} YnXzy|jf|prd| d|Wnt k r} zTt }| j } | dkrd} | dkr| } t| t st jt| t jdd} Wdd} ~ XYn<tk r.} z|s| } d} t }Wdd} ~ XYnXWdt j| d}|jrXd}n | d}XWdQRXt|||| | |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. N)rrFr^r prog_namer)argsrir<r)r4r5r6r7r8r9)rhr,rshlexsplitpopKeyErrorrLmain SystemExitrSr9codeintr:rstrrerUgetvaluerHr3)rrKrjrrFZcatch_exceptionsr^extrar9Z outstreamsr8r7rier:r?rrrinvokesR(      zCliRunner.invokeccs^t}t}t|z |VWdt|yt|Wnttfk rVYnXXdS)zA context manager that creates a temporary folder and changes the current working directory to it for isolated filesystem tests. N) rbgetcwdtempfilemkdtempchdirshutilrmtreeOSErrorIOError)rcwdtrrrisolated_filesystemos   zCliRunner.isolated_filesystem)NNFT)N)NNF)NNNTF) r&r'r(rCrrLrO contextlibcontextmanagerrhrwrrrrrrEts   v UrE)rrbrkr|rSryrRrrr_compatrrr cStringIOr r/r objectr r2r3rErrrrs&       .