blob: 30c1a5b6a9b70cc691bdfe5da7a21774d7789414 [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]),
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 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 Kammer2dd9ca42020-11-25 16:06:39 -0800141 "bool_prop": attr.bool(),
142 "bool_ptr_prop": attr.bool(),
143 "int64_ptr_prop": attr.int(),
144 # nested_props start
145# "nested_prop": attr.string(),
146 # nested_props end
147 # nested_props_ptr start
148# "nested_prop": attr.string(),
149 # nested_props_ptr end
150 "string_list_prop": attr.string_list(),
151 "string_prop": attr.string(),
152 "string_ptr_prop": attr.string(),
153 # test_prop start
154# "test_string_prop": attr.string(),
155 # test_prop end
156 },
157)
158`
159
160 if ruleShim.content != expectedBzl {
161 t.Errorf(
162 "Expected the generated rule shim bzl to be:\n%s\nbut got:\n%s",
163 expectedBzl,
164 ruleShim.content)
165 }
166}
167
168func TestGenerateSoongModuleBzl(t *testing.T) {
169 ruleShims := map[string]RuleShim{
170 "file1": RuleShim{
171 rules: []string{"a", "b"},
172 content: "irrelevant",
173 },
174 "file2": RuleShim{
175 rules: []string{"c", "d"},
176 content: "irrelevant",
177 },
178 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500179 files := CreateBazelFiles(ruleShims, make(map[string]BazelTargets), QueryView)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800180
181 var actualSoongModuleBzl BazelFile
182 for _, f := range files {
183 if f.Basename == "soong_module.bzl" {
184 actualSoongModuleBzl = f
185 }
186 }
187
188 expectedLoad := `load("//build/bazel/queryview_rules:file1.bzl", "a", "b")
189load("//build/bazel/queryview_rules:file2.bzl", "c", "d")
190`
191 expectedRuleMap := `soong_module_rule_map = {
192 "a": a,
193 "b": b,
194 "c": c,
195 "d": d,
196}`
197 if !strings.Contains(actualSoongModuleBzl.Contents, expectedLoad) {
198 t.Errorf(
199 "Generated soong_module.bzl:\n\n%s\n\n"+
200 "Could not find the load statement in the generated soong_module.bzl:\n%s",
201 actualSoongModuleBzl.Contents,
202 expectedLoad)
203 }
204
205 if !strings.Contains(actualSoongModuleBzl.Contents, expectedRuleMap) {
206 t.Errorf(
207 "Generated soong_module.bzl:\n\n%s\n\n"+
208 "Could not find the module -> rule map in the generated soong_module.bzl:\n%s",
209 actualSoongModuleBzl.Contents,
210 expectedRuleMap)
211 }
212}