Debian Codegraph @

test/units/_vendor/test_vendor.py

raw
1 # (c) 2020 Ansible Project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3 from __future__ import annotations
4
5 import os
6 import pkgutil
7 import pytest
8 import sys
9
10 from unittest.mock import patch
11
12
13 def reset_internal_vendor_package():
14 import ansible
15 ansible_vendor_path = os.path.join(os.path.dirname(ansible.__file__), '_vendor')
16
17 list(map(sys.path.remove, [path for path in sys.path if path == ansible_vendor_path]))
18
19 for pkg in ['ansible._vendor', 'ansible']:
20 sys.modules.pop(pkg, None)
21
22
23 def test_package_path_masking():
24 from ansible import _vendor
25
26 assert hasattr(_vendor, '__path__') and _vendor.__path__ == []
27
28
29 def test_no_vendored():
30 reset_internal_vendor_package()
31 with patch.object(pkgutil, 'iter_modules', return_value=[]):
32 previous_path = list(sys.path)
33 import ansible
34 ansible_vendor_path = os.path.join(os.path.dirname(ansible.__file__), '_vendor')
35
36 assert ansible_vendor_path not in sys.path
37 assert sys.path == previous_path
38
39
40 def test_vendored(vendored_pkg_names=None):
41 if not vendored_pkg_names:
42 vendored_pkg_names = ['boguspkg']
43 reset_internal_vendor_package()
44 with patch.object(pkgutil, 'iter_modules', return_value=list((None, p, None) for p in vendored_pkg_names)):
45 previous_path = list(sys.path)
46 import ansible
47 ansible_vendor_path = os.path.join(os.path.dirname(ansible.__file__), '_vendor')
48 assert sys.path[0] == ansible_vendor_path
49 assert sys.path[1:] == previous_path
50
51
52 def test_vendored_conflict():
53 with pytest.warns(UserWarning) as w:
54 test_vendored(vendored_pkg_names=['sys', 'pkgutil']) # pass a real package we know is already loaded
55 assert any(list('pkgutil, sys' in str(msg.message) for msg in w)) # ensure both conflicting modules are listed and sorted