fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

UebelAndreMigrate all settings labels from aliases to using settings package (#3032)c3d71edd

main
12.3 KiB453 linesraw
1"""Unittest to verify properties of rustdoc rules"""
2
3load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
4load("@rules_cc//cc:defs.bzl", "cc_library")
5load("//cargo:defs.bzl", "cargo_build_script")
6load("//rust:defs.bzl", "rust_binary", "rust_doc", "rust_doc_test", "rust_library", "rust_proc_macro", "rust_test")
7load(
8    "//test/unit:common.bzl",
9    "assert_action_mnemonic",
10    "assert_argv_contains",
11    "assert_argv_contains_prefix_not",
12)
13
14def _get_rustdoc_action(env, tut):
15    actions = tut.actions
16    action = actions[0]
17    assert_action_mnemonic(env, action, "Rustdoc")
18
19    return action
20
21def _common_rustdoc_checks(env, tut):
22    action = _get_rustdoc_action(env, tut)
23
24    # These flags, while required for `Rustc` actions, should be omitted for
25    # `Rustdoc` actions
26    assert_argv_contains_prefix_not(env, action, "--remap-path-prefix")
27    assert_argv_contains_prefix_not(env, action, "--emit")
28
29def _rustdoc_for_lib_test_impl(ctx):
30    env = analysistest.begin(ctx)
31    tut = analysistest.target_under_test(env)
32
33    _common_rustdoc_checks(env, tut)
34
35    return analysistest.end(env)
36
37def _rustdoc_for_bin_test_impl(ctx):
38    env = analysistest.begin(ctx)
39    tut = analysistest.target_under_test(env)
40
41    _common_rustdoc_checks(env, tut)
42
43    return analysistest.end(env)
44
45def _rustdoc_for_bin_with_cc_lib_test_impl(ctx):
46    env = analysistest.begin(ctx)
47    tut = analysistest.target_under_test(env)
48
49    _common_rustdoc_checks(env, tut)
50
51    return analysistest.end(env)
52
53def _rustdoc_for_bin_with_transitive_cc_lib_test_impl(ctx):
54    env = analysistest.begin(ctx)
55    tut = analysistest.target_under_test(env)
56
57    _common_rustdoc_checks(env, tut)
58
59    return analysistest.end(env)
60
61def _rustdoc_for_proc_macro_test_impl(ctx):
62    env = analysistest.begin(ctx)
63    tut = analysistest.target_under_test(env)
64
65    _common_rustdoc_checks(env, tut)
66
67    return analysistest.end(env)
68
69def _rustdoc_for_lib_with_proc_macro_test_impl(ctx):
70    env = analysistest.begin(ctx)
71    tut = analysistest.target_under_test(env)
72
73    _common_rustdoc_checks(env, tut)
74
75    return analysistest.end(env)
76
77def _rustdoc_for_lib_with_proc_macro_in_docs_test_impl(ctx):
78    env = analysistest.begin(ctx)
79    tut = analysistest.target_under_test(env)
80
81    _common_rustdoc_checks(env, tut)
82
83    return analysistest.end(env)
84
85def _rustdoc_for_bin_with_transitive_proc_macro_test_impl(ctx):
86    env = analysistest.begin(ctx)
87    tut = analysistest.target_under_test(env)
88
89    _common_rustdoc_checks(env, tut)
90
91    return analysistest.end(env)
92
93def _rustdoc_for_lib_with_cc_lib_test_impl(ctx):
94    env = analysistest.begin(ctx)
95    tut = analysistest.target_under_test(env)
96
97    _common_rustdoc_checks(env, tut)
98
99    return analysistest.end(env)
100
101def _rustdoc_with_args_test_impl(ctx):
102    env = analysistest.begin(ctx)
103    tut = analysistest.target_under_test(env)
104
105    _common_rustdoc_checks(env, tut)
106
107    action = _get_rustdoc_action(env, tut)
108
109    assert_argv_contains(env, action, "--allow=rustdoc::broken_intra_doc_links")
110
111    return analysistest.end(env)
112
113def _rustdoc_zip_output_test_impl(ctx):
114    env = analysistest.begin(ctx)
115    tut = analysistest.target_under_test(env)
116
117    files = tut[DefaultInfo].files.to_list()
118    asserts.equals(
119        env,
120        len(files),
121        1,
122        "The target under this test should have 1 DefaultInfo file but has {}".format(
123            len(files),
124        ),
125    )
126
127    zip_file = files[0]
128    asserts.true(
129        env,
130        zip_file.basename.endswith(".zip"),
131        "{} did not end with `.zip`".format(
132            zip_file.path,
133        ),
134    )
135
136    return analysistest.end(env)
137
138def _rustdoc_with_json_error_format_test_impl(ctx):
139    env = analysistest.begin(ctx)
140    tut = analysistest.target_under_test(env)
141
142    _common_rustdoc_checks(env, tut)
143
144    action = _get_rustdoc_action(env, tut)
145
146    assert_argv_contains(env, action, "--error-format=json")
147
148    return analysistest.end(env)
149
150rustdoc_for_lib_test = analysistest.make(_rustdoc_for_lib_test_impl)
151rustdoc_for_bin_test = analysistest.make(_rustdoc_for_bin_test_impl)
152rustdoc_for_bin_with_cc_lib_test = analysistest.make(_rustdoc_for_bin_with_cc_lib_test_impl)
153rustdoc_for_bin_with_transitive_cc_lib_test = analysistest.make(_rustdoc_for_bin_with_transitive_cc_lib_test_impl)
154rustdoc_for_proc_macro_test = analysistest.make(_rustdoc_for_proc_macro_test_impl)
155rustdoc_for_lib_with_proc_macro_test = analysistest.make(_rustdoc_for_lib_with_proc_macro_test_impl)
156rustdoc_for_lib_with_proc_macro_in_docs_test = analysistest.make(_rustdoc_for_lib_with_proc_macro_in_docs_test_impl)
157rustdoc_for_bin_with_transitive_proc_macro_test = analysistest.make(_rustdoc_for_bin_with_transitive_proc_macro_test_impl)
158rustdoc_for_lib_with_cc_lib_test = analysistest.make(_rustdoc_for_lib_with_cc_lib_test_impl)
159rustdoc_with_args_test = analysistest.make(_rustdoc_with_args_test_impl)
160rustdoc_zip_output_test = analysistest.make(_rustdoc_zip_output_test_impl)
161rustdoc_with_json_error_format_test = analysistest.make(_rustdoc_with_json_error_format_test_impl, config_settings = {
162    str(Label("//rust/settings:error_format")): "json",
163})
164
165def _target_maker(rule_fn, name, rustdoc_deps = [], rustdoc_proc_macro_deps = [], **kwargs):
166    rule_fn(
167        name = name,
168        edition = "2018",
169        **kwargs
170    )
171
172    rust_test(
173        name = "{}_test".format(name),
174        crate = ":{}".format(name),
175        edition = "2018",
176    )
177
178    rust_doc(
179        name = "{}_doc".format(name),
180        crate = ":{}".format(name),
181    )
182
183    rust_doc_test(
184        name = "{}_doctest".format(name),
185        crate = ":{}".format(name),
186        deps = rustdoc_deps,
187        proc_macro_deps = rustdoc_proc_macro_deps,
188    )
189
190def _define_targets():
191    rust_library(
192        name = "adder",
193        srcs = ["adder.rs"],
194        edition = "2018",
195    )
196
197    _target_maker(
198        rust_binary,
199        name = "bin",
200        srcs = ["rustdoc_bin.rs"],
201    )
202
203    _target_maker(
204        rust_binary,
205        name = "bin_with_cc",
206        srcs = ["rustdoc_bin.rs"],
207        crate_features = ["with_cc"],
208        deps = [":cc_lib"],
209    )
210
211    _target_maker(
212        rust_binary,
213        name = "bin_with_transitive_cc",
214        srcs = ["rustdoc_bin.rs"],
215        crate_features = ["with_cc"],
216        deps = [":transitive_cc_lib"],
217    )
218
219    _target_maker(
220        rust_library,
221        name = "lib",
222        srcs = ["rustdoc_lib.rs"],
223        rustdoc_deps = [":adder"],
224    )
225
226    _target_maker(
227        rust_library,
228        name = "nodep_lib",
229        srcs = ["rustdoc_nodep_lib.rs"],
230    )
231
232    _target_maker(
233        rust_proc_macro,
234        name = "rustdoc_proc_macro",
235        srcs = ["rustdoc_proc_macro.rs"],
236    )
237
238    _target_maker(
239        rust_library,
240        name = "lib_with_proc_macro",
241        srcs = ["rustdoc_lib.rs"],
242        rustdoc_deps = [":adder"],
243        proc_macro_deps = [":rustdoc_proc_macro"],
244        crate_features = ["with_proc_macro"],
245    )
246
247    _target_maker(
248        rust_library,
249        name = "lib_with_proc_macro_in_docs",
250        srcs = ["procmacro_in_rustdoc.rs"],
251        proc_macro_deps = [":rustdoc_proc_macro"],
252    )
253
254    _target_maker(
255        rust_library,
256        name = "lib_with_proc_macro_only_in_docs",
257        srcs = ["procmacro_in_rustdoc.rs"],
258        rustdoc_proc_macro_deps = [":rustdoc_proc_macro"],
259    )
260
261    _target_maker(
262        rust_library,
263        name = "lib_nodep_with_proc_macro",
264        srcs = ["rustdoc_nodep_lib.rs"],
265        proc_macro_deps = [":rustdoc_proc_macro"],
266        crate_features = ["with_proc_macro"],
267    )
268
269    _target_maker(
270        rust_binary,
271        name = "bin_with_transitive_proc_macro",
272        srcs = ["rustdoc_bin.rs"],
273        deps = [":lib_with_proc_macro"],
274        proc_macro_deps = [":rustdoc_proc_macro"],
275        crate_features = ["with_proc_macro"],
276    )
277
278    cc_library(
279        name = "cc_lib",
280        hdrs = ["rustdoc.h"],
281        srcs = ["rustdoc.cc"],
282    )
283
284    cc_library(
285        name = "transitive_cc_lib",
286        hdrs = ["rustdoc.h"],
287        srcs = ["rustdoc.cc"],
288        deps = [":cc_lib"],
289        # This is not needed for :cc_lib, but it is needed in other
290        # circumstances to link in system libraries.
291        linkopts = ["-lcc_lib"],
292        linkstatic = True,
293    )
294
295    _target_maker(
296        rust_library,
297        name = "lib_with_cc",
298        srcs = ["rustdoc_lib.rs"],
299        rustdoc_deps = [":adder"],
300        crate_features = ["with_cc"],
301        deps = [":cc_lib"],
302    )
303
304    _target_maker(
305        rust_library,
306        name = "lib_nodep_with_cc",
307        srcs = ["rustdoc_nodep_lib.rs"],
308        crate_features = ["with_cc"],
309        deps = [":cc_lib"],
310    )
311
312    cargo_build_script(
313        name = "lib_build_script",
314        srcs = ["rustdoc_build.rs"],
315        edition = "2018",
316    )
317
318    _target_maker(
319        rust_library,
320        name = "lib_with_build_script",
321        srcs = ["rustdoc_lib.rs"],
322        rustdoc_deps = [":adder"],
323        crate_features = ["with_build_script"],
324        deps = [":lib_build_script"],
325    )
326
327    _target_maker(
328        rust_library,
329        name = "lib_nodep_with_build_script",
330        srcs = ["rustdoc_nodep_lib.rs"],
331        crate_features = ["with_build_script"],
332        deps = [":lib_build_script"],
333    )
334
335    rust_library(
336        name = "lib_requires_args",
337        srcs = ["rustdoc_requires_args.rs"],
338        edition = "2018",
339    )
340
341    rust_doc(
342        name = "rustdoc_with_args",
343        crate = ":lib_requires_args",
344        rustdoc_flags = [
345            "--allow=rustdoc::broken_intra_doc_links",
346        ],
347    )
348
349    rust_library(
350        name = "lib_dep_with_alias",
351        srcs = ["rustdoc_test_dep_with_alias.rs"],
352        edition = "2018",
353        deps = [":adder"],
354        aliases = {
355            ":adder": "aliased_adder",
356        },
357    )
358
359    rust_doc_test(
360        name = "rustdoc_test_with_alias_test",
361        crate = ":lib_dep_with_alias",
362    )
363
364def rustdoc_test_suite(name):
365    """Entry-point macro called from the BUILD file.
366
367    Args:
368        name (str): Name of the macro.
369    """
370
371    _define_targets()
372
373    rustdoc_for_lib_test(
374        name = "rustdoc_for_lib_test",
375        target_under_test = ":lib_doc",
376    )
377
378    rustdoc_for_bin_test(
379        name = "rustdoc_for_bin_test",
380        target_under_test = ":bin_doc",
381    )
382
383    rustdoc_for_bin_with_cc_lib_test(
384        name = "rustdoc_for_bin_with_cc_lib_test",
385        target_under_test = ":bin_with_cc_doc",
386    )
387
388    rustdoc_for_bin_with_transitive_cc_lib_test(
389        name = "rustdoc_for_bin_with_transitive_cc_lib_test",
390        target_under_test = ":bin_with_transitive_cc_doc",
391    )
392
393    rustdoc_for_proc_macro_test(
394        name = "rustdoc_for_proc_macro_test",
395        target_under_test = ":rustdoc_proc_macro_doc",
396    )
397
398    rustdoc_for_lib_with_proc_macro_in_docs_test(
399        name = "rustdoc_for_lib_with_proc_macro_in_docs_test",
400        target_under_test = ":lib_with_proc_macro_in_docs_doc",
401    )
402
403    rustdoc_for_lib_with_proc_macro_test(
404        name = "rustdoc_for_lib_with_proc_macro_test",
405        target_under_test = ":lib_with_proc_macro_doc",
406    )
407
408    rustdoc_for_bin_with_transitive_proc_macro_test(
409        name = "rustdoc_for_bin_with_transitive_proc_macro_test",
410        target_under_test = ":bin_with_transitive_proc_macro_doc",
411    )
412
413    rustdoc_for_lib_with_cc_lib_test(
414        name = "rustdoc_for_lib_with_cc_lib_test",
415        target_under_test = ":lib_with_cc_doc",
416    )
417
418    rustdoc_with_args_test(
419        name = "rustdoc_with_args_test",
420        target_under_test = ":rustdoc_with_args",
421    )
422
423    rustdoc_with_json_error_format_test(
424        name = "rustdoc_with_json_error_format_test",
425        target_under_test = ":lib_doc",
426    )
427
428    native.filegroup(
429        name = "lib_doc_zip",
430        srcs = [":lib_doc.zip"],
431    )
432
433    rustdoc_zip_output_test(
434        name = "rustdoc_zip_output_test",
435        target_under_test = ":lib_doc_zip",
436    )
437
438    native.test_suite(
439        name = name,
440        tests = [
441            ":rustdoc_for_lib_test",
442            ":rustdoc_for_bin_test",
443            ":rustdoc_for_bin_with_cc_lib_test",
444            ":rustdoc_for_bin_with_transitive_cc_lib_test",
445            ":rustdoc_for_proc_macro_test",
446            ":rustdoc_for_lib_with_proc_macro_in_docs_test",
447            ":rustdoc_for_lib_with_proc_macro_test",
448            ":rustdoc_for_lib_with_cc_lib_test",
449            ":rustdoc_with_args_test",
450            ":rustdoc_with_json_error_format_test",
451            ":rustdoc_zip_output_test",
452        ],
453    )