o +ca>@sddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl m Z ddl mZddlmZejrGddlmZGd d d Zejd ejed ejdfd dZGdddejZdejejeeejfded ejfddZ GdddZ!GdddZ"dS)N) TracebackType) formatting)termui)utils)_find_binary_reader) BaseCommandc@seZdZdejdejddfddZdedejfdd Zd e de fd d Z dde de fddZ dde de fddZ dde de fddZdeje fddZdeje fddZdefddZdS) EchoingStdininputoutputreturnNcCs||_||_d|_dS)NF)_input_output_paused)selfr r r//usr/lib/python3/dist-packages/click/testing.py__init__s zEchoingStdin.__init__xcCs t|j|SN)getattrr )rrrrr __getattr__s zEchoingStdin.__getattr__rvcCs|js |j||Sr)rrwrite)rrrrr_echos zEchoingStdin._echoncC||j|Sr)rr readrrrrrr#zEchoingStdin.readcCrr)rr read1rrrrr!&r zEchoingStdin.read1cCrr)rr readlinerrrrr")r zEchoingStdin.readlinecsfddjDS)Ncsg|]}|qSrr.0rrrr -sz*EchoingStdin.readlines..)r readlinesr&rr&rr(,zEchoingStdin.readlinescstfddjDS)Nc3s|]}|VqdSrr#r$r&rr 0sz(EchoingStdin.__iter__..)iterr r&rr&r__iter__/r)zEchoingStdin.__iter__cCs t|jSr)reprr r&rrr__repr__2s zEchoingStdin.__repr__)r)__name__ __module__ __qualname__tBinaryIOrstrAnyrbytesrintrr!r"Listr(Iteratorr,r.rrrrr sr streamr ccs*|dur dVdSd|_dVd|_dS)NTF)r)r:rrr _pause_echo6s   r;c sZeZdZdejdededejddf fdd Zedefd d Z edefd d Z Z S) _NamedTextIOWrapperbuffernamemodekwargsr Nc s$tj|fi|||_||_dSr)superr_name_mode)rr=r>r?r@ __class__rrrAs z_NamedTextIOWrapper.__init__cC|jSr)rBr&rrrr>Hz_NamedTextIOWrapper.namecCrFr)rCr&rrrr?LrGz_NamedTextIOWrapper.mode) r/r0r1r2r3r4r5rpropertyr>r? __classcell__rrrDrr<@s r<r charsetcCsdt|drtttj|}|dur|Std|durd}n t|tr)||}t tt |S)Nrz.Could not find binary reader for input stream.) hasattrrr2castIO TypeError isinstancer4encodeioBytesIOr6)r rJrrrrmake_input_streamQs   rTc@seZdZdZ ddddedejedejded eje d ejej ej e e e ffd d Z ed efddZed efddZed efddZd efddZdS)Resultz3Holds the captured result of an invoked CLI script.Nrunner CliRunner stdout_bytes stderr_bytes return_value exit_code exceptionexc_infocCs.||_||_||_||_||_||_||_dSrrVrXrYrZr[r\r])rrVrXrYrZr[r\r]rrrrhs  zResult.__init__r cCrF)z(The (standard) output as unicode string.)stdoutr&rrrr sz Result.outputcCs|j|jjdddS)z&The standard output as unicode string.replace  )rXdecoderVrJr`r&rrrr_sz Result.stdoutcCs,|jdur td|j|jjdddS)z%The standard error as unicode string.Nzstderr not separately capturedr`rarb)rY ValueErrorrcrVrJr`r&rrrstderrs z Result.stderrcCs,|jrt|jnd}dt|jd|dS)Nokay< >)r\r-typer/)rexc_strrrrr.szResult.__repr__r)r/r0r1__doc__r6r2Optionalr5r7 BaseExceptionTupleTyperrrHr4r r_rer.rrrrrUes6  rUc@seZdZdZ    d dedejejeejefded ed df d d Z d dd efddZ d!dejejeejefd ejeejeffddZ e j   d"dejejeeejfdejejeejefded ejejejejejffddZ     d#d ddejejeejefdejejeeejfdejejeejefdededejd efddZe j d!dejejeejfd ejefddZdS)$rWaThe 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. :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 utf-8NFTrJenv echo_stdin mix_stderrr cCs ||_|pi|_||_||_dSr)rJrrrsrt)rrJrrrsrtrrrrs  zCliRunner.__init__clircCs |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)r>)rrurrrget_default_prog_names zCliRunner.get_default_prog_name overridescCst|j}|r |||S)z8Returns the environment overrides for invoking a script.)dictrrupdate)rrxrrrrmake_envs  zCliRunner.make_envr colorc#st||j}d}tj}tj}tj}tj} dt_||}t } |j r0t t jt|| }}t||jdddt_|j rBd_t| |jdddt_d} |jrVtjt_nt } t| |jd dd d t_t|dd t jtd tffdd } t|dd t jtd tffdd } t|dtd tfdd}| ddt jt jdt jtd tffdd }tj}tj}tj}tj}| t_| t_|t_|t_i}zn|D]%\}}tj !|||<|durztj |=Wqt"yYqw|tj |<q| | fVW|D]\}}|durztj |=Wqt"yYqw|tj |<q|t_|t_|t_|t_|t_|t_|t_| t_dS|D]"\}}|durYztj |=Wq=t"yXYq=w|tj |<q=|t_|t_|t_|t_|t_|t_|t_| t_w)anA 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. :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. .. versionchanged:: 8.0 ``stderr`` is opened with ``errors="backslashreplace"`` instead of the default ``"strict"``. .. versionchanged:: 4.0 Added the ``color`` parameter. NPzr)encodingr>r?rzwzbackslashreplace)rr>r?errorspromptr cs>tj|pdd}tj|dtj|S)Nrarb)sysr_rr"rstripflush)rval text_inputrr visible_inputs  z*CliRunner.isolation..visible_inputcs.tj|pddtjdS)Nrrbra)rr_rrr"r)rrrr hidden_inputs z)CliRunner.isolation..hidden_inputechocSs*tjd}|rtj|tj|S)Nr)rstdinrr_rr)rcharrrr_getchar#s   z%CliRunner.isolation.._getcharr:r|cs|dur S| Srr)r:r|) default_colorrrshould_strip_ansi/sz.CliRunner.isolation..should_strip_ansir)NN)#rTrJrrr_rer FORCED_WIDTHr{rRrSrsr2rMr3r r< _CHUNK_SIZErtr;rmr4boolrNrvisible_prompt_funchidden_prompt_funcrrritemsosenvironget Exception)rr rrr| bytes_input echo_input old_stdin old_stdout old_stderrold_forced_width bytes_output bytes_errorrrrrold_visible_prompt_funcold_hidden_prompt_funcold__getchar_funcold_should_strip_ansiold_envkeyvaluer)rrr isolations                    zCliRunner.isolationargscatch_exceptionsextrac Ksd}|j|||d} d} d} d} t|trt|}z|d} Wnty0||} Ynwzz|jd|p9d| d|} Wnet y}z=t }t t jt jtt jf|j}|durcd}|dkri|} t|ts~t jt|t jdd}|} WYd}~n!d}~wty}z|s|} d} t }WYd}~nd}~wwWt j| d}|jrd}n| d}nt j| d}|jrd}w| d}wWdn1swYt|||| | | |d S) aInvokes 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. :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. .. versionchanged:: 8.0 The result object has the ``return_value`` attribute with the value returned from the invoked command. .. versionchanged:: 4.0 Added the ``color`` parameter. .. versionchanged:: 3.0 Added the ``catch_exceptions`` parameter. .. versionchanged:: 3.0 The result object has the ``exc_info`` attribute with the traceback if available. N)r rrr|r prog_namer)rrrbrr^)rrPr4shlexsplitpopKeyErrorrwmain SystemExitrr]r2rMrmUnionr7r5coder_rrrgetvaluertrU)rrurr rrrr|rr] outstreamsrZr\r[ree_coder_rerrrinvoke]sr,         .zCliRunner.invoketemp_dirccst}tj|d}t|z"|VWt||dur1zt|WdSty0YdSwdSt||durMzt|WwtyLYwww)aA context manager that creates a temporary directory and changes the current working directory to it. This isolates tests that affect the contents of the CWD to prevent them from interfering with each other. :param temp_dir: Create the temporary directory under this directory. If given, the created directory is not removed when exiting. .. versionchanged:: 8.0 Added the ``temp_dir`` parameter. )dirN)rgetcwdtempfilemkdtempchdirshutilrmtreeOSError)rrcwdr2rrrisolated_filesystems,      zCliRunner.isolated_filesystem)rqNFTr)NNF)NNNTF)r/r0r1rlr4r2rmMappingrrrwr{ contextlibcontextmanagerrr6rNr9rorRrSrSequencer5rUrrPathLikerrrrrrWs      erW)#rrRrrrrrtypingr2typesrrrrr_compatr TYPE_CHECKINGcorerr rrmr9r; TextIOWrapperr<rr4r6rNr3rTrUrWrrrrs8      "   :