1# Copyright 2021 Google LLC 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# https://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 16# Run test product configuration and verify its result. 17# The main configuration file is product.rbc. 18# It inherits part1.rbc and also includes include1.rbc 19# TODO(asmundak): more tests are needed to verify that: 20# * multi-level inheritance works as expected 21# * all runtime functions (wildcard, regex, etc.) work 22 23load("//build/make/core:product_config.rbc", "rblf") 24load(":input_variables.rbc", input_variables_init = "init") 25load(":product.rbc", "init") 26load(":board.rbc", board_init = "init") 27load(":board_input_vars.rbc", board_input_vars_init = "init") 28load("//build/make/tests/single_value_inheritance:test.rbc", test_single_value_inheritance = "test") 29load("//build/make/tests/single_value_inheritance_2:test.rbc", test_single_value_inheritance_2 = "test") 30load("//build/make/tests/artifact_path_requirements:test.rbc", test_artifact_path_requirements = "test") 31load("//build/make/tests/prefixed_sort_order:test.rbc", test_prefixed_sort_order = "test") 32load("//build/make/tests/inherits_in_regular_variables:test.rbc", test_inherits_in_regular_variables = "test") 33 34def assert_eq(expected, actual): 35 if expected != actual: 36 fail("Expected '%s', got '%s'" % (expected, actual)) 37 38def assert_dict_subset(expected, actual): 39 for key, val in expected.items(): 40 assert_eq(val, actual[key]) 41 42# Unit tests for non-trivial runtime functions 43assert_eq(["a", "b", "c"], rblf.mksort("b a c c")) 44assert_eq(["a", "b", "c"], rblf.mksort(["b", "a", "c", "c"])) 45 46assert_eq("", rblf.mkstrip(" \n \t ")) 47assert_eq("a b c", rblf.mkstrip(" a b \n c \t")) 48assert_eq("1", rblf.mkstrip("1 ")) 49 50assert_eq(["a", "b"], rblf.words("a b")) 51assert_eq(["a", "b", "c"], rblf.words(["a b", "c"])) 52# 1-tuple like we use in product variables 53assert_eq(["a b", ("c",)], rblf.words(["a b", ("c",)])) 54 55assert_eq("b1 b2", rblf.mksubst("a", "b", "a1 a2")) 56assert_eq(["b1", "x2"], rblf.mksubst("a", "b", ["a1", "x2"])) 57 58assert_eq("ABcdYZ", rblf.mkpatsubst("ab%yz", "AB%YZ", "abcdyz")) 59assert_eq("bcz", rblf.mkpatsubst("a%z", "A%Z", "bcz")) 60assert_eq(["Ay", "Az"], rblf.mkpatsubst("a%", "A%", ["ay", "az"])) 61assert_eq("AcZ bcz", rblf.mkpatsubst("a%z", "A%Z", "acz bcz")) 62assert_eq("Abcd", rblf.mkpatsubst("a%", "A%", "abcd")) 63assert_eq("abcZ", rblf.mkpatsubst("%z", "%Z", "abcz")) 64assert_eq("azx b", rblf.mkpatsubst("az", "AZ", "azx b")) 65assert_eq(["azx", "b"], rblf.mkpatsubst("az", "AZ", ["azx", "b"])) 66assert_eq("ABC", rblf.mkpatsubst("abc", "ABC", "abc")) 67assert_eq(["%/foo"], rblf.mkpatsubst("%", "\\%/%", ["foo"])) 68assert_eq(["foo/%"], rblf.mkpatsubst("%", "%/%", ["foo"])) 69assert_eq(["from/a:to/a", "from/b:to/b"], rblf.product_copy_files_by_pattern("from/%", "to/%", "a b")) 70 71assert_eq([], rblf.filter(["a", "", "b"], "f")) 72assert_eq(["ab%c", "axyzb%c"], rblf.filter(["a%b%c"], ["ab%c", "axyzb%c", "axyzb%cd", "axyzbwc"])) 73assert_eq(["abc", "bcd"], rblf.filter(["a%", "b%"], ["abc", "def", "bcd", "xabc"])) 74assert_eq(["b", "ab"], rblf.filter_out(["a", "" ], ["a", "", "b", "ab"])) 75assert_eq(["c"], rblf.filter_out(["a", "b" ], ["a", "b", "c"])) 76assert_eq(["c"], rblf.filter_out(["a%", "b" ], ["abc", "b", "c"])) 77 78assert_eq("foo.c no_folder", rblf.notdir(["src/foo.c", "no_folder"])) 79assert_eq("foo.c no_folder", rblf.notdir("src/foo.c no_folder")) 80assert_eq("", rblf.notdir("/")) 81assert_eq("", rblf.notdir("")) 82 83cwd = rblf_shell('pwd') 84assert_eq(cwd+"/foo/bar", rblf.abspath("foo/bar")) 85assert_eq(cwd+"/bar", rblf.abspath("foo/.././bar")) 86assert_eq(cwd+"/bar", rblf.abspath("foo/..////bar//")) 87assert_eq("/foo/baz", rblf.abspath("/foo/bar/../baz")) 88assert_eq(cwd+"/foo/bar "+cwd+"/foo/baz", rblf.abspath("foo/bar foo/baz")) 89assert_eq("/baz", rblf.abspath("/../../../../../../../../../../../../../../../../baz")) 90 91assert_eq("foo", rblf.first_word("foo bar")) 92assert_eq("foo", rblf.first_word(["foo", "bar"])) 93assert_eq("", rblf.first_word("")) 94assert_eq("", rblf.first_word([])) 95assert_eq("bar", rblf.last_word("foo bar")) 96assert_eq("bar", rblf.last_word(["foo", "bar"])) 97assert_eq("", rblf.last_word("")) 98assert_eq("", rblf.last_word([])) 99 100assert_eq(["foo", "bar"], rblf.flatten_2d_list([["foo", "bar"]])) 101assert_eq(["foo", "bar"], rblf.flatten_2d_list([["foo"], ["bar"]])) 102assert_eq([], rblf.flatten_2d_list([])) 103 104assert_eq( 105 ["build/make/tests/board.rbc", "build/make/tests/board_input_vars.rbc"], 106 rblf.expand_wildcard("build/make/tests/board*.rbc") 107) 108assert_eq( 109 ["build/make/tests/run.rbc", "build/make/tests/product.rbc"], 110 rblf.expand_wildcard("build/make/tests/run.rbc build/make/tests/product.rbc") 111) 112assert_eq( 113 ["build/make/tests/run.rbc"], 114 rblf.expand_wildcard("build/make/tests/run.rbc build/make/tests/nonexistent.rbc") 115) 116 117(globals, globals_base) = rblf.product_configuration("test/device", init, input_variables_init) 118assert_dict_subset({ 119 "PRODUCTS.test/device.mk.PRODUCT_COPY_FILES": [ 120 "part_from:part_to", 121 "device_from:device_to", 122 "device/google/redfin/audio/audio_platform_info_noextcodec_snd.xml:||VENDOR-PATH-PH||/etc/audio/audio_platform_info_noextcodec_snd.xml", 123 "xyz:/etc/xyz", 124 "x.xml:/etc/x.xml", 125 "y.xml:/etc/y.xml", 126 "from/sub/x:to/x", 127 "from/sub/y:to/y", 128 ], 129 "PRODUCTS.test/device.mk.PRODUCT_HOST_PACKAGES": ["host"], 130 "PRODUCTS.test/device.mk.PRODUCT_PACKAGES": [ 131 "dev", 132 "inc", 133 "dev_after", 134 "board1_in", 135 "board1_is", 136 ], 137 "PRODUCTS.test/device.mk.PRODUCT_PRODUCT_PROPERTIES": ["part_properties"] 138}, globals) 139 140ns = globals["$SOONG_CONFIG_NAMESPACES"] 141assert_eq( 142 { 143 "NS1": { 144 "v1": "abc abc_part1", 145 "v2": "def" 146 }, 147 "NS2": { 148 "v3": "xyz", 149 "v4": "xyz" 150 } 151 }, 152 {k:v for k, v in sorted(ns.items()) } 153) 154 155assert_eq("Tiramisu", globals["PLATFORM_VERSION"]) 156assert_eq("31", globals["PLATFORM_SDK_VERSION"]) 157 158assert_eq("xyz", rblf.soong_config_get(globals, "NS2", "v3")) 159assert_eq(None, rblf.soong_config_get(globals, "NS2", "nonexistant_var")) 160 161goals = globals["$dist_for_goals"] 162assert_eq( 163 { 164 "goal": [("dir1/file1", "out1"), ("dir1/file2", "out2"), ("dir2/file2", "file2")] 165 }, 166 { k:v for k,v in sorted(goals.items()) } 167) 168 169(board_globals, board_globals_base) = rblf.board_configuration(board_init, board_input_vars_init) 170assert_eq({"A_LIST_VARIABLE": ["foo", "bar"]}, board_globals) 171assert_eq({"A_LIST_VARIABLE": ["foo"]}, board_globals_base) 172 173g = {"FOO": "a", "BAR": "c", "BAZ": "e"} 174cfg = {"FOO": "b", "BAR": "d", "BAZ": "f"} 175rblf.clear_var_list(g, struct(cfg=cfg), "FOO BAR NEWVAR") 176assert_eq("", g["FOO"]) 177assert_eq("", cfg["FOO"]) 178assert_eq("", g["BAR"]) 179assert_eq("", cfg["BAR"]) 180assert_eq("e", g["BAZ"]) 181assert_eq("f", cfg["BAZ"]) 182assert_eq("", g.get("NEWVAR")) 183 184test_single_value_inheritance() 185test_single_value_inheritance_2() 186test_artifact_path_requirements() 187test_prefixed_sort_order() 188test_inherits_in_regular_variables() 189