a b/tests/utils/test_utils_available.py
1
from ehrapy._compat import _check_module_importable, _shell_command_accessible
2
3
4
def test_check_module_importable_true():
5
    assert _check_module_importable("math") is True
6
7
8
def test_check_module_importable_false():
9
    assert _check_module_importable("nonexistentmodule12345") is False
10
11
12
def test_shell_command_accessible_true(monkeypatch):
13
    def mock_popen(*args, **kwargs):
14
        class MockProcess:
15
            def __init__(self):
16
                self.returncode = 0
17
18
            def communicate(self):
19
                return ("output", "")
20
21
        return MockProcess()
22
23
    monkeypatch.setattr("subprocess.Popen", mock_popen)
24
    assert _shell_command_accessible(["echo", "hello"]) is True
25
26
27
def test_shell_command_accessible_false(monkeypatch):
28
    def mock_popen(*args, **kwargs):
29
        class MockProcess:
30
            def __init__(self):
31
                self.returncode = 1
32
33
            def communicate(self):
34
                return ("", "error")
35
36
        return MockProcess()
37
38
    monkeypatch.setattr("subprocess.Popen", mock_popen)
39
    assert _shell_command_accessible(["nonexistentcommand"]) is False