xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/unit/check_runfiles/check_runfiles_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_binary",
7    "rust_library",
8    "rust_shared_library",
9    "rust_static_library",
10)
11
12def _check_runfiles_test_impl(ctx):
13    env = analysistest.begin(ctx)
14    tut = analysistest.target_under_test(env)
15    runfiles = tut[DefaultInfo].default_runfiles.files.to_list()
16
17    asserts.true(env, _is_in_runfiles("libbar.so", runfiles))
18
19    return analysistest.end(env)
20
21def _is_in_runfiles(name, runfiles):
22    for file in runfiles:
23        if file.basename == name:
24            return True
25    return False
26
27check_runfiles_test = analysistest.make(_check_runfiles_test_impl)
28
29def _check_runfiles_test():
30    rust_library(
31        name = "foo_lib",
32        srcs = ["foo.rs"],
33        edition = "2018",
34        deps = [":libbar.so"],
35    )
36
37    rust_binary(
38        name = "foo_bin",
39        srcs = ["foo_main.rs"],
40        edition = "2018",
41        deps = [":libbar.so"],
42    )
43
44    rust_shared_library(
45        name = "foo_dylib",
46        srcs = ["foo.rs"],
47        edition = "2018",
48        deps = [":libbar.so"],
49    )
50
51    rust_static_library(
52        name = "foo_static",
53        srcs = ["foo.rs"],
54        edition = "2018",
55        deps = [":libbar.so"],
56    )
57
58    # buildifier: disable=native-cc
59    native.cc_binary(
60        name = "libbar.so",
61        srcs = ["bar.cc"],
62        linkshared = True,
63    )
64
65    check_runfiles_test(
66        name = "check_runfiles_lib_test",
67        target_under_test = ":foo_lib",
68    )
69
70    check_runfiles_test(
71        name = "check_runfiles_bin_test",
72        target_under_test = ":foo_bin",
73    )
74
75    check_runfiles_test(
76        name = "check_runfiles_dylib_test",
77        target_under_test = ":foo_dylib",
78    )
79
80    check_runfiles_test(
81        name = "check_runfiles_static_test",
82        target_under_test = ":foo_static",
83    )
84
85def check_runfiles_test_suite(name):
86    """Entry-point macro called from the BUILD file.
87
88    Args:
89        name: Name of the macro.
90    """
91    _check_runfiles_test()
92
93    native.test_suite(
94        name = name,
95        tests = [
96            ":check_runfiles_lib_test",
97            ":check_runfiles_bin_test",
98            ":check_runfiles_dylib_test",
99            ":check_runfiles_static_test",
100        ],
101    )
102