MCPcopy
hub / github.com/pallets/click / isolation

Method isolation

src/click/testing.py:399–594  ·  src/click/testing.py::CliRunner.isolation

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 f

(
        self,
        input: str | bytes | t.IO[t.Any] | None = None,
        env: cabc.Mapping[str, str | None] | None = None,
        color: bool = False,
    )

Source from the content-addressed store, hash-verified

397
398 @contextlib.contextmanager
399 def isolation(
400 self,
401 input: str | bytes | t.IO[t.Any] | None = None,
402 env: cabc.Mapping[str, str | None] | None = None,
403 color: bool = False,
404 ) -> cabc.Generator[tuple[io.BytesIO, io.BytesIO, io.BytesIO]]:
405 class="st">"""A context manager that sets up the isolation for invoking of a
406 command line tool. This sets up `<stdin>` with the given input data
407 and `os.environ` with the overrides from the given dictionary.
408 This also rebinds some internals in Click to be mocked (like the
409 prompt functionality).
410
411 This is automatically done in the :meth:`invoke` method.
412
413 :param input: the input stream to put into `sys.stdin`.
414 :param env: the environment overrides as dictionary.
415 :param color: whether the output should contain color codes. The
416 application can still override this explicitly.
417
418 .. versionadded:: 8.2
419 An additional output stream is returned, which is a mix of
420 `<stdout>` and `<stderr>` streams.
421
422 .. versionchanged:: 8.2
423 Always returns the `<stderr>` stream.
424
425 .. versionchanged:: 8.0
426 `<stderr>` is opened with ``errors=class="st">"backslashreplace"``
427 instead of the default ``class="st">"strict"``.
428
429 .. versionchanged:: 4.0
430 Added the ``color`` parameter.
431 class="st">"""
432 bytes_input = make_input_stream(input, self.charset)
433 echo_input = None
434
435 old_stdin = sys.stdin
436 old_stdout = sys.stdout
437 old_stderr = sys.stderr
438 old_forced_width = formatting.FORCED_WIDTH
439 formatting.FORCED_WIDTH = 80
440
441 env = self.make_env(env)
442
443 stream_mixer = StreamMixer()
444
445 if self.echo_stdin:
446 bytes_input = echo_input = t.cast(
447 t.BinaryIO, EchoingStdin(bytes_input, stream_mixer.stdout)
448 )
449
450 sys.stdin = text_input = _NamedTextIOWrapper(
451 bytes_input, encoding=self.charset, name=class="st">"<stdin>", mode=class="st">"r"
452 )
453
454 if self.echo_stdin:
455 class="cm"># Force unbuffered reads, otherwise TextIOWrapper reads a
456 class="cm"># large chunk which is echoed early.

Callers 7

invokeMethod · 0.95
test_sechoFunction · 0.80
test_secho_non_textFunction · 0.80
test_echoFunction · 0.80
test_echo_no_streamsFunction · 0.80

Calls 5

make_envMethod · 0.95
make_input_streamFunction · 0.85
StreamMixerClass · 0.85
EchoingStdinClass · 0.85
_NamedTextIOWrapperClass · 0.85

Tested by

no test coverage detected