xref: /aosp_15_r20/build/bazel/rules/linker_config_test.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2022 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
16load("//build/bazel/rules:linker_config.bzl", "linker_config")
17load("//build/bazel/rules:prebuilt_file.bzl", "PrebuiltFileInfo")
18
19SRC = "foo.json"
20OUT_EXP = "foo.pb"
21
22def _test_linker_config_actions_impl(ctx):
23    env = analysistest.begin(ctx)
24    actions = analysistest.target_actions(env)
25    asserts.equals(env, 1, len(actions), "expected  1 action got {}".format(actions))
26
27    in_file = actions[0].inputs.to_list()[0]
28    out_files = actions[0].outputs.to_list()
29    asserts.equals(env, 1, len(out_files), "expected 1 out file  got {}".format(out_files))
30
31    asserts.equals(
32        env,
33        SRC,
34        in_file.basename,
35        "expected source file {} got {}".format(SRC, in_file.basename),
36    )
37    asserts.equals(
38        env,
39        OUT_EXP,
40        out_files[0].basename,
41        "expected out file {} got {}".format(OUT_EXP, out_files[0].basename),
42    )
43
44    # gets build target we are testing for
45    target_under_test = analysistest.target_under_test(env)
46    prebuilt_file_info = target_under_test[PrebuiltFileInfo]
47    asserts.equals(
48        env,
49        "linker.config.pb",
50        prebuilt_file_info.filename,
51        "expected PrebuiltFileInfo filename to be {} but  got {}".format("linkerconfig.pb", prebuilt_file_info.filename),
52    )
53    asserts.equals(
54        env,
55        "etc",
56        prebuilt_file_info.dir,
57        "expected PrebuiltFileInfo dir to be {} but  got {}".format("etc", prebuilt_file_info.dir),
58    )
59    asserts.equals(
60        env,
61        out_files[0],
62        prebuilt_file_info.src,
63        "expected PrebuiltFileInfo src to be {} but got {}".format(out_files[0], prebuilt_file_info.src),
64    )
65
66    return analysistest.end(env)
67
68linker_config_actions_test = analysistest.make(_test_linker_config_actions_impl)
69
70def _test_linker_config_actions():
71    name = "linker_config_actions"
72    test_name = name + "_test"
73
74    linker_config(
75        name = name,
76        src = SRC,
77        tags = ["manual"],
78    )
79
80    linker_config_actions_test(
81        name = test_name,
82        target_under_test = name,
83    )
84    return test_name
85
86def _test_linker_config_commands_impl(ctx):
87    env = analysistest.begin(ctx)
88    actions = analysistest.target_actions(env)
89
90    in_files = actions[0].inputs.to_list()
91    asserts.true(env, len(in_files) > 0, "expected at least 1 input file got {}".format(in_files))
92
93    args = actions[0].argv
94    asserts.equals(env, 6, len(args), "expected 4 args got {}".format(args))
95    asserts.equals(env, "proto", args[1])
96    asserts.equals(env, "-s", args[2])
97    asserts.equals(env, "-o", args[4])
98
99    return analysistest.end(env)
100
101linker_config_commands_test = analysistest.make(_test_linker_config_commands_impl)
102
103def _test_linker_config_commands():
104    name = "linker_config_commands"
105    test_name = name + "_test"
106    linker_config(
107        name = name,
108        src = SRC,
109        tags = ["manual"],
110    )
111
112    linker_config_commands_test(
113        name = test_name,
114        target_under_test = name,
115    )
116
117    return test_name
118
119def linker_config_test_suite(name):
120    native.test_suite(
121        name = name,
122        tests = [
123            _test_linker_config_actions(),
124            _test_linker_config_commands(),
125        ],
126    )
127