Skip to content
Myr

custom temp directory with CLIRunner.isolated_filesystem

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Diaga · committed by David Lord · parent 90a6c78 · Browse files · 4 files+51 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: David Lord <davidism@gmail.com>

Diff stats

4 files+51 added−8 removed

  • tests:+15 added−0 removed
  • other:+36 added−8 removed

4 files+51 added−8 removed

modified:docs/testing.rst+13 added
110 unmodified lines
111 result = runner.invoke(cat, ['hello.txt'])112 assert result.exit_code == 0113 assert result.output == 'Hello World!\n'114115Pass ``temp_dir`` to control where the temporary directory is created.116The directory will not be removed by Click in this case. This is useful117to integrate with a framework like Pytest that manages temporary files.118119.. code-block:: python120121 def test_keep_dir(tmp_path):122 runner = CliRunner()123124 with runner.isolated_filesystem(temp_dir=tmp_path) as td:125 ...126127128Input Streams129-------------130131The test wrapper can also be used to provide input data for the input
27 unmodified lines
modified:src/click/testing.py+20 added-8 removed
378 unmodified lines
379 exc_info=exc_info,380 )381382 @contextlib.contextmanager383 def isolated_filesystem(self):384 """A context manager that creates a temporary folder and changes385 the current working directory to it for isolated filesystem tests.383 def isolated_filesystem(self, temp_dir=None):384 """A context manager that creates a temporary directory and385 changes the current working directory to it. This isolates tests386 that affect the contents of the CWD to prevent them from387 interfering with each other.388389 :param temp_dir: Create the temporary directory under this390 directory. If given, the created directory is not removed391 when exiting.392393 .. versionchanged:: 8.0394 Added the ``temp_dir`` parameter.395 """396 cwd = os.getcwd()388 t = tempfile.mkdtemp()397 t = tempfile.mkdtemp(dir=temp_dir)398 os.chdir(t)399400 try:401 yield t402 finally:403 os.chdir(cwd)404405 if temp_dir is None:394 try:395 shutil.rmtree(t)396 except OSError: # noqa: B014397 pass406 try:407 shutil.rmtree(t)408 except OSError: # noqa: B014409 pass
modified:tests/test_testing.py+15 added
317 unmodified lines
318 click.echo(f.mode, nl=False)319320 result = runner.invoke(cli, ["-"])321 assert result.output == "<stdin>\nr"322323324def test_isolated_runner(runner):325 with runner.isolated_filesystem() as d:326 assert os.path.exists(d)327328 assert not os.path.exists(d)329330331def test_isolated_runner_custom_tempdir(runner, tmp_path):332 with runner.isolated_filesystem(temp_dir=tmp_path) as d:333 assert os.path.exists(d)334335 assert os.path.exists(d)336 os.rmdir(d)
modified:CHANGES.rst+3 added
159 unmodified lines
160- If ``Option.show_default`` is a string, it is displayed even if161 ``default`` is ``None``. :issue:`1732`162- ``click.get_terminal_size()`` is deprecated and will be removed in163 8.1. Use :func:`shutil.get_terminal_size` instead. :issue:`1736`164- Control the location of the temporary directory created by165 ``CLIRunner.isolated_filesystem`` by passing ``temp_dir``. A custom166 directory will not be removed automatically. :issue:`395`167168169Version 7.1.2170-------------
635 unmodified lines