xref: /aosp_15_r20/external/bazelbuild-rules_cc/tests/rule_based_toolchain/args/args_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    "env_entry",
19    "env_set",
20    "flag_group",
21    "flag_set",
22)
23load(
24    "//cc/toolchains:cc_toolchain_info.bzl",
25    "ActionTypeInfo",
26    "ArgsInfo",
27    "ArgsListInfo",
28)
29load(
30    "//cc/toolchains/impl:legacy_converter.bzl",
31    "convert_args",
32)
33load("//tests/rule_based_toolchain:subjects.bzl", "subjects")
34
35visibility("private")
36
37_SIMPLE_FILES = [
38    "tests/rule_based_toolchain/testdata/file1",
39    "tests/rule_based_toolchain/testdata/multiple1",
40    "tests/rule_based_toolchain/testdata/multiple2",
41]
42
43_CONVERTED_ARGS = subjects.struct(
44    flag_sets = subjects.collection,
45    env_sets = subjects.collection,
46)
47
48def _simple_test(env, targets):
49    simple = env.expect.that_target(targets.simple).provider(ArgsInfo)
50    simple.actions().contains_exactly([
51        targets.c_compile.label,
52        targets.cpp_compile.label,
53    ])
54    simple.env().contains_exactly({"BAR": "bar"})
55    simple.files().contains_exactly(_SIMPLE_FILES)
56
57    c_compile = env.expect.that_target(targets.simple).provider(ArgsListInfo).by_action().get(
58        targets.c_compile[ActionTypeInfo],
59    )
60    c_compile.args().contains_exactly([targets.simple[ArgsInfo]])
61    c_compile.files().contains_exactly(_SIMPLE_FILES)
62
63    converted = env.expect.that_value(
64        convert_args(targets.simple[ArgsInfo]),
65        factory = _CONVERTED_ARGS,
66    )
67    converted.env_sets().contains_exactly([env_set(
68        actions = ["c_compile", "cpp_compile"],
69        env_entries = [env_entry(key = "BAR", value = "bar")],
70    )])
71
72    converted.flag_sets().contains_exactly([flag_set(
73        actions = ["c_compile", "cpp_compile"],
74        flag_groups = [flag_group(flags = ["--foo", "foo"])],
75    )])
76
77def _env_only_test(env, targets):
78    env_only = env.expect.that_target(targets.env_only).provider(ArgsInfo)
79    env_only.actions().contains_exactly([
80        targets.c_compile.label,
81        targets.cpp_compile.label,
82    ])
83    env_only.env().contains_exactly({"BAR": "bar"})
84    env_only.files().contains_exactly(_SIMPLE_FILES)
85
86    c_compile = env.expect.that_target(targets.simple).provider(ArgsListInfo).by_action().get(
87        targets.c_compile[ActionTypeInfo],
88    )
89    c_compile.files().contains_exactly(_SIMPLE_FILES)
90
91    converted = env.expect.that_value(
92        convert_args(targets.env_only[ArgsInfo]),
93        factory = _CONVERTED_ARGS,
94    )
95    converted.env_sets().contains_exactly([env_set(
96        actions = ["c_compile", "cpp_compile"],
97        env_entries = [env_entry(key = "BAR", value = "bar")],
98    )])
99
100    converted.flag_sets().contains_exactly([])
101
102TARGETS = [
103    ":simple",
104    ":env_only",
105    "//tests/rule_based_toolchain/actions:c_compile",
106    "//tests/rule_based_toolchain/actions:cpp_compile",
107]
108
109# @unsorted-dict-items
110TESTS = {
111    "simple_test": _simple_test,
112    "env_only_test_test": _env_only_test,
113}
114