From 149b8b9aec93a3bee7729b6a194d6edc1f12067e Mon Sep 17 00:00:00 2001 From: ghiggi Date: Thu, 8 Feb 2024 17:58:21 +0100 Subject: [PATCH] Fix cache for old functools versions --- gpm_api/io/checks.py | 4 ++-- gpm_api/tests/test_io/test_io_checks.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/gpm_api/io/checks.py b/gpm_api/io/checks.py index 0a0e3ecc..0c3072c5 100644 --- a/gpm_api/io/checks.py +++ b/gpm_api/io/checks.py @@ -91,13 +91,13 @@ def check_remote_storage(storage): return storage.lower() -@functools.cache +@functools.lru_cache(maxsize=None) def check_transfer_tool(transfer_tool): """Check the transfer tool.""" valid_transfer_tools = ["curl", "wget"] if transfer_tool not in valid_transfer_tools: raise ValueError( - f"{transfer_tool} is an invalid 'transfer_tool'. Valid values are {valid_transfer_tools}." + f"'{transfer_tool}' is an invalid 'transfer_tool'. Valid values are {valid_transfer_tools}." ) # Check WGET or CURL is installed diff --git a/gpm_api/tests/test_io/test_io_checks.py b/gpm_api/tests/test_io/test_io_checks.py index 55c4a40a..63f5884e 100644 --- a/gpm_api/tests/test_io/test_io_checks.py +++ b/gpm_api/tests/test_io/test_io_checks.py @@ -18,6 +18,8 @@ from typing import List, Dict, Any from gpm_api.io import checks from gpm_api.io.products import available_products, available_scan_modes, available_product_versions +from pytest_mock.plugin import MockerFixture +from subprocess import CalledProcessError def test_check_base_dir() -> None: @@ -180,6 +182,26 @@ def test_check_remote_storage() -> None: checks.check_remote_storage(123) +def test_check_transfer_tool(mocker: MockerFixture): + """Test check_transfer_tool()""" + # Assert "curl" is available and return "curl" + transfer_tool = "curl" # "wget" is not mandatory + assert checks.check_transfer_tool(transfer_tool=transfer_tool) == transfer_tool + + # Test the function with an invalid transfer tool + invalid_tool = "invalid_tool" + with pytest.raises(ValueError) as exc_info: + checks.check_transfer_tool(transfer_tool=invalid_tool) + assert f"'{invalid_tool}' is an invalid 'transfer_tool'." in str(exc_info.value) + + # Test the function with a valid transfer tool that is not installed + transfer_tool = "wget" + mocker.patch("subprocess.run", side_effect=CalledProcessError(1, [transfer_tool, "--version"])) + with pytest.raises(ValueError) as exc_info: + checks.check_transfer_tool(transfer_tool) + assert f"{transfer_tool.upper()} is not installed on your machine !" in str(exc_info.value) + + def test_check_version( versions: List[int], ) -> None: