xref: /aosp_15_r20/external/bazel-skylib/tests/common_settings_tests.bzl (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
1# Copyright 2023 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
15"""Analysis tests for common_settings.bzl."""
16
17load("//lib:unittest.bzl", "analysistest", "asserts")
18load("//rules:common_settings.bzl", "int_flag", "int_setting", "string_flag", "string_setting")
19
20def _template_variable_info_contents_test_impl(ctx):
21    env = analysistest.begin(ctx)
22
23    target_under_test = analysistest.target_under_test(env)
24    if ctx.attr.expected:
25        asserts.equals(
26            env,
27            expected = ctx.attr.expected,
28            actual = target_under_test[platform_common.TemplateVariableInfo].variables,
29        )
30    else:
31        asserts.false(env, platform_common.TemplateVariableInfo in target_under_test)
32
33    return analysistest.end(env)
34
35_template_variable_info_contents_test = analysistest.make(
36    _template_variable_info_contents_test_impl,
37    attrs = {
38        "expected": attr.string_dict(),
39    },
40)
41
42def _test_template_variable_info_contents():
43    int_flag(
44        name = "my_int_flag",
45        build_setting_default = 42,
46        make_variable = "MY_INT_1",
47    )
48
49    _template_variable_info_contents_test(
50        name = "my_int_flag_test",
51        target_under_test = ":my_int_flag",
52        expected = {
53            "MY_INT_1": "42",
54        },
55    )
56
57    int_setting(
58        name = "my_int_setting",
59        build_setting_default = 21,
60        make_variable = "MY_INT_2",
61    )
62
63    _template_variable_info_contents_test(
64        name = "my_int_setting_test",
65        target_under_test = ":my_int_setting",
66        expected = {
67            "MY_INT_2": "21",
68        },
69    )
70
71    string_flag(
72        name = "my_string_flag",
73        build_setting_default = "foo",
74        make_variable = "MY_STRING_1",
75    )
76
77    _template_variable_info_contents_test(
78        name = "my_string_flag_test",
79        target_under_test = ":my_string_flag",
80        expected = {
81            "MY_STRING_1": "foo",
82        },
83    )
84
85    string_setting(
86        name = "my_string_setting",
87        build_setting_default = "bar",
88        make_variable = "MY_STRING_2",
89    )
90
91    _template_variable_info_contents_test(
92        name = "my_string_setting_test",
93        target_under_test = ":my_string_setting",
94        expected = {
95            "MY_STRING_2": "bar",
96        },
97    )
98
99    string_flag(
100        name = "my_string_flag_without_make_variable",
101        build_setting_default = "foo",
102    )
103
104    _template_variable_info_contents_test(
105        name = "my_string_flag_without_make_variable_test",
106        target_under_test = ":my_string_flag_without_make_variable",
107        expected = {},
108    )
109
110def _failure_test_impl(ctx):
111    env = analysistest.begin(ctx)
112
113    asserts.expect_failure(env, ctx.attr.expected_failure)
114
115    return analysistest.end(env)
116
117_failure_test = analysistest.make(
118    _failure_test_impl,
119    attrs = {
120        "expected_failure": attr.string(),
121    },
122    expect_failure = True,
123)
124
125def _test_make_variable_name_failures():
126    int_flag(
127        name = "my_failing_int_flag",
128        build_setting_default = 42,
129        make_variable = "my_int_1",
130        tags = ["manual"],
131    )
132
133    _failure_test(
134        name = "my_failing_int_flag_test",
135        target_under_test = ":my_failing_int_flag",
136        expected_failure = "Error setting //tests:my_failing_int_flag: invalid make variable name 'my_int_1'. Make variable names may only contain uppercase letters, digits, and underscores.",
137    )
138
139    string_flag(
140        name = "my_failing_string_flag",
141        build_setting_default = "foo",
142        make_variable = "MY STRING",
143        tags = ["manual"],
144    )
145
146    _failure_test(
147        name = "my_failing_string_flag_test",
148        target_under_test = ":my_failing_string_flag",
149        expected_failure = "Error setting //tests:my_failing_string_flag: invalid make variable name 'MY STRING'. Make variable names may only contain uppercase letters, digits, and underscores.",
150    )
151
152def common_settings_test_suite(name = "common_settings_test_suite"):
153    _test_template_variable_info_contents()
154    _test_make_variable_name_failures()
155
156    native.test_suite(
157        name = "common_settings_test_suite",
158        tests = [
159            "my_int_flag_test",
160            "my_int_setting_test",
161            "my_string_flag_test",
162            "my_string_setting_test",
163            "my_string_flag_without_make_variable_test",
164            "my_failing_int_flag_test",
165            "my_failing_string_flag_test",
166        ],
167    )
168