Skip to content

Commit

Permalink
tests: Run each test in separate config/cache dirs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apyrgio committed Feb 8, 2023
1 parent b9287a0 commit 285fa9e
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,31 @@ def run_cli(
if os.environ.get("DUMMY_CONVERSION", False):
args = ("--unsafe-dummy-conversion", *args)

# TODO: Replace this with `contextlib.chdir()` [1], which was added in
# Python 3.11.
#
# [1]: # https://docs.python.org/3/library/contextlib.html#contextlib.chdir
try:
if tmp_path is not None:
cwd = os.getcwd()
os.chdir(tmp_path)
result = CliRunner().invoke(cli_main, args)
finally:
if tmp_path is not None:
os.chdir(cwd)
with tempfile.TemporaryDirectory() as tmp_dir:
# Run each test in a separate config/cache directory.
tmp_dir = Path(tmp_dir)
config_dir = tmp_dir / "config"
cache_dir = tmp_dir / "cache"
args = (
"--config-dir",
str(config_dir),
"--cache-dir",
str(cache_dir),
*args,
)

# TODO: Replace this with `contextlib.chdir()` [1], which was added in
# Python 3.11.
#
# [1]: # https://docs.python.org/3/library/contextlib.html#contextlib.chdir
try:
if tmp_path is not None:
cwd = os.getcwd()
os.chdir(tmp_path)
result = CliRunner().invoke(cli_main, args)
finally:
if tmp_path is not None:
os.chdir(cwd)

return CLIResult.reclass_click_result(result, args)

Expand Down

0 comments on commit 285fa9e

Please sign in to comment.