From 4b3a9d6fb7fc7e881aac65ac84bbea9c306f69fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Sat, 8 Jun 2024 16:13:31 +0200 Subject: [PATCH] tests: Fix CLI tests, auto-clean temp files --- tests/conftest.py | 14 +++++++++++--- tests/test_cli.py | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 46a51fd..6aea478 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,6 +8,7 @@ import subprocess import sys import time +from contextlib import suppress from pathlib import Path from typing import Iterator @@ -231,13 +232,13 @@ def destroy(self, *, force: bool = False) -> None: ports_file = Path(".ports.json") +lock_dir = Path(".lockdir") def get_lock() -> None: - lockdir = Path(".lockdir") while True: try: - lockdir.mkdir(exist_ok=False) + lock_dir.mkdir(exist_ok=False) except FileExistsError: time.sleep(0.025) else: @@ -245,7 +246,8 @@ def get_lock() -> None: def release_lock() -> None: - Path(".lockdir").rmdir() + with suppress(FileNotFoundError): + lock_dir.rmdir() def get_random_port() -> int: @@ -296,3 +298,9 @@ def port() -> Iterator[int]: def server(tmp_path: Path, port: int) -> Iterator[Aria2Server]: with Aria2Server(tmp_path, port) as server: yield server + + +@pytest.fixture(scope="session", autouse=True) +def _setup() -> None: + ports_file.unlink(missing_ok=True) + release_lock() diff --git a/tests/test_cli.py b/tests/test_cli.py index 9e73734..57ddfd3 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -303,4 +303,4 @@ def test_show_debug_info(capsys: pytest.CaptureFixture) -> None: @pytest.mark.parametrize("command", ["autoclear", "autopurge", "autoremove"]) def test_cli_autoclear_commands(command: str, tmp_path: Path, port: int) -> None: with Aria2Server(tmp_path, port, session="very-small-download.txt"): - assert main([command]) == 0 + assert main([f"-p{port}", command]) == 0