tests: Run each test in separate config/cache dirs

Run each CLI command in a separate config/cache dir, to avoid leaks
between tests. Moreover, this way we are able to check the contents of
the config/cache dirs for a single CLI run.
This commit is contained in:
Alex Pyrgiotis 2023-02-08 22:11:24 +02:00
parent 44c324f9ac
commit 18bc77332d
No known key found for this signature in database
GPG key ID: B6C15EBA0357C9AA

View file

@ -10,6 +10,7 @@ import tempfile
import traceback import traceback
from pathlib import Path from pathlib import Path
from typing import Sequence from typing import Sequence
from unittest import mock
import pytest import pytest
from click.testing import CliRunner, Result from click.testing import CliRunner, Result
@ -129,18 +130,25 @@ class TestCli(TestBase):
if os.environ.get("DUMMY_CONVERSION", False): if os.environ.get("DUMMY_CONVERSION", False):
args = ("--unsafe-dummy-conversion", *args) args = ("--unsafe-dummy-conversion", *args)
# TODO: Replace this with `contextlib.chdir()` [1], which was added in with tempfile.TemporaryDirectory() as t:
# Python 3.11. tmp_dir = Path(t)
# # TODO: Replace this with `contextlib.chdir()` [1], which was added in
# [1]: # https://docs.python.org/3/library/contextlib.html#contextlib.chdir # Python 3.11.
try: #
if tmp_path is not None: # [1]: # https://docs.python.org/3/library/contextlib.html#contextlib.chdir
cwd = os.getcwd() try:
os.chdir(tmp_path) if tmp_path is not None:
result = CliRunner().invoke(cli_main, args) cwd = os.getcwd()
finally: os.chdir(tmp_path)
if tmp_path is not None:
os.chdir(cwd) with mock.patch(
"dangerzone.isolation_provider.container.get_tmp_dir",
return_value=t,
):
result = CliRunner().invoke(cli_main, args)
finally:
if tmp_path is not None:
os.chdir(cwd)
return CLIResult.reclass_click_result(result, args) return CLIResult.reclass_click_result(result, args)