Merge pull request #1715 from MLH-Fellowship/maaz/issue-395
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by GitHub · parents 90a6c78, a9711c5 · 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.
Diff stats
4 files+51 added−8 removed
- tests:+15 added−0 removed
- other:+36 added−8 removed
Provenance
- Commit
b8a33bf- Tree
2db3f45- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
90a6c78- Parent 2
a9711c5
4 files changed+51 added−8 removed
modified:docs/testing.rst+13 added
110 unmodified lines111 result = runner.invoke(cat, ['hello.txt'])112 assert result.exit_code == 0113 assert result.output == 'Hello World!\n'114 115+Pass ``temp_dir`` to control where the temporary directory is created.116+The directory will not be removed by Click in this case. This is useful117+to integrate with a framework like Pytest that manages temporary files.118+119+.. code-block:: python120+121+ def test_keep_dir(tmp_path):122+ runner = CliRunner()123+124+ with runner.isolated_filesystem(temp_dir=tmp_path) as td:125+ ...126+127+128 Input Streams129 -------------130 131 The test wrapper can also be used to provide input data for the input27 unmodified linesmodified:src/click/testing.py+20 added-8 removed
378 unmodified lines379 exc_info=exc_info,380 )381 382 @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.388+389+ :param temp_dir: Create the temporary directory under this390+ directory. If given, the created directory is not removed391+ when exiting.392+393+ .. 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)399+400 try:401 yield t402 finally:403 os.chdir(cwd)404+405+ 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+ passmodified:tests/test_testing.py+15 added
317 unmodified lines318 click.echo(f.mode, nl=False)319 320 result = runner.invoke(cli, ["-"])321 assert result.output == "<stdin>\nr"322+323+324+def test_isolated_runner(runner):325+ with runner.isolated_filesystem() as d:326+ assert os.path.exists(d)327+328+ assert not os.path.exists(d)329+330+331+def 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)334+335+ assert os.path.exists(d)336+ os.rmdir(d)modified:CHANGES.rst+3 added
159 unmodified lines160 - 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`167 168 169 Version 7.1.2170 -------------635 unmodified lines