blob: fa1bf8af620518bd7b4ee0f1ad942fd3fc0943b6 [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]),
Spandan Das5af0bd32022-09-28 20:43:08 +000088 "api": attr.string(),
Liz Kammer4562a3b2021-04-21 18:15:34 -040089 "arch_paths": attr.string_list(),
Liz Kammer32b77cf2021-08-04 15:17:02 -040090 "arch_paths_exclude": attr.string_list(),
Jingwen Chen77e8b7b2021-02-05 03:03:24 -050091 # bazel_module start
92# "label": attr.string(),
93# "bp2build_available": attr.bool(),
94 # bazel_module end
Liz Kammer2dd9ca42020-11-25 16:06:39 -080095 "bool_prop": attr.bool(),
96 "bool_ptr_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -040097 "embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -080098 "int64_ptr_prop": attr.int(),
99 # nested_props start
100# "nested_prop": attr.string(),
101 # nested_props end
102 # nested_props_ptr start
103# "nested_prop": attr.string(),
104 # nested_props_ptr end
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400105 "one_to_many_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -0400106 "other_embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800107 "string_list_prop": attr.string_list(),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000108 "string_literal_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800109 "string_prop": attr.string(),
110 "string_ptr_prop": attr.string(),
Spandan Das6a448ec2023-04-19 17:36:12 +0000111 "test_config_setting": attr.bool(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800112 },
113)
114
115def _custom_defaults_impl(ctx):
116 return [SoongModuleInfo()]
117
118custom_defaults = rule(
119 implementation = _custom_defaults_impl,
120 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500121 "soong_module_name": attr.string(mandatory = True),
122 "soong_module_variant": attr.string(),
123 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Spandan Das5af0bd32022-09-28 20:43:08 +0000124 "api": attr.string(),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400125 "arch_paths": attr.string_list(),
Liz Kammer32b77cf2021-08-04 15:17:02 -0400126 "arch_paths_exclude": attr.string_list(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800127 "bool_prop": attr.bool(),
128 "bool_ptr_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -0400129 "embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800130 "int64_ptr_prop": attr.int(),
131 # nested_props start
132# "nested_prop": attr.string(),
133 # nested_props end
134 # nested_props_ptr start
135# "nested_prop": attr.string(),
136 # nested_props_ptr end
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400137 "one_to_many_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -0400138 "other_embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800139 "string_list_prop": attr.string_list(),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000140 "string_literal_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800141 "string_prop": attr.string(),
142 "string_ptr_prop": attr.string(),
Spandan Das6a448ec2023-04-19 17:36:12 +0000143 "test_config_setting": attr.bool(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800144 },
145)
146
147def _custom_test__impl(ctx):
148 return [SoongModuleInfo()]
149
150custom_test_ = rule(
151 implementation = _custom_test__impl,
152 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500153 "soong_module_name": attr.string(mandatory = True),
154 "soong_module_variant": attr.string(),
155 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Spandan Das5af0bd32022-09-28 20:43:08 +0000156 "api": attr.string(),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400157 "arch_paths": attr.string_list(),
Liz Kammer32b77cf2021-08-04 15:17:02 -0400158 "arch_paths_exclude": attr.string_list(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800159 "bool_prop": attr.bool(),
160 "bool_ptr_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -0400161 "embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800162 "int64_ptr_prop": attr.int(),
163 # nested_props start
164# "nested_prop": attr.string(),
165 # nested_props end
166 # nested_props_ptr start
167# "nested_prop": attr.string(),
168 # nested_props_ptr end
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400169 "one_to_many_prop": attr.bool(),
Liz Kammer32a03392021-09-14 11:17:21 -0400170 "other_embedded_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800171 "string_list_prop": attr.string_list(),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000172 "string_literal_prop": attr.string(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800173 "string_prop": attr.string(),
174 "string_ptr_prop": attr.string(),
Spandan Das6a448ec2023-04-19 17:36:12 +0000175 "test_config_setting": attr.bool(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800176 # test_prop start
177# "test_string_prop": attr.string(),
178 # test_prop end
179 },
180)
181`
182
183 if ruleShim.content != expectedBzl {
184 t.Errorf(
185 "Expected the generated rule shim bzl to be:\n%s\nbut got:\n%s",
186 expectedBzl,
187 ruleShim.content)
188 }
189}
190
191func TestGenerateSoongModuleBzl(t *testing.T) {
192 ruleShims := map[string]RuleShim{
193 "file1": RuleShim{
194 rules: []string{"a", "b"},
195 content: "irrelevant",
196 },
197 "file2": RuleShim{
198 rules: []string{"c", "d"},
199 content: "irrelevant",
200 },
201 }
Sasha Smundak0fd93e02022-05-19 19:34:31 -0700202 files := CreateBazelFiles(android.NullConfig("out", "out/soong"), ruleShims, make(map[string]BazelTargets), QueryView)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800203
204 var actualSoongModuleBzl BazelFile
205 for _, f := range files {
206 if f.Basename == "soong_module.bzl" {
207 actualSoongModuleBzl = f
208 }
209 }
210
211 expectedLoad := `load("//build/bazel/queryview_rules:file1.bzl", "a", "b")
212load("//build/bazel/queryview_rules:file2.bzl", "c", "d")
213`
214 expectedRuleMap := `soong_module_rule_map = {
215 "a": a,
216 "b": b,
217 "c": c,
218 "d": d,
219}`
220 if !strings.Contains(actualSoongModuleBzl.Contents, expectedLoad) {
221 t.Errorf(
222 "Generated soong_module.bzl:\n\n%s\n\n"+
223 "Could not find the load statement in the generated soong_module.bzl:\n%s",
224 actualSoongModuleBzl.Contents,
225 expectedLoad)
226 }
227
228 if !strings.Contains(actualSoongModuleBzl.Contents, expectedRuleMap) {
229 t.Errorf(
230 "Generated soong_module.bzl:\n\n%s\n\n"+
231 "Could not find the module -> rule map in the generated soong_module.bzl:\n%s",
232 actualSoongModuleBzl.Contents,
233 expectedRuleMap)
234 }
235}