blob: 5cce3e4340e77c141ffebf24055a0ea3639f9deb [file] [log] [blame]
Jeff Gaston088e29e2017-11-29 16:47:17 -08001// Copyright 2017 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 android
16
17import (
18 "errors"
Jeff Gaston088e29e2017-11-29 16:47:17 -080019 "path/filepath"
Jeff Gastonb274ed32017-12-01 17:10:33 -080020 "reflect"
Jeff Gaston088e29e2017-11-29 16:47:17 -080021 "testing"
22
23 "github.com/google/blueprint"
24)
25
26func TestDependingOnModuleInSameNamespace(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070027 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -080028 ctx := setupTest(t,
29 map[string]string{
30 "dir1": `
31 soong_namespace {
32 }
33 test_module {
34 name: "a",
35 }
36 test_module {
37 name: "b",
38 deps: ["a"],
39 }
40 `,
41 },
42 )
43
44 a := getModule(ctx, "a")
45 b := getModule(ctx, "b")
46 if !dependsOn(ctx, b, a) {
47 t.Errorf("module b does not depend on module a in the same namespace")
48 }
49}
50
51func TestDependingOnModuleInRootNamespace(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070052 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -080053 ctx := setupTest(t,
54 map[string]string{
55 ".": `
56 test_module {
57 name: "b",
58 deps: ["a"],
59 }
60 test_module {
61 name: "a",
62 }
63 `,
64 },
65 )
66
67 a := getModule(ctx, "a")
68 b := getModule(ctx, "b")
69 if !dependsOn(ctx, b, a) {
70 t.Errorf("module b in root namespace does not depend on module a in the root namespace")
71 }
72}
73
74func TestImplicitlyImportRootNamespace(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070075 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -080076 _ = setupTest(t,
77 map[string]string{
78 ".": `
79 test_module {
80 name: "a",
81 }
82 `,
83 "dir1": `
84 soong_namespace {
85 }
86 test_module {
87 name: "b",
88 deps: ["a"],
89 }
90 `,
91 },
92 )
93
94 // setupTest will report any errors
95}
96
Colin Crosscd84b4e2019-06-14 11:26:09 -070097func TestDependingOnBlueprintModuleInRootNamespace(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070098 t.Parallel()
Colin Crosscd84b4e2019-06-14 11:26:09 -070099 _ = setupTest(t,
100 map[string]string{
101 ".": `
102 blueprint_test_module {
103 name: "a",
104 }
105 `,
106 "dir1": `
107 soong_namespace {
108 }
109 blueprint_test_module {
110 name: "b",
111 deps: ["a"],
112 }
113 `,
114 },
115 )
116
117 // setupTest will report any errors
118}
119
Jeff Gaston088e29e2017-11-29 16:47:17 -0800120func TestDependingOnModuleInImportedNamespace(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700121 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800122 ctx := setupTest(t,
123 map[string]string{
124 "dir1": `
125 soong_namespace {
126 }
127 test_module {
128 name: "a",
129 }
130 `,
131 "dir2": `
132 soong_namespace {
133 imports: ["dir1"],
134 }
135 test_module {
136 name: "b",
137 deps: ["a"],
138 }
139 `,
140 },
141 )
142
143 a := getModule(ctx, "a")
144 b := getModule(ctx, "b")
145 if !dependsOn(ctx, b, a) {
146 t.Errorf("module b does not depend on module a in the same namespace")
147 }
148}
149
150func TestDependingOnModuleInNonImportedNamespace(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700151 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800152 _, errs := setupTestExpectErrs(
153 map[string]string{
154 "dir1": `
155 soong_namespace {
156 }
157 test_module {
158 name: "a",
159 }
160 `,
161 "dir2": `
162 soong_namespace {
163 }
164 test_module {
165 name: "a",
166 }
167 `,
168 "dir3": `
169 soong_namespace {
170 }
171 test_module {
172 name: "b",
173 deps: ["a"],
174 }
175 `,
176 },
177 )
178
179 expectedErrors := []error{
180 errors.New(
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800181 `dir3/Android.bp:4:4: "b" depends on undefined module "a"
Jeff Gaston088e29e2017-11-29 16:47:17 -0800182Module "b" is defined in namespace "dir3" which can read these 2 namespaces: ["dir3" "."]
183Module "a" can be found in these namespaces: ["dir1" "dir2"]`),
184 }
185
186 if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
187 t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
188 }
189}
190
191func TestDependingOnModuleByFullyQualifiedReference(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700192 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800193 ctx := setupTest(t,
194 map[string]string{
195 "dir1": `
196 soong_namespace {
197 }
198 test_module {
199 name: "a",
200 }
201 `,
202 "dir2": `
203 soong_namespace {
204 }
205 test_module {
206 name: "b",
207 deps: ["//dir1:a"],
208 }
209 `,
210 },
211 )
212 a := getModule(ctx, "a")
213 b := getModule(ctx, "b")
214 if !dependsOn(ctx, b, a) {
215 t.Errorf("module b does not depend on module a")
216 }
217}
218
219func TestSameNameInTwoNamespaces(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700220 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800221 ctx := setupTest(t,
222 map[string]string{
223 "dir1": `
224 soong_namespace {
225 }
226 test_module {
227 name: "a",
228 id: "1",
229 }
230 test_module {
231 name: "b",
232 deps: ["a"],
233 id: "2",
234 }
235 `,
236 "dir2": `
237 soong_namespace {
238 }
239 test_module {
240 name: "a",
241 id:"3",
242 }
243 test_module {
244 name: "b",
245 deps: ["a"],
246 id:"4",
247 }
248 `,
249 },
250 )
251
252 one := findModuleById(ctx, "1")
253 two := findModuleById(ctx, "2")
254 three := findModuleById(ctx, "3")
255 four := findModuleById(ctx, "4")
256 if !dependsOn(ctx, two, one) {
257 t.Fatalf("Module 2 does not depend on module 1 in its namespace")
258 }
259 if dependsOn(ctx, two, three) {
260 t.Fatalf("Module 2 depends on module 3 in another namespace")
261 }
262 if !dependsOn(ctx, four, three) {
263 t.Fatalf("Module 4 does not depend on module 3 in its namespace")
264 }
265 if dependsOn(ctx, four, one) {
266 t.Fatalf("Module 4 depends on module 1 in another namespace")
267 }
268}
269
270func TestSearchOrder(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700271 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800272 ctx := setupTest(t,
273 map[string]string{
274 "dir1": `
275 soong_namespace {
276 }
277 test_module {
278 name: "a",
279 id: "1",
280 }
281 `,
282 "dir2": `
283 soong_namespace {
284 }
285 test_module {
286 name: "a",
287 id:"2",
288 }
289 test_module {
290 name: "b",
291 id:"3",
292 }
293 `,
294 "dir3": `
295 soong_namespace {
296 }
297 test_module {
298 name: "a",
299 id:"4",
300 }
301 test_module {
302 name: "b",
303 id:"5",
304 }
305 test_module {
306 name: "c",
307 id:"6",
308 }
309 `,
310 ".": `
311 test_module {
312 name: "a",
313 id: "7",
314 }
315 test_module {
316 name: "b",
317 id: "8",
318 }
319 test_module {
320 name: "c",
321 id: "9",
322 }
323 test_module {
324 name: "d",
325 id: "10",
326 }
327 `,
328 "dir4": `
329 soong_namespace {
330 imports: ["dir1", "dir2", "dir3"]
331 }
332 test_module {
333 name: "test_me",
334 id:"0",
335 deps: ["a", "b", "c", "d"],
336 }
337 `,
338 },
339 )
340
341 testMe := findModuleById(ctx, "0")
342 if !dependsOn(ctx, testMe, findModuleById(ctx, "1")) {
343 t.Errorf("test_me doesn't depend on id 1")
344 }
345 if !dependsOn(ctx, testMe, findModuleById(ctx, "3")) {
346 t.Errorf("test_me doesn't depend on id 3")
347 }
348 if !dependsOn(ctx, testMe, findModuleById(ctx, "6")) {
349 t.Errorf("test_me doesn't depend on id 6")
350 }
351 if !dependsOn(ctx, testMe, findModuleById(ctx, "10")) {
352 t.Errorf("test_me doesn't depend on id 10")
353 }
354 if numDeps(ctx, testMe) != 4 {
355 t.Errorf("num dependencies of test_me = %v, not 4\n", numDeps(ctx, testMe))
356 }
357}
358
359func TestTwoNamespacesCanImportEachOther(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700360 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800361 _ = setupTest(t,
362 map[string]string{
363 "dir1": `
364 soong_namespace {
365 imports: ["dir2"]
366 }
367 test_module {
368 name: "a",
369 }
370 test_module {
371 name: "c",
372 deps: ["b"],
373 }
374 `,
375 "dir2": `
376 soong_namespace {
377 imports: ["dir1"],
378 }
379 test_module {
380 name: "b",
381 deps: ["a"],
382 }
383 `,
384 },
385 )
386
387 // setupTest will report any errors
388}
389
390func TestImportingNonexistentNamespace(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700391 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800392 _, errs := setupTestExpectErrs(
393 map[string]string{
394 "dir1": `
395 soong_namespace {
396 imports: ["a_nonexistent_namespace"]
397 }
398 test_module {
399 name: "a",
400 deps: ["a_nonexistent_module"]
401 }
402 `,
403 },
404 )
405
406 // should complain about the missing namespace and not complain about the unresolvable dependency
407 expectedErrors := []error{
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800408 errors.New(`dir1/Android.bp:2:4: module "soong_namespace": namespace a_nonexistent_namespace does not exist`),
Jeff Gaston088e29e2017-11-29 16:47:17 -0800409 }
410 if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
411 t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
412 }
413}
414
415func TestNamespacesDontInheritParentNamespaces(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700416 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800417 _, errs := setupTestExpectErrs(
418 map[string]string{
419 "dir1": `
420 soong_namespace {
421 }
422 test_module {
423 name: "a",
424 }
425 `,
426 "dir1/subdir1": `
427 soong_namespace {
428 }
429 test_module {
430 name: "b",
431 deps: ["a"],
432 }
433 `,
434 },
435 )
436
437 expectedErrors := []error{
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800438 errors.New(`dir1/subdir1/Android.bp:4:4: "b" depends on undefined module "a"
Jeff Gaston088e29e2017-11-29 16:47:17 -0800439Module "b" is defined in namespace "dir1/subdir1" which can read these 2 namespaces: ["dir1/subdir1" "."]
440Module "a" can be found in these namespaces: ["dir1"]`),
441 }
442 if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
443 t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
444 }
445}
446
447func TestModulesDoReceiveParentNamespace(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700448 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800449 _ = setupTest(t,
450 map[string]string{
451 "dir1": `
452 soong_namespace {
453 }
454 test_module {
455 name: "a",
456 }
457 `,
458 "dir1/subdir": `
459 test_module {
460 name: "b",
461 deps: ["a"],
462 }
463 `,
464 },
465 )
466
467 // setupTest will report any errors
468}
469
470func TestNamespaceImportsNotTransitive(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700471 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800472 _, errs := setupTestExpectErrs(
473 map[string]string{
474 "dir1": `
475 soong_namespace {
476 }
477 test_module {
478 name: "a",
479 }
480 `,
481 "dir2": `
482 soong_namespace {
483 imports: ["dir1"],
484 }
485 test_module {
486 name: "b",
487 deps: ["a"],
488 }
489 `,
490 "dir3": `
491 soong_namespace {
492 imports: ["dir2"],
493 }
494 test_module {
495 name: "c",
496 deps: ["a"],
497 }
498 `,
499 },
500 )
501
502 expectedErrors := []error{
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800503 errors.New(`dir3/Android.bp:5:4: "c" depends on undefined module "a"
Jeff Gaston088e29e2017-11-29 16:47:17 -0800504Module "c" is defined in namespace "dir3" which can read these 3 namespaces: ["dir3" "dir2" "."]
505Module "a" can be found in these namespaces: ["dir1"]`),
506 }
507 if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
508 t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
509 }
510}
511
512func TestTwoNamepacesInSameDir(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700513 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800514 _, errs := setupTestExpectErrs(
515 map[string]string{
516 "dir1": `
517 soong_namespace {
518 }
519 soong_namespace {
520 }
521 `,
522 },
523 )
524
525 expectedErrors := []error{
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800526 errors.New(`dir1/Android.bp:4:4: namespace dir1 already exists`),
Jeff Gaston088e29e2017-11-29 16:47:17 -0800527 }
528 if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
529 t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
530 }
531}
532
533func TestNamespaceNotAtTopOfFile(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700534 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800535 _, errs := setupTestExpectErrs(
536 map[string]string{
537 "dir1": `
538 test_module {
539 name: "a"
540 }
541 soong_namespace {
542 }
543 `,
544 },
545 )
546
547 expectedErrors := []error{
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800548 errors.New(`dir1/Android.bp:5:4: a namespace must be the first module in the file`),
Jeff Gaston088e29e2017-11-29 16:47:17 -0800549 }
550 if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
551 t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
552 }
553}
554
555func TestTwoModulesWithSameNameInSameNamespace(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700556 t.Parallel()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800557 _, errs := setupTestExpectErrs(
558 map[string]string{
559 "dir1": `
560 soong_namespace {
561 }
562 test_module {
563 name: "a"
564 }
565 test_module {
566 name: "a"
567 }
568 `,
569 },
570 )
571
572 expectedErrors := []error{
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800573 errors.New(`dir1/Android.bp:7:4: module "a" already defined
574 dir1/Android.bp:4:4 <-- previous definition here`),
Jeff Gaston088e29e2017-11-29 16:47:17 -0800575 }
576 if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
577 t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
578 }
579}
580
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800581func TestDeclaringNamespaceInNonAndroidBpFile(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700582 t.Parallel()
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800583 _, errs := setupTestFromFiles(
584 map[string][]byte{
585 "Android.bp": []byte(`
586 build = ["include.bp"]
587 `),
588 "include.bp": []byte(`
589 soong_namespace {
590 }
591 `),
592 },
593 )
594
595 expectedErrors := []error{
596 errors.New(`include.bp:2:5: A namespace may only be declared in a file named Android.bp`),
597 }
598
599 if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
600 t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
601 }
602}
603
Jeff Gastonb274ed32017-12-01 17:10:33 -0800604// so that the generated .ninja file will have consistent names
605func TestConsistentNamespaceNames(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700606 t.Parallel()
Jeff Gastonb274ed32017-12-01 17:10:33 -0800607 ctx := setupTest(t,
608 map[string]string{
609 "dir1": "soong_namespace{}",
610 "dir2": "soong_namespace{}",
611 "dir3": "soong_namespace{}",
612 })
613
614 ns1, _ := ctx.NameResolver.namespaceAt("dir1")
615 ns2, _ := ctx.NameResolver.namespaceAt("dir2")
616 ns3, _ := ctx.NameResolver.namespaceAt("dir3")
617 actualIds := []string{ns1.id, ns2.id, ns3.id}
618 expectedIds := []string{"1", "2", "3"}
619 if !reflect.DeepEqual(actualIds, expectedIds) {
620 t.Errorf("Incorrect namespace ids.\nactual: %s\nexpected: %s\n", actualIds, expectedIds)
621 }
622}
623
Colin Crosseafb10c2018-04-16 13:58:10 -0700624// so that the generated .ninja file will have consistent names
625func TestRename(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700626 t.Parallel()
Colin Crosseafb10c2018-04-16 13:58:10 -0700627 _ = setupTest(t,
628 map[string]string{
629 "dir1": `
630 soong_namespace {
631 }
632 test_module {
633 name: "a",
634 deps: ["c"],
635 }
636 test_module {
637 name: "b",
638 rename: "c",
639 }
640 `})
641 // setupTest will report any errors
642}
643
Jeff Gaston088e29e2017-11-29 16:47:17 -0800644// some utils to support the tests
645
646func mockFiles(bps map[string]string) (files map[string][]byte) {
647 files = make(map[string][]byte, len(bps))
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800648 files["Android.bp"] = []byte("")
Jeff Gaston088e29e2017-11-29 16:47:17 -0800649 for dir, text := range bps {
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800650 files[filepath.Join(dir, "Android.bp")] = []byte(text)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800651 }
652 return files
653}
654
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800655func setupTestFromFiles(bps map[string][]byte) (ctx *TestContext, errs []error) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800656 config := TestConfig(buildDir, nil, "", bps)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800657
658 ctx = NewTestContext()
Colin Cross4b49b762019-11-22 15:25:03 -0800659 ctx.RegisterModuleType("test_module", newTestModule)
660 ctx.RegisterModuleType("soong_namespace", NamespaceFactory)
661 ctx.Context.RegisterModuleType("blueprint_test_module", newBlueprintTestModule)
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800662 ctx.PreArchMutators(RegisterNamespaceMutator)
Colin Crosseafb10c2018-04-16 13:58:10 -0700663 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
664 ctx.BottomUp("rename", renameMutator)
665 })
Colin Cross98be1bb2019-12-13 20:41:13 -0800666 ctx.Register(config)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800667
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800668 _, errs = ctx.ParseBlueprintsFiles("Android.bp")
Jeff Gaston088e29e2017-11-29 16:47:17 -0800669 if len(errs) > 0 {
670 return ctx, errs
671 }
672 _, errs = ctx.PrepareBuildActions(config)
673 return ctx, errs
674}
675
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800676func setupTestExpectErrs(bps map[string]string) (ctx *TestContext, errs []error) {
677 files := make(map[string][]byte, len(bps))
678 files["Android.bp"] = []byte("")
679 for dir, text := range bps {
680 files[filepath.Join(dir, "Android.bp")] = []byte(text)
681 }
682 return setupTestFromFiles(files)
683}
684
Jeff Gaston088e29e2017-11-29 16:47:17 -0800685func setupTest(t *testing.T, bps map[string]string) (ctx *TestContext) {
Colin Crosscd84b4e2019-06-14 11:26:09 -0700686 t.Helper()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800687 ctx, errs := setupTestExpectErrs(bps)
Logan Chien42039712018-03-12 16:29:17 +0800688 FailIfErrored(t, errs)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800689 return ctx
690}
691
692func dependsOn(ctx *TestContext, module TestingModule, possibleDependency TestingModule) bool {
693 depends := false
694 visit := func(dependency blueprint.Module) {
695 if dependency == possibleDependency.module {
696 depends = true
697 }
698 }
699 ctx.VisitDirectDeps(module.module, visit)
700 return depends
701}
702
703func numDeps(ctx *TestContext, module TestingModule) int {
704 count := 0
705 visit := func(dependency blueprint.Module) {
706 count++
707 }
708 ctx.VisitDirectDeps(module.module, visit)
709 return count
710}
711
712func getModule(ctx *TestContext, moduleName string) TestingModule {
713 return ctx.ModuleForTests(moduleName, "")
714}
715
716func findModuleById(ctx *TestContext, id string) (module TestingModule) {
717 visit := func(candidate blueprint.Module) {
718 testModule, ok := candidate.(*testModule)
719 if ok {
720 if testModule.properties.Id == id {
721 module = TestingModule{testModule}
722 }
723 }
724 }
725 ctx.VisitAllModules(visit)
726 return module
727}
728
729type testModule struct {
730 ModuleBase
731 properties struct {
Colin Crosseafb10c2018-04-16 13:58:10 -0700732 Rename string
733 Deps []string
734 Id string
Jeff Gaston088e29e2017-11-29 16:47:17 -0800735 }
736}
737
738func (m *testModule) DepsMutator(ctx BottomUpMutatorContext) {
Colin Crosseafb10c2018-04-16 13:58:10 -0700739 if m.properties.Rename != "" {
740 ctx.Rename(m.properties.Rename)
741 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800742 for _, d := range m.properties.Deps {
743 ctx.AddDependency(ctx.Module(), nil, d)
744 }
745}
746
747func (m *testModule) GenerateAndroidBuildActions(ModuleContext) {
748}
749
Colin Crosseafb10c2018-04-16 13:58:10 -0700750func renameMutator(ctx BottomUpMutatorContext) {
751 if m, ok := ctx.Module().(*testModule); ok {
752 if m.properties.Rename != "" {
753 ctx.Rename(m.properties.Rename)
754 }
755 }
756}
757
Jeff Gaston088e29e2017-11-29 16:47:17 -0800758func newTestModule() Module {
759 m := &testModule{}
760 m.AddProperties(&m.properties)
761 InitAndroidModule(m)
762 return m
763}
Colin Crosscd84b4e2019-06-14 11:26:09 -0700764
765type blueprintTestModule struct {
766 blueprint.SimpleName
767 properties struct {
768 Deps []string
769 }
770}
771
772func (b *blueprintTestModule) DynamicDependencies(ctx blueprint.DynamicDependerModuleContext) []string {
773 return b.properties.Deps
774}
775
776func (b *blueprintTestModule) GenerateBuildActions(blueprint.ModuleContext) {
777}
778
779func newBlueprintTestModule() (blueprint.Module, []interface{}) {
780 m := &blueprintTestModule{}
781 return m, []interface{}{&m.properties, &m.SimpleName.Properties}
782}