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(...)
The
CliRunner.isolated_filesystemis 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 toisolated_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:
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(...)