|
14 | 14 | ApplicationError,
|
15 | 15 | EnvironmentConfig,
|
16 | 16 | run_command,
|
| 17 | + common_environment, |
17 | 18 | )
|
18 | 19 |
|
19 | 20 | from lib.executor import (
|
|
23 | 24 |
|
24 | 25 | COVERAGE_DIR = 'test/results/coverage'
|
25 | 26 | COVERAGE_FILE = os.path.join(COVERAGE_DIR, 'coverage')
|
| 27 | +COVERAGE_GROUPS = ('command', 'target', 'environment', 'version') |
26 | 28 |
|
27 | 29 |
|
28 | 30 | def command_coverage_combine(args):
|
29 | 31 | """Patch paths in coverage files and merge into a single file.
|
30 | 32 | :type args: CoverageConfig
|
| 33 | + :rtype: list[str] |
31 | 34 | """
|
32 | 35 | coverage = initialize_coverage(args)
|
33 | 36 |
|
34 | 37 | modules = dict((t.module, t.path) for t in list(walk_module_targets()))
|
35 | 38 |
|
36 | 39 | coverage_files = [os.path.join(COVERAGE_DIR, f) for f in os.listdir(COVERAGE_DIR) if '=coverage.' in f]
|
37 | 40 |
|
38 |
| - arc_data = {} |
39 |
| - |
40 | 41 | ansible_path = os.path.abspath('lib/ansible/') + '/'
|
41 | 42 | root_path = os.getcwd() + '/'
|
42 | 43 |
|
43 | 44 | counter = 0
|
| 45 | + groups = {} |
44 | 46 |
|
45 | 47 | for coverage_file in coverage_files:
|
46 | 48 | counter += 1
|
47 | 49 | display.info('[%4d/%4d] %s' % (counter, len(coverage_files), coverage_file), verbosity=2)
|
48 | 50 |
|
49 | 51 | original = coverage.CoverageData()
|
50 | 52 |
|
| 53 | + group = get_coverage_group(args, coverage_file) |
| 54 | + |
| 55 | + if group is None: |
| 56 | + display.warning('Unexpected name for coverage file: %s' % coverage_file) |
| 57 | + continue |
| 58 | + |
51 | 59 | if os.path.getsize(coverage_file) == 0:
|
52 | 60 | display.warning('Empty coverage file: %s' % coverage_file)
|
53 | 61 | continue
|
@@ -83,46 +91,77 @@ def command_coverage_combine(args):
|
83 | 91 | display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
84 | 92 | filename = new_name
|
85 | 93 |
|
| 94 | + if group not in groups: |
| 95 | + groups[group] = {} |
| 96 | + |
| 97 | + arc_data = groups[group] |
| 98 | + |
86 | 99 | if filename not in arc_data:
|
87 | 100 | arc_data[filename] = set()
|
88 | 101 |
|
89 | 102 | arc_data[filename].update(arcs)
|
90 | 103 |
|
91 |
| - updated = coverage.CoverageData() |
| 104 | + output_files = [] |
92 | 105 |
|
93 |
| - for filename in arc_data: |
94 |
| - if not os.path.isfile(filename): |
95 |
| - display.warning('Invalid coverage path: %s' % filename) |
96 |
| - continue |
| 106 | + for group in sorted(groups): |
| 107 | + arc_data = groups[group] |
97 | 108 |
|
98 |
| - updated.add_arcs({filename: list(arc_data[filename])}) |
| 109 | + updated = coverage.CoverageData() |
99 | 110 |
|
100 |
| - if not args.explain: |
101 |
| - updated.write_file(COVERAGE_FILE) |
| 111 | + for filename in arc_data: |
| 112 | + if not os.path.isfile(filename): |
| 113 | + display.warning('Invalid coverage path: %s' % filename) |
| 114 | + continue |
| 115 | + |
| 116 | + updated.add_arcs({filename: list(arc_data[filename])}) |
| 117 | + |
| 118 | + if not args.explain: |
| 119 | + output_file = COVERAGE_FILE + group |
| 120 | + updated.write_file(output_file) |
| 121 | + output_files.append(output_file) |
| 122 | + |
| 123 | + return sorted(output_files) |
102 | 124 |
|
103 | 125 |
|
104 | 126 | def command_coverage_report(args):
|
105 | 127 | """
|
106 | 128 | :type args: CoverageConfig
|
107 | 129 | """
|
108 |
| - command_coverage_combine(args) |
109 |
| - run_command(args, ['coverage', 'report']) |
| 130 | + output_files = command_coverage_combine(args) |
| 131 | + |
| 132 | + for output_file in output_files: |
| 133 | + if args.group_by: |
| 134 | + display.info('>>> Coverage Group: %s' % ' '.join(os.path.basename(output_file).split('=')[1:])) |
| 135 | + |
| 136 | + env = common_environment() |
| 137 | + env.update(dict(COVERAGE_FILE=output_file)) |
| 138 | + run_command(args, env=env, cmd=['coverage', 'report']) |
110 | 139 |
|
111 | 140 |
|
112 | 141 | def command_coverage_html(args):
|
113 | 142 | """
|
114 | 143 | :type args: CoverageConfig
|
115 | 144 | """
|
116 |
| - command_coverage_combine(args) |
117 |
| - run_command(args, ['coverage', 'html', '-d', 'test/results/reports/coverage']) |
| 145 | + output_files = command_coverage_combine(args) |
| 146 | + |
| 147 | + for output_file in output_files: |
| 148 | + dir_name = 'test/results/reports/%s' % os.path.basename(output_file) |
| 149 | + env = common_environment() |
| 150 | + env.update(dict(COVERAGE_FILE=output_file)) |
| 151 | + run_command(args, env=env, cmd=['coverage', 'html', '-d', dir_name]) |
118 | 152 |
|
119 | 153 |
|
120 | 154 | def command_coverage_xml(args):
|
121 | 155 | """
|
122 | 156 | :type args: CoverageConfig
|
123 | 157 | """
|
124 |
| - command_coverage_combine(args) |
125 |
| - run_command(args, ['coverage', 'xml', '-o', 'test/results/reports/coverage.xml']) |
| 158 | + output_files = command_coverage_combine(args) |
| 159 | + |
| 160 | + for output_file in output_files: |
| 161 | + xml_name = 'test/results/reports/%s.xml' % os.path.basename(output_file) |
| 162 | + env = common_environment() |
| 163 | + env.update(dict(COVERAGE_FILE=output_file)) |
| 164 | + run_command(args, env=env, cmd=['coverage', 'xml', '-o', xml_name]) |
126 | 165 |
|
127 | 166 |
|
128 | 167 | def command_coverage_erase(args):
|
@@ -163,10 +202,39 @@ def initialize_coverage(args):
|
163 | 202 | return coverage
|
164 | 203 |
|
165 | 204 |
|
| 205 | +def get_coverage_group(args, coverage_file): |
| 206 | + """ |
| 207 | + :type args: CoverageConfig |
| 208 | + :type coverage_file: str |
| 209 | + :rtype: str |
| 210 | + """ |
| 211 | + parts = os.path.basename(coverage_file).split('=', 4) |
| 212 | + |
| 213 | + if len(parts) != 5 or not parts[4].startswith('coverage.'): |
| 214 | + return None |
| 215 | + |
| 216 | + names = dict( |
| 217 | + command=parts[0], |
| 218 | + target=parts[1], |
| 219 | + environment=parts[2], |
| 220 | + version=parts[3], |
| 221 | + ) |
| 222 | + |
| 223 | + group = '' |
| 224 | + |
| 225 | + for part in COVERAGE_GROUPS: |
| 226 | + if part in args.group_by: |
| 227 | + group += '=%s' % names[part] |
| 228 | + |
| 229 | + return group |
| 230 | + |
| 231 | + |
166 | 232 | class CoverageConfig(EnvironmentConfig):
|
167 | 233 | """Configuration for the coverage command."""
|
168 | 234 | def __init__(self, args):
|
169 | 235 | """
|
170 | 236 | :type args: any
|
171 | 237 | """
|
172 | 238 | super(CoverageConfig, self).__init__(args, 'coverage')
|
| 239 | + |
| 240 | + self.group_by = frozenset(args.group_by) if 'group_by' in args and args.group_by else set() # type: frozenset[str] |
0 commit comments