blob: 87d13206bcf3f48cff322a5c573f7d53016e99af [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 (
Jeff Gaston088e29e2017-11-29 16:47:17 -080018 "path/filepath"
Jeff Gastonb274ed32017-12-01 17:10:33 -080019 "reflect"
Jeff Gaston088e29e2017-11-29 16:47:17 -080020 "testing"
21
22 "github.com/google/blueprint"
23)
24
25func TestDependingOnModuleInSameNamespace(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +010026 result := GroupFixturePreparers(
27 prepareForTestWithNamespace,
28 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -080029 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +010030 soong_namespace {
31 }
32 test_module {
33 name: "a",
34 }
35 test_module {
36 name: "b",
37 deps: ["a"],
38 }
Jeff Gaston088e29e2017-11-29 16:47:17 -080039 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +010040 }),
41 ).RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -080042
Paul Duffin0fc6d322021-07-06 22:36:33 +010043 a := getModule(result, "a")
44 b := getModule(result, "b")
45 if !dependsOn(result, b, a) {
Jeff Gaston088e29e2017-11-29 16:47:17 -080046 t.Errorf("module b does not depend on module a in the same namespace")
47 }
48}
49
50func TestDependingOnModuleInRootNamespace(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +010051 result := GroupFixturePreparers(
52 prepareForTestWithNamespace,
53 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -080054 ".": `
Paul Duffin0fc6d322021-07-06 22:36:33 +010055 test_module {
56 name: "b",
57 deps: ["a"],
58 }
59 test_module {
60 name: "a",
61 }
Jeff Gaston088e29e2017-11-29 16:47:17 -080062 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +010063 }),
64 ).RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -080065
Paul Duffin0fc6d322021-07-06 22:36:33 +010066 a := getModule(result, "a")
67 b := getModule(result, "b")
68 if !dependsOn(result, b, a) {
Jeff Gaston088e29e2017-11-29 16:47:17 -080069 t.Errorf("module b in root namespace does not depend on module a in the root namespace")
70 }
71}
72
73func TestImplicitlyImportRootNamespace(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +010074 GroupFixturePreparers(
75 prepareForTestWithNamespace,
76 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -080077 ".": `
Paul Duffin0fc6d322021-07-06 22:36:33 +010078 test_module {
79 name: "a",
80 }
Jeff Gaston088e29e2017-11-29 16:47:17 -080081 `,
82 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +010083 soong_namespace {
84 }
85 test_module {
86 name: "b",
87 deps: ["a"],
88 }
Jeff Gaston088e29e2017-11-29 16:47:17 -080089 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +010090 }),
91 ).RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -080092
Paul Duffin0fc6d322021-07-06 22:36:33 +010093 // RunTest will report any errors
Jeff Gaston088e29e2017-11-29 16:47:17 -080094}
95
Colin Crosscd84b4e2019-06-14 11:26:09 -070096func TestDependingOnBlueprintModuleInRootNamespace(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +010097 GroupFixturePreparers(
98 prepareForTestWithNamespace,
99 dirBpToPreparer(map[string]string{
Colin Crosscd84b4e2019-06-14 11:26:09 -0700100 ".": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100101 blueprint_test_module {
102 name: "a",
103 }
Colin Crosscd84b4e2019-06-14 11:26:09 -0700104 `,
105 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100106 soong_namespace {
107 }
108 blueprint_test_module {
109 name: "b",
110 deps: ["a"],
111 }
Colin Crosscd84b4e2019-06-14 11:26:09 -0700112 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +0100113 }),
114 ).RunTest(t)
Colin Crosscd84b4e2019-06-14 11:26:09 -0700115
Paul Duffin0fc6d322021-07-06 22:36:33 +0100116 // RunTest will report any errors
Colin Crosscd84b4e2019-06-14 11:26:09 -0700117}
118
Jeff Gaston088e29e2017-11-29 16:47:17 -0800119func TestDependingOnModuleInImportedNamespace(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100120 result := GroupFixturePreparers(
121 prepareForTestWithNamespace,
122 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -0800123 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100124 soong_namespace {
125 }
126 test_module {
127 name: "a",
128 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800129 `,
130 "dir2": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100131 soong_namespace {
132 imports: ["dir1"],
133 }
134 test_module {
135 name: "b",
136 deps: ["a"],
137 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800138 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +0100139 }),
140 ).RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800141
Paul Duffin0fc6d322021-07-06 22:36:33 +0100142 a := getModule(result, "a")
143 b := getModule(result, "b")
144 if !dependsOn(result, b, a) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800145 t.Errorf("module b does not depend on module a in the same namespace")
146 }
147}
148
149func TestDependingOnModuleInNonImportedNamespace(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100150 GroupFixturePreparers(
151 prepareForTestWithNamespace,
152 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -0800153 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100154 soong_namespace {
155 }
156 test_module {
157 name: "a",
158 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800159 `,
160 "dir2": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100161 soong_namespace {
162 }
163 test_module {
164 name: "a",
165 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800166 `,
167 "dir3": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100168 soong_namespace {
169 }
170 test_module {
171 name: "b",
172 deps: ["a"],
173 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800174 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +0100175 }),
176 ).
177 ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir3/Android.bp:4:5: "b" depends on undefined module "a"
Jeff Gaston088e29e2017-11-29 16:47:17 -0800178Module "b" is defined in namespace "dir3" which can read these 2 namespaces: ["dir3" "."]
Paul Duffin0fc6d322021-07-06 22:36:33 +0100179Module "a" can be found in these namespaces: ["dir1" "dir2"]\E`)).
180 RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800181}
182
183func TestDependingOnModuleByFullyQualifiedReference(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100184 result := GroupFixturePreparers(
185 prepareForTestWithNamespace,
186 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -0800187 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100188 soong_namespace {
189 }
190 test_module {
191 name: "a",
192 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800193 `,
194 "dir2": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100195 soong_namespace {
196 }
197 test_module {
198 name: "b",
199 deps: ["//dir1:a"],
200 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800201 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +0100202 }),
203 ).RunTest(t)
204
205 a := getModule(result, "a")
206 b := getModule(result, "b")
207 if !dependsOn(result, b, a) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800208 t.Errorf("module b does not depend on module a")
209 }
210}
211
212func TestSameNameInTwoNamespaces(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100213 result := GroupFixturePreparers(
214 prepareForTestWithNamespace,
215 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -0800216 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100217 soong_namespace {
218 }
219 test_module {
220 name: "a",
221 id: "1",
222 }
223 test_module {
224 name: "b",
225 deps: ["a"],
226 id: "2",
227 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800228 `,
229 "dir2": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100230 soong_namespace {
231 }
232 test_module {
233 name: "a",
234 id:"3",
235 }
236 test_module {
237 name: "b",
238 deps: ["a"],
239 id:"4",
240 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800241 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +0100242 }),
243 ).RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800244
Paul Duffin0fc6d322021-07-06 22:36:33 +0100245 one := findModuleById(result, "1")
246 two := findModuleById(result, "2")
247 three := findModuleById(result, "3")
248 four := findModuleById(result, "4")
249 if !dependsOn(result, two, one) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800250 t.Fatalf("Module 2 does not depend on module 1 in its namespace")
251 }
Paul Duffin0fc6d322021-07-06 22:36:33 +0100252 if dependsOn(result, two, three) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800253 t.Fatalf("Module 2 depends on module 3 in another namespace")
254 }
Paul Duffin0fc6d322021-07-06 22:36:33 +0100255 if !dependsOn(result, four, three) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800256 t.Fatalf("Module 4 does not depend on module 3 in its namespace")
257 }
Paul Duffin0fc6d322021-07-06 22:36:33 +0100258 if dependsOn(result, four, one) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800259 t.Fatalf("Module 4 depends on module 1 in another namespace")
260 }
261}
262
263func TestSearchOrder(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100264 result := GroupFixturePreparers(
265 prepareForTestWithNamespace,
266 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -0800267 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100268 soong_namespace {
269 }
270 test_module {
271 name: "a",
272 id: "1",
273 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800274 `,
275 "dir2": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100276 soong_namespace {
277 }
278 test_module {
279 name: "a",
280 id:"2",
281 }
282 test_module {
283 name: "b",
284 id:"3",
285 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800286 `,
287 "dir3": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100288 soong_namespace {
289 }
290 test_module {
291 name: "a",
292 id:"4",
293 }
294 test_module {
295 name: "b",
296 id:"5",
297 }
298 test_module {
299 name: "c",
300 id:"6",
301 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800302 `,
303 ".": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100304 test_module {
305 name: "a",
306 id: "7",
307 }
308 test_module {
309 name: "b",
310 id: "8",
311 }
312 test_module {
313 name: "c",
314 id: "9",
315 }
316 test_module {
317 name: "d",
318 id: "10",
319 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800320 `,
321 "dir4": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100322 soong_namespace {
323 imports: ["dir1", "dir2", "dir3"]
324 }
325 test_module {
326 name: "test_me",
327 id:"0",
328 deps: ["a", "b", "c", "d"],
329 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800330 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +0100331 }),
332 ).RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800333
Paul Duffin0fc6d322021-07-06 22:36:33 +0100334 testMe := findModuleById(result, "0")
335 if !dependsOn(result, testMe, findModuleById(result, "1")) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800336 t.Errorf("test_me doesn't depend on id 1")
337 }
Paul Duffin0fc6d322021-07-06 22:36:33 +0100338 if !dependsOn(result, testMe, findModuleById(result, "3")) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800339 t.Errorf("test_me doesn't depend on id 3")
340 }
Paul Duffin0fc6d322021-07-06 22:36:33 +0100341 if !dependsOn(result, testMe, findModuleById(result, "6")) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800342 t.Errorf("test_me doesn't depend on id 6")
343 }
Paul Duffin0fc6d322021-07-06 22:36:33 +0100344 if !dependsOn(result, testMe, findModuleById(result, "10")) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800345 t.Errorf("test_me doesn't depend on id 10")
346 }
Paul Duffin0fc6d322021-07-06 22:36:33 +0100347 if numDeps(result, testMe) != 4 {
348 t.Errorf("num dependencies of test_me = %v, not 4\n", numDeps(result, testMe))
Jeff Gaston088e29e2017-11-29 16:47:17 -0800349 }
350}
351
352func TestTwoNamespacesCanImportEachOther(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100353 GroupFixturePreparers(
354 prepareForTestWithNamespace,
355 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -0800356 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100357 soong_namespace {
358 imports: ["dir2"]
359 }
360 test_module {
361 name: "a",
362 }
363 test_module {
364 name: "c",
365 deps: ["b"],
366 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800367 `,
368 "dir2": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100369 soong_namespace {
370 imports: ["dir1"],
371 }
372 test_module {
373 name: "b",
374 deps: ["a"],
375 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800376 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +0100377 }),
378 ).RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800379
Paul Duffin0fc6d322021-07-06 22:36:33 +0100380 // RunTest will report any errors
Jeff Gaston088e29e2017-11-29 16:47:17 -0800381}
382
383func TestImportingNonexistentNamespace(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100384 GroupFixturePreparers(
385 prepareForTestWithNamespace,
386 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -0800387 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100388 soong_namespace {
389 imports: ["a_nonexistent_namespace"]
390 }
391 test_module {
392 name: "a",
393 deps: ["a_nonexistent_module"]
394 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800395 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +0100396 }),
397 ).
398 // should complain about the missing namespace and not complain about the unresolvable dependency
399 ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/Android.bp:2:5: module "soong_namespace": namespace a_nonexistent_namespace does not exist\E`)).
400 RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800401}
402
403func TestNamespacesDontInheritParentNamespaces(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100404 GroupFixturePreparers(
405 prepareForTestWithNamespace,
406 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -0800407 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100408 soong_namespace {
409 }
410 test_module {
411 name: "a",
412 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800413 `,
414 "dir1/subdir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100415 soong_namespace {
416 }
417 test_module {
418 name: "b",
419 deps: ["a"],
420 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800421 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +0100422 }),
423 ).
424 ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/subdir1/Android.bp:4:5: "b" depends on undefined module "a"
Jeff Gaston088e29e2017-11-29 16:47:17 -0800425Module "b" is defined in namespace "dir1/subdir1" which can read these 2 namespaces: ["dir1/subdir1" "."]
Paul Duffin0fc6d322021-07-06 22:36:33 +0100426Module "a" can be found in these namespaces: ["dir1"]\E`)).
427 RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800428}
429
430func TestModulesDoReceiveParentNamespace(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100431 GroupFixturePreparers(
432 prepareForTestWithNamespace,
433 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -0800434 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100435 soong_namespace {
436 }
437 test_module {
438 name: "a",
439 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800440 `,
441 "dir1/subdir": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100442 test_module {
443 name: "b",
444 deps: ["a"],
445 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800446 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +0100447 }),
448 ).RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800449
Paul Duffin0fc6d322021-07-06 22:36:33 +0100450 // RunTest will report any errors
Jeff Gaston088e29e2017-11-29 16:47:17 -0800451}
452
453func TestNamespaceImportsNotTransitive(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100454 GroupFixturePreparers(
455 prepareForTestWithNamespace,
456 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -0800457 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100458 soong_namespace {
459 }
460 test_module {
461 name: "a",
462 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800463 `,
464 "dir2": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100465 soong_namespace {
466 imports: ["dir1"],
467 }
468 test_module {
469 name: "b",
470 deps: ["a"],
471 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800472 `,
473 "dir3": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100474 soong_namespace {
475 imports: ["dir2"],
476 }
477 test_module {
478 name: "c",
479 deps: ["a"],
480 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800481 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +0100482 }),
483 ).
484 ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir3/Android.bp:5:5: "c" depends on undefined module "a"
Jeff Gaston088e29e2017-11-29 16:47:17 -0800485Module "c" is defined in namespace "dir3" which can read these 3 namespaces: ["dir3" "dir2" "."]
Paul Duffin0fc6d322021-07-06 22:36:33 +0100486Module "a" can be found in these namespaces: ["dir1"]\E`)).
487 RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800488}
489
490func TestTwoNamepacesInSameDir(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100491 GroupFixturePreparers(
492 prepareForTestWithNamespace,
493 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -0800494 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100495 soong_namespace {
496 }
497 soong_namespace {
498 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800499 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +0100500 }),
501 ).
502 ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/Android.bp:4:5: namespace dir1 already exists\E`)).
503 RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800504}
505
506func TestNamespaceNotAtTopOfFile(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100507 GroupFixturePreparers(
508 prepareForTestWithNamespace,
509 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -0800510 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100511 test_module {
512 name: "a"
513 }
514 soong_namespace {
515 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800516 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +0100517 }),
518 ).
519 ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/Android.bp:5:5: a namespace must be the first module in the file\E`)).
520 RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800521}
522
523func TestTwoModulesWithSameNameInSameNamespace(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100524 GroupFixturePreparers(
525 prepareForTestWithNamespace,
526 dirBpToPreparer(map[string]string{
Jeff Gaston088e29e2017-11-29 16:47:17 -0800527 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100528 soong_namespace {
529 }
530 test_module {
531 name: "a"
532 }
533 test_module {
534 name: "a"
535 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800536 `,
Paul Duffin0fc6d322021-07-06 22:36:33 +0100537 }),
538 ).
539 ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/Android.bp:7:5: module "a" already defined
540 dir1/Android.bp:4:5 <-- previous definition here\E`)).
541 RunTest(t)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800542}
543
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800544func TestDeclaringNamespaceInNonAndroidBpFile(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100545 GroupFixturePreparers(
546 prepareForTestWithNamespace,
547 FixtureWithRootAndroidBp(`
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800548 build = ["include.bp"]
Paul Duffin0fc6d322021-07-06 22:36:33 +0100549 `),
550 FixtureAddTextFile("include.bp", `
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800551 soong_namespace {
552 }
Paul Duffin0fc6d322021-07-06 22:36:33 +0100553 `),
554 ).
555 ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(
556 `\Qinclude.bp:2:5: A namespace may only be declared in a file named Android.bp\E`,
557 )).
558 RunTest(t)
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800559}
560
Jeff Gastonb274ed32017-12-01 17:10:33 -0800561// so that the generated .ninja file will have consistent names
562func TestConsistentNamespaceNames(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100563 result := GroupFixturePreparers(
564 prepareForTestWithNamespace,
565 dirBpToPreparer(map[string]string{
Jeff Gastonb274ed32017-12-01 17:10:33 -0800566 "dir1": "soong_namespace{}",
567 "dir2": "soong_namespace{}",
568 "dir3": "soong_namespace{}",
Paul Duffin0fc6d322021-07-06 22:36:33 +0100569 }),
570 ).RunTest(t)
Jeff Gastonb274ed32017-12-01 17:10:33 -0800571
Paul Duffin0fc6d322021-07-06 22:36:33 +0100572 ns1, _ := result.NameResolver.namespaceAt("dir1")
573 ns2, _ := result.NameResolver.namespaceAt("dir2")
574 ns3, _ := result.NameResolver.namespaceAt("dir3")
Jeff Gastonb274ed32017-12-01 17:10:33 -0800575 actualIds := []string{ns1.id, ns2.id, ns3.id}
576 expectedIds := []string{"1", "2", "3"}
577 if !reflect.DeepEqual(actualIds, expectedIds) {
578 t.Errorf("Incorrect namespace ids.\nactual: %s\nexpected: %s\n", actualIds, expectedIds)
579 }
580}
581
Colin Crosseafb10c2018-04-16 13:58:10 -0700582// so that the generated .ninja file will have consistent names
583func TestRename(t *testing.T) {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100584 GroupFixturePreparers(
585 prepareForTestWithNamespace,
586 dirBpToPreparer(map[string]string{
Colin Crosseafb10c2018-04-16 13:58:10 -0700587 "dir1": `
Paul Duffin0fc6d322021-07-06 22:36:33 +0100588 soong_namespace {
589 }
590 test_module {
591 name: "a",
592 deps: ["c"],
593 }
594 test_module {
595 name: "b",
596 rename: "c",
597 }
598 `,
599 }),
600 ).RunTest(t)
601
602 // RunTest will report any errors
Colin Crosseafb10c2018-04-16 13:58:10 -0700603}
604
Jeff Gaston088e29e2017-11-29 16:47:17 -0800605// some utils to support the tests
606
Paul Duffin0fc6d322021-07-06 22:36:33 +0100607var prepareForTestWithNamespace = GroupFixturePreparers(
608 FixtureRegisterWithContext(registerNamespaceBuildComponents),
609 FixtureRegisterWithContext(func(ctx RegistrationContext) {
610 ctx.PreArchMutators(RegisterNamespaceMutator)
611 }),
612 FixtureModifyContext(func(ctx *TestContext) {
613 ctx.RegisterModuleType("test_module", newTestModule)
614 ctx.Context.RegisterModuleType("blueprint_test_module", newBlueprintTestModule)
615 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
616 ctx.BottomUp("rename", renameMutator)
617 })
618 }),
619)
620
621// dirBpToPreparer takes a map from directory to the contents of the Android.bp file and produces a
622// FixturePreparer.
623func dirBpToPreparer(bps map[string]string) FixturePreparer {
624 files := make(MockFS, len(bps))
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800625 files["Android.bp"] = []byte("")
Jeff Gaston088e29e2017-11-29 16:47:17 -0800626 for dir, text := range bps {
Jeff Gaston5c3886d2017-11-30 16:46:47 -0800627 files[filepath.Join(dir, "Android.bp")] = []byte(text)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800628 }
Paul Duffin0fc6d322021-07-06 22:36:33 +0100629 return files.AddToFixture()
Jeff Gaston088e29e2017-11-29 16:47:17 -0800630}
631
Paul Duffin0fc6d322021-07-06 22:36:33 +0100632func dependsOn(result *TestResult, module TestingModule, possibleDependency TestingModule) bool {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800633 depends := false
634 visit := func(dependency blueprint.Module) {
635 if dependency == possibleDependency.module {
636 depends = true
637 }
638 }
Paul Duffin0fc6d322021-07-06 22:36:33 +0100639 result.VisitDirectDeps(module.module, visit)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800640 return depends
641}
642
Paul Duffin0fc6d322021-07-06 22:36:33 +0100643func numDeps(result *TestResult, module TestingModule) int {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800644 count := 0
645 visit := func(dependency blueprint.Module) {
646 count++
647 }
Paul Duffin0fc6d322021-07-06 22:36:33 +0100648 result.VisitDirectDeps(module.module, visit)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800649 return count
650}
651
Paul Duffin0fc6d322021-07-06 22:36:33 +0100652func getModule(result *TestResult, moduleName string) TestingModule {
653 return result.ModuleForTests(moduleName, "")
Jeff Gaston088e29e2017-11-29 16:47:17 -0800654}
655
Paul Duffin0fc6d322021-07-06 22:36:33 +0100656func findModuleById(result *TestResult, id string) (module TestingModule) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800657 visit := func(candidate blueprint.Module) {
658 testModule, ok := candidate.(*testModule)
659 if ok {
660 if testModule.properties.Id == id {
Paul Duffin0fc6d322021-07-06 22:36:33 +0100661 module = newTestingModule(result.config, testModule)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800662 }
663 }
664 }
Paul Duffin0fc6d322021-07-06 22:36:33 +0100665 result.VisitAllModules(visit)
Jeff Gaston088e29e2017-11-29 16:47:17 -0800666 return module
667}
668
669type testModule struct {
670 ModuleBase
671 properties struct {
Colin Crosseafb10c2018-04-16 13:58:10 -0700672 Rename string
673 Deps []string
674 Id string
Jeff Gaston088e29e2017-11-29 16:47:17 -0800675 }
676}
677
678func (m *testModule) DepsMutator(ctx BottomUpMutatorContext) {
Colin Crosseafb10c2018-04-16 13:58:10 -0700679 if m.properties.Rename != "" {
680 ctx.Rename(m.properties.Rename)
681 }
Jeff Gaston088e29e2017-11-29 16:47:17 -0800682 for _, d := range m.properties.Deps {
683 ctx.AddDependency(ctx.Module(), nil, d)
684 }
685}
686
687func (m *testModule) GenerateAndroidBuildActions(ModuleContext) {
688}
689
Colin Crosseafb10c2018-04-16 13:58:10 -0700690func renameMutator(ctx BottomUpMutatorContext) {
691 if m, ok := ctx.Module().(*testModule); ok {
692 if m.properties.Rename != "" {
693 ctx.Rename(m.properties.Rename)
694 }
695 }
696}
697
Jeff Gaston088e29e2017-11-29 16:47:17 -0800698func newTestModule() Module {
699 m := &testModule{}
700 m.AddProperties(&m.properties)
701 InitAndroidModule(m)
702 return m
703}
Colin Crosscd84b4e2019-06-14 11:26:09 -0700704
705type blueprintTestModule struct {
706 blueprint.SimpleName
707 properties struct {
708 Deps []string
709 }
710}
711
Paul Duffin0fc6d322021-07-06 22:36:33 +0100712func (b *blueprintTestModule) DynamicDependencies(_ blueprint.DynamicDependerModuleContext) []string {
Colin Crosscd84b4e2019-06-14 11:26:09 -0700713 return b.properties.Deps
714}
715
716func (b *blueprintTestModule) GenerateBuildActions(blueprint.ModuleContext) {
717}
718
719func newBlueprintTestModule() (blueprint.Module, []interface{}) {
720 m := &blueprintTestModule{}
721 return m, []interface{}{&m.properties, &m.SimpleName.Properties}
722}