blob: 1e78c0ecde839249c63ed4cc0fd9bf8abb9cce6f [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001// 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
15package bp2build
16
17import (
18 "android/soong/android"
19 "io/ioutil"
20 "os"
21 "strings"
22 "testing"
23)
24
Liz Kammer2dd9ca42020-11-25 16:06:39 -080025func setUp() {
26 var err error
27 buildDir, err = ioutil.TempDir("", "bazel_queryview_test")
28 if err != nil {
29 panic(err)
30 }
31}
32
33func tearDown() {
34 os.RemoveAll(buildDir)
35}
36
37func 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
48func 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
78def _custom_impl(ctx):
79 return [SoongModuleInfo()]
80
81custom = rule(
82 implementation = _custom_impl,
83 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -050084 "soong_module_name": attr.string(mandatory = True),
85 "soong_module_variant": attr.string(),
86 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Liz Kammer4562a3b2021-04-21 18:15:34 -040087 "arch_paths": attr.string_list(),
Liz Kammer32b77cf2021-08-04 15:17:02 -040088 "arch_paths_exclude": attr.string_list(),
Jingwen Chen77e8b7b2021-02-05 03:03:24 -050089 # bazel_module start
90# "label": attr.string(),
91# "bp2build_available": attr.bool(),
92 # bazel_module end
Liz Kammer2dd9ca42020-11-25 16:06:39 -080093 "bool_prop": attr.bool(),
94 "bool_ptr_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -040095 "embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -080096 "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 Kammer32a03392021-09-14 11:17:21 -0400103 "other_embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800104 "string_list_prop": attr.string_list(),
105 "string_prop": attr.string(),
106 "string_ptr_prop": attr.string(),
107 },
108)
109
110def _custom_defaults_impl(ctx):
111 return [SoongModuleInfo()]
112
113custom_defaults = rule(
114 implementation = _custom_defaults_impl,
115 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500116 "soong_module_name": attr.string(mandatory = True),
117 "soong_module_variant": attr.string(),
118 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400119 "arch_paths": attr.string_list(),
Liz Kammer32b77cf2021-08-04 15:17:02 -0400120 "arch_paths_exclude": attr.string_list(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800121 "bool_prop": attr.bool(),
122 "bool_ptr_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -0400123 "embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800124 "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 Kammer32a03392021-09-14 11:17:21 -0400131 "other_embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800132 "string_list_prop": attr.string_list(),
133 "string_prop": attr.string(),
134 "string_ptr_prop": attr.string(),
135 },
136)
137
138def _custom_test__impl(ctx):
139 return [SoongModuleInfo()]
140
141custom_test_ = rule(
142 implementation = _custom_test__impl,
143 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500144 "soong_module_name": attr.string(mandatory = True),
145 "soong_module_variant": attr.string(),
146 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400147 "arch_paths": attr.string_list(),
Liz Kammer32b77cf2021-08-04 15:17:02 -0400148 "arch_paths_exclude": attr.string_list(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800149 "bool_prop": attr.bool(),
150 "bool_ptr_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -0400151 "embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800152 "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 Kammer32a03392021-09-14 11:17:21 -0400159 "other_embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800160 "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
178func 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 Chen40067de2021-01-26 21:58:43 -0500189 files := CreateBazelFiles(ruleShims, make(map[string]BazelTargets), QueryView)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800190
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")
199load("//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}