xref: /aosp_15_r20/external/bazelbuild-rules_cc/tests/rule_based_toolchain/tool/tool_test.bzl (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
1# Copyright 2024 The Bazel Authors. All rights reserved.
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"""Tests for the cc_args rule."""
15
16load(
17    "//cc:cc_toolchain_config_lib.bzl",
18    legacy_with_feature_set = "with_feature_set",
19)
20load("//cc/toolchains:cc_toolchain_info.bzl", "ToolInfo")
21load("//cc/toolchains/impl:collect.bzl", _collect_tools = "collect_tools")
22load("//cc/toolchains/impl:legacy_converter.bzl", "convert_tool")
23load("//tests/rule_based_toolchain:subjects.bzl", "result_fn_wrapper", "subjects")
24
25visibility("private")
26
27collect_tools = result_fn_wrapper(_collect_tools)
28collection_result = subjects.result(subjects.collection)
29
30collect_tool = result_fn_wrapper(
31    lambda ctx, target, fail: _collect_tools(ctx, [target], fail = fail)[0],
32)
33tool_result = subjects.result(subjects.ToolInfo)
34
35# Generated by native_binary.
36_BIN_WRAPPER_SYMLINK = "tests/rule_based_toolchain/testdata/bin_wrapper"
37_BIN_WRAPPER = "tests/rule_based_toolchain/testdata/bin_wrapper.sh"
38_BIN = "tests/rule_based_toolchain/testdata/bin"
39
40def _tool_test(env, targets):
41    tool = env.expect.that_target(targets.tool).provider(ToolInfo)
42    tool.exe().short_path_equals(_BIN_WRAPPER)
43    tool.execution_requirements().contains_exactly(["requires-network"])
44    tool.runfiles().contains_exactly([
45        _BIN_WRAPPER,
46        _BIN,
47    ])
48    tool.requires_any_of().contains_exactly([targets.direct_constraint.label])
49
50    legacy = convert_tool(tool.actual)
51    env.expect.that_file(legacy.tool).equals(tool.actual.exe)
52    env.expect.that_collection(legacy.execution_requirements).contains_exactly(["requires-network"])
53    env.expect.that_collection(legacy.with_features).contains_exactly([
54        legacy_with_feature_set(
55            features = ["feature_name"],
56            not_features = ["simple2"],
57        ),
58    ])
59
60def _wrapped_tool_includes_runfiles_test(env, targets):
61    tool = env.expect.that_target(targets.wrapped_tool).provider(ToolInfo)
62    tool.exe().short_path_equals(_BIN_WRAPPER_SYMLINK)
63    tool.runfiles().contains_exactly([
64        _BIN_WRAPPER_SYMLINK,
65        _BIN,
66    ])
67
68def _collect_tools_collects_tools_test(env, targets):
69    env.expect.that_value(
70        value = collect_tools(env.ctx, [targets.tool, targets.wrapped_tool]),
71        factory = collection_result,
72    ).ok().contains_exactly(
73        [targets.tool[ToolInfo], targets.wrapped_tool[ToolInfo]],
74    ).in_order()
75
76def _collect_tools_collects_binaries_test(env, targets):
77    tool_wrapper = env.expect.that_value(
78        value = collect_tool(env.ctx, targets.bin_wrapper),
79        factory = tool_result,
80    ).ok()
81    tool_wrapper.label().equals(targets.bin_wrapper.label)
82    tool_wrapper.exe().short_path_equals(_BIN_WRAPPER_SYMLINK)
83    tool_wrapper.runfiles().contains_exactly([
84        _BIN_WRAPPER_SYMLINK,
85        _BIN,
86    ])
87
88def _collect_tools_collects_single_files_test(env, targets):
89    bin = env.expect.that_value(
90        value = collect_tool(env.ctx, targets.bin_filegroup),
91        factory = tool_result,
92        expr = "bin_filegroup",
93    ).ok()
94    bin.label().equals(targets.bin_filegroup.label)
95    bin.exe().short_path_equals(_BIN)
96    bin.runfiles().contains_exactly([_BIN])
97
98def _collect_tools_fails_on_non_binary_test(env, targets):
99    env.expect.that_value(
100        value = collect_tools(env.ctx, [targets.multiple]),
101        factory = collection_result,
102        expr = "multiple_non_binary",
103    ).err()
104
105TARGETS = [
106    "//tests/rule_based_toolchain/features:direct_constraint",
107    "//tests/rule_based_toolchain/tool:tool",
108    "//tests/rule_based_toolchain/tool:wrapped_tool",
109    "//tests/rule_based_toolchain/testdata:bin_wrapper",
110    "//tests/rule_based_toolchain/testdata:multiple",
111    "//tests/rule_based_toolchain/testdata:bin_filegroup",
112]
113
114# @unsorted-dict-items
115TESTS = {
116    "tool_test": _tool_test,
117    "wrapped_tool_includes_runfiles_test": _wrapped_tool_includes_runfiles_test,
118    "collect_tools_collects_tools_test": _collect_tools_collects_tools_test,
119    "collect_tools_collects_binaries_test": _collect_tools_collects_binaries_test,
120    "collect_tools_collects_single_files_test": _collect_tools_collects_single_files_test,
121    "collect_tools_fails_on_non_binary_test": _collect_tools_fails_on_non_binary_test,
122}
123