a =jg?@s*ddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl m Z ddl mZddl mZddl mZejrdd lmZGd d d Zejejeejdd d dZGdddejZejejeeejejfeej dddZ!GdddZ"GdddZ#dS)N) TracebackType)_compat) formatting)termui)utils)_find_binary_reader) BaseCommandc@seZdZejejddddZeejdddZe e dd d Z de e d d dZ de e d ddZ de e d ddZeje dddZeje dddZedddZdS) EchoingStdinN)inputoutputreturncCs||_||_d|_dS)NF)_input_output_paused)selfr r r7/usr/local/lib/python3.9/site-packages/click/testing.py__init__szEchoingStdin.__init__)xr cCs t|j|SN)getattrr)rrrrr __getattr__szEchoingStdin.__getattr__)rvr cCs|js|j||Sr)rrwrite)rrrrr_echos zEchoingStdin._echo)nr cCs||j|Sr)rrreadrrrrrr$szEchoingStdin.readcCs||j|Sr)rrread1rrrrr 'szEchoingStdin.read1cCs||j|Sr)rrreadlinerrrrr!*szEchoingStdin.readliner csfddjDS)Ncsg|]}|qSrr.0rrrr .z*EchoingStdin.readlines..)r readlinesr&rr&rr)-szEchoingStdin.readlinescstfddjDS)Nc3s|]}|VqdSrr#r$r&rr 1r(z(EchoingStdin.__iter__..)iterrr&rr&r__iter__0szEchoingStdin.__iter__cCs t|jSr)reprrr&rrr__repr__3szEchoingStdin.__repr__)r)r)r)__name__ __module__ __qualname__tBinaryIOrstrAnyrbytesrintrr r!Listr)Iteratorr,r.rrrrr sr )streamr ccs&|durdVnd|_dVd|_dS)NTF)r)r:rrr _pause_echo7s r;csReZdZejeeejddfdd ZeedddZ eeddd Z Z S) _NamedTextIOWrapperN)buffernamemodekwargsr c s$tj|fi|||_||_dSr)superr_name_mode)rr=r>r?r@ __class__rrrBsz_NamedTextIOWrapper.__init__r"cCs|jSr)rBr&rrrr>Isz_NamedTextIOWrapper.namecCs|jSr)rCr&rrrr?Msz_NamedTextIOWrapper.mode) r/r0r1r2r3r4r5rpropertyr>r? __classcell__rrrDrr<As r<)r charsetr cCsbt|dr6tttjtj|}|dur.|Std|durDd}nt|trX| |}t |S)Nrz.Could not find binary reader for input stream.r() hasattrrr2castIOr5 TypeError isinstancer4encodeioBytesIO)r rHrrrrmake_input_streamRs   rQc @seZdZdZddeejeejeeje ejej ej e e e fdddZ eeddd Zeedd d Zeedd d ZedddZdS)Resultz3Holds the captured result of an invoked CLI script.N CliRunnerrunner stdout_bytes stderr_bytes return_value exit_code exceptionexc_infocCs.||_||_||_||_||_||_||_dSrrT)rrUrVrWrXrYrZr[rrrris zResult.__init__r"cCs|jS)z(The (standard) output as unicode string.)stdoutr&rrrr sz Result.outputcCs|j|jjdddS)z&The standard output as unicode string.replace  )rVdecoderUrHr]r&rrrr\sz Result.stdoutcCs,|jdurtd|j|jjdddS)z%The standard error as unicode string.Nzstderr not separately capturedr]r^r_)rW ValueErrorr`rUrHr]r&rrrstderrs  z Result.stderrcCs,|jrt|jnd}dt|jd|dS)Nokay< >)rZr-typer/)rZexc_strrrrr.szResult.__repr__)N)r/r0r1__doc__r6r2Optionalr5r7 BaseExceptionTupleTyperrrFr4r r\rbr.rrrrrRfs(  rRc @seZdZdZdeejejeejefeeddddZ d ed d d Z dejejeejefejeejefd ddZ e j dejejeeejejfejejeejefeejejejejejfdddZdd ejejeejefejejeeejejfejejeejefeeejedddZe j dejejedfejedddZdS)rSaThe 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-8NFT)rHenv echo_stdin mix_stderrr cCs ||_|p i|_||_||_dSr)rHrnrorp)rrHrnrorprrrrs zCliRunner.__init__r )clir 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)r>)rrqrrrget_default_prog_nameszCliRunner.get_default_prog_name) overridesr cCst|j}|r|||S)z8Returns the environment overrides for invoking a script.)dictrnupdate)rrtrrrrmake_envs  zCliRunner.make_env)r rncolorr c#st||j}d}tj}tj}tj}tj} dt_||}t } |j r^t t jt|| }}t||jdddt_|j rd_t| |jdddt_d} |jrtjt_nt } t| |jd dd d t_t|dt jttd fd d } t|dt jttd fdd } t|ttddd}|dt jt jt jt jttdfdd }tj}tj}tj}tj}tj}| t_| t_|t_|t_|t_i}z| D]R\}}t!j"#|||<|durz t!j"|=Wnt$yYn0n |t!j"|<q| | fVW| D]B\}}|dur@z t!j"|=Wnt$y<Yn0n |t!j"|<q |t_|t_|t_|t_|t_|t_|t_|t_| t_n| D]B\}}|durz t!j"|=Wnt$yYn0n |t!j"|<q|t_|t_|t_|t_|t_|t_|t_|t_| t_0dS)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)r{r>r?errors)promptr cs>tj|p dd}tj|dtj|S)Nr^r_)sysr\rr!rstripflush)rval text_inputrr visible_inputs  z*CliRunner.isolation..visible_inputcs.tj|p ddtjdS)Nrr_r^)rr\rrr!r)rrrr hidden_inputs z)CliRunner.isolation..hidden_input)echor cSs*tjd}|rtj|tj|S)Nr)rstdinrr\rr)rcharrrr_getchar$s    z%CliRunner.isolation.._getchar)r:rxr cs|dur S| Srr)r:rx) default_colorrrshould_strip_ansi0sz.CliRunner.isolation..should_strip_ansi)N)N)NN)%rQrHrrr\rbrZ FORCED_WIDTHrwrOrPror2rJr3r r< _CHUNK_SIZErpr;rir4boolrKr5rZvisible_prompt_funcZhidden_prompt_funcrrrritemsosenvironget Exception)rr rnrxZ bytes_inputZ echo_inputZ old_stdinZ old_stdoutZ old_stderrZold_forced_widthZ bytes_outputZ bytes_errorrrrrZold_visible_prompt_funcZold_hidden_prompt_funcZold__getchar_funcZold_should_strip_ansiZold__compat_should_strip_ansiZold_envkeyvaluer)rrr isolations              zCliRunner.isolation)rqargsr rncatch_exceptionsrxextrar c Ksd}|j|||d} d} d} d} t|tr8t|}z|d} Wntyb||} Yn0z&z|jf|pvd| d|} Wnt y}z|t }t t jt jtt jf|j}|durd}|dkr|} t|tst jt|t jdd}|} WYd}~nFd}~0tyX}z$|s4|} d} t }WYd}~n d}~00Wt j| d}|jrd}n | d}n2t j| d}|jrd}n | d}0Wdn1s0Yt|||| | | |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 rnrxr prog_namer)rrr_rrT)rrMr4shlexsplitpopKeyErrorrsmain SystemExitrr[r2rJriUnionr7r5coder\rrrgetvaluerprR)rrqrr rnrrxrr[Z outstreamsrXrZrYreZe_coder\rbrrrinvokeasd,           .zCliRunner.invokezos.PathLike[str])temp_dirr ccst}tj|d}t|z>|VWt||durzt|WqtyZYq0n6t||durzt|WntyYn00dS)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)rrcwddtrrrisolated_filesystems"      zCliRunner.isolated_filesystem)rmNFT)N)NNF)NNNTF)N)r/r0r1rhr4r2riMappingrrrsrw contextlibcontextmanagerrr6rKr5r9rkrOrPrSequencerRrrrrrrrSsd   erS)$rrOrrrrrtypingr2typesrrrrrrr TYPE_CHECKINGcorer r rrir9r; TextIOWrapperr<rr4r6rKr5r3rQrRrSrrrrs0       "  :