diff --git a/src/bugmon/utils.py b/src/bugmon/utils.py index b65302f..b6285cb 100644 --- a/src/bugmon/utils.py +++ b/src/bugmon/utils.py @@ -5,7 +5,6 @@ import os import shutil import subprocess -import sysconfig import tempfile import zipfile from contextlib import contextmanager @@ -117,9 +116,13 @@ def download_zip_archive(url: str) -> Generator[Path, None, None]: def is_pernosco_available() -> bool: """Determines if pernosco-submit is properly configured""" - py_path = Path(sysconfig.get_path("platlib")) - pernosco_shared = py_path / "pernoscoshared" - return PERNOSCO is not None and pernosco_shared.exists() + result = subprocess.run( + ["pernosco-submit", "--help"], + check=False, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + return result.returncode == 0 def get_pernosco_trace(log_path: Path) -> Optional[Path]: diff --git a/tests/test_bugmon.py b/tests/test_bugmon.py index 8a53a72..4b2fb06 100644 --- a/tests/test_bugmon.py +++ b/tests/test_bugmon.py @@ -30,8 +30,17 @@ def test_bugmon_need_info_on_bisect_fix(mocker, bugmon, build): assert mock_bug.add_needinfo.call_count == 1 -def test_bugmon_throws_without_pernosco_submit(bug, bugsy, pernosco_creds, working_dir): +def test_bugmon_throws_without_pernosco_submit( + bug, + bugsy, + pernosco_creds, + working_dir, + mocker, +): """Test that bugmon throws when pernosco-submit is not available""" + mock_run = mocker.patch("bugmon.utils.subprocess.run") + mock_run.return_value.returncode = -1 + expected = "pernosco-submit is not properly configured!" with pytest.raises(BugmonException, match=expected): BugMonitor(bugsy, bug, working_dir, pernosco_creds, False) diff --git a/tests/test_utils.py b/tests/test_utils.py index 523c1dd..2990930 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,30 +1,28 @@ # This Source Code Form is subject to the terms of the Mozilla Public License, # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at http://mozilla.org/MPL/2.0/. +import subprocess + import pytest import bugmon.utils as utils -@pytest.mark.parametrize("binary", [True, False]) -@pytest.mark.parametrize("library", [True, False]) -def test_is_pernosco_available(binary, library, mocker, tmp_path): - """Verify that is_pernosco_available returns true if both conditions are true""" - bin_path = "/usr/bin/pernosco-submit" if binary else None - mocker.patch("bugmon.utils.PERNOSCO", bin_path) - - if library: - pernosco_shared = tmp_path / "pernoscoshared" - pernosco_shared.mkdir() +@pytest.mark.parametrize("returncode, expected", [(0, True), (1, False)]) +def test_is_pernosco_available(mocker, returncode, expected): + """Test is_pernosco_available with mocked subprocess.run.""" + mock_run = mocker.patch("bugmon.utils.subprocess.run") + mock_run.return_value.returncode = returncode - mocker.patch( - "bugmon.utils.sysconfig.get_path", - return_value=tmp_path, + # Call the function and assert that it returns the expected result + assert utils.is_pernosco_available() == expected + mock_run.assert_called_once_with( + ["pernosco-submit", "--help"], + check=False, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, ) - expected = binary and library - assert utils.is_pernosco_available() is expected - def test_get_pernosco_trace_match(tmp_path): """Verify that get_pernosco_trace returns the correct path"""