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 | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 103 | "other_embedded_prop": attr.string(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 104 | "string_list_prop": attr.string_list(), |
| 105 | "string_prop": attr.string(), |
| 106 | "string_ptr_prop": attr.string(), |
| 107 | }, |
| 108 | ) |
| 109 | |
| 110 | def _custom_defaults_impl(ctx): |
| 111 | return [SoongModuleInfo()] |
| 112 | |
| 113 | custom_defaults = rule( |
| 114 | implementation = _custom_defaults_impl, |
| 115 | attrs = { |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 116 | "soong_module_name": attr.string(mandatory = True), |
| 117 | "soong_module_variant": attr.string(), |
| 118 | "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 119 | "arch_paths": attr.string_list(), |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 120 | "arch_paths_exclude": attr.string_list(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 121 | "bool_prop": attr.bool(), |
| 122 | "bool_ptr_prop": attr.bool(), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 123 | "embedded_prop": attr.string(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 124 | "int64_ptr_prop": attr.int(), |
| 125 | # nested_props start |
| 126 | # "nested_prop": attr.string(), |
| 127 | # nested_props end |
| 128 | # nested_props_ptr start |
| 129 | # "nested_prop": attr.string(), |
| 130 | # nested_props_ptr end |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 131 | "other_embedded_prop": attr.string(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 132 | "string_list_prop": attr.string_list(), |
| 133 | "string_prop": attr.string(), |
| 134 | "string_ptr_prop": attr.string(), |
| 135 | }, |
| 136 | ) |
| 137 | |
| 138 | def _custom_test__impl(ctx): |
| 139 | return [SoongModuleInfo()] |
| 140 | |
| 141 | custom_test_ = rule( |
| 142 | implementation = _custom_test__impl, |
| 143 | attrs = { |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 144 | "soong_module_name": attr.string(mandatory = True), |
| 145 | "soong_module_variant": attr.string(), |
| 146 | "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 147 | "arch_paths": attr.string_list(), |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 148 | "arch_paths_exclude": attr.string_list(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 149 | "bool_prop": attr.bool(), |
| 150 | "bool_ptr_prop": attr.bool(), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 151 | "embedded_prop": attr.string(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 152 | "int64_ptr_prop": attr.int(), |
| 153 | # nested_props start |
| 154 | # "nested_prop": attr.string(), |
| 155 | # nested_props end |
| 156 | # nested_props_ptr start |
| 157 | # "nested_prop": attr.string(), |
| 158 | # nested_props_ptr end |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 159 | "other_embedded_prop": attr.string(), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 160 | "string_list_prop": attr.string_list(), |
| 161 | "string_prop": attr.string(), |
| 162 | "string_ptr_prop": attr.string(), |
| 163 | # test_prop start |
| 164 | # "test_string_prop": attr.string(), |
| 165 | # test_prop end |
| 166 | }, |
| 167 | ) |
| 168 | ` |
| 169 | |
| 170 | if ruleShim.content != expectedBzl { |
| 171 | t.Errorf( |
| 172 | "Expected the generated rule shim bzl to be:\n%s\nbut got:\n%s", |
| 173 | expectedBzl, |
| 174 | ruleShim.content) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | func TestGenerateSoongModuleBzl(t *testing.T) { |
| 179 | ruleShims := map[string]RuleShim{ |
| 180 | "file1": RuleShim{ |
| 181 | rules: []string{"a", "b"}, |
| 182 | content: "irrelevant", |
| 183 | }, |
| 184 | "file2": RuleShim{ |
| 185 | rules: []string{"c", "d"}, |
| 186 | content: "irrelevant", |
| 187 | }, |
| 188 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 189 | files := CreateBazelFiles(ruleShims, make(map[string]BazelTargets), QueryView) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 190 | |
| 191 | var actualSoongModuleBzl BazelFile |
| 192 | for _, f := range files { |
| 193 | if f.Basename == "soong_module.bzl" { |
| 194 | actualSoongModuleBzl = f |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | expectedLoad := `load("//build/bazel/queryview_rules:file1.bzl", "a", "b") |
| 199 | load("//build/bazel/queryview_rules:file2.bzl", "c", "d") |
| 200 | ` |
| 201 | expectedRuleMap := `soong_module_rule_map = { |
| 202 | "a": a, |
| 203 | "b": b, |
| 204 | "c": c, |
| 205 | "d": d, |
| 206 | }` |
| 207 | if !strings.Contains(actualSoongModuleBzl.Contents, expectedLoad) { |
| 208 | t.Errorf( |
| 209 | "Generated soong_module.bzl:\n\n%s\n\n"+ |
| 210 | "Could not find the load statement in the generated soong_module.bzl:\n%s", |
| 211 | actualSoongModuleBzl.Contents, |
| 212 | expectedLoad) |
| 213 | } |
| 214 | |
| 215 | if !strings.Contains(actualSoongModuleBzl.Contents, expectedRuleMap) { |
| 216 | t.Errorf( |
| 217 | "Generated soong_module.bzl:\n\n%s\n\n"+ |
| 218 | "Could not find the module -> rule map in the generated soong_module.bzl:\n%s", |
| 219 | actualSoongModuleBzl.Contents, |
| 220 | expectedRuleMap) |
| 221 | } |
| 222 | } |