blob: f2a405854ffb066364633a1b39ab24465d10872a [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
25var buildDir string
26
27func setUp() {
28 var err error
29 buildDir, err = ioutil.TempDir("", "bazel_queryview_test")
30 if err != nil {
31 panic(err)
32 }
33}
34
35func tearDown() {
36 os.RemoveAll(buildDir)
37}
38
39func 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
50func 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
80def _custom_impl(ctx):
81 return [SoongModuleInfo()]
82
83custom = rule(
84 implementation = _custom_impl,
85 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -050086 "soong_module_name": attr.string(mandatory = True),
87 "soong_module_variant": attr.string(),
88 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Liz Kammer2dd9ca42020-11-25 16:06:39 -080089 "bool_prop": attr.bool(),
90 "bool_ptr_prop": attr.bool(),
91 "int64_ptr_prop": attr.int(),
92 # nested_props start
93# "nested_prop": attr.string(),
94 # nested_props end
95 # nested_props_ptr start
96# "nested_prop": attr.string(),
97 # nested_props_ptr end
98 "string_list_prop": attr.string_list(),
99 "string_prop": attr.string(),
100 "string_ptr_prop": attr.string(),
101 },
102)
103
104def _custom_defaults_impl(ctx):
105 return [SoongModuleInfo()]
106
107custom_defaults = rule(
108 implementation = _custom_defaults_impl,
109 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500110 "soong_module_name": attr.string(mandatory = True),
111 "soong_module_variant": attr.string(),
112 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800113 "bool_prop": attr.bool(),
114 "bool_ptr_prop": attr.bool(),
115 "int64_ptr_prop": attr.int(),
116 # nested_props start
117# "nested_prop": attr.string(),
118 # nested_props end
119 # nested_props_ptr start
120# "nested_prop": attr.string(),
121 # nested_props_ptr end
122 "string_list_prop": attr.string_list(),
123 "string_prop": attr.string(),
124 "string_ptr_prop": attr.string(),
125 },
126)
127
128def _custom_test__impl(ctx):
129 return [SoongModuleInfo()]
130
131custom_test_ = rule(
132 implementation = _custom_test__impl,
133 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500134 "soong_module_name": attr.string(mandatory = True),
135 "soong_module_variant": attr.string(),
136 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800137 "bool_prop": attr.bool(),
138 "bool_ptr_prop": attr.bool(),
139 "int64_ptr_prop": attr.int(),
140 # nested_props start
141# "nested_prop": attr.string(),
142 # nested_props end
143 # nested_props_ptr start
144# "nested_prop": attr.string(),
145 # nested_props_ptr end
146 "string_list_prop": attr.string_list(),
147 "string_prop": attr.string(),
148 "string_ptr_prop": attr.string(),
149 # test_prop start
150# "test_string_prop": attr.string(),
151 # test_prop end
152 },
153)
154`
155
156 if ruleShim.content != expectedBzl {
157 t.Errorf(
158 "Expected the generated rule shim bzl to be:\n%s\nbut got:\n%s",
159 expectedBzl,
160 ruleShim.content)
161 }
162}
163
164func TestGenerateSoongModuleBzl(t *testing.T) {
165 ruleShims := map[string]RuleShim{
166 "file1": RuleShim{
167 rules: []string{"a", "b"},
168 content: "irrelevant",
169 },
170 "file2": RuleShim{
171 rules: []string{"c", "d"},
172 content: "irrelevant",
173 },
174 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500175 files := CreateBazelFiles(ruleShims, make(map[string]BazelTargets), QueryView)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800176
177 var actualSoongModuleBzl BazelFile
178 for _, f := range files {
179 if f.Basename == "soong_module.bzl" {
180 actualSoongModuleBzl = f
181 }
182 }
183
184 expectedLoad := `load("//build/bazel/queryview_rules:file1.bzl", "a", "b")
185load("//build/bazel/queryview_rules:file2.bzl", "c", "d")
186`
187 expectedRuleMap := `soong_module_rule_map = {
188 "a": a,
189 "b": b,
190 "c": c,
191 "d": d,
192}`
193 if !strings.Contains(actualSoongModuleBzl.Contents, expectedLoad) {
194 t.Errorf(
195 "Generated soong_module.bzl:\n\n%s\n\n"+
196 "Could not find the load statement in the generated soong_module.bzl:\n%s",
197 actualSoongModuleBzl.Contents,
198 expectedLoad)
199 }
200
201 if !strings.Contains(actualSoongModuleBzl.Contents, expectedRuleMap) {
202 t.Errorf(
203 "Generated soong_module.bzl:\n\n%s\n\n"+
204 "Could not find the module -> rule map in the generated soong_module.bzl:\n%s",
205 actualSoongModuleBzl.Contents,
206 expectedRuleMap)
207 }
208}