|
| 1 | +import sys |
| 2 | +import unittest |
| 3 | +from pathlib import Path |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +ROOT = Path(__file__).resolve().parents[1] |
| 7 | +sys.path.insert(0, str(ROOT / "lib" / "src")) |
| 8 | + |
| 9 | +import backend_installer # noqa: E402 |
| 10 | + |
| 11 | + |
| 12 | +class FakeResult: |
| 13 | + def __init__(self, returncode=0, stdout=b""): |
| 14 | + self.returncode = returncode |
| 15 | + self.stdout = stdout |
| 16 | + |
| 17 | + |
| 18 | +GPU_SUMMARY = b"GPU0:\n\tapiVersion = 1.4.354\n\tdeviceName = Radeon RX 9070 XT\n\tdeviceType = PHYSICAL_DEVICE_TYPE_DISCRETE_GPU\n" |
| 19 | + |
| 20 | + |
| 21 | +class SetupVulkanSupportTests(unittest.TestCase): |
| 22 | + """The package install is a convenience; `vulkaninfo` is the real check.""" |
| 23 | + |
| 24 | + def _run(self, pacman_result=None, pacman_exc=None, has_gpu=True): |
| 25 | + def fake_which(name): |
| 26 | + return f"/usr/bin/{name}" if name in ("pacman", "vulkaninfo") else None |
| 27 | + |
| 28 | + sudo = mock.Mock(side_effect=pacman_exc) if pacman_exc else mock.Mock(return_value=pacman_result) |
| 29 | + |
| 30 | + with mock.patch.object(backend_installer.shutil, "which", side_effect=fake_which), \ |
| 31 | + mock.patch.object(backend_installer, "run_sudo_command", sudo), \ |
| 32 | + mock.patch.object(backend_installer, "run_command", return_value=FakeResult(0, GPU_SUMMARY)), \ |
| 33 | + mock.patch.object(backend_installer, "vulkaninfo_has_hardware_gpu", return_value=has_gpu): |
| 34 | + return backend_installer.setup_vulkan_support() |
| 35 | + |
| 36 | + def test_failed_package_install_still_detects_working_vulkan(self): |
| 37 | + self.assertTrue(self._run(pacman_result=FakeResult(returncode=1))) |
| 38 | + |
| 39 | + def test_sudo_exception_still_detects_working_vulkan(self): |
| 40 | + self.assertTrue(self._run(pacman_exc=PermissionError("a password is required"))) |
| 41 | + |
| 42 | + def test_no_result_from_package_install_still_detects_working_vulkan(self): |
| 43 | + self.assertTrue(self._run(pacman_result=None)) |
| 44 | + |
| 45 | + def test_successful_package_install_detects_working_vulkan(self): |
| 46 | + self.assertTrue(self._run(pacman_result=FakeResult(returncode=0))) |
| 47 | + |
| 48 | + def test_still_returns_false_when_no_hardware_gpu(self): |
| 49 | + """The capability check must remain authoritative in both directions.""" |
| 50 | + self.assertFalse(self._run(pacman_result=FakeResult(returncode=1), has_gpu=False)) |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + unittest.main() |
0 commit comments