U e([V*@sddlZddlZddlZddlZddlZddlmZmZeje dddZ er`ddl m Z nddlZddlmZGdddeZd d ZGd d d eZGd ddeZdS)N) iteritemsPY2.)StringIO)_find_binary_readerc@sPeZdZddZddZddZddd Zdd d Zd d ZddZ ddZ dS) EchoingStdincCs||_||_dSN)_input_output)selfinputoutputr /testing.py__init__szEchoingStdin.__init__cCs t|j|Sr )getattrr )r xrrr __getattr__szEchoingStdin.__getattr__cCs|j||Sr )r write)r rvrrr_echo!s zEchoingStdin._echocCs||j|Sr )rr readr nrrrr%szEchoingStdin.readcCs||j|Sr )rr readlinerrrrr(szEchoingStdin.readlinecsfddjDS)Ncsg|]}|qSrr.0rr rr ,sz*EchoingStdin.readlines..)r readlinesr rr rr"+szEchoingStdin.readlinescstfddjDS)Nc3s|]}|VqdSr rrr rr /sz(EchoingStdin.__iter__..)iterr r rr r__iter__.szEchoingStdin.__iter__cCs t|jSr )reprr r rrr__repr__1szEchoingStdin.__repr__N)r)r) __name__ __module__ __qualname__rrrrrr"r%r'rrrrrs  rcCsft|dr.tr|St|}|dk r&|Std|dkrZokay)r:r&r rrrr'^szResult.__repr__)N)r(r)r*__doc__rpropertyrr'rrrrr5Hs   r5c@sTeZdZdZdddZddZddd Zejdd d Z dd dZ ejddZ dS) 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&|dkr d}||_|pi|_||_dS)Nzutf-8)r3env echo_stdin)r r3rCrDrrrrts  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)r clirrrget_default_prog_name{szCliRunner.get_default_prog_namecCst|j}|r|||S)z8Returns the environment overrides for invoking a script.)dictrCupdate)r Z overridesrrrrmake_envs  zCliRunner.make_envc#st|jtj}tj}tj}tjj}dtj_| |}t r`t t_t_}|j rt |n@t}|j rxt |tj|jdtj||jdt_t_t_d fdd } d fdd } dd } |dfd d } tjj} tjj}tjj}tjj}| tj_| tj_| tj_| tj_i}zht|D]T\}}tj|||<|dkrrz tj|=Wntk rnYnXn |tj|<q*|VW5t|D]D\}}|dkrz tj|=Wntk rYnXn |tj|<q|t_|t_|t_| tj_|tj_|tj_|tj_|tj_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}tj|dtj|S)Nr=r>)sysstdoutrrrstripflush)promptvalr rr visible_inputs  z*CliRunner.isolation..visible_inputcs,tj|p ddtjdS)NrNr>r=)rOrPrrRrrQ)rSrUrr hidden_inputs z)CliRunner.isolation..hidden_inputcSs*tjd}|r&tj|tj|S)Nr)rOstdinrrPrrR)Zechocharrrr_getchars    z%CliRunner.isolation.._getcharcs|dkr S| Sr r)streamcolor) default_colorrrshould_strip_ansisz.CliRunner.isolation..should_strip_ansi)N)N)NN)r4r3rOrXrPstderrclickpkgZ formattingZ FORCED_WIDTHrKrrrDrr1r2 TextIOWrapperZtermuiZvisible_prompt_funcZhidden_prompt_funcrZZutilsr^rosenviron Exceptionget)r r rCr\Z old_stdinZ old_stdoutZ old_stderrZold_forced_widthZ bytes_outputrVrWrZr^Zold_visible_prompt_funcZold_hidden_prompt_funcZold__getchar_funcZold_should_strip_ansiZold_envkeyvaluer)r]r r isolationsv         zCliRunner.isolationTc Ksd}|j|||d} d} d} zz$|jf|p.d||d|Wntk r} zJ| jdkrd| } t }| j} t | t stj t | tj dd} 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)rhrOrPrRgetvaluemainrH SystemExitcoder;r.intrstrrdr5)r rGrir rCZcatch_exceptionsr\Zextrar;outr:r9rerrrinvokesB      zCliRunner.invokeccs^t}t}t|z |VW5t|zt|Wnttfk rVYnXXdS)zA context manager that creates a temporary folder and changes the current working directory to it for isolated filesystem tests. N) rbgetcwdtempfileZmkdtempchdirshutilZrmtreeOSErrorIOError)r cwdtrrrisolated_filesystem3s   zCliRunner.isolated_filesystem)NNF)N)NNF)NNNTF) r(r)r*r@rrHrK contextlibcontextmanagerrhrrr{rrrrrBds   i @rB)rbrOrvrtr|Z_compatrrmodulesr(rsplitr`Z cStringIOrr1robjectrr4r5rBrrrrs