blob: 28d2c759bac43be4265ffce82457a5566fa33c38 [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 (
Liz Kammer2dd9ca42020-11-25 16:06:39 -080018 "io/ioutil"
19 "os"
20 "strings"
21 "testing"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +000022
23 "android/soong/android"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080024)
25
Liz Kammer2dd9ca42020-11-25 16:06:39 -080026func setUp() {
27 var err error
28 buildDir, err = ioutil.TempDir("", "bazel_queryview_test")
29 if err != nil {
30 panic(err)
31 }
32}
33
34func tearDown() {
35 os.RemoveAll(buildDir)
36}
37
38func 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
49func 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
79def _custom_impl(ctx):
80 return [SoongModuleInfo()]
81
82custom = rule(
83 implementation = _custom_impl,
84 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -050085 "soong_module_name": attr.string(mandatory = True),
86 "soong_module_variant": attr.string(),
87 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Liz Kammer4562a3b2021-04-21 18:15:34 -040088 "arch_paths": attr.string_list(),
Liz Kammer32b77cf2021-08-04 15:17:02 -040089 "arch_paths_exclude": attr.string_list(),
Jingwen Chen77e8b7b2021-02-05 03:03:24 -050090 # bazel_module start
91# "label": attr.string(),
92# "bp2build_available": attr.bool(),
93 # bazel_module end
Liz Kammer2dd9ca42020-11-25 16:06:39 -080094 "bool_prop": attr.bool(),
95 "bool_ptr_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -040096 "embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -080097 "int64_ptr_prop": attr.int(),
98 # nested_props start
99# "nested_prop": attr.string(),
100 # nested_props end
101 # nested_props_ptr start
102# "nested_prop": attr.string(),
103 # nested_props_ptr end
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400104 "one_to_many_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -0400105 "other_embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800106 "string_list_prop": attr.string_list(),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000107 "string_literal_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800108 "string_prop": attr.string(),
109 "string_ptr_prop": attr.string(),
110 },
111)
112
113def _custom_defaults_impl(ctx):
114 return [SoongModuleInfo()]
115
116custom_defaults = rule(
117 implementation = _custom_defaults_impl,
118 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500119 "soong_module_name": attr.string(mandatory = True),
120 "soong_module_variant": attr.string(),
121 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400122 "arch_paths": attr.string_list(),
Liz Kammer32b77cf2021-08-04 15:17:02 -0400123 "arch_paths_exclude": attr.string_list(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800124 "bool_prop": attr.bool(),
125 "bool_ptr_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -0400126 "embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800127 "int64_ptr_prop": attr.int(),
128 # nested_props start
129# "nested_prop": attr.string(),
130 # nested_props end
131 # nested_props_ptr start
132# "nested_prop": attr.string(),
133 # nested_props_ptr end
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400134 "one_to_many_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -0400135 "other_embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800136 "string_list_prop": attr.string_list(),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000137 "string_literal_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800138 "string_prop": attr.string(),
139 "string_ptr_prop": attr.string(),
140 },
141)
142
143def _custom_test__impl(ctx):
144 return [SoongModuleInfo()]
145
146custom_test_ = rule(
147 implementation = _custom_test__impl,
148 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500149 "soong_module_name": attr.string(mandatory = True),
150 "soong_module_variant": attr.string(),
151 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400152 "arch_paths": attr.string_list(),
Liz Kammer32b77cf2021-08-04 15:17:02 -0400153 "arch_paths_exclude": attr.string_list(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800154 "bool_prop": attr.bool(),
155 "bool_ptr_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -0400156 "embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800157 "int64_ptr_prop": attr.int(),
158 # nested_props start
159# "nested_prop": attr.string(),
160 # nested_props end
161 # nested_props_ptr start
162# "nested_prop": attr.string(),
163 # nested_props_ptr end
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400164 "one_to_many_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -0400165 "other_embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800166 "string_list_prop": attr.string_list(),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000167 "string_literal_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800168 "string_prop": attr.string(),
169 "string_ptr_prop": attr.string(),
170 # test_prop start
171# "test_string_prop": attr.string(),
172 # test_prop end
173 },
174)
175`
176
177 if ruleShim.content != expectedBzl {
178 t.Errorf(
179 "Expected the generated rule shim bzl to be:\n%s\nbut got:\n%s",
180 expectedBzl,
181 ruleShim.content)
182 }
183}
184
185func TestGenerateSoongModuleBzl(t *testing.T) {
186 ruleShims := map[string]RuleShim{
187 "file1": RuleShim{
188 rules: []string{"a", "b"},
189 content: "irrelevant",
190 },
191 "file2": RuleShim{
192 rules: []string{"c", "d"},
193 content: "irrelevant",
194 },
195 }
Sasha Smundak0fd93e02022-05-19 19:34:31 -0700196 files := CreateBazelFiles(android.NullConfig("out", "out/soong"), ruleShims, make(map[string]BazelTargets), QueryView)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800197
198 var actualSoongModuleBzl BazelFile
199 for _, f := range files {
200 if f.Basename == "soong_module.bzl" {
201 actualSoongModuleBzl = f
202 }
203 }
204
205 expectedLoad := `load("//build/bazel/queryview_rules:file1.bzl", "a", "b")
206load("//build/bazel/queryview_rules:file2.bzl", "c", "d")
207`
208 expectedRuleMap := `soong_module_rule_map = {
209 "a": a,
210 "b": b,
211 "c": c,
212 "d": d,
213}`
214 if !strings.Contains(actualSoongModuleBzl.Contents, expectedLoad) {
215 t.Errorf(
216 "Generated soong_module.bzl:\n\n%s\n\n"+
217 "Could not find the load statement in the generated soong_module.bzl:\n%s",
218 actualSoongModuleBzl.Contents,
219 expectedLoad)
220 }
221
222 if !strings.Contains(actualSoongModuleBzl.Contents, expectedRuleMap) {
223 t.Errorf(
224 "Generated soong_module.bzl:\n\n%s\n\n"+
225 "Could not find the module -> rule map in the generated soong_module.bzl:\n%s",
226 actualSoongModuleBzl.Contents,
227 expectedRuleMap)
228 }
229}