fork/rules_rust

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

Brian Silvermanrustdoc_test: substitute the root of the current crate (#1777)4e89d52a

rust-analyzer
6.7 KiB254 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("//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_prefix_not",
11)
12
13def _common_rustdoc_checks(env, tut):
14    actions = tut.actions
15    action = actions[0]
16    assert_action_mnemonic(env, action, "Rustdoc")
17
18    # These flags, while required for `Rustc` actions, should be omitted for
19    # `Rustdoc` actions
20    assert_argv_contains_prefix_not(env, action, "--remap-path-prefix")
21    assert_argv_contains_prefix_not(env, action, "--emit")
22
23def _rustdoc_for_lib_test_impl(ctx):
24    env = analysistest.begin(ctx)
25    tut = analysistest.target_under_test(env)
26
27    _common_rustdoc_checks(env, tut)
28
29    return analysistest.end(env)
30
31def _rustdoc_for_bin_test_impl(ctx):
32    env = analysistest.begin(ctx)
33    tut = analysistest.target_under_test(env)
34
35    _common_rustdoc_checks(env, tut)
36
37    return analysistest.end(env)
38
39def _rustdoc_for_proc_macro_test_impl(ctx):
40    env = analysistest.begin(ctx)
41    tut = analysistest.target_under_test(env)
42
43    _common_rustdoc_checks(env, tut)
44
45    return analysistest.end(env)
46
47def _rustdoc_for_lib_with_proc_macro_test_impl(ctx):
48    env = analysistest.begin(ctx)
49    tut = analysistest.target_under_test(env)
50
51    _common_rustdoc_checks(env, tut)
52
53    return analysistest.end(env)
54
55def _rustdoc_for_bin_with_transitive_proc_macro_test_impl(ctx):
56    env = analysistest.begin(ctx)
57    tut = analysistest.target_under_test(env)
58
59    _common_rustdoc_checks(env, tut)
60
61    return analysistest.end(env)
62
63def _rustdoc_for_lib_with_cc_lib_test_impl(ctx):
64    env = analysistest.begin(ctx)
65    tut = analysistest.target_under_test(env)
66
67    _common_rustdoc_checks(env, tut)
68
69    return analysistest.end(env)
70
71rustdoc_for_lib_test = analysistest.make(_rustdoc_for_lib_test_impl)
72rustdoc_for_bin_test = analysistest.make(_rustdoc_for_bin_test_impl)
73rustdoc_for_proc_macro_test = analysistest.make(_rustdoc_for_proc_macro_test_impl)
74rustdoc_for_lib_with_proc_macro_test = analysistest.make(_rustdoc_for_lib_with_proc_macro_test_impl)
75rustdoc_for_bin_with_transitive_proc_macro_test = analysistest.make(_rustdoc_for_bin_with_transitive_proc_macro_test_impl)
76rustdoc_for_lib_with_cc_lib_test = analysistest.make(_rustdoc_for_lib_with_cc_lib_test_impl)
77
78def _target_maker(rule_fn, name, rustdoc_deps = [], **kwargs):
79    rule_fn(
80        name = name,
81        edition = "2018",
82        **kwargs
83    )
84
85    rust_test(
86        name = "{}_test".format(name),
87        crate = ":{}".format(name),
88        edition = "2018",
89    )
90
91    rust_doc(
92        name = "{}_doc".format(name),
93        crate = ":{}".format(name),
94    )
95
96    rust_doc_test(
97        name = "{}_doctest".format(name),
98        crate = ":{}".format(name),
99        deps = rustdoc_deps,
100    )
101
102def _define_targets():
103    rust_library(
104        name = "adder",
105        srcs = ["adder.rs"],
106        edition = "2018",
107    )
108
109    _target_maker(
110        rust_binary,
111        name = "bin",
112        srcs = ["rustdoc_bin.rs"],
113    )
114
115    _target_maker(
116        rust_library,
117        name = "lib",
118        srcs = ["rustdoc_lib.rs"],
119        rustdoc_deps = [":adder"],
120    )
121
122    _target_maker(
123        rust_library,
124        name = "nodep_lib",
125        srcs = ["rustdoc_nodep_lib.rs"],
126    )
127
128    _target_maker(
129        rust_proc_macro,
130        name = "rustdoc_proc_macro",
131        srcs = ["rustdoc_proc_macro.rs"],
132    )
133
134    _target_maker(
135        rust_library,
136        name = "lib_with_proc_macro",
137        srcs = ["rustdoc_lib.rs"],
138        rustdoc_deps = [":adder"],
139        proc_macro_deps = [":rustdoc_proc_macro"],
140        crate_features = ["with_proc_macro"],
141    )
142
143    _target_maker(
144        rust_library,
145        name = "lib_nodep_with_proc_macro",
146        srcs = ["rustdoc_nodep_lib.rs"],
147        proc_macro_deps = [":rustdoc_proc_macro"],
148        crate_features = ["with_proc_macro"],
149    )
150
151    _target_maker(
152        rust_binary,
153        name = "bin_with_transitive_proc_macro",
154        srcs = ["rustdoc_bin.rs"],
155        deps = [":lib_with_proc_macro"],
156        proc_macro_deps = [":rustdoc_proc_macro"],
157        crate_features = ["with_proc_macro"],
158    )
159
160    cc_library(
161        name = "cc_lib",
162        hdrs = ["rustdoc.h"],
163        srcs = ["rustdoc.cc"],
164    )
165
166    _target_maker(
167        rust_library,
168        name = "lib_with_cc",
169        srcs = ["rustdoc_lib.rs"],
170        rustdoc_deps = [":adder"],
171        crate_features = ["with_cc"],
172        deps = [":cc_lib"],
173    )
174
175    _target_maker(
176        rust_library,
177        name = "lib_nodep_with_cc",
178        srcs = ["rustdoc_nodep_lib.rs"],
179        crate_features = ["with_cc"],
180        deps = [":cc_lib"],
181    )
182
183    cargo_build_script(
184        name = "lib_build_script",
185        srcs = ["rustdoc_build.rs"],
186        edition = "2018",
187    )
188
189    _target_maker(
190        rust_library,
191        name = "lib_with_build_script",
192        srcs = ["rustdoc_lib.rs"],
193        rustdoc_deps = [":adder"],
194        crate_features = ["with_build_script"],
195        deps = [":lib_build_script"],
196    )
197
198    _target_maker(
199        rust_library,
200        name = "lib_nodep_with_build_script",
201        srcs = ["rustdoc_nodep_lib.rs"],
202        crate_features = ["with_build_script"],
203        deps = [":lib_build_script"],
204    )
205
206def rustdoc_test_suite(name):
207    """Entry-point macro called from the BUILD file.
208
209    Args:
210        name (str): Name of the macro.
211    """
212
213    _define_targets()
214
215    rustdoc_for_lib_test(
216        name = "rustdoc_for_lib_test",
217        target_under_test = ":lib_doc",
218    )
219
220    rustdoc_for_bin_test(
221        name = "rustdoc_for_bin_test",
222        target_under_test = ":bin_doc",
223    )
224
225    rustdoc_for_proc_macro_test(
226        name = "rustdoc_for_proc_macro_test",
227        target_under_test = ":rustdoc_proc_macro_doc",
228    )
229
230    rustdoc_for_lib_with_proc_macro_test(
231        name = "rustdoc_for_lib_with_proc_macro_test",
232        target_under_test = ":lib_with_proc_macro_doc",
233    )
234
235    rustdoc_for_bin_with_transitive_proc_macro_test(
236        name = "rustdoc_for_bin_with_transitive_proc_macro_test",
237        target_under_test = ":bin_with_transitive_proc_macro_doc",
238    )
239
240    rustdoc_for_lib_with_cc_lib_test(
241        name = "rustdoc_for_lib_with_cc_lib_test",
242        target_under_test = ":lib_with_cc_doc",
243    )
244
245    native.test_suite(
246        name = name,
247        tests = [
248            ":rustdoc_for_lib_test",
249            ":rustdoc_for_bin_test",
250            ":rustdoc_for_proc_macro_test",
251            ":rustdoc_for_lib_with_proc_macro_test",
252            ":rustdoc_for_lib_with_cc_lib_test",
253        ],
254    )