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