blob: 204c5198666ebd3cf013bd78b4d22b5d360552c3 [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(),
Jingwen Chen77e8b7b2021-02-05 03:03:24 -050088 # bazel_module start
89# "label": attr.string(),
90# "bp2build_available": attr.bool(),
91 # bazel_module end
Liz Kammer2dd9ca42020-11-25 16:06:39 -080092 "bool_prop": attr.bool(),
93 "bool_ptr_prop": attr.bool(),
94 "int64_ptr_prop": attr.int(),
95 # nested_props start
96# "nested_prop": attr.string(),
97 # nested_props end
98 # nested_props_ptr start
99# "nested_prop": attr.string(),
100 # nested_props_ptr end
101 "string_list_prop": attr.string_list(),
102 "string_prop": attr.string(),
103 "string_ptr_prop": attr.string(),
104 },
105)
106
107def _custom_defaults_impl(ctx):
108 return [SoongModuleInfo()]
109
110custom_defaults = rule(
111 implementation = _custom_defaults_impl,
112 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500113 "soong_module_name": attr.string(mandatory = True),
114 "soong_module_variant": attr.string(),
115 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400116 "arch_paths": attr.string_list(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800117 "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
132def _custom_test__impl(ctx):
133 return [SoongModuleInfo()]
134
135custom_test_ = rule(
136 implementation = _custom_test__impl,
137 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500138 "soong_module_name": attr.string(mandatory = True),
139 "soong_module_variant": attr.string(),
140 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400141 "arch_paths": attr.string_list(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800142 "bool_prop": attr.bool(),
143 "bool_ptr_prop": attr.bool(),
144 "int64_ptr_prop": attr.int(),
145 # nested_props start
146# "nested_prop": attr.string(),
147 # nested_props end
148 # nested_props_ptr start
149# "nested_prop": attr.string(),
150 # nested_props_ptr end
151 "string_list_prop": attr.string_list(),
152 "string_prop": attr.string(),
153 "string_ptr_prop": attr.string(),
154 # test_prop start
155# "test_string_prop": attr.string(),
156 # test_prop end
157 },
158)
159`
160
161 if ruleShim.content != expectedBzl {
162 t.Errorf(
163 "Expected the generated rule shim bzl to be:\n%s\nbut got:\n%s",
164 expectedBzl,
165 ruleShim.content)
166 }
167}
168
169func TestGenerateSoongModuleBzl(t *testing.T) {
170 ruleShims := map[string]RuleShim{
171 "file1": RuleShim{
172 rules: []string{"a", "b"},
173 content: "irrelevant",
174 },
175 "file2": RuleShim{
176 rules: []string{"c", "d"},
177 content: "irrelevant",
178 },
179 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500180 files := CreateBazelFiles(ruleShims, make(map[string]BazelTargets), QueryView)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800181
182 var actualSoongModuleBzl BazelFile
183 for _, f := range files {
184 if f.Basename == "soong_module.bzl" {
185 actualSoongModuleBzl = f
186 }
187 }
188
189 expectedLoad := `load("//build/bazel/queryview_rules:file1.bzl", "a", "b")
190load("//build/bazel/queryview_rules:file2.bzl", "c", "d")
191`
192 expectedRuleMap := `soong_module_rule_map = {
193 "a": a,
194 "b": b,
195 "c": c,
196 "d": d,
197}`
198 if !strings.Contains(actualSoongModuleBzl.Contents, expectedLoad) {
199 t.Errorf(
200 "Generated soong_module.bzl:\n\n%s\n\n"+
201 "Could not find the load statement in the generated soong_module.bzl:\n%s",
202 actualSoongModuleBzl.Contents,
203 expectedLoad)
204 }
205
206 if !strings.Contains(actualSoongModuleBzl.Contents, expectedRuleMap) {
207 t.Errorf(
208 "Generated soong_module.bzl:\n\n%s\n\n"+
209 "Could not find the module -> rule map in the generated soong_module.bzl:\n%s",
210 actualSoongModuleBzl.Contents,
211 expectedRuleMap)
212 }
213}