xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/unit/crate_variants/crate_variants.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1"""Unittests for rust rules."""
2
3load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
4load("//rust:defs.bzl", "rust_common", "rust_library")
5
6def _crate_variants_test_impl(ctx):
7    env = analysistest.begin(ctx)
8    tut = analysistest.target_under_test(env)
9    transitive_crate_outputs = tut[rust_common.dep_info].transitive_crate_outputs.to_list()
10
11    # Both variants of "foo" occur as dependencies.
12    asserts.equals(env, len(transitive_crate_outputs), 2)
13    return analysistest.end(env)
14
15crate_variants_test = analysistest.make(_crate_variants_test_impl)
16
17def _crate_variants_test():
18    rust_library(
19        name = "foo",
20        srcs = ["foo.rs"],
21        edition = "2018",
22    )
23
24    rust_library(
25        name = "foo2",
26        crate_name = "foo",
27        srcs = ["foo.rs"],
28        edition = "2018",
29    )
30
31    rust_library(
32        name = "bar",
33        srcs = ["bar.rs"],
34        edition = "2018",
35        deps = [":foo", ":foo2"],
36    )
37
38    crate_variants_test(
39        name = "crate_variants_test",
40        target_under_test = ":bar",
41    )
42
43def crate_variants_test_suite(name):
44    """Entry-point macro called from the BUILD file.
45
46    Args:
47        name: Name of the macro.
48    """
49    _crate_variants_test()
50
51    native.test_suite(
52        name = name,
53        tests = [
54            ":crate_variants_test",
55        ],
56    )
57