Debian Codegraph @

test/lib/ansible_test/_internal/executor.py

raw
1 """Execute Ansible tests."""
2
3 from __future__ import annotations
4
5 import typing as t
6
7 from .io import (
8 read_text_file,
9 )
10
11 from .util import (
12 ApplicationWarning,
13 display,
14 )
15
16 from .ci import (
17 get_ci_provider,
18 )
19
20 from .classification import (
21 categorize_changes,
22 )
23
24 from .config import (
25 TestConfig,
26 )
27
28 from .metadata import (
29 ChangeDescription,
30 )
31
32 from .provisioning import (
33 HostState,
34 )
35
36
37 def get_changes_filter(args: TestConfig) -> list[str]:
38 """Return a list of targets which should be tested based on the changes made."""
39 paths = detect_changes(args)
40
41 if not args.metadata.change_description:
42 if paths:
43 changes = categorize_changes(args, paths, args.command)
44 else:
45 changes = ChangeDescription()
46
47 args.metadata.change_description = changes
48
49 if paths is None:
50 return [] # change detection not enabled, do not filter targets
51
52 if not paths:
53 raise NoChangesDetected()
54
55 if args.metadata.change_description.targets is None:
56 raise NoTestsForChanges()
57
58 return args.metadata.change_description.targets
59
60
61 def detect_changes(args: TestConfig) -> t.Optional[list[str]]:
62 """Return a list of changed paths."""
63 if args.changed:
64 paths = get_ci_provider().detect_changes(args)
65 elif args.changed_from or args.changed_path:
66 paths = args.changed_path or []
67 if args.changed_from:
68 paths += read_text_file(args.changed_from).splitlines()
69 else:
70 return None # change detection not enabled
71
72 if paths is None:
73 return None # act as though change detection not enabled, do not filter targets
74
75 display.info('Detected changes in %d file(s).' % len(paths))
76
77 for path in paths:
78 display.info(path, verbosity=1)
79
80 return paths
81
82
83 class NoChangesDetected(ApplicationWarning):
84 """Exception when change detection was performed, but no changes were found."""
85
86 def __init__(self) -> None:
87 super().__init__('No changes detected.')
88
89
90 class NoTestsForChanges(ApplicationWarning):
91 """Exception when changes detected, but no tests trigger as a result."""
92
93 def __init__(self) -> None:
94 super().__init__('No tests found for detected changes.')
95
96
97 class Delegate(Exception):
98 """Trigger command delegation."""
99
100 def __init__(self, host_state: HostState, exclude: list[str] = None, require: list[str] = None) -> None:
101 super().__init__()
102
103 self.host_state = host_state
104 self.exclude = exclude or []
105 self.require = require or []
106
107
108 class ListTargets(Exception):
109 """List integration test targets instead of executing them."""
110
111 def __init__(self, target_names: list[str]) -> None:
112 super().__init__()
113
114 self.target_names = target_names
115
116
117 class AllTargetsSkipped(ApplicationWarning):
118 """All targets skipped."""
119
120 def __init__(self) -> None:
121 super().__init__('All targets skipped.')