xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/unit/crate_info/crate_info_test.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1"""Unittests for rust rules."""
2
3load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
4load(
5    "//rust:defs.bzl",
6    "rust_common",
7    "rust_library",
8    "rust_proc_macro",
9    "rust_shared_library",
10    "rust_static_library",
11)
12
13def _rule_provides_crate_info_test_impl(ctx):
14    env = analysistest.begin(ctx)
15    tut = analysistest.target_under_test(env)
16    asserts.true(
17        env,
18        rust_common.crate_info in tut,
19        "{} should provide CrateInfo".format(tut.label.name),
20    )
21    return analysistest.end(env)
22
23def _rule_does_not_provide_crate_info_test_impl(ctx):
24    env = analysistest.begin(ctx)
25    tut = analysistest.target_under_test(env)
26    asserts.false(
27        env,
28        rust_common.crate_info in tut,
29        "{} should not provide CrateInfo".format(tut.label.name),
30    )
31    asserts.true(
32        env,
33        rust_common.test_crate_info in tut,
34        "{} should provide a TestCrateInfo".format(tut.label.name),
35    )
36    return analysistest.end(env)
37
38rule_provides_crate_info_test = analysistest.make(_rule_provides_crate_info_test_impl)
39rule_does_not_provide_crate_info_test = analysistest.make(_rule_does_not_provide_crate_info_test_impl)
40
41def _crate_info_test():
42    rust_library(
43        name = "rlib",
44        srcs = ["lib.rs"],
45        edition = "2018",
46    )
47
48    rust_proc_macro(
49        name = "proc_macro",
50        srcs = ["lib.rs"],
51        edition = "2018",
52    )
53
54    rust_static_library(
55        name = "staticlib",
56        srcs = ["lib.rs"],
57        edition = "2018",
58    )
59
60    rust_shared_library(
61        name = "cdylib",
62        srcs = ["lib.rs"],
63        edition = "2018",
64    )
65
66    rule_provides_crate_info_test(
67        name = "rlib_provides_crate_info_test",
68        target_under_test = ":rlib",
69    )
70
71    rule_provides_crate_info_test(
72        name = "proc_macro_provides_crate_info_test",
73        target_under_test = ":proc_macro",
74    )
75
76    rule_does_not_provide_crate_info_test(
77        name = "cdylib_does_not_provide_crate_info_test",
78        target_under_test = ":cdylib",
79    )
80
81    rule_does_not_provide_crate_info_test(
82        name = "staticlib_does_not_provide_crate_info_test",
83        target_under_test = ":staticlib",
84    )
85
86def crate_info_test_suite(name):
87    """Entry-point macro called from the BUILD file.
88
89    Args:
90        name: Name of the macro.
91    """
92    _crate_info_test()
93
94    native.test_suite(
95        name = name,
96        tests = [
97            ":rlib_provides_crate_info_test",
98            ":proc_macro_provides_crate_info_test",
99            ":cdylib_does_not_provide_crate_info_test",
100            ":staticlib_does_not_provide_crate_info_test",
101        ],
102    )
103