xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/unit/cdylib_name/cdylib_name_analysis_test.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1"""Analysis tests for the name we assign to cdylib libraries."""
2
3load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
4load("//rust:defs.bzl", "rust_shared_library")
5
6def _cdylib_name_test_impl(ctx):
7    env = analysistest.begin(ctx)
8    target = analysistest.target_under_test(env)
9
10    # We're expecting the `.dylib`/`.so` to be the only file on Unix and Windows to
11    # contain a pair of `.dll` and `.lib` files.
12    files = target[DefaultInfo].files.to_list()
13    if len(files) == 1:
14        asserts.true(env, files[0].extension in ("so", "dylib"))
15        if files[0].extension == "so":
16            asserts.equals(env, files[0].basename, "libsomething.so")
17        elif files[0].extension == "dylib":
18            asserts.equals(env, files[0].basename, "libsomething.dylib")
19    elif len(files) == 2:
20        expected_filenames = ["something.dll", "something.dll.lib"]
21        for file in files:
22            asserts.true(env, file.basename in expected_filenames)
23            expected_filenames.remove(file.basename)
24
25    return analysistest.end(env)
26
27cdylib_name_test = analysistest.make(_cdylib_name_test_impl)
28
29def cdylib_name_analysis_test_suite(name):
30    """Analysis tests for the name we assign to cdylib libraries.
31
32    Args:
33        name: the test suite name
34    """
35    rust_shared_library(
36        name = "something",
37        srcs = ["lib.rs"],
38        edition = "2018",
39    )
40
41    cdylib_name_test(
42        name = "cdylib_name_test",
43        target_under_test = ":something",
44    )
45
46    native.test_suite(
47        name = name,
48        tests = [":cdylib_name_test"],
49    )
50