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