visibility_test.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. package android
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/google/blueprint"
  6. )
  7. var visibilityTests = []struct {
  8. name string
  9. fs MockFS
  10. expectedErrors []string
  11. effectiveVisibility map[qualifiedModuleName][]string
  12. }{
  13. {
  14. name: "invalid visibility: empty list",
  15. fs: MockFS{
  16. "top/Android.bp": []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: MockFS{
  27. "top/Android.bp": []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: MockFS{
  38. "top/Android.bp": []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: MockFS{
  49. "top/Android.bp": []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: MockFS{
  60. "top/Android.bp": []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: MockFS{
  71. "top/Android.bp": []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: MockFS{
  82. "top/Android.bp": []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: MockFS{
  93. "top/Android.bp": []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: MockFS{
  113. "top/Android.bp": []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: MockFS{
  129. "top/Android.bp": []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/Android.bp": []byte(`
  139. mock_library {
  140. name: "libnested",
  141. deps: ["libexample"],
  142. }`),
  143. "other/Android.bp": []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: MockFS{
  155. "top/Android.bp": []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/Android.bp": []byte(`
  165. mock_library {
  166. name: "libnested",
  167. deps: ["libexample"],
  168. }`),
  169. "other/Android.bp": []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: MockFS{
  186. "top/Android.bp": []byte(`
  187. mock_library {
  188. name: "libexample",
  189. visibility: [":__pkg__"],
  190. }
  191. mock_library {
  192. name: "libsamepackage",
  193. deps: ["libexample"],
  194. }`),
  195. "top/nested/Android.bp": []byte(`
  196. mock_library {
  197. name: "libnested",
  198. deps: ["libexample"],
  199. }`),
  200. "other/Android.bp": []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: MockFS{
  218. "top/Android.bp": []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/Android.bp": []byte(`
  228. mock_library {
  229. name: "libnested",
  230. deps: ["libexample"],
  231. }`),
  232. "top/nested/again/Android.bp": []byte(`
  233. mock_library {
  234. name: "libnestedagain",
  235. deps: ["libexample"],
  236. }`),
  237. "peak/Android.bp": []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: MockFS{
  255. "top/Android.bp": []byte(`
  256. mock_library {
  257. name: "libexample",
  258. visibility: [":__subpackages__"],
  259. }
  260. mock_library {
  261. name: "libsamepackage",
  262. deps: ["libexample"],
  263. }`),
  264. "top/nested/Android.bp": []byte(`
  265. mock_library {
  266. name: "libnested",
  267. deps: ["libexample"],
  268. }`),
  269. "peak/other/Android.bp": []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: MockFS{
  285. "top/Android.bp": []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/Android.bp": []byte(`
  295. mock_library {
  296. name: "libnested",
  297. deps: ["libexample"],
  298. }`),
  299. "top/other/Android.bp": []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: MockFS{
  315. "top/Android.bp": []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/Android.bp": []byte(`
  325. mock_library {
  326. name: "libnested",
  327. deps: ["libexample"],
  328. }`),
  329. "peak/other/Android.bp": []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: MockFS{
  340. "top/Android.bp": []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/Android.bp": []byte(`
  350. mock_library {
  351. name: "libvendorexample",
  352. deps: ["libexample"],
  353. visibility: ["//vendor/nested"],
  354. }`),
  355. "vendor/nested/Android.bp": []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: MockFS{
  372. "top/Android.bp": []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/Android.bp": []byte(`
  387. mock_library {
  388. name: "libnested",
  389. deps: ["libexample"],
  390. }`),
  391. "other/Android.bp": []byte(`
  392. mock_library {
  393. name: "libother",
  394. deps: ["libexample"],
  395. }`),
  396. "outsider/Android.bp": []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: MockFS{
  410. "top/Android.bp": []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/Android.bp": []byte(`
  428. mock_library {
  429. name: "libnested",
  430. deps: ["libexample"],
  431. }`),
  432. "other/Android.bp": []byte(`
  433. mock_library {
  434. name: "libother",
  435. deps: ["libexample"],
  436. }`),
  437. "outsider/Android.bp": []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: MockFS{
  451. "top/Android.bp": []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: MockFS{
  469. "top/Android.bp": []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/Android.bp": []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: MockFS{
  492. "top/Android.bp": []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/Android.bp": []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: MockFS{
  515. "top/Android.bp": []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/Android.bp": []byte(`
  529. mock_library {
  530. name: "liboutsider",
  531. deps: ["libexample"],
  532. }`),
  533. },
  534. },
  535. {
  536. name: "//visibility:private in defaults",
  537. fs: MockFS{
  538. "top/Android.bp": []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/Android.bp": []byte(`
  552. mock_library {
  553. name: "libnested",
  554. deps: ["libexample"],
  555. }`),
  556. "other/Android.bp": []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: MockFS{
  572. "top/Android.bp": []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: MockFS{
  590. "top/Android.bp": []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: MockFS{
  609. "top/Android.bp": []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: MockFS{
  628. "top/Android.bp": []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: MockFS{
  646. "top/Android.bp": []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: MockFS{
  664. "top/Android.bp": []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: MockFS{
  677. "top/Android.bp": []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/Android.bp": []byte(`
  689. mock_library {
  690. name: "libother",
  691. deps: ["libexample"],
  692. }`),
  693. },
  694. },
  695. {
  696. name: "//visibility:override discards //visibility:public",
  697. fs: MockFS{
  698. "top/Android.bp": []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/Android.bp": []byte(`
  710. mock_library {
  711. name: "libother",
  712. deps: ["libexample"],
  713. }`),
  714. "namespace/Android.bp": []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\nYou may need to add "//namespace" to its visibility`,
  722. },
  723. },
  724. {
  725. name: "//visibility:override discards defaults supplied rules",
  726. fs: MockFS{
  727. "top/Android.bp": []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/Android.bp": []byte(`
  739. mock_library {
  740. name: "libother",
  741. deps: ["libexample"],
  742. }`),
  743. "namespace/Android.bp": []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\nYou may need to add "//namespace" to its visibility`,
  751. },
  752. },
  753. {
  754. name: "//visibility:override can override //visibility:public with //visibility:private",
  755. fs: MockFS{
  756. "top/Android.bp": []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/Android.bp": []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: MockFS{
  779. "top/Android.bp": []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/Android.bp": []byte(`
  790. mock_library {
  791. name: "libnamespace",
  792. deps: ["libexample"],
  793. }`),
  794. },
  795. },
  796. {
  797. name: "//visibility:private mixed with itself",
  798. fs: MockFS{
  799. "top/Android.bp": []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/Android.bp": []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: MockFS{
  828. "top/Android.bp": []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: MockFS{
  841. "top/Android.bp": []byte(`
  842. package {
  843. default_visibility: ["//visibility:private"],
  844. }
  845. mock_defaults {
  846. name: "top_defaults",
  847. defaults_visibility: ["//visibility:public"],
  848. }`),
  849. "outsider/Android.bp": []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: MockFS{
  860. "top/Android.bp": []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: MockFS{
  871. "top/Android.bp": []byte(`
  872. package {
  873. default_visibility: ["//visibility:private"],
  874. }
  875. mock_library {
  876. name: "libexample",
  877. }`),
  878. "outsider/Android.bp": []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: MockFS{
  892. "top/Android.bp": []byte(`
  893. package {
  894. default_visibility: ["//visibility:public"],
  895. }
  896. mock_library {
  897. name: "libexample",
  898. visibility: ["//visibility:private"],
  899. }`),
  900. "outsider/Android.bp": []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: MockFS{
  914. "top/Android.bp": []byte(`
  915. package {
  916. default_visibility: ["//visibility:private"],
  917. }
  918. mock_library {
  919. name: "libexample",
  920. visibility: ["//visibility:public"],
  921. }`),
  922. "outsider/Android.bp": []byte(`
  923. mock_library {
  924. name: "liboutsider",
  925. deps: ["libexample"],
  926. }`),
  927. },
  928. },
  929. {
  930. name: "package default_visibility :__subpackages__",
  931. fs: MockFS{
  932. "top/Android.bp": []byte(`
  933. package {
  934. default_visibility: [":__subpackages__"],
  935. }
  936. mock_library {
  937. name: "libexample",
  938. }`),
  939. "top/nested/Android.bp": []byte(`
  940. mock_library {
  941. name: "libnested",
  942. deps: ["libexample"],
  943. }`),
  944. "outsider/Android.bp": []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: MockFS{
  958. "top/Android.bp": []byte(`
  959. package {
  960. default_visibility: ["//outsider"],
  961. }
  962. mock_library {
  963. name: "libexample",
  964. visibility: [":__subpackages__"],
  965. }`),
  966. "top/nested/Android.bp": []byte(`
  967. mock_library {
  968. name: "libnested",
  969. deps: ["libexample"],
  970. }`),
  971. "outsider/Android.bp": []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: MockFS{
  985. "top/Android.bp": []byte(`
  986. package {
  987. default_visibility: ["//visibility:private"],
  988. }`),
  989. "top/nested/Android.bp": []byte(`
  990. package {
  991. default_visibility: ["//outsider"],
  992. }
  993. mock_library {
  994. name: "libnested",
  995. }`),
  996. "top/other/Android.bp": []byte(`
  997. mock_library {
  998. name: "libother",
  999. }`),
  1000. "outsider/Android.bp": []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: MockFS{
  1014. "prebuilts/Android.bp": []byte(`
  1015. prebuilt {
  1016. name: "module",
  1017. visibility: ["//top/other"],
  1018. }`),
  1019. "top/sources/source_file": nil,
  1020. "top/sources/Android.bp": []byte(`
  1021. source {
  1022. name: "module",
  1023. visibility: ["//top/other"],
  1024. }`),
  1025. "top/other/source_file": nil,
  1026. "top/other/Android.bp": []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: MockFS{
  1036. "prebuilts/Android.bp": []byte(`
  1037. prebuilt {
  1038. name: "module",
  1039. visibility: ["//top/other"],
  1040. prefer: true,
  1041. }`),
  1042. "top/sources/source_file": nil,
  1043. "top/sources/Android.bp": []byte(`
  1044. source {
  1045. name: "module",
  1046. visibility: ["//top/other"],
  1047. }`),
  1048. "top/other/source_file": nil,
  1049. "top/other/Android.bp": []byte(`
  1050. source {
  1051. name: "other",
  1052. deps: [":module"],
  1053. }`),
  1054. },
  1055. },
  1056. {
  1057. name: "ensure visibility properties are checked for correctness",
  1058. fs: MockFS{
  1059. "top/Android.bp": []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: MockFS{
  1076. "top/Android.bp": []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: MockFS{
  1098. "top/Android.bp": []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/Android.bp": []byte(`
  1108. mock_library {
  1109. name: "libnested",
  1110. deps: ["libchild"],
  1111. }`),
  1112. "top/other/Android.bp": []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. result := GroupFixturePreparers(
  1124. // General preparers in alphabetical order as test infrastructure will enforce correct
  1125. // registration order.
  1126. PrepareForTestWithArchMutator,
  1127. PrepareForTestWithDefaults,
  1128. PrepareForTestWithOverrides,
  1129. PrepareForTestWithPackageModule,
  1130. PrepareForTestWithPrebuilts,
  1131. PrepareForTestWithVisibility,
  1132. // Additional test specific preparers.
  1133. FixtureRegisterWithContext(func(ctx RegistrationContext) {
  1134. ctx.RegisterModuleType("mock_library", newMockLibraryModule)
  1135. ctx.RegisterModuleType("mock_parent", newMockParentFactory)
  1136. ctx.RegisterModuleType("mock_defaults", defaultsFactory)
  1137. }),
  1138. prepareForTestWithFakePrebuiltModules,
  1139. // Add additional files to the mock filesystem
  1140. test.fs.AddToFixture(),
  1141. ).
  1142. ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
  1143. RunTest(t)
  1144. if test.effectiveVisibility != nil {
  1145. checkEffectiveVisibility(t, result, test.effectiveVisibility)
  1146. }
  1147. })
  1148. }
  1149. }
  1150. func checkEffectiveVisibility(t *testing.T, result *TestResult, effectiveVisibility map[qualifiedModuleName][]string) {
  1151. for moduleName, expectedRules := range effectiveVisibility {
  1152. rule := effectiveVisibilityRules(result.Config, moduleName)
  1153. stringRules := rule.Strings()
  1154. AssertDeepEquals(t, "effective rules mismatch", expectedRules, stringRules)
  1155. }
  1156. }
  1157. type mockLibraryProperties struct {
  1158. Deps []string
  1159. }
  1160. type mockLibraryModule struct {
  1161. ModuleBase
  1162. DefaultableModuleBase
  1163. properties mockLibraryProperties
  1164. }
  1165. func newMockLibraryModule() Module {
  1166. m := &mockLibraryModule{}
  1167. m.AddProperties(&m.properties)
  1168. InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon)
  1169. InitDefaultableModule(m)
  1170. return m
  1171. }
  1172. type dependencyTag struct {
  1173. blueprint.BaseDependencyTag
  1174. name string
  1175. }
  1176. func (j *mockLibraryModule) DepsMutator(ctx BottomUpMutatorContext) {
  1177. ctx.AddVariationDependencies(nil, dependencyTag{name: "mockdeps"}, j.properties.Deps...)
  1178. }
  1179. func (p *mockLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
  1180. }
  1181. type mockDefaults struct {
  1182. ModuleBase
  1183. DefaultsModuleBase
  1184. }
  1185. func defaultsFactory() Module {
  1186. m := &mockDefaults{}
  1187. InitDefaultsModule(m)
  1188. return m
  1189. }
  1190. type mockParentProperties struct {
  1191. Child struct {
  1192. Name *string
  1193. // Visibility to pass to the child module.
  1194. Visibility []string
  1195. // Purposely not validated visibility to pass to the child.
  1196. Invalid_visibility []string
  1197. }
  1198. }
  1199. type mockParent struct {
  1200. ModuleBase
  1201. DefaultableModuleBase
  1202. properties mockParentProperties
  1203. }
  1204. func (p *mockParent) GenerateAndroidBuildActions(ModuleContext) {
  1205. }
  1206. func newMockParentFactory() Module {
  1207. m := &mockParent{}
  1208. m.AddProperties(&m.properties)
  1209. InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon)
  1210. InitDefaultableModule(m)
  1211. AddVisibilityProperty(m, "child.visibility", &m.properties.Child.Visibility)
  1212. m.SetDefaultableHook(func(ctx DefaultableHookContext) {
  1213. visibility := m.properties.Child.Visibility
  1214. visibility = append(visibility, m.properties.Child.Invalid_visibility...)
  1215. ctx.CreateModule(newMockLibraryModule, &struct {
  1216. Name *string
  1217. Visibility []string
  1218. }{m.properties.Child.Name, visibility})
  1219. })
  1220. return m
  1221. }
  1222. func testVisibilityRuleSet(t *testing.T, rules, extra, expected []string) {
  1223. t.Helper()
  1224. set := &visibilityRuleSet{rules}
  1225. err := set.Widen(extra)
  1226. if err != nil {
  1227. t.Error(err)
  1228. return
  1229. }
  1230. actual := set.Strings()
  1231. if !reflect.DeepEqual(actual, expected) {
  1232. t.Errorf("mismatching rules after extend: expected %#v, actual %#v", expected, actual)
  1233. }
  1234. }
  1235. func TestVisibilityRuleSet(t *testing.T) {
  1236. t.Run("extend empty", func(t *testing.T) {
  1237. testVisibilityRuleSet(t, nil, []string{"//foo"}, []string{"//foo"})
  1238. })
  1239. t.Run("extend", func(t *testing.T) {
  1240. testVisibilityRuleSet(t, []string{"//foo"}, []string{"//bar"}, []string{"//bar", "//foo"})
  1241. })
  1242. t.Run("extend duplicate", func(t *testing.T) {
  1243. testVisibilityRuleSet(t, []string{"//foo"}, []string{"//bar", "//foo"}, []string{"//bar", "//foo"})
  1244. })
  1245. t.Run("extend public", func(t *testing.T) {
  1246. testVisibilityRuleSet(t, []string{"//visibility:public"}, []string{"//foo"}, []string{"//visibility:public"})
  1247. })
  1248. t.Run("extend private", func(t *testing.T) {
  1249. testVisibilityRuleSet(t, []string{"//visibility:private"}, []string{"//foo"}, []string{"//foo"})
  1250. })
  1251. t.Run("extend with public", func(t *testing.T) {
  1252. testVisibilityRuleSet(t, []string{"//foo"}, []string{"//visibility:public"}, []string{"//visibility:public"})
  1253. })
  1254. t.Run("extend with private", func(t *testing.T) {
  1255. t.Helper()
  1256. set := &visibilityRuleSet{[]string{"//foo"}}
  1257. err := set.Widen([]string{"//visibility:private"})
  1258. expectedError := `"//visibility:private" does not widen the visibility`
  1259. if err == nil {
  1260. t.Errorf("missing error")
  1261. } else if err.Error() != expectedError {
  1262. t.Errorf("expected error %q found error %q", expectedError, err)
  1263. }
  1264. })
  1265. }