• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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
25func 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 = {
84        "soong_module_name": attr.string(mandatory = True),
85        "soong_module_variant": attr.string(),
86        "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
87        "arch_paths": attr.string_list(),
88        "arch_paths_exclude": attr.string_list(),
89        # bazel_module start
90#         "label": attr.string(),
91#         "bp2build_available": attr.bool(),
92        # bazel_module end
93        "bool_prop": attr.bool(),
94        "bool_ptr_prop": attr.bool(),
95        "embedded_prop": attr.string(),
96        "int64_ptr_prop": attr.int(),
97        # nested_props start
98#         "nested_prop": attr.string(),
99        # nested_props end
100        # nested_props_ptr start
101#         "nested_prop": attr.string(),
102        # nested_props_ptr end
103        "one_to_many_prop": attr.bool(),
104        "other_embedded_prop": attr.string(),
105        "string_list_prop": attr.string_list(),
106        "string_prop": attr.string(),
107        "string_ptr_prop": attr.string(),
108    },
109)
110
111def _custom_defaults_impl(ctx):
112    return [SoongModuleInfo()]
113
114custom_defaults = rule(
115    implementation = _custom_defaults_impl,
116    attrs = {
117        "soong_module_name": attr.string(mandatory = True),
118        "soong_module_variant": attr.string(),
119        "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
120        "arch_paths": attr.string_list(),
121        "arch_paths_exclude": attr.string_list(),
122        "bool_prop": attr.bool(),
123        "bool_ptr_prop": attr.bool(),
124        "embedded_prop": attr.string(),
125        "int64_ptr_prop": attr.int(),
126        # nested_props start
127#         "nested_prop": attr.string(),
128        # nested_props end
129        # nested_props_ptr start
130#         "nested_prop": attr.string(),
131        # nested_props_ptr end
132        "one_to_many_prop": attr.bool(),
133        "other_embedded_prop": attr.string(),
134        "string_list_prop": attr.string_list(),
135        "string_prop": attr.string(),
136        "string_ptr_prop": attr.string(),
137    },
138)
139
140def _custom_test__impl(ctx):
141    return [SoongModuleInfo()]
142
143custom_test_ = rule(
144    implementation = _custom_test__impl,
145    attrs = {
146        "soong_module_name": attr.string(mandatory = True),
147        "soong_module_variant": attr.string(),
148        "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
149        "arch_paths": attr.string_list(),
150        "arch_paths_exclude": attr.string_list(),
151        "bool_prop": attr.bool(),
152        "bool_ptr_prop": attr.bool(),
153        "embedded_prop": attr.string(),
154        "int64_ptr_prop": attr.int(),
155        # nested_props start
156#         "nested_prop": attr.string(),
157        # nested_props end
158        # nested_props_ptr start
159#         "nested_prop": attr.string(),
160        # nested_props_ptr end
161        "one_to_many_prop": attr.bool(),
162        "other_embedded_prop": attr.string(),
163        "string_list_prop": attr.string_list(),
164        "string_prop": attr.string(),
165        "string_ptr_prop": attr.string(),
166        # test_prop start
167#         "test_string_prop": attr.string(),
168        # test_prop end
169    },
170)
171`
172
173	if ruleShim.content != expectedBzl {
174		t.Errorf(
175			"Expected the generated rule shim bzl to be:\n%s\nbut got:\n%s",
176			expectedBzl,
177			ruleShim.content)
178	}
179}
180
181func TestGenerateSoongModuleBzl(t *testing.T) {
182	ruleShims := map[string]RuleShim{
183		"file1": RuleShim{
184			rules:   []string{"a", "b"},
185			content: "irrelevant",
186		},
187		"file2": RuleShim{
188			rules:   []string{"c", "d"},
189			content: "irrelevant",
190		},
191	}
192	files := CreateBazelFiles(ruleShims, make(map[string]BazelTargets), QueryView)
193
194	var actualSoongModuleBzl BazelFile
195	for _, f := range files {
196		if f.Basename == "soong_module.bzl" {
197			actualSoongModuleBzl = f
198		}
199	}
200
201	expectedLoad := `load("//build/bazel/queryview_rules:file1.bzl", "a", "b")
202load("//build/bazel/queryview_rules:file2.bzl", "c", "d")
203`
204	expectedRuleMap := `soong_module_rule_map = {
205    "a": a,
206    "b": b,
207    "c": c,
208    "d": d,
209}`
210	if !strings.Contains(actualSoongModuleBzl.Contents, expectedLoad) {
211		t.Errorf(
212			"Generated soong_module.bzl:\n\n%s\n\n"+
213				"Could not find the load statement in the generated soong_module.bzl:\n%s",
214			actualSoongModuleBzl.Contents,
215			expectedLoad)
216	}
217
218	if !strings.Contains(actualSoongModuleBzl.Contents, expectedRuleMap) {
219		t.Errorf(
220			"Generated soong_module.bzl:\n\n%s\n\n"+
221				"Could not find the module -> rule map in the generated soong_module.bzl:\n%s",
222			actualSoongModuleBzl.Contents,
223			expectedRuleMap)
224	}
225}
226