Skip to content

Add CliRunner.set_filesystem() to use a specific working directory #3123

Description

@bphunter1972

The CliRunner.isolated_filesystem is find for testing tools that have no preconceived notion of what the directory structure should look like, or what files must be present. While it's true that you can provide a parent directory to isolated_filesystem(), it will still create a brand new empty directory and operate within it.

CliRunner.set_filesystem() operates in the same way, except that you provide it with a pre-existing directory to operate in.

Suppose your tool requires that it be run in a directory named 'library' containing certain data files. Your test might look start like this:

def test_library(tmp_path):
    lib_dir = tmp_path / "library"
    data_file = lib_dir / "data"
    lib_dir.mkdir()
    data_file.touch()

With isolated_filesystem(), running your command in this directory will not work, because it will run not in the library directory just created, but in a sub-directory of that one.

With set_filesystem(), you get what you need:

    ...
    runner = CliRunner()
    with runner.set_filesystem(lib_dir):
        runner.invoke(...)

Activity

  1. davidism commented on Oct 22, 2025

    @davidism
    Member

    I'm not clear why this is needed. I can't think of a good reason for a command to require being run in a directory with a certain name. That said, if you need it for some reason, create it in the isolated context, then cd further.

    with runner.isolated_filesystem() as tmp_dir:
        lib_dir = Path(tmp_dir, "library")
        lib_dir.mkdir()
        os.chdir(lib_dir)
        ...

    Nowadays you can also use pytest directly for this, or use with tempfile.TemporaryDirectory(). I don't think we'd add isolated_filesystem today, I think it was added before Python 3 and pytest were in common use.

    def test_isolated(tmp_path, monkeypatch):
        lib_dir = tmp_path / "library"
        lib_dir.mkdir()
        monkeypatch.chdir(lib_dir)
    with tempfile.TemporaryDirectory() as tmp_dir:
        lib_dir = Path(tmp_dir, "library")
        lib_dir.mkdir()
        os.chdir(lib_dir)
  2. bphunter1972 commented on Oct 22, 2025

    @bphunter1972
    Author

    In that case, perhaps consider marking isolated_filesystem() as deprecated. It seemed to me to be the way things should be done, and without knowing otherwise I assumed that it did more than just os.chdir().

  3. kdeldycke commented on Oct 25, 2025

    @kdeldycke
    Collaborator

    I am in favor of deprecating isolated_filesystem() and simplify code. Maybe for Click 9.x.

    Pytest is maintained and wide-spread. We can simply add a snippet in the documentation to explain how to use the CliRunner with vanilla Pytest.

  4. locked as resolved and limited conversation to collaborators on Nov 9, 2025
  5. changed the title [-]Add CliRunner.set_filesystem() to use a specific working directory[/-] [+]Add `CliRunner.set_filesystem()` to use a specific working directory[/+] on May 23, 2026
  6. added this to the 8.5.0 milestone on May 23, 2026
  7. kdeldycke commented on May 23, 2026

    @kdeldycke
    Collaborator

    This discussion is related to #3501

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    test runnerCliRunner and Click's test utilities feature

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions