fork/rules_rust

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

David TolnayOutside of examples, fill in all `edition` attrs (#1257)03a70796

0.11.0
5.5 KiB208 linesraw
1"""Unittest to verify properties of rustdoc rules"""
2
3load("@bazel_skylib//lib:unittest.bzl", "analysistest")
4load("@rules_cc//cc:defs.bzl", "cc_library")
5load("//rust:defs.bzl", "rust_binary", "rust_doc", "rust_doc_test", "rust_library", "rust_proc_macro", "rust_test")
6load(
7    "//test/unit:common.bzl",
8    "assert_action_mnemonic",
9    "assert_argv_contains_prefix_not",
10)
11
12def _common_rustdoc_checks(env, tut):
13    actions = tut.actions
14    action = actions[0]
15    assert_action_mnemonic(env, action, "Rustdoc")
16
17    # These flags, while required for `Rustc` actions, should be omitted for
18    # `Rustdoc` actions
19    assert_argv_contains_prefix_not(env, action, "--remap-path-prefix")
20    assert_argv_contains_prefix_not(env, action, "--emit")
21
22def _rustdoc_for_lib_test_impl(ctx):
23    env = analysistest.begin(ctx)
24    tut = analysistest.target_under_test(env)
25
26    _common_rustdoc_checks(env, tut)
27
28    return analysistest.end(env)
29
30def _rustdoc_for_bin_test_impl(ctx):
31    env = analysistest.begin(ctx)
32    tut = analysistest.target_under_test(env)
33
34    _common_rustdoc_checks(env, tut)
35
36    return analysistest.end(env)
37
38def _rustdoc_for_proc_macro_test_impl(ctx):
39    env = analysistest.begin(ctx)
40    tut = analysistest.target_under_test(env)
41
42    _common_rustdoc_checks(env, tut)
43
44    return analysistest.end(env)
45
46def _rustdoc_for_lib_with_proc_macro_test_impl(ctx):
47    env = analysistest.begin(ctx)
48    tut = analysistest.target_under_test(env)
49
50    _common_rustdoc_checks(env, tut)
51
52    return analysistest.end(env)
53
54def _rustdoc_for_bin_with_transitive_proc_macro_test_impl(ctx):
55    env = analysistest.begin(ctx)
56    tut = analysistest.target_under_test(env)
57
58    _common_rustdoc_checks(env, tut)
59
60    return analysistest.end(env)
61
62def _rustdoc_for_lib_with_cc_lib_test_impl(ctx):
63    env = analysistest.begin(ctx)
64    tut = analysistest.target_under_test(env)
65
66    _common_rustdoc_checks(env, tut)
67
68    return analysistest.end(env)
69
70rustdoc_for_lib_test = analysistest.make(_rustdoc_for_lib_test_impl)
71rustdoc_for_bin_test = analysistest.make(_rustdoc_for_bin_test_impl)
72rustdoc_for_proc_macro_test = analysistest.make(_rustdoc_for_proc_macro_test_impl)
73rustdoc_for_lib_with_proc_macro_test = analysistest.make(_rustdoc_for_lib_with_proc_macro_test_impl)
74rustdoc_for_bin_with_transitive_proc_macro_test = analysistest.make(_rustdoc_for_bin_with_transitive_proc_macro_test_impl)
75rustdoc_for_lib_with_cc_lib_test = analysistest.make(_rustdoc_for_lib_with_cc_lib_test_impl)
76
77def _target_maker(rule_fn, name, rustdoc_deps = [], **kwargs):
78    rule_fn(
79        name = name,
80        edition = "2018",
81        **kwargs
82    )
83
84    rust_test(
85        name = "{}_test".format(name),
86        crate = ":{}".format(name),
87        edition = "2018",
88    )
89
90    rust_doc(
91        name = "{}_doc".format(name),
92        crate = ":{}".format(name),
93    )
94
95    rust_doc_test(
96        name = "{}_doctest".format(name),
97        crate = ":{}".format(name),
98        deps = rustdoc_deps,
99    )
100
101def _define_targets():
102    rust_library(
103        name = "adder",
104        srcs = ["adder.rs"],
105        edition = "2018",
106    )
107
108    _target_maker(
109        rust_binary,
110        name = "bin",
111        srcs = ["rustdoc_bin.rs"],
112    )
113
114    _target_maker(
115        rust_library,
116        name = "lib",
117        srcs = ["rustdoc_lib.rs"],
118        rustdoc_deps = [":adder"],
119    )
120
121    _target_maker(
122        rust_proc_macro,
123        name = "rustdoc_proc_macro",
124        srcs = ["rustdoc_proc_macro.rs"],
125    )
126
127    _target_maker(
128        rust_library,
129        name = "lib_with_proc_macro",
130        srcs = ["rustdoc_lib.rs"],
131        rustdoc_deps = [":adder"],
132        proc_macro_deps = [":rustdoc_proc_macro"],
133        crate_features = ["with_proc_macro"],
134    )
135
136    _target_maker(
137        rust_binary,
138        name = "bin_with_transitive_proc_macro",
139        srcs = ["rustdoc_bin.rs"],
140        deps = [":lib_with_proc_macro"],
141        proc_macro_deps = [":rustdoc_proc_macro"],
142        crate_features = ["with_proc_macro"],
143    )
144
145    cc_library(
146        name = "cc_lib",
147        hdrs = ["rustdoc.h"],
148        srcs = ["rustdoc.cc"],
149    )
150
151    _target_maker(
152        rust_library,
153        name = "lib_with_cc",
154        srcs = ["rustdoc_lib.rs"],
155        rustdoc_deps = [":adder"],
156        crate_features = ["with_cc"],
157        deps = [":cc_lib"],
158    )
159
160def rustdoc_test_suite(name):
161    """Entry-point macro called from the BUILD file.
162
163    Args:
164        name (str): Name of the macro.
165    """
166
167    _define_targets()
168
169    rustdoc_for_lib_test(
170        name = "rustdoc_for_lib_test",
171        target_under_test = ":lib_doc",
172    )
173
174    rustdoc_for_bin_test(
175        name = "rustdoc_for_bin_test",
176        target_under_test = ":bin_doc",
177    )
178
179    rustdoc_for_proc_macro_test(
180        name = "rustdoc_for_proc_macro_test",
181        target_under_test = ":rustdoc_proc_macro_doc",
182    )
183
184    rustdoc_for_lib_with_proc_macro_test(
185        name = "rustdoc_for_lib_with_proc_macro_test",
186        target_under_test = ":lib_with_proc_macro_doc",
187    )
188
189    rustdoc_for_bin_with_transitive_proc_macro_test(
190        name = "rustdoc_for_bin_with_transitive_proc_macro_test",
191        target_under_test = ":bin_with_transitive_proc_macro_doc",
192    )
193
194    rustdoc_for_lib_with_cc_lib_test(
195        name = "rustdoc_for_lib_with_cc_lib_test",
196        target_under_test = ":lib_with_cc_doc",
197    )
198
199    native.test_suite(
200        name = name,
201        tests = [
202            ":rustdoc_for_lib_test",
203            ":rustdoc_for_bin_test",
204            ":rustdoc_for_proc_macro_test",
205            ":rustdoc_for_lib_with_proc_macro_test",
206            ":rustdoc_for_lib_with_cc_lib_test",
207        ],
208    )