| 1 |
"""Test metadata for passing data to delegated tests.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import dataclasses |
| 6 |
import typing as t |
| 7 |
|
| 8 |
from .util import ( |
| 9 |
display, |
| 10 |
generate_name, |
| 11 |
ANSIBLE_TEST_ROOT, |
| 12 |
ANSIBLE_LIB_ROOT, |
| 13 |
) |
| 14 |
|
| 15 |
from .io import ( |
| 16 |
write_json_file, |
| 17 |
read_json_file, |
| 18 |
) |
| 19 |
|
| 20 |
from .diff import ( |
| 21 |
parse_diff, |
| 22 |
FileDiff, |
| 23 |
) |
| 24 |
|
| 25 |
if t.TYPE_CHECKING: |
| 26 |
from .debugging import DebuggerSettings |
| 27 |
|
| 28 |
|
| 29 |
class Metadata: |
| 30 |
"""Metadata object for passing data to delegated tests.""" |
| 31 |
|
| 32 |
def __init__(self, debugger_flags: DebuggerFlags) -> None: |
| 33 |
"""Initialize metadata.""" |
| 34 |
self.changes: dict[str, tuple[tuple[int, int], ...]] = {} |
| 35 |
self.cloud_config: t.Optional[dict[str, dict[str, t.Union[int, str, bool]]]] = None |
| 36 |
self.change_description: t.Optional[ChangeDescription] = None |
| 37 |
self.ci_provider: t.Optional[str] = None |
| 38 |
self.session_id = generate_name() |
| 39 |
self.ansible_lib_root = ANSIBLE_LIB_ROOT |
| 40 |
self.ansible_test_root = ANSIBLE_TEST_ROOT |
| 41 |
self.collection_root: str | None = None |
| 42 |
self.debugger_flags = debugger_flags |
| 43 |
self.debugger_settings: DebuggerSettings | None = None |
| 44 |
self.loaded = False |
| 45 |
|
| 46 |
def populate_changes(self, diff: t.Optional[list[str]]) -> None: |
| 47 |
"""Populate the changeset using the given diff.""" |
| 48 |
patches = parse_diff(diff) |
| 49 |
patches: list[FileDiff] = sorted(patches, key=lambda k: k.new.path) |
| 50 |
|
| 51 |
self.changes = dict((patch.new.path, tuple(patch.new.ranges)) for patch in patches) |
| 52 |
|
| 53 |
renames = [patch.old.path for patch in patches if patch.old.path != patch.new.path and patch.old.exists and patch.new.exists] |
| 54 |
deletes = [patch.old.path for patch in patches if not patch.new.exists] |
| 55 |
|
| 56 |
# make sure old paths which were renamed or deleted are registered in changes |
| 57 |
for path in renames + deletes: |
| 58 |
if path in self.changes: |
| 59 |
# old path was replaced with another file |
| 60 |
continue |
| 61 |
|
| 62 |
# failed tests involving deleted files should be using line 0 since there is no content remaining |
| 63 |
self.changes[path] = ((0, 0),) |
| 64 |
|
| 65 |
def to_dict(self) -> dict[str, t.Any]: |
| 66 |
"""Return a dictionary representation of the metadata.""" |
| 67 |
return dict( |
| 68 |
changes=self.changes, |
| 69 |
cloud_config=self.cloud_config, |
| 70 |
ci_provider=self.ci_provider, |
| 71 |
change_description=self.change_description.to_dict() if self.change_description else None, |
| 72 |
session_id=self.session_id, |
| 73 |
ansible_lib_root=self.ansible_lib_root, |
| 74 |
ansible_test_root=self.ansible_test_root, |
| 75 |
collection_root=self.collection_root, |
| 76 |
debugger_flags=dataclasses.asdict(self.debugger_flags), |
| 77 |
debugger_settings=self.debugger_settings.as_dict() if self.debugger_settings else None, |
| 78 |
) |
| 79 |
|
| 80 |
def to_file(self, path: str) -> None: |
| 81 |
"""Write the metadata to the specified file.""" |
| 82 |
data = self.to_dict() |
| 83 |
|
| 84 |
display.info('>>> Metadata: %s\n%s' % (path, data), verbosity=3) |
| 85 |
|
| 86 |
write_json_file(path, data) |
| 87 |
|
| 88 |
@staticmethod |
| 89 |
def from_file(path: str) -> Metadata: |
| 90 |
"""Return metadata loaded from the specified file.""" |
| 91 |
data = read_json_file(path) |
| 92 |
return Metadata.from_dict(data) |
| 93 |
|
| 94 |
@staticmethod |
| 95 |
def from_dict(data: dict[str, t.Any]) -> Metadata: |
| 96 |
"""Return metadata loaded from the specified dictionary.""" |
| 97 |
from .debugging import DebuggerSettings |
| 98 |
|
| 99 |
metadata = Metadata( |
| 100 |
debugger_flags=DebuggerFlags(**data['debugger_flags']), |
| 101 |
) |
| 102 |
|
| 103 |
metadata.changes = data['changes'] |
| 104 |
metadata.cloud_config = data['cloud_config'] |
| 105 |
metadata.ci_provider = data['ci_provider'] |
| 106 |
metadata.change_description = ChangeDescription.from_dict(data['change_description']) if data['change_description'] else None |
| 107 |
metadata.session_id = data['session_id'] |
| 108 |
metadata.ansible_lib_root = data['ansible_lib_root'] |
| 109 |
metadata.ansible_test_root = data['ansible_test_root'] |
| 110 |
metadata.collection_root = data['collection_root'] |
| 111 |
metadata.debugger_settings = DebuggerSettings.from_dict(data['debugger_settings']) if data['debugger_settings'] else None |
| 112 |
metadata.loaded = True |
| 113 |
|
| 114 |
return metadata |
| 115 |
|
| 116 |
|
| 117 |
class ChangeDescription: |
| 118 |
"""Description of changes.""" |
| 119 |
|
| 120 |
def __init__(self) -> None: |
| 121 |
self.command: str = '' |
| 122 |
self.changed_paths: list[str] = [] |
| 123 |
self.deleted_paths: list[str] = [] |
| 124 |
self.regular_command_targets: dict[str, list[str]] = {} |
| 125 |
self.focused_command_targets: dict[str, list[str]] = {} |
| 126 |
self.no_integration_paths: list[str] = [] |
| 127 |
|
| 128 |
@property |
| 129 |
def targets(self) -> t.Optional[list[str]]: |
| 130 |
"""Optional list of target names.""" |
| 131 |
return self.regular_command_targets.get(self.command) |
| 132 |
|
| 133 |
@property |
| 134 |
def focused_targets(self) -> t.Optional[list[str]]: |
| 135 |
"""Optional list of focused target names.""" |
| 136 |
return self.focused_command_targets.get(self.command) |
| 137 |
|
| 138 |
def to_dict(self) -> dict[str, t.Any]: |
| 139 |
"""Return a dictionary representation of the change description.""" |
| 140 |
return dict( |
| 141 |
command=self.command, |
| 142 |
changed_paths=self.changed_paths, |
| 143 |
deleted_paths=self.deleted_paths, |
| 144 |
regular_command_targets=self.regular_command_targets, |
| 145 |
focused_command_targets=self.focused_command_targets, |
| 146 |
no_integration_paths=self.no_integration_paths, |
| 147 |
) |
| 148 |
|
| 149 |
@staticmethod |
| 150 |
def from_dict(data: dict[str, t.Any]) -> ChangeDescription: |
| 151 |
"""Return a change description loaded from the given dictionary.""" |
| 152 |
changes = ChangeDescription() |
| 153 |
changes.command = data['command'] |
| 154 |
changes.changed_paths = data['changed_paths'] |
| 155 |
changes.deleted_paths = data['deleted_paths'] |
| 156 |
changes.regular_command_targets = data['regular_command_targets'] |
| 157 |
changes.focused_command_targets = data['focused_command_targets'] |
| 158 |
changes.no_integration_paths = data['no_integration_paths'] |
| 159 |
|
| 160 |
return changes |
| 161 |
|
| 162 |
|
| 163 |
@dataclasses.dataclass(frozen=True, kw_only=True) |
| 164 |
class DebuggerFlags: |
| 165 |
"""Flags for enabling specific debugging features.""" |
| 166 |
|
| 167 |
self: bool = False |
| 168 |
"""Debug ansible-test itself.""" |
| 169 |
|
| 170 |
ansiballz: bool = False |
| 171 |
"""Debug AnsiballZ modules.""" |
| 172 |
|
| 173 |
cli: bool = False |
| 174 |
"""Debug Ansible CLI programs other than ansible-test.""" |
| 175 |
|
| 176 |
on_demand: bool = False |
| 177 |
"""Enable debugging features only when ansible-test is running under a debugger.""" |
| 178 |
|
| 179 |
@property |
| 180 |
def enable(self) -> bool: |
| 181 |
"""Return `True` if any debugger feature other than on-demand is enabled.""" |
| 182 |
return any(getattr(self, field.name) for field in dataclasses.fields(self) if field.name != 'on_demand') |
| 183 |
|
| 184 |
@classmethod |
| 185 |
def all(cls, enabled: bool) -> t.Self: |
| 186 |
"""Return a `DebuggerFlags` instance with all flags enabled or disabled.""" |
| 187 |
return cls(**{field.name: enabled for field in dataclasses.fields(cls)}) |