Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 19 | "path/filepath" |
Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 20 | "reflect" |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 21 | "testing" |
| 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | ) |
| 25 | |
| 26 | func TestDependingOnModuleInSameNamespace(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 27 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 28 | 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 | |
| 51 | func TestDependingOnModuleInRootNamespace(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 52 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 53 | 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 | |
| 74 | func TestImplicitlyImportRootNamespace(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 75 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 76 | _ = 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 Cross | cd84b4e | 2019-06-14 11:26:09 -0700 | [diff] [blame] | 97 | func TestDependingOnBlueprintModuleInRootNamespace(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 98 | t.Parallel() |
Colin Cross | cd84b4e | 2019-06-14 11:26:09 -0700 | [diff] [blame] | 99 | _ = 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 Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 120 | func TestDependingOnModuleInImportedNamespace(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 121 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 122 | 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 | |
| 150 | func TestDependingOnModuleInNonImportedNamespace(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 151 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 152 | _, 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 Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 181 | `dir3/Android.bp:4:4: "b" depends on undefined module "a" |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 182 | Module "b" is defined in namespace "dir3" which can read these 2 namespaces: ["dir3" "."] |
| 183 | Module "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 | |
| 191 | func TestDependingOnModuleByFullyQualifiedReference(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 192 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 193 | 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 | |
| 219 | func TestSameNameInTwoNamespaces(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 220 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 221 | 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 | |
| 270 | func TestSearchOrder(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 271 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 272 | 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 | |
| 359 | func TestTwoNamespacesCanImportEachOther(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 360 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 361 | _ = 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 | |
| 390 | func TestImportingNonexistentNamespace(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 391 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 392 | _, 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 Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 408 | errors.New(`dir1/Android.bp:2:4: module "soong_namespace": namespace a_nonexistent_namespace does not exist`), |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 409 | } |
| 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 | |
| 415 | func TestNamespacesDontInheritParentNamespaces(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 416 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 417 | _, 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 Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 438 | errors.New(`dir1/subdir1/Android.bp:4:4: "b" depends on undefined module "a" |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 439 | Module "b" is defined in namespace "dir1/subdir1" which can read these 2 namespaces: ["dir1/subdir1" "."] |
| 440 | Module "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 | |
| 447 | func TestModulesDoReceiveParentNamespace(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 448 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 449 | _ = 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 | |
| 470 | func TestNamespaceImportsNotTransitive(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 471 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 472 | _, 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 Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 503 | errors.New(`dir3/Android.bp:5:4: "c" depends on undefined module "a" |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 504 | Module "c" is defined in namespace "dir3" which can read these 3 namespaces: ["dir3" "dir2" "."] |
| 505 | Module "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 | |
| 512 | func TestTwoNamepacesInSameDir(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 513 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 514 | _, errs := setupTestExpectErrs( |
| 515 | map[string]string{ |
| 516 | "dir1": ` |
| 517 | soong_namespace { |
| 518 | } |
| 519 | soong_namespace { |
| 520 | } |
| 521 | `, |
| 522 | }, |
| 523 | ) |
| 524 | |
| 525 | expectedErrors := []error{ |
Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 526 | errors.New(`dir1/Android.bp:4:4: namespace dir1 already exists`), |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 527 | } |
| 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 | |
| 533 | func TestNamespaceNotAtTopOfFile(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 534 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 535 | _, 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 Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 548 | errors.New(`dir1/Android.bp:5:4: a namespace must be the first module in the file`), |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 549 | } |
| 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 | |
| 555 | func TestTwoModulesWithSameNameInSameNamespace(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 556 | t.Parallel() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 557 | _, 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 Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 573 | errors.New(`dir1/Android.bp:7:4: module "a" already defined |
| 574 | dir1/Android.bp:4:4 <-- previous definition here`), |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 575 | } |
| 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 Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 581 | func TestDeclaringNamespaceInNonAndroidBpFile(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 582 | t.Parallel() |
Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 583 | _, 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 Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 604 | // so that the generated .ninja file will have consistent names |
| 605 | func TestConsistentNamespaceNames(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 606 | t.Parallel() |
Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 607 | 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 Cross | eafb10c | 2018-04-16 13:58:10 -0700 | [diff] [blame] | 624 | // so that the generated .ninja file will have consistent names |
| 625 | func TestRename(t *testing.T) { |
Colin Cross | 323dc60 | 2020-09-18 14:25:31 -0700 | [diff] [blame^] | 626 | t.Parallel() |
Colin Cross | eafb10c | 2018-04-16 13:58:10 -0700 | [diff] [blame] | 627 | _ = 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 Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 644 | // some utils to support the tests |
| 645 | |
| 646 | func mockFiles(bps map[string]string) (files map[string][]byte) { |
| 647 | files = make(map[string][]byte, len(bps)) |
Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 648 | files["Android.bp"] = []byte("") |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 649 | for dir, text := range bps { |
Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 650 | files[filepath.Join(dir, "Android.bp")] = []byte(text) |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 651 | } |
| 652 | return files |
| 653 | } |
| 654 | |
Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 655 | func setupTestFromFiles(bps map[string][]byte) (ctx *TestContext, errs []error) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 656 | config := TestConfig(buildDir, nil, "", bps) |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 657 | |
| 658 | ctx = NewTestContext() |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 659 | ctx.RegisterModuleType("test_module", newTestModule) |
| 660 | ctx.RegisterModuleType("soong_namespace", NamespaceFactory) |
| 661 | ctx.Context.RegisterModuleType("blueprint_test_module", newBlueprintTestModule) |
Dan Willemsen | 6e72ef7 | 2018-01-26 18:27:02 -0800 | [diff] [blame] | 662 | ctx.PreArchMutators(RegisterNamespaceMutator) |
Colin Cross | eafb10c | 2018-04-16 13:58:10 -0700 | [diff] [blame] | 663 | ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { |
| 664 | ctx.BottomUp("rename", renameMutator) |
| 665 | }) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 666 | ctx.Register(config) |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 667 | |
Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 668 | _, errs = ctx.ParseBlueprintsFiles("Android.bp") |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 669 | if len(errs) > 0 { |
| 670 | return ctx, errs |
| 671 | } |
| 672 | _, errs = ctx.PrepareBuildActions(config) |
| 673 | return ctx, errs |
| 674 | } |
| 675 | |
Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 676 | func 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 Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 685 | func setupTest(t *testing.T, bps map[string]string) (ctx *TestContext) { |
Colin Cross | cd84b4e | 2019-06-14 11:26:09 -0700 | [diff] [blame] | 686 | t.Helper() |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 687 | ctx, errs := setupTestExpectErrs(bps) |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 688 | FailIfErrored(t, errs) |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 689 | return ctx |
| 690 | } |
| 691 | |
| 692 | func 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 | |
| 703 | func 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 | |
| 712 | func getModule(ctx *TestContext, moduleName string) TestingModule { |
| 713 | return ctx.ModuleForTests(moduleName, "") |
| 714 | } |
| 715 | |
| 716 | func 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 | |
| 729 | type testModule struct { |
| 730 | ModuleBase |
| 731 | properties struct { |
Colin Cross | eafb10c | 2018-04-16 13:58:10 -0700 | [diff] [blame] | 732 | Rename string |
| 733 | Deps []string |
| 734 | Id string |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
| 738 | func (m *testModule) DepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | eafb10c | 2018-04-16 13:58:10 -0700 | [diff] [blame] | 739 | if m.properties.Rename != "" { |
| 740 | ctx.Rename(m.properties.Rename) |
| 741 | } |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 742 | for _, d := range m.properties.Deps { |
| 743 | ctx.AddDependency(ctx.Module(), nil, d) |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | func (m *testModule) GenerateAndroidBuildActions(ModuleContext) { |
| 748 | } |
| 749 | |
Colin Cross | eafb10c | 2018-04-16 13:58:10 -0700 | [diff] [blame] | 750 | func 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 Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 758 | func newTestModule() Module { |
| 759 | m := &testModule{} |
| 760 | m.AddProperties(&m.properties) |
| 761 | InitAndroidModule(m) |
| 762 | return m |
| 763 | } |
Colin Cross | cd84b4e | 2019-06-14 11:26:09 -0700 | [diff] [blame] | 764 | |
| 765 | type blueprintTestModule struct { |
| 766 | blueprint.SimpleName |
| 767 | properties struct { |
| 768 | Deps []string |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | func (b *blueprintTestModule) DynamicDependencies(ctx blueprint.DynamicDependerModuleContext) []string { |
| 773 | return b.properties.Deps |
| 774 | } |
| 775 | |
| 776 | func (b *blueprintTestModule) GenerateBuildActions(blueprint.ModuleContext) { |
| 777 | } |
| 778 | |
| 779 | func newBlueprintTestModule() (blueprint.Module, []interface{}) { |
| 780 | m := &blueprintTestModule{} |
| 781 | return m, []interface{}{&m.properties, &m.SimpleName.Properties} |
| 782 | } |