Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 1 | package android |
| 2 | |
| 3 | import ( |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 4 | "testing" |
Martin Stjernholm | 8edeb63 | 2019-05-21 12:18:38 +0100 | [diff] [blame] | 5 | |
| 6 | "github.com/google/blueprint" |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 7 | ) |
| 8 | |
| 9 | var visibilityTests = []struct { |
| 10 | name string |
| 11 | fs map[string][]byte |
| 12 | expectedErrors []string |
| 13 | }{ |
| 14 | { |
| 15 | name: "invalid visibility: empty list", |
| 16 | fs: map[string][]byte{ |
| 17 | "top/Blueprints": []byte(` |
| 18 | mock_library { |
| 19 | name: "libexample", |
| 20 | visibility: [], |
| 21 | }`), |
| 22 | }, |
| 23 | expectedErrors: []string{`visibility: must contain at least one visibility rule`}, |
| 24 | }, |
| 25 | { |
| 26 | name: "invalid visibility: empty rule", |
| 27 | fs: map[string][]byte{ |
| 28 | "top/Blueprints": []byte(` |
| 29 | mock_library { |
| 30 | name: "libexample", |
| 31 | visibility: [""], |
| 32 | }`), |
| 33 | }, |
| 34 | expectedErrors: []string{`visibility: invalid visibility pattern ""`}, |
| 35 | }, |
| 36 | { |
| 37 | name: "invalid visibility: unqualified", |
| 38 | fs: map[string][]byte{ |
| 39 | "top/Blueprints": []byte(` |
| 40 | mock_library { |
| 41 | name: "libexample", |
| 42 | visibility: ["target"], |
| 43 | }`), |
| 44 | }, |
| 45 | expectedErrors: []string{`visibility: invalid visibility pattern "target"`}, |
| 46 | }, |
| 47 | { |
| 48 | name: "invalid visibility: empty namespace", |
| 49 | fs: map[string][]byte{ |
| 50 | "top/Blueprints": []byte(` |
| 51 | mock_library { |
| 52 | name: "libexample", |
| 53 | visibility: ["//"], |
| 54 | }`), |
| 55 | }, |
| 56 | expectedErrors: []string{`visibility: invalid visibility pattern "//"`}, |
| 57 | }, |
| 58 | { |
| 59 | name: "invalid visibility: empty module", |
| 60 | fs: map[string][]byte{ |
| 61 | "top/Blueprints": []byte(` |
| 62 | mock_library { |
| 63 | name: "libexample", |
| 64 | visibility: [":"], |
| 65 | }`), |
| 66 | }, |
| 67 | expectedErrors: []string{`visibility: invalid visibility pattern ":"`}, |
| 68 | }, |
| 69 | { |
| 70 | name: "invalid visibility: empty namespace and module", |
| 71 | fs: map[string][]byte{ |
| 72 | "top/Blueprints": []byte(` |
| 73 | mock_library { |
| 74 | name: "libexample", |
| 75 | visibility: ["//:"], |
| 76 | }`), |
| 77 | }, |
| 78 | expectedErrors: []string{`visibility: invalid visibility pattern "//:"`}, |
| 79 | }, |
| 80 | { |
| 81 | name: "//visibility:unknown", |
| 82 | fs: map[string][]byte{ |
| 83 | "top/Blueprints": []byte(` |
| 84 | mock_library { |
| 85 | name: "libexample", |
| 86 | visibility: ["//visibility:unknown"], |
| 87 | }`), |
| 88 | }, |
| 89 | expectedErrors: []string{`unrecognized visibility rule "//visibility:unknown"`}, |
| 90 | }, |
| 91 | { |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 92 | name: "//visibility:xxx mixed", |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 93 | fs: map[string][]byte{ |
| 94 | "top/Blueprints": []byte(` |
| 95 | mock_library { |
| 96 | name: "libexample", |
| 97 | visibility: ["//visibility:public", "//namespace"], |
| 98 | } |
| 99 | |
| 100 | mock_library { |
| 101 | name: "libother", |
| 102 | visibility: ["//visibility:private", "//namespace"], |
| 103 | }`), |
| 104 | }, |
| 105 | expectedErrors: []string{ |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 106 | `module "libother": visibility: cannot mix "//visibility:private"` + |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 107 | ` with any other visibility rules`, |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 108 | `module "libexample": visibility: cannot mix "//visibility:public"` + |
| 109 | ` with any other visibility rules`, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 110 | }, |
| 111 | }, |
| 112 | { |
| 113 | name: "//visibility:legacy_public", |
| 114 | fs: map[string][]byte{ |
| 115 | "top/Blueprints": []byte(` |
| 116 | mock_library { |
| 117 | name: "libexample", |
| 118 | visibility: ["//visibility:legacy_public"], |
| 119 | }`), |
| 120 | }, |
| 121 | expectedErrors: []string{ |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 122 | `module "libexample": visibility: //visibility:legacy_public must` + |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 123 | ` not be used`, |
| 124 | }, |
| 125 | }, |
| 126 | { |
| 127 | // Verify that //visibility:public will allow the module to be referenced from anywhere, e.g. |
| 128 | // the current directory, a nested directory and a directory in a separate tree. |
| 129 | name: "//visibility:public", |
| 130 | fs: map[string][]byte{ |
| 131 | "top/Blueprints": []byte(` |
| 132 | mock_library { |
| 133 | name: "libexample", |
| 134 | visibility: ["//visibility:public"], |
| 135 | } |
| 136 | |
| 137 | mock_library { |
| 138 | name: "libsamepackage", |
| 139 | deps: ["libexample"], |
| 140 | }`), |
| 141 | "top/nested/Blueprints": []byte(` |
| 142 | mock_library { |
| 143 | name: "libnested", |
| 144 | deps: ["libexample"], |
| 145 | }`), |
| 146 | "other/Blueprints": []byte(` |
| 147 | mock_library { |
| 148 | name: "libother", |
| 149 | deps: ["libexample"], |
| 150 | }`), |
| 151 | }, |
| 152 | }, |
| 153 | { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 154 | // Verify that //visibility:private allows the module to be referenced from the current |
| 155 | // directory only. |
| 156 | name: "//visibility:private", |
| 157 | fs: map[string][]byte{ |
| 158 | "top/Blueprints": []byte(` |
| 159 | mock_library { |
| 160 | name: "libexample", |
| 161 | visibility: ["//visibility:private"], |
| 162 | } |
| 163 | |
| 164 | mock_library { |
| 165 | name: "libsamepackage", |
| 166 | deps: ["libexample"], |
| 167 | }`), |
| 168 | "top/nested/Blueprints": []byte(` |
| 169 | mock_library { |
| 170 | name: "libnested", |
| 171 | deps: ["libexample"], |
| 172 | }`), |
Martin Stjernholm | 8edeb63 | 2019-05-21 12:18:38 +0100 | [diff] [blame] | 173 | "other/Blueprints": []byte(` |
| 174 | mock_library { |
| 175 | name: "libother", |
| 176 | deps: ["libexample"], |
| 177 | }`), |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 178 | }, |
| 179 | expectedErrors: []string{ |
| 180 | `module "libnested" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 181 | ` visible to this module`, |
Martin Stjernholm | 8edeb63 | 2019-05-21 12:18:38 +0100 | [diff] [blame] | 182 | `module "libother" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 183 | ` visible to this module`, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 184 | }, |
| 185 | }, |
| 186 | { |
| 187 | // Verify that :__pkg__ allows the module to be referenced from the current directory only. |
| 188 | name: ":__pkg__", |
| 189 | fs: map[string][]byte{ |
| 190 | "top/Blueprints": []byte(` |
| 191 | mock_library { |
| 192 | name: "libexample", |
| 193 | visibility: [":__pkg__"], |
| 194 | } |
| 195 | |
| 196 | mock_library { |
| 197 | name: "libsamepackage", |
| 198 | deps: ["libexample"], |
| 199 | }`), |
| 200 | "top/nested/Blueprints": []byte(` |
| 201 | mock_library { |
| 202 | name: "libnested", |
| 203 | deps: ["libexample"], |
| 204 | }`), |
Martin Stjernholm | 8edeb63 | 2019-05-21 12:18:38 +0100 | [diff] [blame] | 205 | "other/Blueprints": []byte(` |
| 206 | mock_library { |
| 207 | name: "libother", |
| 208 | deps: ["libexample"], |
| 209 | }`), |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 210 | }, |
| 211 | expectedErrors: []string{ |
| 212 | `module "libnested" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 213 | ` visible to this module`, |
Martin Stjernholm | 8edeb63 | 2019-05-21 12:18:38 +0100 | [diff] [blame] | 214 | `module "libother" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 215 | ` visible to this module`, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 216 | }, |
| 217 | }, |
| 218 | { |
| 219 | // Verify that //top/nested allows the module to be referenced from the current directory and |
| 220 | // the top/nested directory only, not a subdirectory of top/nested and not peak directory. |
| 221 | name: "//top/nested", |
| 222 | fs: map[string][]byte{ |
| 223 | "top/Blueprints": []byte(` |
| 224 | mock_library { |
| 225 | name: "libexample", |
| 226 | visibility: ["//top/nested"], |
| 227 | } |
| 228 | |
| 229 | mock_library { |
| 230 | name: "libsamepackage", |
| 231 | deps: ["libexample"], |
| 232 | }`), |
| 233 | "top/nested/Blueprints": []byte(` |
| 234 | mock_library { |
| 235 | name: "libnested", |
| 236 | deps: ["libexample"], |
| 237 | }`), |
| 238 | "top/nested/again/Blueprints": []byte(` |
| 239 | mock_library { |
| 240 | name: "libnestedagain", |
| 241 | deps: ["libexample"], |
| 242 | }`), |
| 243 | "peak/Blueprints": []byte(` |
| 244 | mock_library { |
| 245 | name: "libother", |
| 246 | deps: ["libexample"], |
| 247 | }`), |
| 248 | }, |
| 249 | expectedErrors: []string{ |
| 250 | `module "libother" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 251 | ` visible to this module`, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 252 | `module "libnestedagain" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 253 | ` visible to this module`, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 254 | }, |
| 255 | }, |
| 256 | { |
| 257 | // Verify that :__subpackages__ allows the module to be referenced from the current directory |
| 258 | // and sub directories but nowhere else. |
| 259 | name: ":__subpackages__", |
| 260 | fs: map[string][]byte{ |
| 261 | "top/Blueprints": []byte(` |
| 262 | mock_library { |
| 263 | name: "libexample", |
| 264 | visibility: [":__subpackages__"], |
| 265 | } |
| 266 | |
| 267 | mock_library { |
| 268 | name: "libsamepackage", |
| 269 | deps: ["libexample"], |
| 270 | }`), |
| 271 | "top/nested/Blueprints": []byte(` |
| 272 | mock_library { |
| 273 | name: "libnested", |
| 274 | deps: ["libexample"], |
| 275 | }`), |
| 276 | "peak/other/Blueprints": []byte(` |
| 277 | mock_library { |
| 278 | name: "libother", |
| 279 | deps: ["libexample"], |
| 280 | }`), |
| 281 | }, |
| 282 | expectedErrors: []string{ |
| 283 | `module "libother" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 284 | ` visible to this module`, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 285 | }, |
| 286 | }, |
| 287 | { |
| 288 | // Verify that //top/nested:__subpackages__ allows the module to be referenced from the current |
| 289 | // directory and sub directories but nowhere else. |
| 290 | name: "//top/nested:__subpackages__", |
| 291 | fs: map[string][]byte{ |
| 292 | "top/Blueprints": []byte(` |
| 293 | mock_library { |
| 294 | name: "libexample", |
| 295 | visibility: ["//top/nested:__subpackages__", "//other"], |
| 296 | } |
| 297 | |
| 298 | mock_library { |
| 299 | name: "libsamepackage", |
| 300 | deps: ["libexample"], |
| 301 | }`), |
| 302 | "top/nested/Blueprints": []byte(` |
| 303 | mock_library { |
| 304 | name: "libnested", |
| 305 | deps: ["libexample"], |
| 306 | }`), |
| 307 | "top/other/Blueprints": []byte(` |
| 308 | mock_library { |
| 309 | name: "libother", |
| 310 | deps: ["libexample"], |
| 311 | }`), |
| 312 | }, |
| 313 | expectedErrors: []string{ |
| 314 | `module "libother" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 315 | ` visible to this module`, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 316 | }, |
| 317 | }, |
| 318 | { |
| 319 | // Verify that ["//top/nested", "//peak:__subpackages"] allows the module to be referenced from |
| 320 | // the current directory, top/nested and peak and all its subpackages. |
| 321 | name: `["//top/nested", "//peak:__subpackages__"]`, |
| 322 | fs: map[string][]byte{ |
| 323 | "top/Blueprints": []byte(` |
| 324 | mock_library { |
| 325 | name: "libexample", |
| 326 | visibility: ["//top/nested", "//peak:__subpackages__"], |
| 327 | } |
| 328 | |
| 329 | mock_library { |
| 330 | name: "libsamepackage", |
| 331 | deps: ["libexample"], |
| 332 | }`), |
| 333 | "top/nested/Blueprints": []byte(` |
| 334 | mock_library { |
| 335 | name: "libnested", |
| 336 | deps: ["libexample"], |
| 337 | }`), |
| 338 | "peak/other/Blueprints": []byte(` |
| 339 | mock_library { |
| 340 | name: "libother", |
| 341 | deps: ["libexample"], |
| 342 | }`), |
| 343 | }, |
| 344 | }, |
| 345 | { |
| 346 | // Verify that //vendor... cannot be used outside vendor apart from //vendor:__subpackages__ |
| 347 | name: `//vendor`, |
| 348 | fs: map[string][]byte{ |
| 349 | "top/Blueprints": []byte(` |
| 350 | mock_library { |
| 351 | name: "libexample", |
| 352 | visibility: ["//vendor:__subpackages__"], |
| 353 | } |
| 354 | |
| 355 | mock_library { |
| 356 | name: "libsamepackage", |
| 357 | visibility: ["//vendor/apps/AcmeSettings"], |
| 358 | }`), |
| 359 | "vendor/Blueprints": []byte(` |
| 360 | mock_library { |
| 361 | name: "libvendorexample", |
| 362 | deps: ["libexample"], |
| 363 | visibility: ["//vendor/nested"], |
| 364 | }`), |
| 365 | "vendor/nested/Blueprints": []byte(` |
| 366 | mock_library { |
| 367 | name: "libvendornested", |
| 368 | deps: ["libexample", "libvendorexample"], |
| 369 | }`), |
| 370 | }, |
| 371 | expectedErrors: []string{ |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 372 | `module "libsamepackage": visibility: "//vendor/apps/AcmeSettings"` + |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 373 | ` is not allowed. Packages outside //vendor cannot make themselves visible to specific` + |
| 374 | ` targets within //vendor, they can only use //vendor:__subpackages__.`, |
| 375 | }, |
| 376 | }, |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 377 | |
| 378 | // Defaults propagation tests |
| 379 | { |
| 380 | // Check that visibility is the union of the defaults modules. |
| 381 | name: "defaults union, basic", |
| 382 | fs: map[string][]byte{ |
| 383 | "top/Blueprints": []byte(` |
| 384 | mock_defaults { |
| 385 | name: "libexample_defaults", |
| 386 | visibility: ["//other"], |
| 387 | } |
| 388 | mock_library { |
| 389 | name: "libexample", |
| 390 | visibility: ["//top/nested"], |
| 391 | defaults: ["libexample_defaults"], |
| 392 | } |
| 393 | mock_library { |
| 394 | name: "libsamepackage", |
| 395 | deps: ["libexample"], |
| 396 | }`), |
| 397 | "top/nested/Blueprints": []byte(` |
| 398 | mock_library { |
| 399 | name: "libnested", |
| 400 | deps: ["libexample"], |
| 401 | }`), |
| 402 | "other/Blueprints": []byte(` |
| 403 | mock_library { |
| 404 | name: "libother", |
| 405 | deps: ["libexample"], |
| 406 | }`), |
| 407 | "outsider/Blueprints": []byte(` |
| 408 | mock_library { |
| 409 | name: "liboutsider", |
| 410 | deps: ["libexample"], |
| 411 | }`), |
| 412 | }, |
| 413 | expectedErrors: []string{ |
| 414 | `module "liboutsider" variant "android_common": depends on //top:libexample which is not` + |
| 415 | ` visible to this module`, |
| 416 | }, |
| 417 | }, |
| 418 | { |
| 419 | name: "defaults union, multiple defaults", |
| 420 | fs: map[string][]byte{ |
| 421 | "top/Blueprints": []byte(` |
| 422 | mock_defaults { |
| 423 | name: "libexample_defaults_1", |
| 424 | visibility: ["//other"], |
| 425 | } |
| 426 | mock_defaults { |
| 427 | name: "libexample_defaults_2", |
| 428 | visibility: ["//top/nested"], |
| 429 | } |
| 430 | mock_library { |
| 431 | name: "libexample", |
| 432 | defaults: ["libexample_defaults_1", "libexample_defaults_2"], |
| 433 | } |
| 434 | mock_library { |
| 435 | name: "libsamepackage", |
| 436 | deps: ["libexample"], |
| 437 | }`), |
| 438 | "top/nested/Blueprints": []byte(` |
| 439 | mock_library { |
| 440 | name: "libnested", |
| 441 | deps: ["libexample"], |
| 442 | }`), |
| 443 | "other/Blueprints": []byte(` |
| 444 | mock_library { |
| 445 | name: "libother", |
| 446 | deps: ["libexample"], |
| 447 | }`), |
| 448 | "outsider/Blueprints": []byte(` |
| 449 | mock_library { |
| 450 | name: "liboutsider", |
| 451 | deps: ["libexample"], |
| 452 | }`), |
| 453 | }, |
| 454 | expectedErrors: []string{ |
| 455 | `module "liboutsider" variant "android_common": depends on //top:libexample which is not` + |
| 456 | ` visible to this module`, |
| 457 | }, |
| 458 | }, |
| 459 | { |
| 460 | name: "//visibility:public mixed with other in defaults", |
| 461 | fs: map[string][]byte{ |
| 462 | "top/Blueprints": []byte(` |
| 463 | mock_defaults { |
| 464 | name: "libexample_defaults", |
| 465 | visibility: ["//visibility:public", "//namespace"], |
| 466 | } |
| 467 | mock_library { |
| 468 | name: "libexample", |
| 469 | defaults: ["libexample_defaults"], |
| 470 | }`), |
| 471 | }, |
| 472 | expectedErrors: []string{ |
| 473 | `module "libexample_defaults": visibility: cannot mix "//visibility:public"` + |
| 474 | ` with any other visibility rules`, |
| 475 | }, |
| 476 | }, |
| 477 | { |
| 478 | name: "//visibility:public overriding defaults", |
| 479 | fs: map[string][]byte{ |
| 480 | "top/Blueprints": []byte(` |
| 481 | mock_defaults { |
| 482 | name: "libexample_defaults", |
| 483 | visibility: ["//namespace"], |
| 484 | } |
| 485 | mock_library { |
| 486 | name: "libexample", |
| 487 | visibility: ["//visibility:public"], |
| 488 | defaults: ["libexample_defaults"], |
| 489 | }`), |
| 490 | "outsider/Blueprints": []byte(` |
| 491 | mock_library { |
| 492 | name: "liboutsider", |
| 493 | deps: ["libexample"], |
| 494 | }`), |
| 495 | }, |
| 496 | }, |
| 497 | { |
| 498 | name: "//visibility:public mixed with other from different defaults 1", |
| 499 | fs: map[string][]byte{ |
| 500 | "top/Blueprints": []byte(` |
| 501 | mock_defaults { |
| 502 | name: "libexample_defaults_1", |
| 503 | visibility: ["//namespace"], |
| 504 | } |
| 505 | mock_defaults { |
| 506 | name: "libexample_defaults_2", |
| 507 | visibility: ["//visibility:public"], |
| 508 | } |
| 509 | mock_library { |
| 510 | name: "libexample", |
| 511 | defaults: ["libexample_defaults_1", "libexample_defaults_2"], |
| 512 | }`), |
| 513 | "outsider/Blueprints": []byte(` |
| 514 | mock_library { |
| 515 | name: "liboutsider", |
| 516 | deps: ["libexample"], |
| 517 | }`), |
| 518 | }, |
| 519 | }, |
| 520 | { |
| 521 | name: "//visibility:public mixed with other from different defaults 2", |
| 522 | fs: map[string][]byte{ |
| 523 | "top/Blueprints": []byte(` |
| 524 | mock_defaults { |
| 525 | name: "libexample_defaults_1", |
| 526 | visibility: ["//visibility:public"], |
| 527 | } |
| 528 | mock_defaults { |
| 529 | name: "libexample_defaults_2", |
| 530 | visibility: ["//namespace"], |
| 531 | } |
| 532 | mock_library { |
| 533 | name: "libexample", |
| 534 | defaults: ["libexample_defaults_1", "libexample_defaults_2"], |
| 535 | }`), |
| 536 | "outsider/Blueprints": []byte(` |
| 537 | mock_library { |
| 538 | name: "liboutsider", |
| 539 | deps: ["libexample"], |
| 540 | }`), |
| 541 | }, |
| 542 | }, |
| 543 | { |
| 544 | name: "//visibility:private in defaults", |
| 545 | fs: map[string][]byte{ |
| 546 | "top/Blueprints": []byte(` |
| 547 | mock_defaults { |
| 548 | name: "libexample_defaults", |
| 549 | visibility: ["//visibility:private"], |
| 550 | } |
| 551 | mock_library { |
| 552 | name: "libexample", |
| 553 | defaults: ["libexample_defaults"], |
| 554 | } |
| 555 | mock_library { |
| 556 | name: "libsamepackage", |
| 557 | deps: ["libexample"], |
| 558 | }`), |
| 559 | "top/nested/Blueprints": []byte(` |
| 560 | mock_library { |
| 561 | name: "libnested", |
| 562 | deps: ["libexample"], |
| 563 | }`), |
| 564 | "other/Blueprints": []byte(` |
| 565 | mock_library { |
| 566 | name: "libother", |
| 567 | deps: ["libexample"], |
| 568 | }`), |
| 569 | }, |
| 570 | expectedErrors: []string{ |
| 571 | `module "libnested" variant "android_common": depends on //top:libexample which is not` + |
| 572 | ` visible to this module`, |
| 573 | `module "libother" variant "android_common": depends on //top:libexample which is not` + |
| 574 | ` visible to this module`, |
| 575 | }, |
| 576 | }, |
| 577 | { |
| 578 | name: "//visibility:private mixed with other in defaults", |
| 579 | fs: map[string][]byte{ |
| 580 | "top/Blueprints": []byte(` |
| 581 | mock_defaults { |
| 582 | name: "libexample_defaults", |
| 583 | visibility: ["//visibility:private", "//namespace"], |
| 584 | } |
| 585 | mock_library { |
| 586 | name: "libexample", |
| 587 | defaults: ["libexample_defaults"], |
| 588 | }`), |
| 589 | }, |
| 590 | expectedErrors: []string{ |
| 591 | `module "libexample_defaults": visibility: cannot mix "//visibility:private"` + |
| 592 | ` with any other visibility rules`, |
| 593 | }, |
| 594 | }, |
| 595 | { |
| 596 | name: "//visibility:private overriding defaults", |
| 597 | fs: map[string][]byte{ |
| 598 | "top/Blueprints": []byte(` |
| 599 | mock_defaults { |
| 600 | name: "libexample_defaults", |
| 601 | visibility: ["//namespace"], |
| 602 | } |
| 603 | mock_library { |
| 604 | name: "libexample", |
| 605 | visibility: ["//visibility:private"], |
| 606 | defaults: ["libexample_defaults"], |
| 607 | }`), |
| 608 | }, |
| 609 | expectedErrors: []string{ |
| 610 | `module "libexample": visibility: cannot mix "//visibility:private"` + |
| 611 | ` with any other visibility rules`, |
| 612 | }, |
| 613 | }, |
| 614 | { |
| 615 | name: "//visibility:private in defaults overridden", |
| 616 | fs: map[string][]byte{ |
| 617 | "top/Blueprints": []byte(` |
| 618 | mock_defaults { |
| 619 | name: "libexample_defaults", |
| 620 | visibility: ["//visibility:private"], |
| 621 | } |
| 622 | mock_library { |
| 623 | name: "libexample", |
| 624 | visibility: ["//namespace"], |
| 625 | defaults: ["libexample_defaults"], |
| 626 | }`), |
| 627 | }, |
| 628 | expectedErrors: []string{ |
| 629 | `module "libexample": visibility: cannot mix "//visibility:private"` + |
| 630 | ` with any other visibility rules`, |
| 631 | }, |
| 632 | }, |
| 633 | { |
| 634 | name: "//visibility:private mixed with itself", |
| 635 | fs: map[string][]byte{ |
| 636 | "top/Blueprints": []byte(` |
| 637 | mock_defaults { |
| 638 | name: "libexample_defaults_1", |
| 639 | visibility: ["//visibility:private"], |
| 640 | } |
| 641 | mock_defaults { |
| 642 | name: "libexample_defaults_2", |
| 643 | visibility: ["//visibility:private"], |
| 644 | } |
| 645 | mock_library { |
| 646 | name: "libexample", |
| 647 | visibility: ["//visibility:private"], |
| 648 | defaults: ["libexample_defaults_1", "libexample_defaults_2"], |
| 649 | }`), |
| 650 | "outsider/Blueprints": []byte(` |
| 651 | mock_library { |
| 652 | name: "liboutsider", |
| 653 | deps: ["libexample"], |
| 654 | }`), |
| 655 | }, |
| 656 | expectedErrors: []string{ |
| 657 | `module "liboutsider" variant "android_common": depends on //top:libexample which is not` + |
| 658 | ` visible to this module`, |
| 659 | }, |
| 660 | }, |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame] | 661 | |
| 662 | // Defaults module's defaults_visibility tests |
| 663 | { |
| 664 | name: "defaults_visibility invalid", |
| 665 | fs: map[string][]byte{ |
| 666 | "top/Blueprints": []byte(` |
| 667 | mock_defaults { |
| 668 | name: "top_defaults", |
| 669 | defaults_visibility: ["//visibility:invalid"], |
| 670 | }`), |
| 671 | }, |
| 672 | expectedErrors: []string{ |
| 673 | `defaults_visibility: unrecognized visibility rule "//visibility:invalid"`, |
| 674 | }, |
| 675 | }, |
| 676 | { |
| 677 | name: "defaults_visibility overrides package default", |
| 678 | fs: map[string][]byte{ |
| 679 | "top/Blueprints": []byte(` |
| 680 | package { |
| 681 | default_visibility: ["//visibility:private"], |
| 682 | } |
| 683 | mock_defaults { |
| 684 | name: "top_defaults", |
| 685 | defaults_visibility: ["//visibility:public"], |
| 686 | }`), |
| 687 | "outsider/Blueprints": []byte(` |
| 688 | mock_library { |
| 689 | name: "liboutsider", |
| 690 | defaults: ["top_defaults"], |
| 691 | }`), |
| 692 | }, |
| 693 | }, |
| 694 | |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 695 | // Package default_visibility tests |
| 696 | { |
| 697 | name: "package default_visibility property is checked", |
| 698 | fs: map[string][]byte{ |
| 699 | "top/Blueprints": []byte(` |
| 700 | package { |
| 701 | default_visibility: ["//visibility:invalid"], |
| 702 | }`), |
| 703 | }, |
| 704 | expectedErrors: []string{`default_visibility: unrecognized visibility rule "//visibility:invalid"`}, |
| 705 | }, |
| 706 | { |
| 707 | // This test relies on the default visibility being legacy_public. |
| 708 | name: "package default_visibility property used when no visibility specified", |
| 709 | fs: map[string][]byte{ |
| 710 | "top/Blueprints": []byte(` |
| 711 | package { |
| 712 | default_visibility: ["//visibility:private"], |
| 713 | } |
| 714 | |
| 715 | mock_library { |
| 716 | name: "libexample", |
| 717 | }`), |
| 718 | "outsider/Blueprints": []byte(` |
| 719 | mock_library { |
| 720 | name: "liboutsider", |
| 721 | deps: ["libexample"], |
| 722 | }`), |
| 723 | }, |
| 724 | expectedErrors: []string{ |
| 725 | `module "liboutsider" variant "android_common": depends on //top:libexample which is not` + |
| 726 | ` visible to this module`, |
| 727 | }, |
| 728 | }, |
| 729 | { |
| 730 | name: "package default_visibility public does not override visibility private", |
| 731 | fs: map[string][]byte{ |
| 732 | "top/Blueprints": []byte(` |
| 733 | package { |
| 734 | default_visibility: ["//visibility:public"], |
| 735 | } |
| 736 | |
| 737 | mock_library { |
| 738 | name: "libexample", |
| 739 | visibility: ["//visibility:private"], |
| 740 | }`), |
| 741 | "outsider/Blueprints": []byte(` |
| 742 | mock_library { |
| 743 | name: "liboutsider", |
| 744 | deps: ["libexample"], |
| 745 | }`), |
| 746 | }, |
| 747 | expectedErrors: []string{ |
| 748 | `module "liboutsider" variant "android_common": depends on //top:libexample which is not` + |
| 749 | ` visible to this module`, |
| 750 | }, |
| 751 | }, |
| 752 | { |
| 753 | name: "package default_visibility private does not override visibility public", |
| 754 | fs: map[string][]byte{ |
| 755 | "top/Blueprints": []byte(` |
| 756 | package { |
| 757 | default_visibility: ["//visibility:private"], |
| 758 | } |
| 759 | |
| 760 | mock_library { |
| 761 | name: "libexample", |
| 762 | visibility: ["//visibility:public"], |
| 763 | }`), |
| 764 | "outsider/Blueprints": []byte(` |
| 765 | mock_library { |
| 766 | name: "liboutsider", |
| 767 | deps: ["libexample"], |
| 768 | }`), |
| 769 | }, |
| 770 | }, |
| 771 | { |
| 772 | name: "package default_visibility :__subpackages__", |
| 773 | fs: map[string][]byte{ |
| 774 | "top/Blueprints": []byte(` |
| 775 | package { |
| 776 | default_visibility: [":__subpackages__"], |
| 777 | } |
| 778 | |
| 779 | mock_library { |
| 780 | name: "libexample", |
| 781 | }`), |
| 782 | "top/nested/Blueprints": []byte(` |
| 783 | mock_library { |
| 784 | name: "libnested", |
| 785 | deps: ["libexample"], |
| 786 | }`), |
| 787 | "outsider/Blueprints": []byte(` |
| 788 | mock_library { |
| 789 | name: "liboutsider", |
| 790 | deps: ["libexample"], |
| 791 | }`), |
| 792 | }, |
| 793 | expectedErrors: []string{ |
| 794 | `module "liboutsider" variant "android_common": depends on //top:libexample which is not` + |
| 795 | ` visible to this module`, |
| 796 | }, |
| 797 | }, |
Paul Duffin | e484f47 | 2019-06-20 16:38:08 +0100 | [diff] [blame] | 798 | { |
| 799 | name: "package default_visibility inherited to subpackages", |
| 800 | fs: map[string][]byte{ |
| 801 | "top/Blueprints": []byte(` |
| 802 | package { |
| 803 | default_visibility: ["//outsider"], |
| 804 | } |
| 805 | |
| 806 | mock_library { |
| 807 | name: "libexample", |
| 808 | visibility: [":__subpackages__"], |
| 809 | }`), |
| 810 | "top/nested/Blueprints": []byte(` |
| 811 | mock_library { |
| 812 | name: "libnested", |
| 813 | deps: ["libexample"], |
| 814 | }`), |
| 815 | "outsider/Blueprints": []byte(` |
| 816 | mock_library { |
| 817 | name: "liboutsider", |
| 818 | deps: ["libexample", "libnested"], |
| 819 | }`), |
| 820 | }, |
| 821 | expectedErrors: []string{ |
| 822 | `module "liboutsider" variant "android_common": depends on //top:libexample which is not` + |
| 823 | ` visible to this module`, |
| 824 | }, |
| 825 | }, |
| 826 | { |
| 827 | name: "package default_visibility inherited to subpackages", |
| 828 | fs: map[string][]byte{ |
| 829 | "top/Blueprints": []byte(` |
| 830 | package { |
| 831 | default_visibility: ["//visibility:private"], |
| 832 | }`), |
| 833 | "top/nested/Blueprints": []byte(` |
| 834 | package { |
| 835 | default_visibility: ["//outsider"], |
| 836 | } |
| 837 | |
| 838 | mock_library { |
| 839 | name: "libnested", |
| 840 | }`), |
| 841 | "top/other/Blueprints": []byte(` |
| 842 | mock_library { |
| 843 | name: "libother", |
| 844 | }`), |
| 845 | "outsider/Blueprints": []byte(` |
| 846 | mock_library { |
| 847 | name: "liboutsider", |
| 848 | deps: ["libother", "libnested"], |
| 849 | }`), |
| 850 | }, |
| 851 | expectedErrors: []string{ |
| 852 | `module "liboutsider" variant "android_common": depends on //top/other:libother which is` + |
| 853 | ` not visible to this module`, |
| 854 | }, |
| 855 | }, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | func TestVisibility(t *testing.T) { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 859 | for _, test := range visibilityTests { |
| 860 | t.Run(test.name, func(t *testing.T) { |
| 861 | _, errs := testVisibility(buildDir, test.fs) |
| 862 | |
Paul Duffin | 91e3819 | 2019-08-05 15:07:57 +0100 | [diff] [blame] | 863 | CheckErrorsAgainstExpectations(t, errs, test.expectedErrors) |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 864 | }) |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | func testVisibility(buildDir string, fs map[string][]byte) (*TestContext, []error) { |
| 869 | |
| 870 | // Create a new config per test as visibility information is stored in the config. |
| 871 | config := TestArchConfig(buildDir, nil) |
| 872 | |
| 873 | ctx := NewTestArchContext() |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 874 | ctx.RegisterModuleType("package", PackageFactory) |
| 875 | ctx.RegisterModuleType("mock_library", newMockLibraryModule) |
| 876 | ctx.RegisterModuleType("mock_defaults", defaultsFactory) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame^] | 877 | ctx.PreArchMutators(RegisterPackageRenamer) |
| 878 | ctx.PreArchMutators(RegisterVisibilityRuleChecker) |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 879 | ctx.PreArchMutators(RegisterDefaultsPreArchMutators) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame^] | 880 | ctx.PreArchMutators(RegisterVisibilityRuleGatherer) |
| 881 | ctx.PostDepsMutators(RegisterVisibilityRuleEnforcer) |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 882 | ctx.Register() |
| 883 | |
| 884 | ctx.MockFileSystem(fs) |
| 885 | |
| 886 | _, errs := ctx.ParseBlueprintsFiles(".") |
| 887 | if len(errs) > 0 { |
| 888 | return ctx, errs |
| 889 | } |
| 890 | |
| 891 | _, errs = ctx.PrepareBuildActions(config) |
| 892 | return ctx, errs |
| 893 | } |
| 894 | |
| 895 | type mockLibraryProperties struct { |
| 896 | Deps []string |
| 897 | } |
| 898 | |
| 899 | type mockLibraryModule struct { |
| 900 | ModuleBase |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 901 | DefaultableModuleBase |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 902 | properties mockLibraryProperties |
| 903 | } |
| 904 | |
| 905 | func newMockLibraryModule() Module { |
| 906 | m := &mockLibraryModule{} |
| 907 | m.AddProperties(&m.properties) |
| 908 | InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon) |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 909 | InitDefaultableModule(m) |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 910 | return m |
| 911 | } |
| 912 | |
| 913 | type dependencyTag struct { |
| 914 | blueprint.BaseDependencyTag |
| 915 | name string |
| 916 | } |
| 917 | |
| 918 | func (j *mockLibraryModule) DepsMutator(ctx BottomUpMutatorContext) { |
| 919 | ctx.AddVariationDependencies(nil, dependencyTag{name: "mockdeps"}, j.properties.Deps...) |
| 920 | } |
| 921 | |
| 922 | func (p *mockLibraryModule) GenerateAndroidBuildActions(ModuleContext) { |
| 923 | } |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 924 | |
| 925 | type mockDefaults struct { |
| 926 | ModuleBase |
| 927 | DefaultsModuleBase |
| 928 | } |
| 929 | |
| 930 | func defaultsFactory() Module { |
| 931 | m := &mockDefaults{} |
| 932 | InitDefaultsModule(m) |
| 933 | return m |
| 934 | } |