• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TOOL_DIRECTORY = "tests/rule_based_toolchain/testdata"
43
44_CONVERTED_ARGS = subjects.struct(
45    flag_sets = subjects.collection,
46    env_sets = subjects.collection,
47)
48
49def _simple_test(env, targets):
50    simple = env.expect.that_target(targets.simple).provider(ArgsInfo)
51    simple.actions().contains_exactly([
52        targets.c_compile.label,
53        targets.cpp_compile.label,
54    ])
55    simple.env().contains_exactly({"BAR": "bar"})
56    simple.files().contains_exactly(_SIMPLE_FILES)
57
58    c_compile = env.expect.that_target(targets.simple).provider(ArgsListInfo).by_action().get(
59        targets.c_compile[ActionTypeInfo],
60    )
61    c_compile.args().contains_exactly([targets.simple[ArgsInfo]])
62    c_compile.files().contains_exactly(_SIMPLE_FILES)
63
64    converted = env.expect.that_value(
65        convert_args(targets.simple[ArgsInfo]),
66        factory = _CONVERTED_ARGS,
67    )
68    converted.env_sets().contains_exactly([env_set(
69        actions = ["c_compile", "cpp_compile"],
70        env_entries = [env_entry(key = "BAR", value = "bar")],
71    )])
72
73    converted.flag_sets().contains_exactly([flag_set(
74        actions = ["c_compile", "cpp_compile"],
75        flag_groups = [flag_group(flags = ["--foo", "foo"])],
76    )])
77
78def _env_only_test(env, targets):
79    env_only = env.expect.that_target(targets.env_only).provider(ArgsInfo)
80    env_only.actions().contains_exactly([
81        targets.c_compile.label,
82        targets.cpp_compile.label,
83    ])
84    env_only.env().contains_exactly({"BAR": "bar"})
85    env_only.files().contains_exactly(_SIMPLE_FILES)
86
87    c_compile = env.expect.that_target(targets.simple).provider(ArgsListInfo).by_action().get(
88        targets.c_compile[ActionTypeInfo],
89    )
90    c_compile.files().contains_exactly(_SIMPLE_FILES)
91
92    converted = env.expect.that_value(
93        convert_args(targets.env_only[ArgsInfo]),
94        factory = _CONVERTED_ARGS,
95    )
96    converted.env_sets().contains_exactly([env_set(
97        actions = ["c_compile", "cpp_compile"],
98        env_entries = [env_entry(key = "BAR", value = "bar")],
99    )])
100
101    converted.flag_sets().contains_exactly([])
102
103def _with_dir_test(env, targets):
104    with_dir = env.expect.that_target(targets.with_dir).provider(ArgsInfo)
105    with_dir.allowlist_include_directories().contains_exactly([_TOOL_DIRECTORY])
106    with_dir.files().contains_at_least(_SIMPLE_FILES)
107
108    c_compile = env.expect.that_target(targets.with_dir).provider(ArgsListInfo).by_action().get(
109        targets.c_compile[ActionTypeInfo],
110    )
111    c_compile.files().contains_at_least(_SIMPLE_FILES)
112
113TARGETS = [
114    ":simple",
115    ":env_only",
116    ":with_dir",
117    "//tests/rule_based_toolchain/actions:c_compile",
118    "//tests/rule_based_toolchain/actions:cpp_compile",
119]
120
121# @unsorted-dict-items
122TESTS = {
123    "simple_test": _simple_test,
124    "env_only_test": _env_only_test,
125    "with_dir_test": _with_dir_test,
126}
127