forked from bazelbuild/rules_rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile_data_test.bzl
More file actions
239 lines (197 loc) · 6.98 KB
/
compile_data_test.bzl
File metadata and controls
239 lines (197 loc) · 6.98 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""Unittest to verify compile_data (attribute) propagation"""
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//rust:defs.bzl", "rust_common", "rust_doc", "rust_library", "rust_test")
load(
"//test/unit:common.bzl",
"assert_action_mnemonic",
)
def _target_has_compile_data(ctx, expected):
env = analysistest.begin(ctx)
target = analysistest.target_under_test(env)
# Extract compile_data from a target expected to have a `CrateInfo` provider
crate_info = target[rust_common.crate_info]
compile_data = crate_info.compile_data.to_list()
# Ensure compile data was correctly propagated to the provider
asserts.equals(
env,
sorted([data.short_path for data in compile_data]),
expected,
)
return analysistest.end(env)
def _compile_data_propagates_to_crate_info_test_impl(ctx):
return _target_has_compile_data(
ctx,
["test/unit/compile_data/compile_data.txt"],
)
def _wrapper_rule_propagates_to_crate_info_test_impl(ctx):
return _target_has_compile_data(
ctx,
["test/unit/compile_data/compile_data.txt"],
)
def _wrapper_rule_propagates_and_joins_compile_data_test_impl(ctx):
return _target_has_compile_data(
ctx,
[
"test/unit/compile_data/compile_data.txt",
"test/unit/compile_data/test_compile_data.txt",
],
)
def _compile_data_propagates_to_rust_doc_test_impl(ctx):
env = analysistest.begin(ctx)
target = analysistest.target_under_test(env)
actions = target.actions
action = actions[0]
assert_action_mnemonic(env, action, "Rustdoc")
return analysistest.end(env)
def _transitive_data_not_in_compile_inputs_test_impl(ctx):
env = analysistest.begin(ctx)
target = analysistest.target_under_test(env)
rustc_action = None
for action in target.actions:
if action.mnemonic == "Rustc":
rustc_action = action
break
asserts.false(env, rustc_action == None, "Expected a Rustc action")
data_inputs = [i for i in rustc_action.inputs.to_list() if "transitive_data_dep.txt" in i.path]
asserts.equals(
env,
0,
len(data_inputs),
"Expected transitive data dep file to NOT appear in Rustc action inputs, but found: " +
str([i.path for i in data_inputs]),
)
return analysistest.end(env)
compile_data_propagates_to_crate_info_test = analysistest.make(_compile_data_propagates_to_crate_info_test_impl)
wrapper_rule_propagates_to_crate_info_test = analysistest.make(_wrapper_rule_propagates_to_crate_info_test_impl)
wrapper_rule_propagates_and_joins_compile_data_test = analysistest.make(_wrapper_rule_propagates_and_joins_compile_data_test_impl)
compile_data_propagates_to_rust_doc_test = analysistest.make(_compile_data_propagates_to_rust_doc_test_impl)
transitive_data_not_in_compile_inputs_test = analysistest.make(_transitive_data_not_in_compile_inputs_test_impl)
def _define_test_targets():
rust_library(
name = "compile_data",
srcs = ["compile_data.rs"],
compile_data = ["compile_data.txt"],
edition = "2018",
)
rust_test(
name = "compile_data_unit_test",
crate = ":compile_data",
)
rust_test(
name = "test_compile_data_unit_test",
compile_data = ["test_compile_data.txt"],
crate = ":compile_data",
rustc_flags = ["--cfg=test_compile_data"],
)
rust_library(
name = "compile_data_env",
srcs = ["compile_data_env.rs"],
compile_data = ["compile_data.txt"],
rustc_env = {
"COMPILE_DATA_PATH": "$(execpath :compile_data.txt)",
},
edition = "2018",
)
rust_doc(
name = "compile_data_env_rust_doc",
crate = ":compile_data_env",
)
write_file(
name = "generated_compile_data",
out = "generated.txt",
content = ["generated compile data contents", ""],
newline = "unix",
)
rust_library(
name = "compile_data_gen",
srcs = ["compile_data_gen.rs"],
compile_data = [":generated.txt"],
edition = "2021",
)
rust_test(
name = "compile_data_gen_unit_test",
crate = ":compile_data_gen",
)
write_file(
name = "generated_src",
out = "generated.rs",
content = ["pub const GENERATED: &str = \"generated\";", ""],
newline = "unix",
)
rust_library(
name = "compile_data_gen_srcs",
srcs = ["compile_data_gen_srcs.rs", ":generated.rs"],
compile_data = ["compile_data.txt"],
edition = "2021",
)
rust_test(
name = "compile_data_gen_srcs_unit_test",
crate = ":compile_data_gen_srcs",
)
write_file(
name = "transitive_data_dep_file",
out = "transitive_data_dep.txt",
content = ["transitive data dep", ""],
newline = "unix",
)
write_file(
name = "lib_with_data_src",
out = "lib_with_data.rs",
content = ["pub fn hello() {}", ""],
newline = "unix",
)
rust_library(
name = "lib_with_data",
srcs = [":lib_with_data.rs"],
data = [":transitive_data_dep.txt"],
edition = "2021",
)
write_file(
name = "lib_depending_on_data_src",
out = "lib_depending_on_data.rs",
content = ["extern crate lib_with_data;", ""],
newline = "unix",
)
rust_library(
name = "lib_depending_on_data",
srcs = [":lib_depending_on_data.rs"],
deps = [":lib_with_data"],
edition = "2021",
)
def compile_data_test_suite(name):
"""Entry-point macro called from the BUILD file.
Args:
name (str): Name of the macro.
"""
_define_test_targets()
compile_data_propagates_to_crate_info_test(
name = "compile_data_propagates_to_crate_info_test",
target_under_test = ":compile_data",
)
wrapper_rule_propagates_to_crate_info_test(
name = "wrapper_rule_propagates_to_crate_info_test",
target_under_test = ":compile_data_unit_test",
)
wrapper_rule_propagates_and_joins_compile_data_test(
name = "wrapper_rule_propagates_and_joins_compile_data_test",
target_under_test = ":test_compile_data_unit_test",
)
compile_data_propagates_to_rust_doc_test(
name = "compile_data_propagates_to_rust_doc_test",
target_under_test = ":compile_data_env_rust_doc",
)
transitive_data_not_in_compile_inputs_test(
name = "transitive_data_not_in_compile_inputs_test",
target_under_test = ":lib_depending_on_data",
)
native.test_suite(
name = name,
tests = [
":compile_data_propagates_to_crate_info_test",
":wrapper_rule_propagates_to_crate_info_test",
":wrapper_rule_propagates_and_joins_compile_data_test",
":compile_data_propagates_to_rust_doc_test",
":transitive_data_not_in_compile_inputs_test",
],
)