Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/module_utils/test__deps.py

raw
1 # (c) 2024, Alexei Znamensky <[email protected]>
2 # Copyright (c) 2024 Ansible Project
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 from unittest.mock import MagicMock
9
10 import pytest
11
12 from ansible_collections.community.general.plugins.module_utils import _deps as deps
13
14
15 @pytest.fixture
16 def module():
17 m = MagicMock()
18 m.fail_json.side_effect = RuntimeError
19 return m
20
21
22 def test_wrong_name(module):
23 with deps.declare("sys"):
24 import sys # noqa: F401, pylint: disable=unused-import
25
26 with pytest.raises(KeyError):
27 deps.validate(module, "wrong_name")
28
29
30 def test_fail_potatoes(module):
31 with deps.declare("potatoes", reason="Must have potatoes") as potatoes_dep:
32 import potatoes_that_will_never_be_there # noqa: F401, pylint: disable=unused-import
33
34 with pytest.raises(RuntimeError):
35 deps.validate(module)
36
37 assert potatoes_dep.failed is True
38 assert potatoes_dep.message.startswith("Failed to import the required Python library")
39
40
41 def test_sys(module):
42 with deps.declare("sys") as sys_dep:
43 import sys # noqa: F401, pylint: disable=unused-import
44
45 deps.validate(module)
46
47 assert sys_dep.failed is False
48
49
50 def test_multiple(module):
51 with deps.declare("mpotatoes", reason="Must have mpotatoes"):
52 import potatoes_that_will_never_be_there # noqa: F401, pylint: disable=unused-import
53
54 with deps.declare("msys", reason="Must have msys"):
55 import sys # noqa: F401, pylint: disable=unused-import
56
57 deps.validate(module, "msys")
58 deps.validate(module, "-mpotatoes")
59
60 with pytest.raises(RuntimeError):
61 deps.validate(module)
62
63 with pytest.raises(RuntimeError):
64 deps.validate(module, "-msys")
65
66 with pytest.raises(RuntimeError):
67 deps.validate(module, "mpotatoes")