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
|
# Copyright 2018 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.
"""Bazel rules that test the Android Local Test rule.
rule_test: Inspect and assert on rule providers.
"""
load("//rules:providers.bzl", "AndroidFilteredJdepsInfo")
load("//test/utils:asserts.bzl", "asserts")
VALIDATION = "_validation"
def _rule_test_impl(ctx):
# Assert on expected providers
if not JavaInfo in ctx.attr.target_under_test:
fail("Missing JavaInfo provider")
if not AndroidFilteredJdepsInfo in ctx.attr.target_under_test:
fail("Missing AndroidFilteredJdepsInfo provider")
# Collecting validation outputs from deps
transitive_validation_outputs = []
for dep in ctx.attr.deps:
if hasattr(dep[OutputGroupInfo], VALIDATION):
transitive_validation_outputs.append(dep[OutputGroupInfo]._validation)
output_group_info = dict(ctx.attr.expected_output_group_info)
if VALIDATION in output_group_info:
output_group_info[VALIDATION] = (
output_group_info[VALIDATION] +
[f.basename for f in depset(transitive = transitive_validation_outputs).to_list()]
)
asserts.provider.output_group_info(
output_group_info,
ctx.attr.target_under_test[OutputGroupInfo],
)
# Create test script to assert on provider contents
args = dict(
jdeps_print_tool = ctx.executable._jdeps_print_tool.short_path,
jdeps = ctx.attr.target_under_test[JavaInfo].outputs.jdeps.short_path,
filtered_jdeps = ctx.attr.target_under_test[AndroidFilteredJdepsInfo].jdeps.short_path,
res_jar = ctx.attr.target_under_test.label.name + "_resources.jar",
expect_resources = str(ctx.attr.expect_resources),
)
test_script = ctx.actions.declare_file("%s_script.sh" % ctx.label.name)
ctx.actions.write(
test_script,
"""
jdeps=`{jdeps_print_tool} --in {jdeps} | sed 's#_migrated/##g'`
filtered_jdeps=`{jdeps_print_tool} --in {filtered_jdeps} | sed 's#_migrated/##g'`
entries=`echo "$jdeps" | wc -l`
matches=`echo "$jdeps" | grep '{res_jar}' | wc -l`
filtered_entries=`echo "$filtered_jdeps" | wc -l`
filtered_matches=`echo "$filtered_jdeps" | grep '{res_jar}' | wc -l`
expected_matches=1
expected_filtering_differences=1
if [ {expect_resources} == "False" ]; then
expected_matches=0
expected_filtering_differences=0
fi
if [ $matches -ne $expected_matches ]; then
echo "Expected one resource.jar in jdeps"
exit 1
elif [ $filtered_matches -ne 0 ]; then
echo "Expected no resource.jar in filtered jdeps"
exit 1
elif [ $(($entries-$filtered_entries)) -ne $expected_filtering_differences ]; then
echo "Expected to remove one item when filtering"
exit 1
fi
""".format(**args),
is_executable = True,
)
return [
DefaultInfo(
runfiles = ctx.runfiles(
files = [
test_script,
ctx.executable._jdeps_print_tool,
ctx.attr.target_under_test[JavaInfo].outputs.jdeps,
ctx.attr.target_under_test[AndroidFilteredJdepsInfo].jdeps,
],
),
executable = test_script,
),
]
rule_test = rule(
attrs = dict(
asserts.provider.attrs.items(),
expect_resources = attr.bool(default = True),
target_under_test = attr.label(),
deps = attr.label_list(),
_jdeps_print_tool = attr.label(
cfg = "exec",
default = "//src/tools/jdeps:print_jdeps",
executable = True,
),
),
implementation = _rule_test_impl,
test = True,
)
|