blob: b578cca0419af1cc535b60731c70269068c26e72 [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 },
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
Cole Faust324a92e2022-08-23 15:29:05 -0700161 allowlist Bp2BuildConversionAllowlist
Sam Delmericocc518432022-03-30 15:50:34 +0000162 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{
Cole Faust324a92e2022-08-23 15:29:05 -0700186 Bp2buildPackageConfig: bcc.allowlist,
Sam Delmericocc518432022-03-30 15:50:34 +0000187 },
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
Cole Faust324a92e2022-08-23 15:29:05 -0700205 allowlist Bp2BuildConversionAllowlist
Sam Delmericocc518432022-03-30 15:50:34 +0000206 }{
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 },
Cole Faust324a92e2022-08-23 15:29:05 -0700218 allowlist: Bp2BuildConversionAllowlist{
Sam Delmericocc518432022-03-30 15:50:34 +0000219 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 },
Cole Faust324a92e2022-08-23 15:29:05 -0700236 allowlist: Bp2BuildConversionAllowlist{
Sam Delmericocc518432022-03-30 15:50:34 +0000237 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 },
Cole Faust324a92e2022-08-23 15:29:05 -0700257 allowlist: Bp2BuildConversionAllowlist{
Sam Delmericocc518432022-03-30 15:50:34 +0000258 moduleAlwaysConvert: map[string]bool{
259 "foo": true,
260 },
261 moduleDoNotConvert: map[string]bool{
262 "foo": true,
263 },
264 },
265 },
266 {
Sam Delmericocc518432022-03-30 15:50:34 +0000267 description: "module allowlist and enabled directory",
268 shouldConvert: false,
269 expectedErrors: []string{"A module cannot be in a directory marked Bp2BuildDefaultTrue or Bp2BuildDefaultTrueRecursively 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 },
Cole Faust324a92e2022-08-23 15:29:05 -0700278 allowlist: Bp2BuildConversionAllowlist{
Sam Delmericocc518432022-03-30 15:50:34 +0000279 moduleAlwaysConvert: map[string]bool{
280 "foo": true,
281 },
282 defaultConfig: allowlists.Bp2BuildConfig{
283 "existing/build/dir": allowlists.Bp2BuildDefaultTrue,
284 },
285 },
286 },
287 {
288 description: "module allowlist and enabled subdirectory",
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/subdir",
296 },
297 BazelModuleBase: bazelableBazelModuleBase,
298 },
Cole Faust324a92e2022-08-23 15:29:05 -0700299 allowlist: Bp2BuildConversionAllowlist{
Sam Delmericocc518432022-03-30 15:50:34 +0000300 moduleAlwaysConvert: map[string]bool{
301 "foo": true,
302 },
303 defaultConfig: allowlists.Bp2BuildConfig{
304 "existing/build/dir": allowlists.Bp2BuildDefaultTrueRecursively,
305 },
306 },
307 },
308 {
309 description: "module enabled in unit test short-circuits other allowlists",
310 shouldConvert: true,
311 module: TestBazelModule{
312 TestModuleInfo: bazel.TestModuleInfo{
313 ModuleName: "foo",
314 Typ: "rule1",
315 Dir: ".",
316 },
317 BazelModuleBase: BazelModuleBase{
318 bazelProperties: properties{
319 Bazel_module: bazelModuleProperties{
320 CanConvertToBazel: true,
321 Bp2build_available: proptools.BoolPtr(true),
322 },
323 },
324 },
325 },
Cole Faust324a92e2022-08-23 15:29:05 -0700326 allowlist: Bp2BuildConversionAllowlist{
Sam Delmericocc518432022-03-30 15:50:34 +0000327 moduleAlwaysConvert: map[string]bool{
328 "foo": true,
329 },
330 moduleDoNotConvert: map[string]bool{
331 "foo": true,
332 },
333 },
334 },
335 }
336
337 for _, test := range testCases {
338 t.Run(test.description, func(t *testing.T) {
339 bcc := &TestBazelConversionContext{
340 omc: bazel.OtherModuleTestContext{
341 Modules: []bazel.TestModuleInfo{
342 test.module.TestModuleInfo,
343 },
344 },
345 allowlist: test.allowlist,
346 }
347
348 shouldConvert := test.module.shouldConvertWithBp2build(bcc, test.module.TestModuleInfo)
349 if test.shouldConvert != shouldConvert {
350 t.Errorf("Module shouldConvert expected to be: %v, but was: %v", test.shouldConvert, shouldConvert)
351 }
352
353 errorsMatch := true
354 if len(test.expectedErrors) != len(bcc.errors) {
355 errorsMatch = false
356 } else {
357 for i, err := range test.expectedErrors {
358 if err != bcc.errors[i] {
359 errorsMatch = false
360 }
361 }
362 }
363 if !errorsMatch {
364 t.Errorf("Expected errors to be: %v, but were: %v", test.expectedErrors, bcc.errors)
365 }
366 })
367 }
368}
Wei Lid7736ec2022-05-12 23:37:53 -0700369
370func TestBp2buildAllowList(t *testing.T) {
Chris Parsonsad876012022-08-20 14:48:32 -0400371 allowlist := GetBp2BuildAllowList()
Wei Lid7736ec2022-05-12 23:37:53 -0700372 for k, v := range allowlists.Bp2buildDefaultConfig {
373 if allowlist.defaultConfig[k] != v {
374 t.Errorf("bp2build default config of %s: expected: %v, got: %v", k, v, allowlist.defaultConfig[k])
375 }
376 }
377 for k, v := range allowlists.Bp2buildKeepExistingBuildFile {
378 if allowlist.keepExistingBuildFile[k] != v {
379 t.Errorf("bp2build keep existing build file of %s: expected: %v, got: %v", k, v, allowlist.keepExistingBuildFile[k])
380 }
381 }
382 for _, k := range allowlists.Bp2buildModuleTypeAlwaysConvertList {
383 if !allowlist.moduleTypeAlwaysConvert[k] {
384 t.Errorf("bp2build module type always convert of %s: expected: true, got: %v", k, allowlist.moduleTypeAlwaysConvert[k])
385 }
386 }
387 for _, k := range allowlists.Bp2buildModuleDoNotConvertList {
388 if !allowlist.moduleDoNotConvert[k] {
389 t.Errorf("bp2build module do not convert of %s: expected: true, got: %v", k, allowlist.moduleDoNotConvert[k])
390 }
391 }
392 for _, k := range allowlists.Bp2buildCcLibraryStaticOnlyList {
393 if !allowlist.ccLibraryStaticOnly[k] {
394 t.Errorf("bp2build cc library static only of %s: expected: true, got: %v", k, allowlist.ccLibraryStaticOnly[k])
395 }
396 }
Wei Lid7736ec2022-05-12 23:37:53 -0700397}