Repository: pallets/click
File: src/click/testing.py (Python)

Code before:
            except Exception as e:
                if not catch_exceptions:
                    raise
                exception = e
                exit_code = 1
                exc_info = sys.exc_info()
            finally:
                sys.stdout.flush()
                stdout = outstreams[0].getvalue()
                if self.mix_stderr:
                    stderr = None
                else:
                    stderr = outstreams[1].getvalue()

        return Result(
            runner=self,
            stdout_bytes=stdout,
            stderr_bytes=stderr,
            return_value=return_value,
            exit_code=exit_code,
            exception=exception,
            exc_info=exc_info,
        )

    @contextlib.contextmanager
    def isolated_filesystem(self, tempdir=None):
        """A context manager that creates a temporary folder and changes
        the current working directory to it for isolated filesystem tests.

        :param tempdir: the path where the temporary directory should be created

        .. versionchanged:: 7.1.2
        """
        tempfile.tempdir = tempdir

        cwd = os.getcwd()
        t = tempfile.mkdtemp()
        os.chdir(t)
        try:
            yield t
        finally:
            os.chdir(cwd)
            if tempdir is None:
                try:
                    shutil.rmtree(t)
                except OSError:  # noqa: B014
                    pass


Review comment (suggestion):
You should add a test [here](https://github.com/pallets/click/blob/master/tests/test_testing.py) to exercise this new behavior. Actually, since this function is not tested directly (at present), you might want to add two tests: one where you call `isolated_filesystem` with an arg, and one without.

In each test, you can just use Python's `os` library to inspect the temporary directory. You should be able to call `os.path.exists` on the yielded directory both during and after the lifetime of the context manager. Something like

```
lingering_directory = None
with CLIRunner.isolated_filesystem() as directory:
    lingering_directory = directory
    self.assertTrue(os.path.exists(directory))
self.assertFalse(os.path.exists(lingering_directory))
```

I leave it to you to determine the testing logic for when `isolated_filesystem` takes an argument.

Code after:
            except Exception as e:
                if not catch_exceptions:
                    raise
                exception = e
                exit_code = 1
                exc_info = sys.exc_info()
            finally:
                sys.stdout.flush()
                stdout = outstreams[0].getvalue()
                if self.mix_stderr:
                    stderr = None
                else:
                    stderr = outstreams[1].getvalue()

        return Result(
            runner=self,
            stdout_bytes=stdout,
            stderr_bytes=stderr,
            return_value=return_value,
            exit_code=exit_code,
            exception=exception,
            exc_info=exc_info,
        )

    @contextlib.contextmanager
    def isolated_filesystem(self, temp_dir=None):
        """A 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.
        """
        cwd = os.getcwd()
        t = tempfile.mkdtemp(dir=temp_dir)
        os.chdir(t)

        try:
            yield t
        finally:
            os.chdir(cwd)

            if temp_dir is None:
                try:
                    shutil.rmtree(t)
                except OSError:  # noqa: B014