1*bcb5dc79SHONG Yifan# Copyright 2023 The Bazel Authors. All rights reserved. 2*bcb5dc79SHONG Yifan# 3*bcb5dc79SHONG Yifan# Licensed under the Apache License, Version 2.0 (the "License"); 4*bcb5dc79SHONG Yifan# you may not use this file except in compliance with the License. 5*bcb5dc79SHONG Yifan# You may obtain a copy of the License at 6*bcb5dc79SHONG Yifan# 7*bcb5dc79SHONG Yifan# http://www.apache.org/licenses/LICENSE-2.0 8*bcb5dc79SHONG Yifan# 9*bcb5dc79SHONG Yifan# Unless required by applicable law or agreed to in writing, software 10*bcb5dc79SHONG Yifan# distributed under the License is distributed on an "AS IS" BASIS, 11*bcb5dc79SHONG Yifan# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*bcb5dc79SHONG Yifan# See the License for the specific language governing permissions and 13*bcb5dc79SHONG Yifan# limitations under the License. 14*bcb5dc79SHONG Yifan 15*bcb5dc79SHONG Yifan"""Analysis tests for common_settings.bzl.""" 16*bcb5dc79SHONG Yifan 17*bcb5dc79SHONG Yifanload("//lib:unittest.bzl", "analysistest", "asserts") 18*bcb5dc79SHONG Yifanload("//rules:common_settings.bzl", "int_flag", "int_setting", "string_flag", "string_setting") 19*bcb5dc79SHONG Yifan 20*bcb5dc79SHONG Yifandef _template_variable_info_contents_test_impl(ctx): 21*bcb5dc79SHONG Yifan env = analysistest.begin(ctx) 22*bcb5dc79SHONG Yifan 23*bcb5dc79SHONG Yifan target_under_test = analysistest.target_under_test(env) 24*bcb5dc79SHONG Yifan if ctx.attr.expected: 25*bcb5dc79SHONG Yifan asserts.equals( 26*bcb5dc79SHONG Yifan env, 27*bcb5dc79SHONG Yifan expected = ctx.attr.expected, 28*bcb5dc79SHONG Yifan actual = target_under_test[platform_common.TemplateVariableInfo].variables, 29*bcb5dc79SHONG Yifan ) 30*bcb5dc79SHONG Yifan else: 31*bcb5dc79SHONG Yifan asserts.false(env, platform_common.TemplateVariableInfo in target_under_test) 32*bcb5dc79SHONG Yifan 33*bcb5dc79SHONG Yifan return analysistest.end(env) 34*bcb5dc79SHONG Yifan 35*bcb5dc79SHONG Yifan_template_variable_info_contents_test = analysistest.make( 36*bcb5dc79SHONG Yifan _template_variable_info_contents_test_impl, 37*bcb5dc79SHONG Yifan attrs = { 38*bcb5dc79SHONG Yifan "expected": attr.string_dict(), 39*bcb5dc79SHONG Yifan }, 40*bcb5dc79SHONG Yifan) 41*bcb5dc79SHONG Yifan 42*bcb5dc79SHONG Yifandef _test_template_variable_info_contents(): 43*bcb5dc79SHONG Yifan int_flag( 44*bcb5dc79SHONG Yifan name = "my_int_flag", 45*bcb5dc79SHONG Yifan build_setting_default = 42, 46*bcb5dc79SHONG Yifan make_variable = "MY_INT_1", 47*bcb5dc79SHONG Yifan ) 48*bcb5dc79SHONG Yifan 49*bcb5dc79SHONG Yifan _template_variable_info_contents_test( 50*bcb5dc79SHONG Yifan name = "my_int_flag_test", 51*bcb5dc79SHONG Yifan target_under_test = ":my_int_flag", 52*bcb5dc79SHONG Yifan expected = { 53*bcb5dc79SHONG Yifan "MY_INT_1": "42", 54*bcb5dc79SHONG Yifan }, 55*bcb5dc79SHONG Yifan ) 56*bcb5dc79SHONG Yifan 57*bcb5dc79SHONG Yifan int_setting( 58*bcb5dc79SHONG Yifan name = "my_int_setting", 59*bcb5dc79SHONG Yifan build_setting_default = 21, 60*bcb5dc79SHONG Yifan make_variable = "MY_INT_2", 61*bcb5dc79SHONG Yifan ) 62*bcb5dc79SHONG Yifan 63*bcb5dc79SHONG Yifan _template_variable_info_contents_test( 64*bcb5dc79SHONG Yifan name = "my_int_setting_test", 65*bcb5dc79SHONG Yifan target_under_test = ":my_int_setting", 66*bcb5dc79SHONG Yifan expected = { 67*bcb5dc79SHONG Yifan "MY_INT_2": "21", 68*bcb5dc79SHONG Yifan }, 69*bcb5dc79SHONG Yifan ) 70*bcb5dc79SHONG Yifan 71*bcb5dc79SHONG Yifan string_flag( 72*bcb5dc79SHONG Yifan name = "my_string_flag", 73*bcb5dc79SHONG Yifan build_setting_default = "foo", 74*bcb5dc79SHONG Yifan make_variable = "MY_STRING_1", 75*bcb5dc79SHONG Yifan ) 76*bcb5dc79SHONG Yifan 77*bcb5dc79SHONG Yifan _template_variable_info_contents_test( 78*bcb5dc79SHONG Yifan name = "my_string_flag_test", 79*bcb5dc79SHONG Yifan target_under_test = ":my_string_flag", 80*bcb5dc79SHONG Yifan expected = { 81*bcb5dc79SHONG Yifan "MY_STRING_1": "foo", 82*bcb5dc79SHONG Yifan }, 83*bcb5dc79SHONG Yifan ) 84*bcb5dc79SHONG Yifan 85*bcb5dc79SHONG Yifan string_setting( 86*bcb5dc79SHONG Yifan name = "my_string_setting", 87*bcb5dc79SHONG Yifan build_setting_default = "bar", 88*bcb5dc79SHONG Yifan make_variable = "MY_STRING_2", 89*bcb5dc79SHONG Yifan ) 90*bcb5dc79SHONG Yifan 91*bcb5dc79SHONG Yifan _template_variable_info_contents_test( 92*bcb5dc79SHONG Yifan name = "my_string_setting_test", 93*bcb5dc79SHONG Yifan target_under_test = ":my_string_setting", 94*bcb5dc79SHONG Yifan expected = { 95*bcb5dc79SHONG Yifan "MY_STRING_2": "bar", 96*bcb5dc79SHONG Yifan }, 97*bcb5dc79SHONG Yifan ) 98*bcb5dc79SHONG Yifan 99*bcb5dc79SHONG Yifan string_flag( 100*bcb5dc79SHONG Yifan name = "my_string_flag_without_make_variable", 101*bcb5dc79SHONG Yifan build_setting_default = "foo", 102*bcb5dc79SHONG Yifan ) 103*bcb5dc79SHONG Yifan 104*bcb5dc79SHONG Yifan _template_variable_info_contents_test( 105*bcb5dc79SHONG Yifan name = "my_string_flag_without_make_variable_test", 106*bcb5dc79SHONG Yifan target_under_test = ":my_string_flag_without_make_variable", 107*bcb5dc79SHONG Yifan expected = {}, 108*bcb5dc79SHONG Yifan ) 109*bcb5dc79SHONG Yifan 110*bcb5dc79SHONG Yifandef _failure_test_impl(ctx): 111*bcb5dc79SHONG Yifan env = analysistest.begin(ctx) 112*bcb5dc79SHONG Yifan 113*bcb5dc79SHONG Yifan asserts.expect_failure(env, ctx.attr.expected_failure) 114*bcb5dc79SHONG Yifan 115*bcb5dc79SHONG Yifan return analysistest.end(env) 116*bcb5dc79SHONG Yifan 117*bcb5dc79SHONG Yifan_failure_test = analysistest.make( 118*bcb5dc79SHONG Yifan _failure_test_impl, 119*bcb5dc79SHONG Yifan attrs = { 120*bcb5dc79SHONG Yifan "expected_failure": attr.string(), 121*bcb5dc79SHONG Yifan }, 122*bcb5dc79SHONG Yifan expect_failure = True, 123*bcb5dc79SHONG Yifan) 124*bcb5dc79SHONG Yifan 125*bcb5dc79SHONG Yifandef _test_make_variable_name_failures(): 126*bcb5dc79SHONG Yifan int_flag( 127*bcb5dc79SHONG Yifan name = "my_failing_int_flag", 128*bcb5dc79SHONG Yifan build_setting_default = 42, 129*bcb5dc79SHONG Yifan make_variable = "my_int_1", 130*bcb5dc79SHONG Yifan tags = ["manual"], 131*bcb5dc79SHONG Yifan ) 132*bcb5dc79SHONG Yifan 133*bcb5dc79SHONG Yifan _failure_test( 134*bcb5dc79SHONG Yifan name = "my_failing_int_flag_test", 135*bcb5dc79SHONG Yifan target_under_test = ":my_failing_int_flag", 136*bcb5dc79SHONG Yifan 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*bcb5dc79SHONG Yifan ) 138*bcb5dc79SHONG Yifan 139*bcb5dc79SHONG Yifan string_flag( 140*bcb5dc79SHONG Yifan name = "my_failing_string_flag", 141*bcb5dc79SHONG Yifan build_setting_default = "foo", 142*bcb5dc79SHONG Yifan make_variable = "MY STRING", 143*bcb5dc79SHONG Yifan tags = ["manual"], 144*bcb5dc79SHONG Yifan ) 145*bcb5dc79SHONG Yifan 146*bcb5dc79SHONG Yifan _failure_test( 147*bcb5dc79SHONG Yifan name = "my_failing_string_flag_test", 148*bcb5dc79SHONG Yifan target_under_test = ":my_failing_string_flag", 149*bcb5dc79SHONG Yifan 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*bcb5dc79SHONG Yifan ) 151*bcb5dc79SHONG Yifan 152*bcb5dc79SHONG Yifandef common_settings_test_suite(name = "common_settings_test_suite"): 153*bcb5dc79SHONG Yifan _test_template_variable_info_contents() 154*bcb5dc79SHONG Yifan _test_make_variable_name_failures() 155*bcb5dc79SHONG Yifan 156*bcb5dc79SHONG Yifan native.test_suite( 157*bcb5dc79SHONG Yifan name = "common_settings_test_suite", 158*bcb5dc79SHONG Yifan tests = [ 159*bcb5dc79SHONG Yifan "my_int_flag_test", 160*bcb5dc79SHONG Yifan "my_int_setting_test", 161*bcb5dc79SHONG Yifan "my_string_flag_test", 162*bcb5dc79SHONG Yifan "my_string_setting_test", 163*bcb5dc79SHONG Yifan "my_string_flag_without_make_variable_test", 164*bcb5dc79SHONG Yifan "my_failing_int_flag_test", 165*bcb5dc79SHONG Yifan "my_failing_string_flag_test", 166*bcb5dc79SHONG Yifan ], 167*bcb5dc79SHONG Yifan ) 168