Debian Codegraph @

test/lib/ansible_test/_internal/provider/layout/collection.py

raw
1 """Layout provider for Ansible collections."""
2
3 from __future__ import annotations
4
5 import os
6
7 from . import (
8 ContentLayout,
9 LayoutProvider,
10 CollectionDetail,
11 LayoutMessages,
12 )
13
14 from ...util import (
15 is_valid_identifier,
16 )
17
18
19 class CollectionLayout(LayoutProvider):
20 """Layout provider for Ansible collections."""
21
22 @staticmethod
23 def is_content_root(path: str) -> bool:
24 """Return True if the given path is a content root for this provider."""
25 if os.path.basename(os.path.dirname(os.path.dirname(path))) == 'ansible_collections':
26 return True
27
28 return False
29
30 def create(self, root: str, paths: list[str]) -> ContentLayout:
31 """Create a Layout using the given root and paths."""
32 plugin_paths = dict((p, os.path.join('plugins', p)) for p in self.PLUGIN_TYPES)
33
34 collection_root = os.path.dirname(os.path.dirname(root))
35 collection_dir = os.path.relpath(root, collection_root)
36
37 collection_namespace: str
38 collection_name: str
39
40 collection_namespace, collection_name = collection_dir.split(os.path.sep)
41
42 collection_root = os.path.dirname(collection_root)
43
44 sanity_messages = LayoutMessages()
45 integration_messages = LayoutMessages()
46 unit_messages = LayoutMessages()
47
48 # these apply to all test commands
49 self.__check_test_path(paths, sanity_messages)
50 self.__check_test_path(paths, integration_messages)
51 self.__check_test_path(paths, unit_messages)
52
53 # these apply to specific test commands
54 integration_targets_path = self.__check_integration_path(paths, integration_messages)
55 self.__check_unit_path(paths, unit_messages)
56
57 errors: list[str] = []
58
59 if not is_valid_identifier(collection_namespace):
60 errors.append(f'The namespace "{collection_namespace}" is an invalid identifier or a reserved keyword.')
61
62 if not is_valid_identifier(collection_name):
63 errors.append(f'The name "{collection_name}" is an invalid identifier or a reserved keyword.')
64
65 return ContentLayout(
66 root,
67 paths,
68 plugin_paths=plugin_paths,
69 collection=CollectionDetail(
70 name=collection_name,
71 namespace=collection_namespace,
72 root=collection_root,
73 ),
74 test_path='tests',
75 results_path='tests/output',
76 sanity_path='tests/sanity',
77 sanity_messages=sanity_messages,
78 integration_path='tests/integration',
79 integration_targets_path=integration_targets_path.rstrip(os.path.sep),
80 integration_vars_path='tests/integration/integration_config.yml',
81 integration_messages=integration_messages,
82 unit_path='tests/unit',
83 unit_module_path='tests/unit/plugins/modules',
84 unit_module_utils_path='tests/unit/plugins/module_utils',
85 unit_messages=unit_messages,
86 unsupported=errors,
87 )
88
89 @staticmethod
90 def __check_test_path(paths: list[str], messages: LayoutMessages) -> None:
91 modern_test_path = 'tests/'
92 modern_test_path_found = any(path.startswith(modern_test_path) for path in paths)
93 legacy_test_path = 'test/'
94 legacy_test_path_found = any(path.startswith(legacy_test_path) for path in paths)
95
96 if modern_test_path_found and legacy_test_path_found:
97 messages.warning.append('Ignoring tests in "%s" in favor of "%s".' % (legacy_test_path, modern_test_path))
98 elif legacy_test_path_found:
99 messages.warning.append('Ignoring tests in "%s" that should be in "%s".' % (legacy_test_path, modern_test_path))
100
101 @staticmethod
102 def __check_integration_path(paths: list[str], messages: LayoutMessages) -> str:
103 modern_integration_path = 'roles/test/'
104 modern_integration_path_found = any(path.startswith(modern_integration_path) for path in paths)
105 legacy_integration_path = 'tests/integration/targets/'
106 legacy_integration_path_found = any(path.startswith(legacy_integration_path) for path in paths)
107
108 if modern_integration_path_found and legacy_integration_path_found:
109 messages.warning.append('Ignoring tests in "%s" in favor of "%s".' % (legacy_integration_path, modern_integration_path))
110 integration_targets_path = modern_integration_path
111 elif legacy_integration_path_found:
112 messages.info.append('Falling back to tests in "%s" because "%s" was not found.' % (legacy_integration_path, modern_integration_path))
113 integration_targets_path = legacy_integration_path
114 elif modern_integration_path_found:
115 messages.info.append('Loading tests from "%s".' % modern_integration_path)
116 integration_targets_path = modern_integration_path
117 else:
118 messages.error.append('Cannot run integration tests without "%s" or "%s".' % (modern_integration_path, legacy_integration_path))
119 integration_targets_path = modern_integration_path
120
121 return integration_targets_path
122
123 @staticmethod
124 def __check_unit_path(paths: list[str], messages: LayoutMessages) -> None:
125 modern_unit_path = 'tests/unit/'
126 modern_unit_path_found = any(path.startswith(modern_unit_path) for path in paths)
127 legacy_unit_path = 'tests/units/' # test/units/ will be covered by the warnings for test/ vs tests/
128 legacy_unit_path_found = any(path.startswith(legacy_unit_path) for path in paths)
129
130 if modern_unit_path_found and legacy_unit_path_found:
131 messages.warning.append('Ignoring tests in "%s" in favor of "%s".' % (legacy_unit_path, modern_unit_path))
132 elif legacy_unit_path_found:
133 messages.warning.append('Rename "%s" to "%s" to run unit tests.' % (legacy_unit_path, modern_unit_path))
134 elif modern_unit_path_found:
135 pass # unit tests only run from one directory so no message is needed
136 else:
137 messages.error.append('Cannot run unit tests without "%s".' % modern_unit_path)