blob: e14649e3f0c8bf93b4e70c747df6633a55ae0116 [file] [log] [blame]
Jingwen Chen12b4c272021-03-10 02:05:59 -05001// Copyright 2021 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.
14package android
15
Sam Delmerico24c56032022-03-28 19:53:03 +000016import (
Sam Delmericocc518432022-03-30 15:50:34 +000017 "fmt"
Sam Delmerico24c56032022-03-28 19:53:03 +000018 "testing"
Sam Delmericocc518432022-03-30 15:50:34 +000019
Wei Lid7736ec2022-05-12 23:37:53 -070020 "android/soong/android/allowlists"
21 "android/soong/bazel"
22
Sam Delmericocc518432022-03-30 15:50:34 +000023 "github.com/google/blueprint"
24 "github.com/google/blueprint/proptools"
Sam Delmerico24c56032022-03-28 19:53:03 +000025)
Jingwen Chen12b4c272021-03-10 02:05:59 -050026
27func TestConvertAllModulesInPackage(t *testing.T) {
28 testCases := []struct {
Sam Delmerico24c56032022-03-28 19:53:03 +000029 prefixes allowlists.Bp2BuildConfig
Jingwen Chen12b4c272021-03-10 02:05:59 -050030 packageDir string
31 }{
32 {
Sam Delmerico24c56032022-03-28 19:53:03 +000033 prefixes: allowlists.Bp2BuildConfig{
34 "a": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -050035 },
36 packageDir: "a",
37 },
38 {
Sam Delmerico24c56032022-03-28 19:53:03 +000039 prefixes: allowlists.Bp2BuildConfig{
40 "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -050041 },
42 packageDir: "a/b",
43 },
44 {
Sam Delmerico24c56032022-03-28 19:53:03 +000045 prefixes: allowlists.Bp2BuildConfig{
46 "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
47 "a/b/c": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -050048 },
49 packageDir: "a/b",
50 },
51 {
Sam Delmerico24c56032022-03-28 19:53:03 +000052 prefixes: allowlists.Bp2BuildConfig{
53 "a": allowlists.Bp2BuildDefaultTrueRecursively,
54 "d/e/f": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -050055 },
56 packageDir: "a/b",
57 },
58 {
Sam Delmerico24c56032022-03-28 19:53:03 +000059 prefixes: allowlists.Bp2BuildConfig{
60 "a": allowlists.Bp2BuildDefaultFalse,
61 "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
62 "a/b/c": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -050063 },
64 packageDir: "a/b",
65 },
66 {
Sam Delmerico24c56032022-03-28 19:53:03 +000067 prefixes: allowlists.Bp2BuildConfig{
68 "a": allowlists.Bp2BuildDefaultTrueRecursively,
69 "a/b": allowlists.Bp2BuildDefaultFalse,
70 "a/b/c": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -050071 },
72 packageDir: "a",
73 },
74 }
75
76 for _, test := range testCases {
Sam Delmerico24c56032022-03-28 19:53:03 +000077 if ok, _ := bp2buildDefaultTrueRecursively(test.packageDir, test.prefixes); !ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -050078 t.Errorf("Expected to convert all modules in %s based on %v, but failed.", test.packageDir, test.prefixes)
79 }
80 }
81}
82
83func TestModuleOptIn(t *testing.T) {
84 testCases := []struct {
Sam Delmerico24c56032022-03-28 19:53:03 +000085 prefixes allowlists.Bp2BuildConfig
Jingwen Chen12b4c272021-03-10 02:05:59 -050086 packageDir string
87 }{
88 {
Sam Delmerico24c56032022-03-28 19:53:03 +000089 prefixes: allowlists.Bp2BuildConfig{
90 "a/b": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -050091 },
92 packageDir: "a/b",
93 },
94 {
Sam Delmerico24c56032022-03-28 19:53:03 +000095 prefixes: allowlists.Bp2BuildConfig{
96 "a": allowlists.Bp2BuildDefaultFalse,
97 "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -050098 },
99 packageDir: "a",
100 },
101 {
Sam Delmerico24c56032022-03-28 19:53:03 +0000102 prefixes: allowlists.Bp2BuildConfig{
103 "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500104 },
105 packageDir: "a", // opt-in by default
106 },
107 {
Sam Delmerico24c56032022-03-28 19:53:03 +0000108 prefixes: allowlists.Bp2BuildConfig{
109 "a/b/c": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500110 },
111 packageDir: "a/b",
112 },
113 {
Sam Delmerico24c56032022-03-28 19:53:03 +0000114 prefixes: allowlists.Bp2BuildConfig{
115 "a": allowlists.Bp2BuildDefaultTrueRecursively,
116 "d/e/f": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500117 },
118 packageDir: "foo/bar",
119 },
120 {
Sam Delmerico24c56032022-03-28 19:53:03 +0000121 prefixes: allowlists.Bp2BuildConfig{
122 "a": allowlists.Bp2BuildDefaultTrueRecursively,
123 "a/b": allowlists.Bp2BuildDefaultFalse,
124 "a/b/c": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500125 },
126 packageDir: "a/b",
127 },
128 {
Sam Delmerico24c56032022-03-28 19:53:03 +0000129 prefixes: allowlists.Bp2BuildConfig{
130 "a": allowlists.Bp2BuildDefaultFalse,
131 "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
132 "a/b/c": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500133 },
134 packageDir: "a",
135 },
136 }
137
138 for _, test := range testCases {
Sam Delmerico24c56032022-03-28 19:53:03 +0000139 if ok, _ := bp2buildDefaultTrueRecursively(test.packageDir, test.prefixes); ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500140 t.Errorf("Expected to allow module opt-in in %s based on %v, but failed.", test.packageDir, test.prefixes)
141 }
142 }
143}
Sam Delmericocc518432022-03-30 15:50:34 +0000144
145type TestBazelModule struct {
146 bazel.TestModuleInfo
147 BazelModuleBase
148}
149
150var _ blueprint.Module = TestBazelModule{}
151
152func (m TestBazelModule) Name() string {
153 return m.TestModuleInfo.ModuleName
154}
155
156func (m TestBazelModule) GenerateBuildActions(blueprint.ModuleContext) {
157}
158
159type TestBazelConversionContext struct {
160 omc bazel.OtherModuleTestContext
161 allowlist bp2BuildConversionAllowlist
162 errors []string
163}
164
165var _ bazelOtherModuleContext = &TestBazelConversionContext{}
166
167func (bcc *TestBazelConversionContext) OtherModuleType(m blueprint.Module) string {
168 return bcc.omc.OtherModuleType(m)
169}
170
171func (bcc *TestBazelConversionContext) OtherModuleName(m blueprint.Module) string {
172 return bcc.omc.OtherModuleName(m)
173}
174
175func (bcc *TestBazelConversionContext) OtherModuleDir(m blueprint.Module) string {
176 return bcc.omc.OtherModuleDir(m)
177}
178
179func (bcc *TestBazelConversionContext) ModuleErrorf(format string, args ...interface{}) {
180 bcc.errors = append(bcc.errors, fmt.Sprintf(format, args...))
181}
182
183func (bcc *TestBazelConversionContext) Config() Config {
184 return Config{
185 &config{
186 bp2buildPackageConfig: bcc.allowlist,
187 },
188 }
189}
190
191var bazelableBazelModuleBase = BazelModuleBase{
192 bazelProperties: properties{
193 Bazel_module: bazelModuleProperties{
194 CanConvertToBazel: true,
195 },
196 },
197}
198
199func TestBp2BuildAllowlist(t *testing.T) {
200 testCases := []struct {
201 description string
202 shouldConvert bool
203 expectedErrors []string
204 module TestBazelModule
205 allowlist bp2BuildConversionAllowlist
206 }{
207 {
208 description: "allowlist enables module",
209 shouldConvert: true,
210 module: TestBazelModule{
211 TestModuleInfo: bazel.TestModuleInfo{
212 ModuleName: "foo",
213 Typ: "rule1",
214 Dir: "dir1",
215 },
216 BazelModuleBase: bazelableBazelModuleBase,
217 },
218 allowlist: bp2BuildConversionAllowlist{
219 moduleAlwaysConvert: map[string]bool{
220 "foo": true,
221 },
222 },
223 },
224 {
225 description: "module in name allowlist and type allowlist fails",
226 shouldConvert: false,
227 expectedErrors: []string{"A module cannot be in moduleAlwaysConvert and also be in moduleTypeAlwaysConvert"},
228 module: TestBazelModule{
229 TestModuleInfo: bazel.TestModuleInfo{
230 ModuleName: "foo",
231 Typ: "rule1",
232 Dir: "dir1",
233 },
234 BazelModuleBase: bazelableBazelModuleBase,
235 },
236 allowlist: bp2BuildConversionAllowlist{
237 moduleAlwaysConvert: map[string]bool{
238 "foo": true,
239 },
240 moduleTypeAlwaysConvert: map[string]bool{
241 "rule1": true,
242 },
243 },
244 },
245 {
246 description: "module in allowlist and denylist fails",
247 shouldConvert: false,
248 expectedErrors: []string{"a module cannot be in moduleDoNotConvert and also be in moduleAlwaysConvert"},
249 module: TestBazelModule{
250 TestModuleInfo: bazel.TestModuleInfo{
251 ModuleName: "foo",
252 Typ: "rule1",
253 Dir: "dir1",
254 },
255 BazelModuleBase: bazelableBazelModuleBase,
256 },
257 allowlist: bp2BuildConversionAllowlist{
258 moduleAlwaysConvert: map[string]bool{
259 "foo": true,
260 },
261 moduleDoNotConvert: map[string]bool{
262 "foo": true,
263 },
264 },
265 },
266 {
267 description: "module in allowlist and existing BUILD file",
268 shouldConvert: false,
269 expectedErrors: []string{"A module cannot be in a directory listed in keepExistingBuildFile and also be in moduleAlwaysConvert. Directory: 'existing/build/dir'"},
270 module: TestBazelModule{
271 TestModuleInfo: bazel.TestModuleInfo{
272 ModuleName: "foo",
273 Typ: "rule1",
274 Dir: "existing/build/dir",
275 },
276 BazelModuleBase: bazelableBazelModuleBase,
277 },
278 allowlist: bp2BuildConversionAllowlist{
279 moduleAlwaysConvert: map[string]bool{
280 "foo": true,
281 },
282 keepExistingBuildFile: map[string]bool{
283 "existing/build/dir": true,
284 },
285 },
286 },
287 {
288 description: "module allowlist and enabled directory",
289 shouldConvert: false,
290 expectedErrors: []string{"A module cannot be in a directory marked Bp2BuildDefaultTrue or Bp2BuildDefaultTrueRecursively and also be in moduleAlwaysConvert. Directory: 'existing/build/dir'"},
291 module: TestBazelModule{
292 TestModuleInfo: bazel.TestModuleInfo{
293 ModuleName: "foo",
294 Typ: "rule1",
295 Dir: "existing/build/dir",
296 },
297 BazelModuleBase: bazelableBazelModuleBase,
298 },
299 allowlist: bp2BuildConversionAllowlist{
300 moduleAlwaysConvert: map[string]bool{
301 "foo": true,
302 },
303 defaultConfig: allowlists.Bp2BuildConfig{
304 "existing/build/dir": allowlists.Bp2BuildDefaultTrue,
305 },
306 },
307 },
308 {
309 description: "module allowlist and enabled subdirectory",
310 shouldConvert: false,
311 expectedErrors: []string{"A module cannot be in a directory marked Bp2BuildDefaultTrue or Bp2BuildDefaultTrueRecursively and also be in moduleAlwaysConvert. Directory: 'existing/build/dir'"},
312 module: TestBazelModule{
313 TestModuleInfo: bazel.TestModuleInfo{
314 ModuleName: "foo",
315 Typ: "rule1",
316 Dir: "existing/build/dir/subdir",
317 },
318 BazelModuleBase: bazelableBazelModuleBase,
319 },
320 allowlist: bp2BuildConversionAllowlist{
321 moduleAlwaysConvert: map[string]bool{
322 "foo": true,
323 },
324 defaultConfig: allowlists.Bp2BuildConfig{
325 "existing/build/dir": allowlists.Bp2BuildDefaultTrueRecursively,
326 },
327 },
328 },
329 {
330 description: "module enabled in unit test short-circuits other allowlists",
331 shouldConvert: true,
332 module: TestBazelModule{
333 TestModuleInfo: bazel.TestModuleInfo{
334 ModuleName: "foo",
335 Typ: "rule1",
336 Dir: ".",
337 },
338 BazelModuleBase: BazelModuleBase{
339 bazelProperties: properties{
340 Bazel_module: bazelModuleProperties{
341 CanConvertToBazel: true,
342 Bp2build_available: proptools.BoolPtr(true),
343 },
344 },
345 },
346 },
347 allowlist: bp2BuildConversionAllowlist{
348 moduleAlwaysConvert: map[string]bool{
349 "foo": true,
350 },
351 moduleDoNotConvert: map[string]bool{
352 "foo": true,
353 },
354 },
355 },
356 }
357
358 for _, test := range testCases {
359 t.Run(test.description, func(t *testing.T) {
360 bcc := &TestBazelConversionContext{
361 omc: bazel.OtherModuleTestContext{
362 Modules: []bazel.TestModuleInfo{
363 test.module.TestModuleInfo,
364 },
365 },
366 allowlist: test.allowlist,
367 }
368
369 shouldConvert := test.module.shouldConvertWithBp2build(bcc, test.module.TestModuleInfo)
370 if test.shouldConvert != shouldConvert {
371 t.Errorf("Module shouldConvert expected to be: %v, but was: %v", test.shouldConvert, shouldConvert)
372 }
373
374 errorsMatch := true
375 if len(test.expectedErrors) != len(bcc.errors) {
376 errorsMatch = false
377 } else {
378 for i, err := range test.expectedErrors {
379 if err != bcc.errors[i] {
380 errorsMatch = false
381 }
382 }
383 }
384 if !errorsMatch {
385 t.Errorf("Expected errors to be: %v, but were: %v", test.expectedErrors, bcc.errors)
386 }
387 })
388 }
389}
Wei Lid7736ec2022-05-12 23:37:53 -0700390
391func TestBp2buildAllowList(t *testing.T) {
392 allowlist := getBp2BuildAllowList()
393 for k, v := range allowlists.Bp2buildDefaultConfig {
394 if allowlist.defaultConfig[k] != v {
395 t.Errorf("bp2build default config of %s: expected: %v, got: %v", k, v, allowlist.defaultConfig[k])
396 }
397 }
398 for k, v := range allowlists.Bp2buildKeepExistingBuildFile {
399 if allowlist.keepExistingBuildFile[k] != v {
400 t.Errorf("bp2build keep existing build file of %s: expected: %v, got: %v", k, v, allowlist.keepExistingBuildFile[k])
401 }
402 }
403 for _, k := range allowlists.Bp2buildModuleTypeAlwaysConvertList {
404 if !allowlist.moduleTypeAlwaysConvert[k] {
405 t.Errorf("bp2build module type always convert of %s: expected: true, got: %v", k, allowlist.moduleTypeAlwaysConvert[k])
406 }
407 }
408 for _, k := range allowlists.Bp2buildModuleDoNotConvertList {
409 if !allowlist.moduleDoNotConvert[k] {
410 t.Errorf("bp2build module do not convert of %s: expected: true, got: %v", k, allowlist.moduleDoNotConvert[k])
411 }
412 }
413 for _, k := range allowlists.Bp2buildCcLibraryStaticOnlyList {
414 if !allowlist.ccLibraryStaticOnly[k] {
415 t.Errorf("bp2build cc library static only of %s: expected: true, got: %v", k, allowlist.ccLibraryStaticOnly[k])
416 }
417 }
418 for _, k := range allowlists.MixedBuildsDisabledList {
419 if !allowlist.mixedBuildsDisabled[k] {
420 t.Errorf("bp2build mix build disabled of %s: expected: true, got: %v", k, allowlist.mixedBuildsDisabled[k])
421 }
422 }
423}