-
Notifications
You must be signed in to change notification settings - Fork 271
/
apple_test_rule_support.bzl
216 lines (182 loc) · 7.83 KB
/
apple_test_rule_support.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Helper methods for implementing test rules."""
load(
"@build_bazel_rules_apple//apple:providers.bzl",
"AppleBundleInfo",
"AppleExtraOutputsInfo",
"AppleTestInfo",
"AppleTestRunnerInfo",
)
load(
"@bazel_skylib//lib:dicts.bzl",
"dicts",
)
CoverageFilesInfo = provider(
doc = """
Provider used by the `coverage_files_aspect` aspect to propagate the
transitive closure of sources and binaries that a test depends on. These files
are then made available during the coverage action as they are required by the
coverage insfrastructure. The sources are provided in the `coverage_files` field,
and the binaries in the `covered_binaries` field. This provider is only available
if when coverage collecting is enabled.
""",
fields = {
"coverage_files": "`depset` of files required to be present during a coverage run.",
"covered_binaries": """
`depset` of files representing the binaries that are being tested under a coverage run.
""",
},
)
def _coverage_files_aspect_impl(target, ctx):
"""Implementation for the `coverage_files_aspect` aspect."""
# Skip collecting files if coverage is not enabled.
if not ctx.configuration.coverage_enabled:
return []
coverage_files = []
# Collect this target's coverage files.
for attr in ["srcs", "hdrs", "non_arc_srcs"]:
for files in [x.files for x in getattr(ctx.rule.attr, attr, [])]:
coverage_files.append(files)
# Collect the binaries themselves from the various bundles involved in the test. These will be
# passed through the test environment so that `llvm-cov` can access the coverage mapping data
# embedded in them.
direct_binaries = []
transitive_binaries_sets = []
if AppleBundleInfo in target:
direct_binaries.append(target[AppleBundleInfo].binary)
# Collect dependencies coverage files.
for dep in getattr(ctx.rule.attr, "deps", []):
coverage_files.append(dep[CoverageFilesInfo].coverage_files)
for fmwk in getattr(ctx.rule.attr, "frameworks", []):
coverage_files.append(fmwk[CoverageFilesInfo].coverage_files)
transitive_binaries_sets.append(fmwk[CoverageFilesInfo].covered_binaries)
if hasattr(ctx.rule.attr, "test_host") and ctx.rule.attr.test_host:
coverage_files.append(ctx.rule.attr.test_host[CoverageFilesInfo].coverage_files)
transitive_binaries_sets.append(ctx.rule.attr.test_host[CoverageFilesInfo].covered_binaries)
return [
CoverageFilesInfo(
coverage_files = depset(transitive = coverage_files),
covered_binaries = depset(
direct = direct_binaries,
transitive = transitive_binaries_sets,
),
),
]
coverage_files_aspect = aspect(
attr_aspects = ["deps", "frameworks", "test_host"],
doc = """
This aspect walks the dependency graph through the dependency graph and collects all the sources and
headers that are depended upon transitively. These files are needed to calculate test coverage on a
test run.
This aspect propagates a `CoverageFilesInfo` provider.
""",
implementation = _coverage_files_aspect_impl,
)
def _get_template_substitutions(test_type, test_bundle, test_environment, test_host = None):
"""Dictionary with the substitutions to be applied to the template script."""
subs = {}
if test_host:
subs["test_host_path"] = test_host.short_path
else:
subs["test_host_path"] = ""
subs["test_bundle_path"] = test_bundle.short_path
subs["test_type"] = test_type.upper()
subs["test_env"] = ",".join([k + "=" + v for (k, v) in test_environment.items()])
return {"%(" + k + ")s": subs[k] for k in subs}
def _get_coverage_execution_environment(ctx, covered_binaries):
"""Returns environment variables required for test coverage support."""
covered_binary_paths = [f.short_path for f in covered_binaries.to_list()]
return {
"APPLE_COVERAGE": "1",
"TEST_BINARIES_FOR_LLVM_COV": ";".join(covered_binary_paths),
}
def _apple_test_rule_impl(ctx, test_type):
"""Implementation for the Apple test rules."""
runner = ctx.attr.runner[AppleTestRunnerInfo]
execution_requirements = getattr(runner, "execution_requirements", {})
test_bundle_target = ctx.attr.deps[0]
test_bundle = test_bundle_target[AppleTestInfo].test_bundle
# Environment variables to be set as the %(test_env)s substitution, which includes the
# --test_env and env attribute values, but not the execution environment variables.
test_environment = dicts.add(
ctx.configuration.test_env,
ctx.attr.env,
getattr(runner, "test_environment", {}),
)
# Environment variables for the Bazel test action itself.
execution_environment = dict(getattr(runner, "execution_environment", {}))
direct_runfiles = []
transitive_runfiles = []
test_host_archive = test_bundle_target[AppleTestInfo].test_host
if test_host_archive:
direct_runfiles.append(test_host_archive)
direct_runfiles.append(test_bundle)
if ctx.configuration.coverage_enabled:
covered_binaries = test_bundle_target[CoverageFilesInfo].covered_binaries
execution_environment = dicts.add(
execution_environment,
_get_coverage_execution_environment(
ctx,
covered_binaries,
),
)
transitive_runfiles.append(covered_binaries)
transitive_runfiles.append(test_bundle_target[CoverageFilesInfo].coverage_files)
transitive_runfiles.append(ctx.attr._apple_coverage_support.files)
executable = ctx.actions.declare_file("%s" % ctx.label.name)
ctx.actions.expand_template(
template = runner.test_runner_template,
output = executable,
substitutions = _get_template_substitutions(
test_type,
test_bundle,
test_environment,
test_host = test_host_archive,
),
is_executable = True,
)
# Add required data into the runfiles to make it available during test
# execution.
for data_dep in ctx.attr.data:
transitive_runfiles.append(data_dep.files)
return [
# Repropagate the AppleBundleInfo and AppleTestInfo providers from the test bundle so that
# clients interacting with the test targets themselves can access the bundle's structure.
test_bundle_target[AppleBundleInfo],
test_bundle_target[AppleTestInfo],
test_bundle_target[OutputGroupInfo],
coverage_common.instrumented_files_info(
ctx,
dependency_attributes = ["deps"],
),
testing.ExecutionInfo(execution_requirements),
testing.TestEnvironment(execution_environment),
DefaultInfo(
executable = executable,
files = depset(
[executable, test_bundle],
transitive = [test_bundle_target[AppleExtraOutputsInfo].files],
),
runfiles = ctx.runfiles(
files = direct_runfiles,
transitive_files = depset(transitive = transitive_runfiles),
)
.merge(ctx.attr.runner.default_runfiles)
.merge(ctx.attr.runner.data_runfiles),
),
]
apple_test_rule_support = struct(
apple_test_rule_impl = _apple_test_rule_impl,
)