blob: 77e251575c5cdf93c57e3d8371e321f6ed4bf39e [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//
Colin Crossd079e0b2022-08-16 10:27:33 -07007// http://www.apache.org/licenses/LICENSE-2.0
Jingwen Chen12b4c272021-03-10 02:05:59 -05008//
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 },
MarkDacek756b2962022-10-13 17:50:17 +000074 {
75 prefixes: allowlists.Bp2BuildConfig{
76 "a": allowlists.Bp2BuildDefaultFalseRecursively,
77 "a/b": allowlists.Bp2BuildDefaultTrue,
78 },
79 packageDir: "a/b",
80 },
81 {
82 prefixes: allowlists.Bp2BuildConfig{
83 "a": allowlists.Bp2BuildDefaultFalseRecursively,
84 "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
85 },
86 packageDir: "a/b/c",
87 },
Jingwen Chen12b4c272021-03-10 02:05:59 -050088 }
89
90 for _, test := range testCases {
Sam Delmerico24c56032022-03-28 19:53:03 +000091 if ok, _ := bp2buildDefaultTrueRecursively(test.packageDir, test.prefixes); !ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -050092 t.Errorf("Expected to convert all modules in %s based on %v, but failed.", test.packageDir, test.prefixes)
93 }
94 }
95}
96
97func TestModuleOptIn(t *testing.T) {
98 testCases := []struct {
Sam Delmerico24c56032022-03-28 19:53:03 +000099 prefixes allowlists.Bp2BuildConfig
Jingwen Chen12b4c272021-03-10 02:05:59 -0500100 packageDir string
101 }{
102 {
Sam Delmerico24c56032022-03-28 19:53:03 +0000103 prefixes: allowlists.Bp2BuildConfig{
104 "a/b": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500105 },
106 packageDir: "a/b",
107 },
108 {
Sam Delmerico24c56032022-03-28 19:53:03 +0000109 prefixes: allowlists.Bp2BuildConfig{
110 "a": allowlists.Bp2BuildDefaultFalse,
111 "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500112 },
113 packageDir: "a",
114 },
115 {
Sam Delmerico24c56032022-03-28 19:53:03 +0000116 prefixes: allowlists.Bp2BuildConfig{
117 "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500118 },
119 packageDir: "a", // opt-in by default
120 },
121 {
Sam Delmerico24c56032022-03-28 19:53:03 +0000122 prefixes: allowlists.Bp2BuildConfig{
123 "a/b/c": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500124 },
125 packageDir: "a/b",
126 },
127 {
Sam Delmerico24c56032022-03-28 19:53:03 +0000128 prefixes: allowlists.Bp2BuildConfig{
129 "a": allowlists.Bp2BuildDefaultTrueRecursively,
130 "d/e/f": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500131 },
132 packageDir: "foo/bar",
133 },
134 {
Sam Delmerico24c56032022-03-28 19:53:03 +0000135 prefixes: allowlists.Bp2BuildConfig{
136 "a": allowlists.Bp2BuildDefaultTrueRecursively,
137 "a/b": allowlists.Bp2BuildDefaultFalse,
138 "a/b/c": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500139 },
140 packageDir: "a/b",
141 },
142 {
Sam Delmerico24c56032022-03-28 19:53:03 +0000143 prefixes: allowlists.Bp2BuildConfig{
144 "a": allowlists.Bp2BuildDefaultFalse,
145 "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
146 "a/b/c": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500147 },
148 packageDir: "a",
149 },
MarkDacek756b2962022-10-13 17:50:17 +0000150 {
151 prefixes: allowlists.Bp2BuildConfig{
152 "a": allowlists.Bp2BuildDefaultFalseRecursively,
153 "a/b": allowlists.Bp2BuildDefaultTrue,
154 },
155 packageDir: "a/b/c",
156 },
157 {
158 prefixes: allowlists.Bp2BuildConfig{
159 "a": allowlists.Bp2BuildDefaultTrueRecursively,
160 "a/b": allowlists.Bp2BuildDefaultFalseRecursively,
161 },
162 packageDir: "a/b/c",
163 },
Jingwen Chen12b4c272021-03-10 02:05:59 -0500164 }
165
166 for _, test := range testCases {
Sam Delmerico24c56032022-03-28 19:53:03 +0000167 if ok, _ := bp2buildDefaultTrueRecursively(test.packageDir, test.prefixes); ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500168 t.Errorf("Expected to allow module opt-in in %s based on %v, but failed.", test.packageDir, test.prefixes)
169 }
170 }
171}
Sam Delmericocc518432022-03-30 15:50:34 +0000172
173type TestBazelModule struct {
174 bazel.TestModuleInfo
175 BazelModuleBase
176}
177
178var _ blueprint.Module = TestBazelModule{}
179
180func (m TestBazelModule) Name() string {
181 return m.TestModuleInfo.ModuleName
182}
183
184func (m TestBazelModule) GenerateBuildActions(blueprint.ModuleContext) {
185}
186
187type TestBazelConversionContext struct {
188 omc bazel.OtherModuleTestContext
Cole Faust324a92e2022-08-23 15:29:05 -0700189 allowlist Bp2BuildConversionAllowlist
Sam Delmericocc518432022-03-30 15:50:34 +0000190 errors []string
191}
192
193var _ bazelOtherModuleContext = &TestBazelConversionContext{}
194
195func (bcc *TestBazelConversionContext) OtherModuleType(m blueprint.Module) string {
196 return bcc.omc.OtherModuleType(m)
197}
198
199func (bcc *TestBazelConversionContext) OtherModuleName(m blueprint.Module) string {
200 return bcc.omc.OtherModuleName(m)
201}
202
203func (bcc *TestBazelConversionContext) OtherModuleDir(m blueprint.Module) string {
204 return bcc.omc.OtherModuleDir(m)
205}
206
207func (bcc *TestBazelConversionContext) ModuleErrorf(format string, args ...interface{}) {
208 bcc.errors = append(bcc.errors, fmt.Sprintf(format, args...))
209}
210
211func (bcc *TestBazelConversionContext) Config() Config {
212 return Config{
213 &config{
Cole Faust324a92e2022-08-23 15:29:05 -0700214 Bp2buildPackageConfig: bcc.allowlist,
Sam Delmericocc518432022-03-30 15:50:34 +0000215 },
216 }
217}
218
219var bazelableBazelModuleBase = BazelModuleBase{
220 bazelProperties: properties{
Romain Jobredeaux8242b432023-05-04 10:16:26 -0400221 Bazel_module: BazelModuleProperties{
Sam Delmericocc518432022-03-30 15:50:34 +0000222 CanConvertToBazel: true,
223 },
224 },
225}
226
227func TestBp2BuildAllowlist(t *testing.T) {
228 testCases := []struct {
229 description string
230 shouldConvert bool
231 expectedErrors []string
232 module TestBazelModule
Cole Faust324a92e2022-08-23 15:29:05 -0700233 allowlist Bp2BuildConversionAllowlist
Sam Delmericocc518432022-03-30 15:50:34 +0000234 }{
235 {
236 description: "allowlist enables module",
237 shouldConvert: true,
238 module: TestBazelModule{
239 TestModuleInfo: bazel.TestModuleInfo{
240 ModuleName: "foo",
241 Typ: "rule1",
242 Dir: "dir1",
243 },
244 BazelModuleBase: bazelableBazelModuleBase,
245 },
Cole Faust324a92e2022-08-23 15:29:05 -0700246 allowlist: Bp2BuildConversionAllowlist{
Sam Delmericocc518432022-03-30 15:50:34 +0000247 moduleAlwaysConvert: map[string]bool{
248 "foo": true,
249 },
250 },
251 },
252 {
253 description: "module in name allowlist and type allowlist fails",
254 shouldConvert: false,
255 expectedErrors: []string{"A module cannot be in moduleAlwaysConvert and also be in moduleTypeAlwaysConvert"},
256 module: TestBazelModule{
257 TestModuleInfo: bazel.TestModuleInfo{
258 ModuleName: "foo",
259 Typ: "rule1",
260 Dir: "dir1",
261 },
262 BazelModuleBase: bazelableBazelModuleBase,
263 },
Cole Faust324a92e2022-08-23 15:29:05 -0700264 allowlist: Bp2BuildConversionAllowlist{
Sam Delmericocc518432022-03-30 15:50:34 +0000265 moduleAlwaysConvert: map[string]bool{
266 "foo": true,
267 },
268 moduleTypeAlwaysConvert: map[string]bool{
269 "rule1": true,
270 },
271 },
272 },
273 {
274 description: "module in allowlist and denylist fails",
275 shouldConvert: false,
276 expectedErrors: []string{"a module cannot be in moduleDoNotConvert and also be in moduleAlwaysConvert"},
277 module: TestBazelModule{
278 TestModuleInfo: bazel.TestModuleInfo{
279 ModuleName: "foo",
280 Typ: "rule1",
281 Dir: "dir1",
282 },
283 BazelModuleBase: bazelableBazelModuleBase,
284 },
Cole Faust324a92e2022-08-23 15:29:05 -0700285 allowlist: Bp2BuildConversionAllowlist{
Sam Delmericocc518432022-03-30 15:50:34 +0000286 moduleAlwaysConvert: map[string]bool{
287 "foo": true,
288 },
289 moduleDoNotConvert: map[string]bool{
290 "foo": true,
291 },
292 },
293 },
294 {
Sam Delmericocc518432022-03-30 15:50:34 +0000295 description: "module allowlist and enabled directory",
296 shouldConvert: false,
Yu Liu10853f92022-09-14 16:05:22 -0700297 expectedErrors: []string{"A module cannot be in a directory marked Bp2BuildDefaultTrue or Bp2BuildDefaultTrueRecursively and also be in moduleAlwaysConvert. Directory: 'existing/build/dir' Module: 'foo'"},
Sam Delmericocc518432022-03-30 15:50:34 +0000298 module: TestBazelModule{
299 TestModuleInfo: bazel.TestModuleInfo{
300 ModuleName: "foo",
301 Typ: "rule1",
302 Dir: "existing/build/dir",
303 },
304 BazelModuleBase: bazelableBazelModuleBase,
305 },
Cole Faust324a92e2022-08-23 15:29:05 -0700306 allowlist: Bp2BuildConversionAllowlist{
Sam Delmericocc518432022-03-30 15:50:34 +0000307 moduleAlwaysConvert: map[string]bool{
308 "foo": true,
309 },
310 defaultConfig: allowlists.Bp2BuildConfig{
311 "existing/build/dir": allowlists.Bp2BuildDefaultTrue,
312 },
313 },
314 },
315 {
316 description: "module allowlist and enabled subdirectory",
317 shouldConvert: false,
Yu Liu10853f92022-09-14 16:05:22 -0700318 expectedErrors: []string{"A module cannot be in a directory marked Bp2BuildDefaultTrue or Bp2BuildDefaultTrueRecursively and also be in moduleAlwaysConvert. Directory: 'existing/build/dir' Module: 'foo'"},
Sam Delmericocc518432022-03-30 15:50:34 +0000319 module: TestBazelModule{
320 TestModuleInfo: bazel.TestModuleInfo{
321 ModuleName: "foo",
322 Typ: "rule1",
323 Dir: "existing/build/dir/subdir",
324 },
325 BazelModuleBase: bazelableBazelModuleBase,
326 },
Cole Faust324a92e2022-08-23 15:29:05 -0700327 allowlist: Bp2BuildConversionAllowlist{
Sam Delmericocc518432022-03-30 15:50:34 +0000328 moduleAlwaysConvert: map[string]bool{
329 "foo": true,
330 },
331 defaultConfig: allowlists.Bp2BuildConfig{
332 "existing/build/dir": allowlists.Bp2BuildDefaultTrueRecursively,
333 },
334 },
335 },
336 {
337 description: "module enabled in unit test short-circuits other allowlists",
338 shouldConvert: true,
339 module: TestBazelModule{
340 TestModuleInfo: bazel.TestModuleInfo{
341 ModuleName: "foo",
342 Typ: "rule1",
343 Dir: ".",
344 },
345 BazelModuleBase: BazelModuleBase{
346 bazelProperties: properties{
Romain Jobredeaux8242b432023-05-04 10:16:26 -0400347 Bazel_module: BazelModuleProperties{
Sam Delmericocc518432022-03-30 15:50:34 +0000348 CanConvertToBazel: true,
349 Bp2build_available: proptools.BoolPtr(true),
350 },
351 },
352 },
353 },
Cole Faust324a92e2022-08-23 15:29:05 -0700354 allowlist: Bp2BuildConversionAllowlist{
Sam Delmericocc518432022-03-30 15:50:34 +0000355 moduleAlwaysConvert: map[string]bool{
356 "foo": true,
357 },
358 moduleDoNotConvert: map[string]bool{
359 "foo": true,
360 },
361 },
362 },
363 }
364
365 for _, test := range testCases {
366 t.Run(test.description, func(t *testing.T) {
367 bcc := &TestBazelConversionContext{
368 omc: bazel.OtherModuleTestContext{
369 Modules: []bazel.TestModuleInfo{
370 test.module.TestModuleInfo,
371 },
372 },
373 allowlist: test.allowlist,
374 }
375
376 shouldConvert := test.module.shouldConvertWithBp2build(bcc, test.module.TestModuleInfo)
377 if test.shouldConvert != shouldConvert {
378 t.Errorf("Module shouldConvert expected to be: %v, but was: %v", test.shouldConvert, shouldConvert)
379 }
380
381 errorsMatch := true
382 if len(test.expectedErrors) != len(bcc.errors) {
383 errorsMatch = false
384 } else {
385 for i, err := range test.expectedErrors {
386 if err != bcc.errors[i] {
387 errorsMatch = false
388 }
389 }
390 }
391 if !errorsMatch {
392 t.Errorf("Expected errors to be: %v, but were: %v", test.expectedErrors, bcc.errors)
393 }
394 })
395 }
396}
Wei Lid7736ec2022-05-12 23:37:53 -0700397
398func TestBp2buildAllowList(t *testing.T) {
Chris Parsonsad876012022-08-20 14:48:32 -0400399 allowlist := GetBp2BuildAllowList()
Wei Lid7736ec2022-05-12 23:37:53 -0700400 for k, v := range allowlists.Bp2buildDefaultConfig {
401 if allowlist.defaultConfig[k] != v {
402 t.Errorf("bp2build default config of %s: expected: %v, got: %v", k, v, allowlist.defaultConfig[k])
403 }
404 }
405 for k, v := range allowlists.Bp2buildKeepExistingBuildFile {
406 if allowlist.keepExistingBuildFile[k] != v {
407 t.Errorf("bp2build keep existing build file of %s: expected: %v, got: %v", k, v, allowlist.keepExistingBuildFile[k])
408 }
409 }
410 for _, k := range allowlists.Bp2buildModuleTypeAlwaysConvertList {
411 if !allowlist.moduleTypeAlwaysConvert[k] {
412 t.Errorf("bp2build module type always convert of %s: expected: true, got: %v", k, allowlist.moduleTypeAlwaysConvert[k])
413 }
414 }
415 for _, k := range allowlists.Bp2buildModuleDoNotConvertList {
416 if !allowlist.moduleDoNotConvert[k] {
417 t.Errorf("bp2build module do not convert of %s: expected: true, got: %v", k, allowlist.moduleDoNotConvert[k])
418 }
419 }
Wei Lid7736ec2022-05-12 23:37:53 -0700420}
Usta Shrestha342f28f2022-11-02 00:59:16 -0400421
422func TestShouldKeepExistingBuildFileForDir(t *testing.T) {
423 allowlist := NewBp2BuildAllowlist()
424 // entry "a/b2/c2" is moot because of its parent "a/b2"
425 allowlist.SetKeepExistingBuildFile(map[string]bool{"a": false, "a/b1": false, "a/b2": true, "a/b1/c1": true, "a/b2/c2": false})
426 truths := []string{"a", "a/b1", "a/b2", "a/b1/c1", "a/b2/c", "a/b2/c2", "a/b2/c2/d"}
427 falsities := []string{"a1", "a/b", "a/b1/c"}
428 for _, dir := range truths {
429 if !allowlist.ShouldKeepExistingBuildFileForDir(dir) {
430 t.Errorf("%s expected TRUE but was FALSE", dir)
431 }
432 }
433 for _, dir := range falsities {
434 if allowlist.ShouldKeepExistingBuildFileForDir(dir) {
435 t.Errorf("%s expected FALSE but was TRUE", dir)
436 }
437 }
438}