visibility_test.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. package android
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/google/blueprint"
  6. )
  7. var visibilityTests = []struct {
  8. name string
  9. fs map[string][]byte
  10. expectedErrors []string
  11. effectiveVisibility map[qualifiedModuleName][]string
  12. }{
  13. {
  14. name: "invalid visibility: empty list",
  15. fs: map[string][]byte{
  16. "top/Blueprints": []byte(`
  17. mock_library {
  18. name: "libexample",
  19. visibility: [],
  20. }`),
  21. },
  22. expectedErrors: []string{`visibility: must contain at least one visibility rule`},
  23. },
  24. {
  25. name: "invalid visibility: empty rule",
  26. fs: map[string][]byte{
  27. "top/Blueprints": []byte(`
  28. mock_library {
  29. name: "libexample",
  30. visibility: [""],
  31. }`),
  32. },
  33. expectedErrors: []string{`visibility: invalid visibility pattern ""`},
  34. },
  35. {
  36. name: "invalid visibility: unqualified",
  37. fs: map[string][]byte{
  38. "top/Blueprints": []byte(`
  39. mock_library {
  40. name: "libexample",
  41. visibility: ["target"],
  42. }`),
  43. },
  44. expectedErrors: []string{`visibility: invalid visibility pattern "target"`},
  45. },
  46. {
  47. name: "invalid visibility: empty namespace",
  48. fs: map[string][]byte{
  49. "top/Blueprints": []byte(`
  50. mock_library {
  51. name: "libexample",
  52. visibility: ["//"],
  53. }`),
  54. },
  55. expectedErrors: []string{`visibility: invalid visibility pattern "//"`},
  56. },
  57. {
  58. name: "invalid visibility: empty module",
  59. fs: map[string][]byte{
  60. "top/Blueprints": []byte(`
  61. mock_library {
  62. name: "libexample",
  63. visibility: [":"],
  64. }`),
  65. },
  66. expectedErrors: []string{`visibility: invalid visibility pattern ":"`},
  67. },
  68. {
  69. name: "invalid visibility: empty namespace and module",
  70. fs: map[string][]byte{
  71. "top/Blueprints": []byte(`
  72. mock_library {
  73. name: "libexample",
  74. visibility: ["//:"],
  75. }`),
  76. },
  77. expectedErrors: []string{`visibility: invalid visibility pattern "//:"`},
  78. },
  79. {
  80. name: "//visibility:unknown",
  81. fs: map[string][]byte{
  82. "top/Blueprints": []byte(`
  83. mock_library {
  84. name: "libexample",
  85. visibility: ["//visibility:unknown"],
  86. }`),
  87. },
  88. expectedErrors: []string{`unrecognized visibility rule "//visibility:unknown"`},
  89. },
  90. {
  91. name: "//visibility:xxx mixed",
  92. fs: map[string][]byte{
  93. "top/Blueprints": []byte(`
  94. mock_library {
  95. name: "libexample",
  96. visibility: ["//visibility:public", "//namespace"],
  97. }
  98. mock_library {
  99. name: "libother",
  100. visibility: ["//visibility:private", "//namespace"],
  101. }`),
  102. },
  103. expectedErrors: []string{
  104. `module "libother": visibility: cannot mix "//visibility:private"` +
  105. ` with any other visibility rules`,
  106. `module "libexample": visibility: cannot mix "//visibility:public"` +
  107. ` with any other visibility rules`,
  108. },
  109. },
  110. {
  111. name: "//visibility:legacy_public",
  112. fs: map[string][]byte{
  113. "top/Blueprints": []byte(`
  114. mock_library {
  115. name: "libexample",
  116. visibility: ["//visibility:legacy_public"],
  117. }`),
  118. },
  119. expectedErrors: []string{
  120. `module "libexample": visibility: //visibility:legacy_public must` +
  121. ` not be used`,
  122. },
  123. },
  124. {
  125. // Verify that //visibility:public will allow the module to be referenced from anywhere, e.g.
  126. // the current directory, a nested directory and a directory in a separate tree.
  127. name: "//visibility:public",
  128. fs: map[string][]byte{
  129. "top/Blueprints": []byte(`
  130. mock_library {
  131. name: "libexample",
  132. visibility: ["//visibility:public"],
  133. }
  134. mock_library {
  135. name: "libsamepackage",
  136. deps: ["libexample"],
  137. }`),
  138. "top/nested/Blueprints": []byte(`
  139. mock_library {
  140. name: "libnested",
  141. deps: ["libexample"],
  142. }`),
  143. "other/Blueprints": []byte(`
  144. mock_library {
  145. name: "libother",
  146. deps: ["libexample"],
  147. }`),
  148. },
  149. },
  150. {
  151. // Verify that //visibility:private allows the module to be referenced from the current
  152. // directory only.
  153. name: "//visibility:private",
  154. fs: map[string][]byte{
  155. "top/Blueprints": []byte(`
  156. mock_library {
  157. name: "libexample",
  158. visibility: ["//visibility:private"],
  159. }
  160. mock_library {
  161. name: "libsamepackage",
  162. deps: ["libexample"],
  163. }`),
  164. "top/nested/Blueprints": []byte(`
  165. mock_library {
  166. name: "libnested",
  167. deps: ["libexample"],
  168. }`),
  169. "other/Blueprints": []byte(`
  170. mock_library {
  171. name: "libother",
  172. deps: ["libexample"],
  173. }`),
  174. },
  175. expectedErrors: []string{
  176. `module "libnested" variant "android_common": depends on //top:libexample which is not` +
  177. ` visible to this module`,
  178. `module "libother" variant "android_common": depends on //top:libexample which is not` +
  179. ` visible to this module`,
  180. },
  181. },
  182. {
  183. // Verify that :__pkg__ allows the module to be referenced from the current directory only.
  184. name: ":__pkg__",
  185. fs: map[string][]byte{
  186. "top/Blueprints": []byte(`
  187. mock_library {
  188. name: "libexample",
  189. visibility: [":__pkg__"],
  190. }
  191. mock_library {
  192. name: "libsamepackage",
  193. deps: ["libexample"],
  194. }`),
  195. "top/nested/Blueprints": []byte(`
  196. mock_library {
  197. name: "libnested",
  198. deps: ["libexample"],
  199. }`),
  200. "other/Blueprints": []byte(`
  201. mock_library {
  202. name: "libother",
  203. deps: ["libexample"],
  204. }`),
  205. },
  206. expectedErrors: []string{
  207. `module "libnested" variant "android_common": depends on //top:libexample which is not` +
  208. ` visible to this module`,
  209. `module "libother" variant "android_common": depends on //top:libexample which is not` +
  210. ` visible to this module`,
  211. },
  212. },
  213. {
  214. // Verify that //top/nested allows the module to be referenced from the current directory and
  215. // the top/nested directory only, not a subdirectory of top/nested and not peak directory.
  216. name: "//top/nested",
  217. fs: map[string][]byte{
  218. "top/Blueprints": []byte(`
  219. mock_library {
  220. name: "libexample",
  221. visibility: ["//top/nested"],
  222. }
  223. mock_library {
  224. name: "libsamepackage",
  225. deps: ["libexample"],
  226. }`),
  227. "top/nested/Blueprints": []byte(`
  228. mock_library {
  229. name: "libnested",
  230. deps: ["libexample"],
  231. }`),
  232. "top/nested/again/Blueprints": []byte(`
  233. mock_library {
  234. name: "libnestedagain",
  235. deps: ["libexample"],
  236. }`),
  237. "peak/Blueprints": []byte(`
  238. mock_library {
  239. name: "libother",
  240. deps: ["libexample"],
  241. }`),
  242. },
  243. expectedErrors: []string{
  244. `module "libother" variant "android_common": depends on //top:libexample which is not` +
  245. ` visible to this module`,
  246. `module "libnestedagain" variant "android_common": depends on //top:libexample which is not` +
  247. ` visible to this module`,
  248. },
  249. },
  250. {
  251. // Verify that :__subpackages__ allows the module to be referenced from the current directory
  252. // and sub directories but nowhere else.
  253. name: ":__subpackages__",
  254. fs: map[string][]byte{
  255. "top/Blueprints": []byte(`
  256. mock_library {
  257. name: "libexample",
  258. visibility: [":__subpackages__"],
  259. }
  260. mock_library {
  261. name: "libsamepackage",
  262. deps: ["libexample"],
  263. }`),
  264. "top/nested/Blueprints": []byte(`
  265. mock_library {
  266. name: "libnested",
  267. deps: ["libexample"],
  268. }`),
  269. "peak/other/Blueprints": []byte(`
  270. mock_library {
  271. name: "libother",
  272. deps: ["libexample"],
  273. }`),
  274. },
  275. expectedErrors: []string{
  276. `module "libother" variant "android_common": depends on //top:libexample which is not` +
  277. ` visible to this module`,
  278. },
  279. },
  280. {
  281. // Verify that //top/nested:__subpackages__ allows the module to be referenced from the current
  282. // directory and sub directories but nowhere else.
  283. name: "//top/nested:__subpackages__",
  284. fs: map[string][]byte{
  285. "top/Blueprints": []byte(`
  286. mock_library {
  287. name: "libexample",
  288. visibility: ["//top/nested:__subpackages__", "//other"],
  289. }
  290. mock_library {
  291. name: "libsamepackage",
  292. deps: ["libexample"],
  293. }`),
  294. "top/nested/Blueprints": []byte(`
  295. mock_library {
  296. name: "libnested",
  297. deps: ["libexample"],
  298. }`),
  299. "top/other/Blueprints": []byte(`
  300. mock_library {
  301. name: "libother",
  302. deps: ["libexample"],
  303. }`),
  304. },
  305. expectedErrors: []string{
  306. `module "libother" variant "android_common": depends on //top:libexample which is not` +
  307. ` visible to this module`,
  308. },
  309. },
  310. {
  311. // Verify that ["//top/nested", "//peak:__subpackages"] allows the module to be referenced from
  312. // the current directory, top/nested and peak and all its subpackages.
  313. name: `["//top/nested", "//peak:__subpackages__"]`,
  314. fs: map[string][]byte{
  315. "top/Blueprints": []byte(`
  316. mock_library {
  317. name: "libexample",
  318. visibility: ["//top/nested", "//peak:__subpackages__"],
  319. }
  320. mock_library {
  321. name: "libsamepackage",
  322. deps: ["libexample"],
  323. }`),
  324. "top/nested/Blueprints": []byte(`
  325. mock_library {
  326. name: "libnested",
  327. deps: ["libexample"],
  328. }`),
  329. "peak/other/Blueprints": []byte(`
  330. mock_library {
  331. name: "libother",
  332. deps: ["libexample"],
  333. }`),
  334. },
  335. },
  336. {
  337. // Verify that //vendor... cannot be used outside vendor apart from //vendor:__subpackages__
  338. name: `//vendor`,
  339. fs: map[string][]byte{
  340. "top/Blueprints": []byte(`
  341. mock_library {
  342. name: "libexample",
  343. visibility: ["//vendor:__subpackages__"],
  344. }
  345. mock_library {
  346. name: "libsamepackage",
  347. visibility: ["//vendor/apps/AcmeSettings"],
  348. }`),
  349. "vendor/Blueprints": []byte(`
  350. mock_library {
  351. name: "libvendorexample",
  352. deps: ["libexample"],
  353. visibility: ["//vendor/nested"],
  354. }`),
  355. "vendor/nested/Blueprints": []byte(`
  356. mock_library {
  357. name: "libvendornested",
  358. deps: ["libexample", "libvendorexample"],
  359. }`),
  360. },
  361. expectedErrors: []string{
  362. `module "libsamepackage": visibility: "//vendor/apps/AcmeSettings"` +
  363. ` is not allowed. Packages outside //vendor cannot make themselves visible to specific` +
  364. ` targets within //vendor, they can only use //vendor:__subpackages__.`,
  365. },
  366. },
  367. // Defaults propagation tests
  368. {
  369. // Check that visibility is the union of the defaults modules.
  370. name: "defaults union, basic",
  371. fs: map[string][]byte{
  372. "top/Blueprints": []byte(`
  373. mock_defaults {
  374. name: "libexample_defaults",
  375. visibility: ["//other"],
  376. }
  377. mock_library {
  378. name: "libexample",
  379. visibility: ["//top/nested"],
  380. defaults: ["libexample_defaults"],
  381. }
  382. mock_library {
  383. name: "libsamepackage",
  384. deps: ["libexample"],
  385. }`),
  386. "top/nested/Blueprints": []byte(`
  387. mock_library {
  388. name: "libnested",
  389. deps: ["libexample"],
  390. }`),
  391. "other/Blueprints": []byte(`
  392. mock_library {
  393. name: "libother",
  394. deps: ["libexample"],
  395. }`),
  396. "outsider/Blueprints": []byte(`
  397. mock_library {
  398. name: "liboutsider",
  399. deps: ["libexample"],
  400. }`),
  401. },
  402. expectedErrors: []string{
  403. `module "liboutsider" variant "android_common": depends on //top:libexample which is not` +
  404. ` visible to this module`,
  405. },
  406. },
  407. {
  408. name: "defaults union, multiple defaults",
  409. fs: map[string][]byte{
  410. "top/Blueprints": []byte(`
  411. mock_defaults {
  412. name: "libexample_defaults_1",
  413. visibility: ["//other"],
  414. }
  415. mock_defaults {
  416. name: "libexample_defaults_2",
  417. visibility: ["//top/nested"],
  418. }
  419. mock_library {
  420. name: "libexample",
  421. defaults: ["libexample_defaults_1", "libexample_defaults_2"],
  422. }
  423. mock_library {
  424. name: "libsamepackage",
  425. deps: ["libexample"],
  426. }`),
  427. "top/nested/Blueprints": []byte(`
  428. mock_library {
  429. name: "libnested",
  430. deps: ["libexample"],
  431. }`),
  432. "other/Blueprints": []byte(`
  433. mock_library {
  434. name: "libother",
  435. deps: ["libexample"],
  436. }`),
  437. "outsider/Blueprints": []byte(`
  438. mock_library {
  439. name: "liboutsider",
  440. deps: ["libexample"],
  441. }`),
  442. },
  443. expectedErrors: []string{
  444. `module "liboutsider" variant "android_common": depends on //top:libexample which is not` +
  445. ` visible to this module`,
  446. },
  447. },
  448. {
  449. name: "//visibility:public mixed with other in defaults",
  450. fs: map[string][]byte{
  451. "top/Blueprints": []byte(`
  452. mock_defaults {
  453. name: "libexample_defaults",
  454. visibility: ["//visibility:public", "//namespace"],
  455. }
  456. mock_library {
  457. name: "libexample",
  458. defaults: ["libexample_defaults"],
  459. }`),
  460. },
  461. expectedErrors: []string{
  462. `module "libexample_defaults": visibility: cannot mix "//visibility:public"` +
  463. ` with any other visibility rules`,
  464. },
  465. },
  466. {
  467. name: "//visibility:public overriding defaults",
  468. fs: map[string][]byte{
  469. "top/Blueprints": []byte(`
  470. mock_defaults {
  471. name: "libexample_defaults",
  472. visibility: ["//namespace"],
  473. }
  474. mock_library {
  475. name: "libexample",
  476. visibility: ["//visibility:public"],
  477. defaults: ["libexample_defaults"],
  478. }`),
  479. "outsider/Blueprints": []byte(`
  480. mock_library {
  481. name: "liboutsider",
  482. deps: ["libexample"],
  483. }`),
  484. },
  485. effectiveVisibility: map[qualifiedModuleName][]string{
  486. qualifiedModuleName{pkg: "top", name: "libexample"}: {"//visibility:public"},
  487. },
  488. },
  489. {
  490. name: "//visibility:public mixed with other from different defaults 1",
  491. fs: map[string][]byte{
  492. "top/Blueprints": []byte(`
  493. mock_defaults {
  494. name: "libexample_defaults_1",
  495. visibility: ["//namespace"],
  496. }
  497. mock_defaults {
  498. name: "libexample_defaults_2",
  499. visibility: ["//visibility:public"],
  500. }
  501. mock_library {
  502. name: "libexample",
  503. defaults: ["libexample_defaults_1", "libexample_defaults_2"],
  504. }`),
  505. "outsider/Blueprints": []byte(`
  506. mock_library {
  507. name: "liboutsider",
  508. deps: ["libexample"],
  509. }`),
  510. },
  511. },
  512. {
  513. name: "//visibility:public mixed with other from different defaults 2",
  514. fs: map[string][]byte{
  515. "top/Blueprints": []byte(`
  516. mock_defaults {
  517. name: "libexample_defaults_1",
  518. visibility: ["//visibility:public"],
  519. }
  520. mock_defaults {
  521. name: "libexample_defaults_2",
  522. visibility: ["//namespace"],
  523. }
  524. mock_library {
  525. name: "libexample",
  526. defaults: ["libexample_defaults_1", "libexample_defaults_2"],
  527. }`),
  528. "outsider/Blueprints": []byte(`
  529. mock_library {
  530. name: "liboutsider",
  531. deps: ["libexample"],
  532. }`),
  533. },
  534. },
  535. {
  536. name: "//visibility:private in defaults",
  537. fs: map[string][]byte{
  538. "top/Blueprints": []byte(`
  539. mock_defaults {
  540. name: "libexample_defaults",
  541. visibility: ["//visibility:private"],
  542. }
  543. mock_library {
  544. name: "libexample",
  545. defaults: ["libexample_defaults"],
  546. }
  547. mock_library {
  548. name: "libsamepackage",
  549. deps: ["libexample"],
  550. }`),
  551. "top/nested/Blueprints": []byte(`
  552. mock_library {
  553. name: "libnested",
  554. deps: ["libexample"],
  555. }`),
  556. "other/Blueprints": []byte(`
  557. mock_library {
  558. name: "libother",
  559. deps: ["libexample"],
  560. }`),
  561. },
  562. expectedErrors: []string{
  563. `module "libnested" variant "android_common": depends on //top:libexample which is not` +
  564. ` visible to this module`,
  565. `module "libother" variant "android_common": depends on //top:libexample which is not` +
  566. ` visible to this module`,
  567. },
  568. },
  569. {
  570. name: "//visibility:private mixed with other in defaults",
  571. fs: map[string][]byte{
  572. "top/Blueprints": []byte(`
  573. mock_defaults {
  574. name: "libexample_defaults",
  575. visibility: ["//visibility:private", "//namespace"],
  576. }
  577. mock_library {
  578. name: "libexample",
  579. defaults: ["libexample_defaults"],
  580. }`),
  581. },
  582. expectedErrors: []string{
  583. `module "libexample_defaults": visibility: cannot mix "//visibility:private"` +
  584. ` with any other visibility rules`,
  585. },
  586. },
  587. {
  588. name: "//visibility:private overriding defaults",
  589. fs: map[string][]byte{
  590. "top/Blueprints": []byte(`
  591. mock_defaults {
  592. name: "libexample_defaults",
  593. visibility: ["//namespace"],
  594. }
  595. mock_library {
  596. name: "libexample",
  597. visibility: ["//visibility:private"],
  598. defaults: ["libexample_defaults"],
  599. }`),
  600. },
  601. expectedErrors: []string{
  602. `module "libexample": visibility: cannot mix "//visibility:private"` +
  603. ` with any other visibility rules`,
  604. },
  605. },
  606. {
  607. name: "//visibility:private in defaults overridden",
  608. fs: map[string][]byte{
  609. "top/Blueprints": []byte(`
  610. mock_defaults {
  611. name: "libexample_defaults",
  612. visibility: ["//visibility:private"],
  613. }
  614. mock_library {
  615. name: "libexample",
  616. visibility: ["//namespace"],
  617. defaults: ["libexample_defaults"],
  618. }`),
  619. },
  620. expectedErrors: []string{
  621. `module "libexample": visibility: cannot mix "//visibility:private"` +
  622. ` with any other visibility rules`,
  623. },
  624. },
  625. {
  626. name: "//visibility:private override //visibility:public",
  627. fs: map[string][]byte{
  628. "top/Blueprints": []byte(`
  629. mock_defaults {
  630. name: "libexample_defaults",
  631. visibility: ["//visibility:public"],
  632. }
  633. mock_library {
  634. name: "libexample",
  635. visibility: ["//visibility:private"],
  636. defaults: ["libexample_defaults"],
  637. }`),
  638. },
  639. expectedErrors: []string{
  640. `module "libexample": visibility: cannot mix "//visibility:private" with any other visibility rules`,
  641. },
  642. },
  643. {
  644. name: "//visibility:public override //visibility:private",
  645. fs: map[string][]byte{
  646. "top/Blueprints": []byte(`
  647. mock_defaults {
  648. name: "libexample_defaults",
  649. visibility: ["//visibility:private"],
  650. }
  651. mock_library {
  652. name: "libexample",
  653. visibility: ["//visibility:public"],
  654. defaults: ["libexample_defaults"],
  655. }`),
  656. },
  657. expectedErrors: []string{
  658. `module "libexample": visibility: cannot mix "//visibility:private" with any other visibility rules`,
  659. },
  660. },
  661. {
  662. name: "//visibility:override must be first in the list",
  663. fs: map[string][]byte{
  664. "top/Blueprints": []byte(`
  665. mock_library {
  666. name: "libexample",
  667. visibility: ["//other", "//visibility:override", "//namespace"],
  668. }`),
  669. },
  670. expectedErrors: []string{
  671. `module "libexample": visibility: "//visibility:override" may only be used at the start of the visibility rules`,
  672. },
  673. },
  674. {
  675. name: "//visibility:override discards //visibility:private",
  676. fs: map[string][]byte{
  677. "top/Blueprints": []byte(`
  678. mock_defaults {
  679. name: "libexample_defaults",
  680. visibility: ["//visibility:private"],
  681. }
  682. mock_library {
  683. name: "libexample",
  684. // Make this visibility to //other but not //visibility:private
  685. visibility: ["//visibility:override", "//other"],
  686. defaults: ["libexample_defaults"],
  687. }`),
  688. "other/Blueprints": []byte(`
  689. mock_library {
  690. name: "libother",
  691. deps: ["libexample"],
  692. }`),
  693. },
  694. },
  695. {
  696. name: "//visibility:override discards //visibility:public",
  697. fs: map[string][]byte{
  698. "top/Blueprints": []byte(`
  699. mock_defaults {
  700. name: "libexample_defaults",
  701. visibility: ["//visibility:public"],
  702. }
  703. mock_library {
  704. name: "libexample",
  705. // Make this visibility to //other but not //visibility:public
  706. visibility: ["//visibility:override", "//other"],
  707. defaults: ["libexample_defaults"],
  708. }`),
  709. "other/Blueprints": []byte(`
  710. mock_library {
  711. name: "libother",
  712. deps: ["libexample"],
  713. }`),
  714. "namespace/Blueprints": []byte(`
  715. mock_library {
  716. name: "libnamespace",
  717. deps: ["libexample"],
  718. }`),
  719. },
  720. expectedErrors: []string{
  721. `module "libnamespace" variant "android_common": depends on //top:libexample which is not visible to this module`,
  722. },
  723. },
  724. {
  725. name: "//visibility:override discards defaults supplied rules",
  726. fs: map[string][]byte{
  727. "top/Blueprints": []byte(`
  728. mock_defaults {
  729. name: "libexample_defaults",
  730. visibility: ["//namespace"],
  731. }
  732. mock_library {
  733. name: "libexample",
  734. // Make this visibility to //other but not //namespace
  735. visibility: ["//visibility:override", "//other"],
  736. defaults: ["libexample_defaults"],
  737. }`),
  738. "other/Blueprints": []byte(`
  739. mock_library {
  740. name: "libother",
  741. deps: ["libexample"],
  742. }`),
  743. "namespace/Blueprints": []byte(`
  744. mock_library {
  745. name: "libnamespace",
  746. deps: ["libexample"],
  747. }`),
  748. },
  749. expectedErrors: []string{
  750. `module "libnamespace" variant "android_common": depends on //top:libexample which is not visible to this module`,
  751. },
  752. },
  753. {
  754. name: "//visibility:override can override //visibility:public with //visibility:private",
  755. fs: map[string][]byte{
  756. "top/Blueprints": []byte(`
  757. mock_defaults {
  758. name: "libexample_defaults",
  759. visibility: ["//visibility:public"],
  760. }
  761. mock_library {
  762. name: "libexample",
  763. visibility: ["//visibility:override", "//visibility:private"],
  764. defaults: ["libexample_defaults"],
  765. }`),
  766. "namespace/Blueprints": []byte(`
  767. mock_library {
  768. name: "libnamespace",
  769. deps: ["libexample"],
  770. }`),
  771. },
  772. expectedErrors: []string{
  773. `module "libnamespace" variant "android_common": depends on //top:libexample which is not visible to this module`,
  774. },
  775. },
  776. {
  777. name: "//visibility:override can override //visibility:private with //visibility:public",
  778. fs: map[string][]byte{
  779. "top/Blueprints": []byte(`
  780. mock_defaults {
  781. name: "libexample_defaults",
  782. visibility: ["//visibility:private"],
  783. }
  784. mock_library {
  785. name: "libexample",
  786. visibility: ["//visibility:override", "//visibility:public"],
  787. defaults: ["libexample_defaults"],
  788. }`),
  789. "namespace/Blueprints": []byte(`
  790. mock_library {
  791. name: "libnamespace",
  792. deps: ["libexample"],
  793. }`),
  794. },
  795. },
  796. {
  797. name: "//visibility:private mixed with itself",
  798. fs: map[string][]byte{
  799. "top/Blueprints": []byte(`
  800. mock_defaults {
  801. name: "libexample_defaults_1",
  802. visibility: ["//visibility:private"],
  803. }
  804. mock_defaults {
  805. name: "libexample_defaults_2",
  806. visibility: ["//visibility:private"],
  807. }
  808. mock_library {
  809. name: "libexample",
  810. visibility: ["//visibility:private"],
  811. defaults: ["libexample_defaults_1", "libexample_defaults_2"],
  812. }`),
  813. "outsider/Blueprints": []byte(`
  814. mock_library {
  815. name: "liboutsider",
  816. deps: ["libexample"],
  817. }`),
  818. },
  819. expectedErrors: []string{
  820. `module "liboutsider" variant "android_common": depends on //top:libexample which is not` +
  821. ` visible to this module`,
  822. },
  823. },
  824. // Defaults module's defaults_visibility tests
  825. {
  826. name: "defaults_visibility invalid",
  827. fs: map[string][]byte{
  828. "top/Blueprints": []byte(`
  829. mock_defaults {
  830. name: "top_defaults",
  831. defaults_visibility: ["//visibility:invalid"],
  832. }`),
  833. },
  834. expectedErrors: []string{
  835. `defaults_visibility: unrecognized visibility rule "//visibility:invalid"`,
  836. },
  837. },
  838. {
  839. name: "defaults_visibility overrides package default",
  840. fs: map[string][]byte{
  841. "top/Blueprints": []byte(`
  842. package {
  843. default_visibility: ["//visibility:private"],
  844. }
  845. mock_defaults {
  846. name: "top_defaults",
  847. defaults_visibility: ["//visibility:public"],
  848. }`),
  849. "outsider/Blueprints": []byte(`
  850. mock_library {
  851. name: "liboutsider",
  852. defaults: ["top_defaults"],
  853. }`),
  854. },
  855. },
  856. // Package default_visibility tests
  857. {
  858. name: "package default_visibility property is checked",
  859. fs: map[string][]byte{
  860. "top/Blueprints": []byte(`
  861. package {
  862. default_visibility: ["//visibility:invalid"],
  863. }`),
  864. },
  865. expectedErrors: []string{`default_visibility: unrecognized visibility rule "//visibility:invalid"`},
  866. },
  867. {
  868. // This test relies on the default visibility being legacy_public.
  869. name: "package default_visibility property used when no visibility specified",
  870. fs: map[string][]byte{
  871. "top/Blueprints": []byte(`
  872. package {
  873. default_visibility: ["//visibility:private"],
  874. }
  875. mock_library {
  876. name: "libexample",
  877. }`),
  878. "outsider/Blueprints": []byte(`
  879. mock_library {
  880. name: "liboutsider",
  881. deps: ["libexample"],
  882. }`),
  883. },
  884. expectedErrors: []string{
  885. `module "liboutsider" variant "android_common": depends on //top:libexample which is not` +
  886. ` visible to this module`,
  887. },
  888. },
  889. {
  890. name: "package default_visibility public does not override visibility private",
  891. fs: map[string][]byte{
  892. "top/Blueprints": []byte(`
  893. package {
  894. default_visibility: ["//visibility:public"],
  895. }
  896. mock_library {
  897. name: "libexample",
  898. visibility: ["//visibility:private"],
  899. }`),
  900. "outsider/Blueprints": []byte(`
  901. mock_library {
  902. name: "liboutsider",
  903. deps: ["libexample"],
  904. }`),
  905. },
  906. expectedErrors: []string{
  907. `module "liboutsider" variant "android_common": depends on //top:libexample which is not` +
  908. ` visible to this module`,
  909. },
  910. },
  911. {
  912. name: "package default_visibility private does not override visibility public",
  913. fs: map[string][]byte{
  914. "top/Blueprints": []byte(`
  915. package {
  916. default_visibility: ["//visibility:private"],
  917. }
  918. mock_library {
  919. name: "libexample",
  920. visibility: ["//visibility:public"],
  921. }`),
  922. "outsider/Blueprints": []byte(`
  923. mock_library {
  924. name: "liboutsider",
  925. deps: ["libexample"],
  926. }`),
  927. },
  928. },
  929. {
  930. name: "package default_visibility :__subpackages__",
  931. fs: map[string][]byte{
  932. "top/Blueprints": []byte(`
  933. package {
  934. default_visibility: [":__subpackages__"],
  935. }
  936. mock_library {
  937. name: "libexample",
  938. }`),
  939. "top/nested/Blueprints": []byte(`
  940. mock_library {
  941. name: "libnested",
  942. deps: ["libexample"],
  943. }`),
  944. "outsider/Blueprints": []byte(`
  945. mock_library {
  946. name: "liboutsider",
  947. deps: ["libexample"],
  948. }`),
  949. },
  950. expectedErrors: []string{
  951. `module "liboutsider" variant "android_common": depends on //top:libexample which is not` +
  952. ` visible to this module`,
  953. },
  954. },
  955. {
  956. name: "package default_visibility inherited to subpackages",
  957. fs: map[string][]byte{
  958. "top/Blueprints": []byte(`
  959. package {
  960. default_visibility: ["//outsider"],
  961. }
  962. mock_library {
  963. name: "libexample",
  964. visibility: [":__subpackages__"],
  965. }`),
  966. "top/nested/Blueprints": []byte(`
  967. mock_library {
  968. name: "libnested",
  969. deps: ["libexample"],
  970. }`),
  971. "outsider/Blueprints": []byte(`
  972. mock_library {
  973. name: "liboutsider",
  974. deps: ["libexample", "libnested"],
  975. }`),
  976. },
  977. expectedErrors: []string{
  978. `module "liboutsider" variant "android_common": depends on //top:libexample which is not` +
  979. ` visible to this module`,
  980. },
  981. },
  982. {
  983. name: "package default_visibility inherited to subpackages",
  984. fs: map[string][]byte{
  985. "top/Blueprints": []byte(`
  986. package {
  987. default_visibility: ["//visibility:private"],
  988. }`),
  989. "top/nested/Blueprints": []byte(`
  990. package {
  991. default_visibility: ["//outsider"],
  992. }
  993. mock_library {
  994. name: "libnested",
  995. }`),
  996. "top/other/Blueprints": []byte(`
  997. mock_library {
  998. name: "libother",
  999. }`),
  1000. "outsider/Blueprints": []byte(`
  1001. mock_library {
  1002. name: "liboutsider",
  1003. deps: ["libother", "libnested"],
  1004. }`),
  1005. },
  1006. expectedErrors: []string{
  1007. `module "liboutsider" variant "android_common": depends on //top/other:libother which is` +
  1008. ` not visible to this module`,
  1009. },
  1010. },
  1011. {
  1012. name: "verify that prebuilt dependencies are ignored for visibility reasons (not preferred)",
  1013. fs: map[string][]byte{
  1014. "prebuilts/Blueprints": []byte(`
  1015. prebuilt {
  1016. name: "module",
  1017. visibility: ["//top/other"],
  1018. }`),
  1019. "top/sources/source_file": nil,
  1020. "top/sources/Blueprints": []byte(`
  1021. source {
  1022. name: "module",
  1023. visibility: ["//top/other"],
  1024. }`),
  1025. "top/other/source_file": nil,
  1026. "top/other/Blueprints": []byte(`
  1027. source {
  1028. name: "other",
  1029. deps: [":module"],
  1030. }`),
  1031. },
  1032. },
  1033. {
  1034. name: "verify that prebuilt dependencies are ignored for visibility reasons (preferred)",
  1035. fs: map[string][]byte{
  1036. "prebuilts/Blueprints": []byte(`
  1037. prebuilt {
  1038. name: "module",
  1039. visibility: ["//top/other"],
  1040. prefer: true,
  1041. }`),
  1042. "top/sources/source_file": nil,
  1043. "top/sources/Blueprints": []byte(`
  1044. source {
  1045. name: "module",
  1046. visibility: ["//top/other"],
  1047. }`),
  1048. "top/other/source_file": nil,
  1049. "top/other/Blueprints": []byte(`
  1050. source {
  1051. name: "other",
  1052. deps: [":module"],
  1053. }`),
  1054. },
  1055. },
  1056. {
  1057. name: "ensure visibility properties are checked for correctness",
  1058. fs: map[string][]byte{
  1059. "top/Blueprints": []byte(`
  1060. mock_parent {
  1061. name: "parent",
  1062. visibility: ["//top/nested"],
  1063. child: {
  1064. name: "libchild",
  1065. visibility: ["top/other"],
  1066. },
  1067. }`),
  1068. },
  1069. expectedErrors: []string{
  1070. `module "parent": child.visibility: invalid visibility pattern "top/other"`,
  1071. },
  1072. },
  1073. {
  1074. name: "invalid visibility added to child detected during gather phase",
  1075. fs: map[string][]byte{
  1076. "top/Blueprints": []byte(`
  1077. mock_parent {
  1078. name: "parent",
  1079. visibility: ["//top/nested"],
  1080. child: {
  1081. name: "libchild",
  1082. invalid_visibility: ["top/other"],
  1083. },
  1084. }`),
  1085. },
  1086. expectedErrors: []string{
  1087. // That this error is reported against the child not the parent shows it was
  1088. // not being detected in the parent which is correct as invalid_visibility is
  1089. // purposely not added to the list of visibility properties to check, and was
  1090. // in fact detected in the child in the gather phase. Contrast this error message
  1091. // with the preceding one.
  1092. `module "libchild" \(created by module "parent"\): visibility: invalid visibility pattern "top/other"`,
  1093. },
  1094. },
  1095. {
  1096. name: "automatic visibility inheritance enabled",
  1097. fs: map[string][]byte{
  1098. "top/Blueprints": []byte(`
  1099. mock_parent {
  1100. name: "parent",
  1101. visibility: ["//top/nested"],
  1102. child: {
  1103. name: "libchild",
  1104. visibility: ["//top/other"],
  1105. },
  1106. }`),
  1107. "top/nested/Blueprints": []byte(`
  1108. mock_library {
  1109. name: "libnested",
  1110. deps: ["libchild"],
  1111. }`),
  1112. "top/other/Blueprints": []byte(`
  1113. mock_library {
  1114. name: "libother",
  1115. deps: ["libchild"],
  1116. }`),
  1117. },
  1118. },
  1119. }
  1120. func TestVisibility(t *testing.T) {
  1121. for _, test := range visibilityTests {
  1122. t.Run(test.name, func(t *testing.T) {
  1123. ctx, errs := testVisibility(buildDir, test.fs)
  1124. CheckErrorsAgainstExpectations(t, errs, test.expectedErrors)
  1125. if test.effectiveVisibility != nil {
  1126. checkEffectiveVisibility(t, ctx, test.effectiveVisibility)
  1127. }
  1128. })
  1129. }
  1130. }
  1131. func checkEffectiveVisibility(t *testing.T, ctx *TestContext, effectiveVisibility map[qualifiedModuleName][]string) {
  1132. for moduleName, expectedRules := range effectiveVisibility {
  1133. rule := effectiveVisibilityRules(ctx.config, moduleName)
  1134. stringRules := rule.Strings()
  1135. if !reflect.DeepEqual(expectedRules, stringRules) {
  1136. t.Errorf("effective rules mismatch: expected %q, found %q", expectedRules, stringRules)
  1137. }
  1138. }
  1139. }
  1140. func testVisibility(buildDir string, fs map[string][]byte) (*TestContext, []error) {
  1141. // Create a new config per test as visibility information is stored in the config.
  1142. config := TestArchConfig(buildDir, nil, "", fs)
  1143. ctx := NewTestArchContext()
  1144. ctx.RegisterModuleType("mock_library", newMockLibraryModule)
  1145. ctx.RegisterModuleType("mock_parent", newMockParentFactory)
  1146. ctx.RegisterModuleType("mock_defaults", defaultsFactory)
  1147. // Order of the following method calls is significant.
  1148. RegisterPackageBuildComponents(ctx)
  1149. registerTestPrebuiltBuildComponents(ctx)
  1150. ctx.PreArchMutators(RegisterVisibilityRuleChecker)
  1151. ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
  1152. ctx.PreArchMutators(RegisterVisibilityRuleGatherer)
  1153. ctx.PostDepsMutators(RegisterVisibilityRuleEnforcer)
  1154. ctx.Register(config)
  1155. _, errs := ctx.ParseBlueprintsFiles(".")
  1156. if len(errs) > 0 {
  1157. return ctx, errs
  1158. }
  1159. _, errs = ctx.PrepareBuildActions(config)
  1160. return ctx, errs
  1161. }
  1162. type mockLibraryProperties struct {
  1163. Deps []string
  1164. }
  1165. type mockLibraryModule struct {
  1166. ModuleBase
  1167. DefaultableModuleBase
  1168. properties mockLibraryProperties
  1169. }
  1170. func newMockLibraryModule() Module {
  1171. m := &mockLibraryModule{}
  1172. m.AddProperties(&m.properties)
  1173. InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon)
  1174. InitDefaultableModule(m)
  1175. return m
  1176. }
  1177. type dependencyTag struct {
  1178. blueprint.BaseDependencyTag
  1179. name string
  1180. }
  1181. func (j *mockLibraryModule) DepsMutator(ctx BottomUpMutatorContext) {
  1182. ctx.AddVariationDependencies(nil, dependencyTag{name: "mockdeps"}, j.properties.Deps...)
  1183. }
  1184. func (p *mockLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
  1185. }
  1186. type mockDefaults struct {
  1187. ModuleBase
  1188. DefaultsModuleBase
  1189. }
  1190. func defaultsFactory() Module {
  1191. m := &mockDefaults{}
  1192. InitDefaultsModule(m)
  1193. return m
  1194. }
  1195. type mockParentProperties struct {
  1196. Child struct {
  1197. Name *string
  1198. // Visibility to pass to the child module.
  1199. Visibility []string
  1200. // Purposely not validated visibility to pass to the child.
  1201. Invalid_visibility []string
  1202. }
  1203. }
  1204. type mockParent struct {
  1205. ModuleBase
  1206. DefaultableModuleBase
  1207. properties mockParentProperties
  1208. }
  1209. func (p *mockParent) GenerateAndroidBuildActions(ModuleContext) {
  1210. }
  1211. func newMockParentFactory() Module {
  1212. m := &mockParent{}
  1213. m.AddProperties(&m.properties)
  1214. InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon)
  1215. InitDefaultableModule(m)
  1216. AddVisibilityProperty(m, "child.visibility", &m.properties.Child.Visibility)
  1217. m.SetDefaultableHook(func(ctx DefaultableHookContext) {
  1218. visibility := m.properties.Child.Visibility
  1219. visibility = append(visibility, m.properties.Child.Invalid_visibility...)
  1220. ctx.CreateModule(newMockLibraryModule, &struct {
  1221. Name *string
  1222. Visibility []string
  1223. }{m.properties.Child.Name, visibility})
  1224. })
  1225. return m
  1226. }
  1227. func testVisibilityRuleSet(t *testing.T, rules, extra, expected []string) {
  1228. t.Helper()
  1229. set := &visibilityRuleSet{rules}
  1230. err := set.Widen(extra)
  1231. if err != nil {
  1232. t.Error(err)
  1233. return
  1234. }
  1235. actual := set.Strings()
  1236. if !reflect.DeepEqual(actual, expected) {
  1237. t.Errorf("mismatching rules after extend: expected %#v, actual %#v", expected, actual)
  1238. }
  1239. }
  1240. func TestVisibilityRuleSet(t *testing.T) {
  1241. t.Run("extend empty", func(t *testing.T) {
  1242. testVisibilityRuleSet(t, nil, []string{"//foo"}, []string{"//foo"})
  1243. })
  1244. t.Run("extend", func(t *testing.T) {
  1245. testVisibilityRuleSet(t, []string{"//foo"}, []string{"//bar"}, []string{"//bar", "//foo"})
  1246. })
  1247. t.Run("extend duplicate", func(t *testing.T) {
  1248. testVisibilityRuleSet(t, []string{"//foo"}, []string{"//bar", "//foo"}, []string{"//bar", "//foo"})
  1249. })
  1250. t.Run("extend public", func(t *testing.T) {
  1251. testVisibilityRuleSet(t, []string{"//visibility:public"}, []string{"//foo"}, []string{"//visibility:public"})
  1252. })
  1253. t.Run("extend private", func(t *testing.T) {
  1254. testVisibilityRuleSet(t, []string{"//visibility:private"}, []string{"//foo"}, []string{"//foo"})
  1255. })
  1256. t.Run("extend with public", func(t *testing.T) {
  1257. testVisibilityRuleSet(t, []string{"//foo"}, []string{"//visibility:public"}, []string{"//visibility:public"})
  1258. })
  1259. t.Run("extend with private", func(t *testing.T) {
  1260. t.Helper()
  1261. set := &visibilityRuleSet{[]string{"//foo"}}
  1262. err := set.Widen([]string{"//visibility:private"})
  1263. expectedError := `"//visibility:private" does not widen the visibility`
  1264. if err == nil {
  1265. t.Errorf("missing error")
  1266. } else if err.Error() != expectedError {
  1267. t.Errorf("expected error %q found error %q", expectedError, err)
  1268. }
  1269. })
  1270. }