blob: 9e0c0a1e93ad8317da2d8abd588fa48374ee23d4 [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(),
95 "int64_ptr_prop": attr.int(),
96 # nested_props start
97# "nested_prop": attr.string(),
98 # nested_props end
99 # nested_props_ptr start
100# "nested_prop": attr.string(),
101 # nested_props_ptr end
102 "string_list_prop": attr.string_list(),
103 "string_prop": attr.string(),
104 "string_ptr_prop": attr.string(),
105 },
106)
107
108def _custom_defaults_impl(ctx):
109 return [SoongModuleInfo()]
110
111custom_defaults = rule(
112 implementation = _custom_defaults_impl,
113 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500114 "soong_module_name": attr.string(mandatory = True),
115 "soong_module_variant": attr.string(),
116 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400117 "arch_paths": attr.string_list(),
Liz Kammer32b77cf2021-08-04 15:17:02 -0400118 "arch_paths_exclude": attr.string_list(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800119 "bool_prop": attr.bool(),
120 "bool_ptr_prop": attr.bool(),
121 "int64_ptr_prop": attr.int(),
122 # nested_props start
123# "nested_prop": attr.string(),
124 # nested_props end
125 # nested_props_ptr start
126# "nested_prop": attr.string(),
127 # nested_props_ptr end
128 "string_list_prop": attr.string_list(),
129 "string_prop": attr.string(),
130 "string_ptr_prop": attr.string(),
131 },
132)
133
134def _custom_test__impl(ctx):
135 return [SoongModuleInfo()]
136
137custom_test_ = rule(
138 implementation = _custom_test__impl,
139 attrs = {
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500140 "soong_module_name": attr.string(mandatory = True),
141 "soong_module_variant": attr.string(),
142 "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400143 "arch_paths": attr.string_list(),
Liz Kammer32b77cf2021-08-04 15:17:02 -0400144 "arch_paths_exclude": attr.string_list(),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800145 "bool_prop": attr.bool(),
146 "bool_ptr_prop": attr.bool(),
147 "int64_ptr_prop": attr.int(),
148 # nested_props start
149# "nested_prop": attr.string(),
150 # nested_props end
151 # nested_props_ptr start
152# "nested_prop": attr.string(),
153 # nested_props_ptr end
154 "string_list_prop": attr.string_list(),
155 "string_prop": attr.string(),
156 "string_ptr_prop": attr.string(),
157 # test_prop start
158# "test_string_prop": attr.string(),
159 # test_prop end
160 },
161)
162`
163
164 if ruleShim.content != expectedBzl {
165 t.Errorf(
166 "Expected the generated rule shim bzl to be:\n%s\nbut got:\n%s",
167 expectedBzl,
168 ruleShim.content)
169 }
170}
171
172func TestGenerateSoongModuleBzl(t *testing.T) {
173 ruleShims := map[string]RuleShim{
174 "file1": RuleShim{
175 rules: []string{"a", "b"},
176 content: "irrelevant",
177 },
178 "file2": RuleShim{
179 rules: []string{"c", "d"},
180 content: "irrelevant",
181 },
182 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500183 files := CreateBazelFiles(ruleShims, make(map[string]BazelTargets), QueryView)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800184
185 var actualSoongModuleBzl BazelFile
186 for _, f := range files {
187 if f.Basename == "soong_module.bzl" {
188 actualSoongModuleBzl = f
189 }
190 }
191
192 expectedLoad := `load("//build/bazel/queryview_rules:file1.bzl", "a", "b")
193load("//build/bazel/queryview_rules:file2.bzl", "c", "d")
194`
195 expectedRuleMap := `soong_module_rule_map = {
196 "a": a,
197 "b": b,
198 "c": c,
199 "d": d,
200}`
201 if !strings.Contains(actualSoongModuleBzl.Contents, expectedLoad) {
202 t.Errorf(
203 "Generated soong_module.bzl:\n\n%s\n\n"+
204 "Could not find the load statement in the generated soong_module.bzl:\n%s",
205 actualSoongModuleBzl.Contents,
206 expectedLoad)
207 }
208
209 if !strings.Contains(actualSoongModuleBzl.Contents, expectedRuleMap) {
210 t.Errorf(
211 "Generated soong_module.bzl:\n\n%s\n\n"+
212 "Could not find the module -> rule map in the generated soong_module.bzl:\n%s",
213 actualSoongModuleBzl.Contents,
214 expectedRuleMap)
215 }
216}