From 4cb1aba36d561c76508b4c9facf58200f1d2ee2c Mon Sep 17 00:00:00 2001 From: Yannick DAYER Date: Tue, 27 Oct 2026 19:13:18 +0100 Subject: [PATCH] fix(tests): future-proof isolated filesystem Click 9.0 will drop its isolated_filesystem test feature. This implements our own CLI Runner with isolated_filesystem using tempfile. --- tests/conftest.py | 43 +++++++++++++++++++++++++++++++++++++++++++ tests/test_gridtk.py | 7 ------- tests/test_guard.py | 7 ------- 3 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..4591129 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,43 @@ +# SPDX-FileCopyrightText: 2026 Idiap Research Institute +# +# SPDX-License-Identifier: GPL-3.0-or-later + +import contextlib +import os +import tempfile + +from collections.abc import Iterator +from pathlib import Path + +import pytest + +from click.testing import CliRunner + + +class Runner(CliRunner): + """A CliRunner with the ``isolated_filesystem`` that Click deprecated. + + Click 9.0 removes ``CliRunner.isolated_filesystem``, advising to use a + temporary directory instead. The tests run commands that use relative paths, + so this keeps the same behaviour: run in a new temporary directory. + """ + + @contextlib.contextmanager + def isolated_filesystem( + self, temp_dir: str | os.PathLike[str] | None = None + ) -> Iterator[str]: + """Change to a new temporary directory (created in ``temp_dir``) until the + end of the ``with`` block, and yield its path. + """ + cwd = Path.cwd() + with tempfile.TemporaryDirectory(dir=temp_dir) as path: + os.chdir(path) + try: + yield path + finally: + os.chdir(cwd) + + +@pytest.fixture +def runner(): + return Runner() diff --git a/tests/test_gridtk.py b/tests/test_gridtk.py index 1f92fe8..36fb514 100644 --- a/tests/test_gridtk.py +++ b/tests/test_gridtk.py @@ -13,8 +13,6 @@ import pytest -from click.testing import CliRunner - from gridtk.__main__ import cli from gridtk.tools import ( add_default_dep_type, @@ -24,11 +22,6 @@ ) -@pytest.fixture -def runner(): - return CliRunner() - - def _sbatch_output(job_id): return f"Submitted batch job {job_id}\n" diff --git a/tests/test_guard.py b/tests/test_guard.py index 1fe3960..7c5db21 100644 --- a/tests/test_guard.py +++ b/tests/test_guard.py @@ -14,17 +14,10 @@ import pytest -from click.testing import CliRunner - from gridtk import guard from gridtk.cli import cli -@pytest.fixture -def runner(): - return CliRunner() - - def _git(repo: Path, *args: str) -> str: return subprocess.run( [