Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1 | // Copyright 2020 Google Inc. 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 | package bp2build |
| 16 | |
| 17 | import ( |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 18 | "io/ioutil" |
| 19 | "os" |
| 20 | "strings" |
| 21 | "testing" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 22 | |
| 23 | "android/soong/android" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 26 | func setUp() { |
| 27 | var err error |
| 28 | buildDir, err = ioutil.TempDir("", "bazel_queryview_test") |
| 29 | if err != nil { |
| 30 | panic(err) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func tearDown() { |
| 35 | os.RemoveAll(buildDir) |
| 36 | } |
| 37 | |
| 38 | func TestMain(m *testing.M) { |
| 39 | run := func() int { |
| 40 | setUp() |
| 41 | defer tearDown() |
| 42 | |
| 43 | return m.Run() |
| 44 | } |
| 45 | |
| 46 | os.Exit(run()) |
| 47 | } |
| 48 | |
| 49 | func TestGenerateModuleRuleShims(t *testing.T) { |
| 50 | moduleTypeFactories := map[string]android.ModuleFactory{ |
| 51 | "custom": customModuleFactoryBase, |
| 52 | "custom_test": customTestModuleFactoryBase, |
| 53 | "custom_defaults": customDefaultsModuleFactoryBasic, |
| 54 | } |
| 55 | ruleShims := CreateRuleShims(moduleTypeFactories) |
| 56 | |
| 57 | if len(ruleShims) != 1 { |
| 58 | t.Errorf("Expected to generate 1 rule shim, but got %d", len(ruleShims)) |
| 59 | } |
| 60 | |
| 61 | ruleShim := ruleShims["bp2build"] |
| 62 | expectedRules := []string{ |
| 63 | "custom", |
| 64 | "custom_defaults", |
| 65 | "custom_test_", |
| 66 | } |
| 67 | |
| 68 | if len(ruleShim.rules) != len(expectedRules) { |
| 69 | t.Errorf("Expected %d rules, but got %d", len(expectedRules), len(ruleShim.rules)) |
| 70 | } |
| 71 | |
| 72 | for i, rule := range ruleShim.rules { |
| 73 | if rule != expectedRules[i] { |
| 74 | t.Errorf("Expected rule shim to contain %s, but got %s", expectedRules[i], rule) |
| 75 | } |
| 76 | } |
| 77 | expectedBzl := `load("//build/bazel/queryview_rules:providers.bzl", "SoongModuleInfo") |
| 78 | |
| 79 | def _custom_impl(ctx): |
| 80 | return [SoongModuleInfo()] |
| 81 | |
| 82 | custom = rule( |
| 83 | implementation = _custom_impl, |
| 84 | attrs = { |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 85 | "soong_module_name": attr.string(mandatory = True), |
| 86 | "soong_module_variant": attr.string(), |
| 87 | "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]), |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 88 | "api": attr.string(), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 89 | "arch_paths": attr.string_list(), |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 90 | "arch_paths_exclude": attr.string_list(), |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 91 | # bazel_module start |
| 92 | # "label": attr.string(), |
| 93 | # "bp2build_available": attr.bool(), |
| 94 | # bazel_module end |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 95 | "bool_prop": attr.bool(), |
| 96 | "bool_ptr_prop": attr.bool(), |
Spandan Das | 3131d67 | 2023-08-03 22:33:47 +0000 | [diff] [blame] | 97 | "dir": attr.string(), |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 98 | "does_not_convert_to_bazel": attr.bool(), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 99 | "embedded_prop": attr.string(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 100 | "int64_ptr_prop": attr.int(), |
| 101 | # nested_props start |
| 102 | # "nested_prop": attr.string(), |
| 103 | # nested_props end |
| 104 | # nested_props_ptr start |
| 105 | # "nested_prop": attr.string(), |
| 106 | # nested_props_ptr end |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 107 | "one_to_many_prop": attr.bool(), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 108 | "other_embedded_prop": attr.string(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 109 | "string_list_prop": attr.string_list(), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 110 | "string_literal_prop": attr.string(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 111 | "string_prop": attr.string(), |
| 112 | "string_ptr_prop": attr.string(), |
Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 113 | "test_config_setting": attr.bool(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 114 | }, |
| 115 | ) |
| 116 | |
| 117 | def _custom_defaults_impl(ctx): |
| 118 | return [SoongModuleInfo()] |
| 119 | |
| 120 | custom_defaults = rule( |
| 121 | implementation = _custom_defaults_impl, |
| 122 | attrs = { |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 123 | "soong_module_name": attr.string(mandatory = True), |
| 124 | "soong_module_variant": attr.string(), |
| 125 | "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]), |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 126 | "api": attr.string(), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 127 | "arch_paths": attr.string_list(), |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 128 | "arch_paths_exclude": attr.string_list(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 129 | "bool_prop": attr.bool(), |
| 130 | "bool_ptr_prop": attr.bool(), |
Spandan Das | 3131d67 | 2023-08-03 22:33:47 +0000 | [diff] [blame] | 131 | "dir": attr.string(), |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 132 | "does_not_convert_to_bazel": attr.bool(), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 133 | "embedded_prop": attr.string(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 134 | "int64_ptr_prop": attr.int(), |
| 135 | # nested_props start |
| 136 | # "nested_prop": attr.string(), |
| 137 | # nested_props end |
| 138 | # nested_props_ptr start |
| 139 | # "nested_prop": attr.string(), |
| 140 | # nested_props_ptr end |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 141 | "one_to_many_prop": attr.bool(), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 142 | "other_embedded_prop": attr.string(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 143 | "string_list_prop": attr.string_list(), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 144 | "string_literal_prop": attr.string(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 145 | "string_prop": attr.string(), |
| 146 | "string_ptr_prop": attr.string(), |
Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 147 | "test_config_setting": attr.bool(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 148 | }, |
| 149 | ) |
| 150 | |
| 151 | def _custom_test__impl(ctx): |
| 152 | return [SoongModuleInfo()] |
| 153 | |
| 154 | custom_test_ = rule( |
| 155 | implementation = _custom_test__impl, |
| 156 | attrs = { |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 157 | "soong_module_name": attr.string(mandatory = True), |
| 158 | "soong_module_variant": attr.string(), |
| 159 | "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]), |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 160 | "api": attr.string(), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 161 | "arch_paths": attr.string_list(), |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 162 | "arch_paths_exclude": attr.string_list(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 163 | "bool_prop": attr.bool(), |
| 164 | "bool_ptr_prop": attr.bool(), |
Spandan Das | 3131d67 | 2023-08-03 22:33:47 +0000 | [diff] [blame] | 165 | "dir": attr.string(), |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 166 | "does_not_convert_to_bazel": attr.bool(), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 167 | "embedded_prop": attr.string(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 168 | "int64_ptr_prop": attr.int(), |
| 169 | # nested_props start |
| 170 | # "nested_prop": attr.string(), |
| 171 | # nested_props end |
| 172 | # nested_props_ptr start |
| 173 | # "nested_prop": attr.string(), |
| 174 | # nested_props_ptr end |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 175 | "one_to_many_prop": attr.bool(), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 176 | "other_embedded_prop": attr.string(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 177 | "string_list_prop": attr.string_list(), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 178 | "string_literal_prop": attr.string(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 179 | "string_prop": attr.string(), |
| 180 | "string_ptr_prop": attr.string(), |
Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 181 | "test_config_setting": attr.bool(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 182 | # test_prop start |
| 183 | # "test_string_prop": attr.string(), |
| 184 | # test_prop end |
| 185 | }, |
| 186 | ) |
| 187 | ` |
| 188 | |
| 189 | if ruleShim.content != expectedBzl { |
| 190 | t.Errorf( |
| 191 | "Expected the generated rule shim bzl to be:\n%s\nbut got:\n%s", |
| 192 | expectedBzl, |
| 193 | ruleShim.content) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func TestGenerateSoongModuleBzl(t *testing.T) { |
| 198 | ruleShims := map[string]RuleShim{ |
| 199 | "file1": RuleShim{ |
| 200 | rules: []string{"a", "b"}, |
| 201 | content: "irrelevant", |
| 202 | }, |
| 203 | "file2": RuleShim{ |
| 204 | rules: []string{"c", "d"}, |
| 205 | content: "irrelevant", |
| 206 | }, |
| 207 | } |
usta | 40caf95 | 2023-08-04 16:52:14 -0400 | [diff] [blame] | 208 | files := CreateBazelFiles(ruleShims, make(map[string]BazelTargets), QueryView) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 209 | |
| 210 | var actualSoongModuleBzl BazelFile |
| 211 | for _, f := range files { |
| 212 | if f.Basename == "soong_module.bzl" { |
| 213 | actualSoongModuleBzl = f |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | expectedLoad := `load("//build/bazel/queryview_rules:file1.bzl", "a", "b") |
| 218 | load("//build/bazel/queryview_rules:file2.bzl", "c", "d") |
| 219 | ` |
| 220 | expectedRuleMap := `soong_module_rule_map = { |
| 221 | "a": a, |
| 222 | "b": b, |
| 223 | "c": c, |
| 224 | "d": d, |
| 225 | }` |
| 226 | if !strings.Contains(actualSoongModuleBzl.Contents, expectedLoad) { |
| 227 | t.Errorf( |
| 228 | "Generated soong_module.bzl:\n\n%s\n\n"+ |
| 229 | "Could not find the load statement in the generated soong_module.bzl:\n%s", |
| 230 | actualSoongModuleBzl.Contents, |
| 231 | expectedLoad) |
| 232 | } |
| 233 | |
| 234 | if !strings.Contains(actualSoongModuleBzl.Contents, expectedRuleMap) { |
| 235 | t.Errorf( |
| 236 | "Generated soong_module.bzl:\n\n%s\n\n"+ |
| 237 | "Could not find the module -> rule map in the generated soong_module.bzl:\n%s", |
| 238 | actualSoongModuleBzl.Contents, |
| 239 | expectedRuleMap) |
| 240 | } |
| 241 | } |