Skip to content

Commit

Permalink
feat: check return code of pernosco-submit to determine availability
Browse files Browse the repository at this point in the history
  • Loading branch information
pyoor committed Nov 12, 2024
1 parent 1d2bc0d commit efd7f47
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
11 changes: 7 additions & 4 deletions src/bugmon/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import os
import shutil
import subprocess
import sysconfig
import tempfile
import zipfile
from contextlib import contextmanager
Expand Down Expand Up @@ -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]:
Expand Down
11 changes: 10 additions & 1 deletion tests/test_bugmon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 14 additions & 16 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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"""
Expand Down

0 comments on commit efd7f47

Please sign in to comment.