visibility_test.go 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075
  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. gen_notice {
  139. name: "libexample-notice",
  140. for: ["libexample"],
  141. }`),
  142. "top/nested/Android.bp": []byte(`
  143. mock_library {
  144. name: "libnested",
  145. deps: ["libexample"],
  146. }
  147. gen_notice {
  148. name: "nested-notice",
  149. for: ["libexample"],
  150. }`),
  151. "other/Android.bp": []byte(`
  152. mock_library {
  153. name: "libother",
  154. deps: ["libexample"],
  155. }
  156. gen_notice {
  157. name: "other-notice",
  158. for: ["libexample"],
  159. }`),
  160. },
  161. },
  162. {
  163. // Verify that //visibility:private allows the module to be referenced from the current
  164. // directory only.
  165. name: "//visibility:private",
  166. fs: MockFS{
  167. "top/Android.bp": []byte(`
  168. mock_library {
  169. name: "libexample",
  170. visibility: ["//visibility:private"],
  171. }
  172. mock_library {
  173. name: "libsamepackage",
  174. deps: ["libexample"],
  175. }`),
  176. "top/nested/Android.bp": []byte(`
  177. mock_library {
  178. name: "libnested",
  179. deps: ["libexample"],
  180. }`),
  181. "other/Android.bp": []byte(`
  182. mock_library {
  183. name: "libother",
  184. deps: ["libexample"],
  185. }`),
  186. },
  187. expectedErrors: []string{
  188. `module "libnested" variant "android_common": depends on //top:libexample which is not` +
  189. ` visible to this module`,
  190. `module "libother" variant "android_common": depends on //top:libexample which is not` +
  191. ` visible to this module`,
  192. },
  193. },
  194. {
  195. // Verify that //visibility:private allows the module to be referenced from the current
  196. // directory only.
  197. name: "//visibility:private (notices)",
  198. fs: MockFS{
  199. "top/Android.bp": []byte(`
  200. mock_library {
  201. name: "libexample",
  202. visibility: ["//visibility:private"],
  203. }
  204. mock_library {
  205. name: "libsamepackage",
  206. deps: ["libexample"],
  207. }
  208. gen_notice {
  209. name: "libexample-notice",
  210. for: ["libexample"],
  211. }`),
  212. "top/nested/Android.bp": []byte(`
  213. gen_notice {
  214. name: "nested-notice",
  215. for: ["libexample"],
  216. }`),
  217. "other/Android.bp": []byte(`
  218. gen_notice {
  219. name: "other-notice",
  220. for: ["libexample"],
  221. }`),
  222. },
  223. expectedErrors: []string{
  224. `module "nested-notice" references "//top:libexample" which is not visible to this` +
  225. ` module\nYou may need to add "//top/nested" to its visibility`,
  226. `module "other-notice" references "//top:libexample" which is not visible to this module\n` +
  227. `You may need to add "//other" to its visibility`,
  228. },
  229. },
  230. {
  231. // Verify that :__pkg__ allows the module to be referenced from the current directory only.
  232. name: ":__pkg__",
  233. fs: MockFS{
  234. "top/Android.bp": []byte(`
  235. mock_library {
  236. name: "libexample",
  237. visibility: [":__pkg__"],
  238. }
  239. mock_library {
  240. name: "libsamepackage",
  241. deps: ["libexample"],
  242. }`),
  243. "top/nested/Android.bp": []byte(`
  244. mock_library {
  245. name: "libnested",
  246. deps: ["libexample"],
  247. }`),
  248. "other/Android.bp": []byte(`
  249. mock_library {
  250. name: "libother",
  251. deps: ["libexample"],
  252. }`),
  253. },
  254. expectedErrors: []string{
  255. `module "libnested" variant "android_common": depends on //top:libexample which is not` +
  256. ` visible to this module`,
  257. `module "libother" variant "android_common": depends on //top:libexample which is not` +
  258. ` visible to this module`,
  259. },
  260. },
  261. {
  262. // Verify that :__pkg__ allows the module to be referenced from the current directory only.
  263. name: ":__pkg__ (notices)",
  264. fs: MockFS{
  265. "top/Android.bp": []byte(`
  266. mock_library {
  267. name: "libexample",
  268. visibility: [":__pkg__"],
  269. }
  270. gen_notice {
  271. name: "libexample-notice",
  272. for: ["libexample"],
  273. }`),
  274. "top/nested/Android.bp": []byte(`
  275. gen_notice {
  276. name: "nested-notice",
  277. for: ["libexample"],
  278. }`),
  279. "other/Android.bp": []byte(`
  280. gen_notice {
  281. name: "other-notice",
  282. for: ["libexample"],
  283. }`),
  284. },
  285. expectedErrors: []string{
  286. `module "nested-notice" references "//top:libexample" which is not visible to this module`,
  287. `module "other-notice" references "//top:libexample" which is not visible to this module`,
  288. },
  289. },
  290. {
  291. // Verify that //top/nested allows the module to be referenced from the current directory and
  292. // the top/nested directory only, not a subdirectory of top/nested and not peak directory.
  293. name: "//top/nested",
  294. fs: MockFS{
  295. "top/Android.bp": []byte(`
  296. mock_library {
  297. name: "libexample",
  298. visibility: ["//top/nested"],
  299. }
  300. mock_library {
  301. name: "libsamepackage",
  302. deps: ["libexample"],
  303. }`),
  304. "top/nested/Android.bp": []byte(`
  305. mock_library {
  306. name: "libnested",
  307. deps: ["libexample"],
  308. }`),
  309. "top/nested/again/Android.bp": []byte(`
  310. mock_library {
  311. name: "libnestedagain",
  312. deps: ["libexample"],
  313. }`),
  314. "peak/Android.bp": []byte(`
  315. mock_library {
  316. name: "libother",
  317. deps: ["libexample"],
  318. }`),
  319. },
  320. expectedErrors: []string{
  321. `module "libother" variant "android_common": depends on //top:libexample which is not` +
  322. ` visible to this module`,
  323. `module "libnestedagain" variant "android_common": depends on //top:libexample which is not` +
  324. ` visible to this module`,
  325. },
  326. },
  327. {
  328. // Verify that //top/nested allows the module to be referenced from the current directory and
  329. // the top/nested directory only, not a subdirectory of top/nested and not peak directory.
  330. name: "//top/nested (notices)",
  331. fs: MockFS{
  332. "top/Android.bp": []byte(`
  333. mock_library {
  334. name: "libexample",
  335. visibility: ["//top/nested"],
  336. }
  337. gen_notice {
  338. name: "libexample-notice",
  339. for: ["libexample"],
  340. }`),
  341. "top/nested/Android.bp": []byte(`
  342. gen_notice {
  343. name: "nested-notice",
  344. for: ["libexample"],
  345. }`),
  346. "top/nested/again/Android.bp": []byte(`
  347. gen_notice {
  348. name: "nestedagain-notice",
  349. for: ["libexample"],
  350. }`),
  351. "peak/Android.bp": []byte(`
  352. gen_notice {
  353. name: "other-notice",
  354. for: ["libexample"],
  355. }`),
  356. },
  357. expectedErrors: []string{
  358. `module "other-notice" references "//top:libexample" which is not visible to this module`,
  359. `module "nestedagain-notice" references "//top:libexample" which is not visible to this module`,
  360. },
  361. },
  362. {
  363. // Verify that :__subpackages__ allows the module to be referenced from the current directory
  364. // and sub directories but nowhere else.
  365. name: ":__subpackages__",
  366. fs: MockFS{
  367. "top/Android.bp": []byte(`
  368. mock_library {
  369. name: "libexample",
  370. visibility: [":__subpackages__"],
  371. }
  372. mock_library {
  373. name: "libsamepackage",
  374. deps: ["libexample"],
  375. }`),
  376. "top/nested/Android.bp": []byte(`
  377. mock_library {
  378. name: "libnested",
  379. deps: ["libexample"],
  380. }`),
  381. "peak/other/Android.bp": []byte(`
  382. mock_library {
  383. name: "libother",
  384. deps: ["libexample"],
  385. }`),
  386. },
  387. expectedErrors: []string{
  388. `module "libother" variant "android_common": depends on //top:libexample which is not` +
  389. ` visible to this module`,
  390. },
  391. },
  392. {
  393. // Verify that :__subpackages__ allows the module to be referenced from the current directory
  394. // and sub directories but nowhere else.
  395. name: ":__subpackages__ (notices)",
  396. fs: MockFS{
  397. "top/Android.bp": []byte(`
  398. mock_library {
  399. name: "libexample",
  400. visibility: [":__subpackages__"],
  401. }
  402. gen_notice {
  403. name: "libexample-notice",
  404. for: ["libexample"],
  405. }`),
  406. "top/nested/Android.bp": []byte(`
  407. gen_notice {
  408. name: "nested-notice",
  409. for: ["libexample"],
  410. }`),
  411. "peak/other/Android.bp": []byte(`
  412. gen_notice {
  413. name: "other-notice",
  414. for: ["libexample"],
  415. }`),
  416. },
  417. expectedErrors: []string{
  418. `module "other-notice" references "//top:libexample" which is not visible to this module`,
  419. },
  420. },
  421. {
  422. // Verify that //top/nested:__subpackages__ allows the module to be referenced from the current
  423. // directory and sub directories but nowhere else.
  424. name: "//top/nested:__subpackages__",
  425. fs: MockFS{
  426. "top/Android.bp": []byte(`
  427. mock_library {
  428. name: "libexample",
  429. visibility: ["//top/nested:__subpackages__", "//other"],
  430. }
  431. mock_library {
  432. name: "libsamepackage",
  433. deps: ["libexample"],
  434. }`),
  435. "top/nested/Android.bp": []byte(`
  436. mock_library {
  437. name: "libnested",
  438. deps: ["libexample"],
  439. }`),
  440. "top/other/Android.bp": []byte(`
  441. mock_library {
  442. name: "libother",
  443. deps: ["libexample"],
  444. }`),
  445. },
  446. expectedErrors: []string{
  447. `module "libother" variant "android_common": depends on //top:libexample which is not` +
  448. ` visible to this module`,
  449. },
  450. },
  451. {
  452. // Verify that //top/nested:__subpackages__ allows the module to be referenced from the current
  453. // directory and sub directories but nowhere else.
  454. name: "//top/nested:__subpackages__ (notices)",
  455. fs: MockFS{
  456. "top/Android.bp": []byte(`
  457. mock_library {
  458. name: "libexample",
  459. visibility: ["//top/nested:__subpackages__", "//other"],
  460. }
  461. gen_notice {
  462. name: "libexample-notice",
  463. for: ["libexample"],
  464. }`),
  465. "top/nested/Android.bp": []byte(`
  466. gen_notice {
  467. name: "nested-notice",
  468. for: ["libexample"],
  469. }`),
  470. "top/other/Android.bp": []byte(`
  471. gen_notice {
  472. name: "other-notice",
  473. for: ["libexample"],
  474. }`),
  475. },
  476. expectedErrors: []string{
  477. `module "other-notice" references "//top:libexample" which is not visible to this module`,
  478. },
  479. },
  480. {
  481. // Verify that ["//top/nested", "//peak:__subpackages"] allows the module to be referenced from
  482. // the current directory, top/nested and peak and all its subpackages.
  483. name: `["//top/nested", "//peak:__subpackages__"]`,
  484. fs: MockFS{
  485. "top/Android.bp": []byte(`
  486. mock_library {
  487. name: "libexample",
  488. visibility: ["//top/nested", "//peak:__subpackages__"],
  489. }
  490. mock_library {
  491. name: "libsamepackage",
  492. deps: ["libexample"],
  493. }`),
  494. "top/nested/Android.bp": []byte(`
  495. mock_library {
  496. name: "libnested",
  497. deps: ["libexample"],
  498. }`),
  499. "peak/other/Android.bp": []byte(`
  500. mock_library {
  501. name: "libother",
  502. deps: ["libexample"],
  503. }`),
  504. },
  505. },
  506. {
  507. // Verify that ["//top/nested", "//peak:__subpackages"] allows the module to be referenced from
  508. // the current directory, top/nested and peak and all its subpackages.
  509. name: `["//top/nested", "//peak:__subpackages__ (notices)"]`,
  510. fs: MockFS{
  511. "top/Android.bp": []byte(`
  512. mock_library {
  513. name: "libexample",
  514. visibility: ["//top/nested", "//peak:__subpackages__"],
  515. }
  516. gen_notice {
  517. name: "libexample-notice",
  518. for: ["libexample"],
  519. }`),
  520. "top/nested/Android.bp": []byte(`
  521. gen_notice {
  522. name: "nested-notice",
  523. for: ["libexample"],
  524. }`),
  525. "peak/other/Android.bp": []byte(`
  526. gen_notice {
  527. name: "other-notice",
  528. for: ["libexample"],
  529. }`),
  530. },
  531. },
  532. {
  533. // Verify that //vendor... cannot be used outside vendor apart from //vendor:__subpackages__
  534. name: `//vendor`,
  535. fs: MockFS{
  536. "top/Android.bp": []byte(`
  537. mock_library {
  538. name: "libexample",
  539. visibility: ["//vendor:__subpackages__"],
  540. }
  541. mock_library {
  542. name: "libsamepackage",
  543. visibility: ["//vendor/apps/AcmeSettings"],
  544. }`),
  545. "vendor/Android.bp": []byte(`
  546. mock_library {
  547. name: "libvendorexample",
  548. deps: ["libexample"],
  549. visibility: ["//vendor/nested"],
  550. }`),
  551. "vendor/nested/Android.bp": []byte(`
  552. mock_library {
  553. name: "libvendornested",
  554. deps: ["libexample", "libvendorexample"],
  555. }`),
  556. },
  557. expectedErrors: []string{
  558. `module "libsamepackage": visibility: "//vendor/apps/AcmeSettings"` +
  559. ` is not allowed. Packages outside //vendor cannot make themselves visible to specific` +
  560. ` targets within //vendor, they can only use //vendor:__subpackages__.`,
  561. },
  562. },
  563. // Defaults propagation tests
  564. {
  565. // Check that visibility is the union of the defaults modules.
  566. name: "defaults union, basic",
  567. fs: MockFS{
  568. "top/Android.bp": []byte(`
  569. mock_defaults {
  570. name: "libexample_defaults",
  571. visibility: ["//other"],
  572. }
  573. mock_library {
  574. name: "libexample",
  575. visibility: ["//top/nested"],
  576. defaults: ["libexample_defaults"],
  577. }
  578. mock_library {
  579. name: "libsamepackage",
  580. deps: ["libexample"],
  581. }`),
  582. "top/nested/Android.bp": []byte(`
  583. mock_library {
  584. name: "libnested",
  585. deps: ["libexample"],
  586. }`),
  587. "other/Android.bp": []byte(`
  588. mock_library {
  589. name: "libother",
  590. deps: ["libexample"],
  591. }`),
  592. "outsider/Android.bp": []byte(`
  593. mock_library {
  594. name: "liboutsider",
  595. deps: ["libexample"],
  596. }`),
  597. },
  598. expectedErrors: []string{
  599. `module "liboutsider" variant "android_common": depends on //top:libexample which is not` +
  600. ` visible to this module`,
  601. },
  602. },
  603. {
  604. // Check that visibility is the union of the defaults modules.
  605. name: "defaults union, basic (notices)",
  606. fs: MockFS{
  607. "top/Android.bp": []byte(`
  608. mock_defaults {
  609. name: "libexample_defaults",
  610. visibility: ["//other"],
  611. }
  612. mock_library {
  613. name: "libexample",
  614. visibility: ["//top/nested"],
  615. defaults: ["libexample_defaults"],
  616. }
  617. gen_notice {
  618. name: "libexample-notice",
  619. for: ["libexample"],
  620. }`),
  621. "top/nested/Android.bp": []byte(`
  622. gen_notice {
  623. name: "nested-notice",
  624. for: ["libexample"],
  625. }`),
  626. "other/Android.bp": []byte(`
  627. gen_notice {
  628. name: "other-notice",
  629. for: ["libexample"],
  630. }`),
  631. "outsider/Android.bp": []byte(`
  632. gen_notice {
  633. name: "outsider-notice",
  634. for: ["libexample"],
  635. }`),
  636. },
  637. expectedErrors: []string{
  638. `module "outsider-notice" references "//top:libexample" which is not visible to this module`,
  639. },
  640. },
  641. {
  642. name: "defaults union, multiple defaults",
  643. fs: MockFS{
  644. "top/Android.bp": []byte(`
  645. mock_defaults {
  646. name: "libexample_defaults_1",
  647. visibility: ["//other"],
  648. }
  649. mock_defaults {
  650. name: "libexample_defaults_2",
  651. visibility: ["//top/nested"],
  652. }
  653. mock_library {
  654. name: "libexample",
  655. defaults: ["libexample_defaults_1", "libexample_defaults_2"],
  656. }
  657. mock_library {
  658. name: "libsamepackage",
  659. deps: ["libexample"],
  660. }`),
  661. "top/nested/Android.bp": []byte(`
  662. mock_library {
  663. name: "libnested",
  664. deps: ["libexample"],
  665. }`),
  666. "other/Android.bp": []byte(`
  667. mock_library {
  668. name: "libother",
  669. deps: ["libexample"],
  670. }`),
  671. "outsider/Android.bp": []byte(`
  672. mock_library {
  673. name: "liboutsider",
  674. deps: ["libexample"],
  675. }`),
  676. },
  677. expectedErrors: []string{
  678. `module "liboutsider" variant "android_common": depends on //top:libexample which is not` +
  679. ` visible to this module`,
  680. },
  681. },
  682. {
  683. name: "defaults union, multiple defaults (notices)",
  684. fs: MockFS{
  685. "top/Android.bp": []byte(`
  686. mock_defaults {
  687. name: "libexample_defaults_1",
  688. visibility: ["//other"],
  689. }
  690. mock_defaults {
  691. name: "libexample_defaults_2",
  692. visibility: ["//top/nested"],
  693. }
  694. mock_library {
  695. name: "libexample",
  696. defaults: ["libexample_defaults_1", "libexample_defaults_2"],
  697. }
  698. gen_notice {
  699. name: "libexample-notice",
  700. for: ["libexample"],
  701. }`),
  702. "top/nested/Android.bp": []byte(`
  703. gen_notice {
  704. name: "nested-notice",
  705. for: ["libexample"],
  706. }`),
  707. "other/Android.bp": []byte(`
  708. gen_notice {
  709. name: "other-notice",
  710. for: ["libexample"],
  711. }`),
  712. "outsider/Android.bp": []byte(`
  713. gen_notice {
  714. name: "outsider-notice",
  715. for: ["libexample"],
  716. }`),
  717. },
  718. expectedErrors: []string{
  719. `module "outsider-notice" references "//top:libexample" which is not visible to this module`,
  720. },
  721. },
  722. {
  723. name: "//visibility:public mixed with other in defaults",
  724. fs: MockFS{
  725. "top/Android.bp": []byte(`
  726. mock_defaults {
  727. name: "libexample_defaults",
  728. visibility: ["//visibility:public", "//namespace"],
  729. }
  730. mock_library {
  731. name: "libexample",
  732. defaults: ["libexample_defaults"],
  733. }`),
  734. },
  735. expectedErrors: []string{
  736. `module "libexample_defaults": visibility: cannot mix "//visibility:public"` +
  737. ` with any other visibility rules`,
  738. },
  739. },
  740. {
  741. name: "//visibility:public overriding defaults",
  742. fs: MockFS{
  743. "top/Android.bp": []byte(`
  744. mock_defaults {
  745. name: "libexample_defaults",
  746. visibility: ["//namespace"],
  747. }
  748. mock_library {
  749. name: "libexample",
  750. visibility: ["//visibility:public"],
  751. defaults: ["libexample_defaults"],
  752. }`),
  753. "outsider/Android.bp": []byte(`
  754. mock_library {
  755. name: "liboutsider",
  756. deps: ["libexample"],
  757. }`),
  758. },
  759. effectiveVisibility: map[qualifiedModuleName][]string{
  760. qualifiedModuleName{pkg: "top", name: "libexample"}: {"//visibility:public"},
  761. },
  762. },
  763. {
  764. name: "//visibility:public overriding defaults (notices)",
  765. fs: MockFS{
  766. "top/Android.bp": []byte(`
  767. mock_defaults {
  768. name: "libexample_defaults",
  769. visibility: ["//namespace"],
  770. }
  771. mock_library {
  772. name: "libexample",
  773. visibility: ["//visibility:public"],
  774. defaults: ["libexample_defaults"],
  775. }`),
  776. "outsider/Android.bp": []byte(`
  777. gen_notice {
  778. name: "outsider-notice",
  779. for: ["libexample"],
  780. }`),
  781. },
  782. effectiveVisibility: map[qualifiedModuleName][]string{
  783. qualifiedModuleName{pkg: "top", name: "libexample"}: {"//visibility:public"},
  784. },
  785. },
  786. {
  787. name: "//visibility:public mixed with other from different defaults 1",
  788. fs: MockFS{
  789. "top/Android.bp": []byte(`
  790. mock_defaults {
  791. name: "libexample_defaults_1",
  792. visibility: ["//namespace"],
  793. }
  794. mock_defaults {
  795. name: "libexample_defaults_2",
  796. visibility: ["//visibility:public"],
  797. }
  798. mock_library {
  799. name: "libexample",
  800. defaults: ["libexample_defaults_1", "libexample_defaults_2"],
  801. }`),
  802. "outsider/Android.bp": []byte(`
  803. mock_library {
  804. name: "liboutsider",
  805. deps: ["libexample"],
  806. }`),
  807. },
  808. },
  809. {
  810. name: "//visibility:public mixed with other from different defaults 1",
  811. fs: MockFS{
  812. "top/Android.bp": []byte(`
  813. mock_defaults {
  814. name: "libexample_defaults_1",
  815. visibility: ["//namespace"],
  816. }
  817. mock_defaults {
  818. name: "libexample_defaults_2",
  819. visibility: ["//visibility:public"],
  820. }
  821. mock_library {
  822. name: "libexample",
  823. defaults: ["libexample_defaults_1", "libexample_defaults_2"],
  824. }
  825. gen_notice {
  826. name: "libexample-notice",
  827. for: ["libexample"],
  828. }`),
  829. "outsider/Android.bp": []byte(`
  830. gen_notice {
  831. name: "outsider-notice",
  832. for: ["libexample"],
  833. }`),
  834. },
  835. },
  836. {
  837. name: "//visibility:public mixed with other from different defaults 2",
  838. fs: MockFS{
  839. "top/Android.bp": []byte(`
  840. mock_defaults {
  841. name: "libexample_defaults_1",
  842. visibility: ["//visibility:public"],
  843. }
  844. mock_defaults {
  845. name: "libexample_defaults_2",
  846. visibility: ["//namespace"],
  847. }
  848. mock_library {
  849. name: "libexample",
  850. defaults: ["libexample_defaults_1", "libexample_defaults_2"],
  851. }`),
  852. "outsider/Android.bp": []byte(`
  853. mock_library {
  854. name: "liboutsider",
  855. deps: ["libexample"],
  856. }`),
  857. },
  858. },
  859. {
  860. name: "//visibility:public mixed with other from different defaults 2 (notices)",
  861. fs: MockFS{
  862. "top/Android.bp": []byte(`
  863. mock_defaults {
  864. name: "libexample_defaults_1",
  865. visibility: ["//visibility:public"],
  866. }
  867. mock_defaults {
  868. name: "libexample_defaults_2",
  869. visibility: ["//namespace"],
  870. }
  871. mock_library {
  872. name: "libexample",
  873. defaults: ["libexample_defaults_1", "libexample_defaults_2"],
  874. }`),
  875. "outsider/Android.bp": []byte(`
  876. gen_notice {
  877. name: "outsider-notice",
  878. for: ["libexample"],
  879. }`),
  880. },
  881. },
  882. {
  883. name: "//visibility:private in defaults",
  884. fs: MockFS{
  885. "top/Android.bp": []byte(`
  886. mock_defaults {
  887. name: "libexample_defaults",
  888. visibility: ["//visibility:private"],
  889. }
  890. mock_library {
  891. name: "libexample",
  892. defaults: ["libexample_defaults"],
  893. }
  894. mock_library {
  895. name: "libsamepackage",
  896. deps: ["libexample"],
  897. }`),
  898. "top/nested/Android.bp": []byte(`
  899. mock_library {
  900. name: "libnested",
  901. deps: ["libexample"],
  902. }`),
  903. "other/Android.bp": []byte(`
  904. mock_library {
  905. name: "libother",
  906. deps: ["libexample"],
  907. }`),
  908. },
  909. expectedErrors: []string{
  910. `module "libnested" variant "android_common": depends on //top:libexample which is not` +
  911. ` visible to this module`,
  912. `module "libother" variant "android_common": depends on //top:libexample which is not` +
  913. ` visible to this module`,
  914. },
  915. },
  916. {
  917. name: "//visibility:private in defaults (notices)",
  918. fs: MockFS{
  919. "top/Android.bp": []byte(`
  920. mock_defaults {
  921. name: "libexample_defaults",
  922. visibility: ["//visibility:private"],
  923. }
  924. mock_library {
  925. name: "libexample",
  926. defaults: ["libexample_defaults"],
  927. }
  928. gen_notice {
  929. name: "libexample-notice",
  930. for: ["libexample"],
  931. }`),
  932. "top/nested/Android.bp": []byte(`
  933. gen_notice {
  934. name: "nested-notice",
  935. for: ["libexample"],
  936. }`),
  937. "other/Android.bp": []byte(`
  938. gen_notice {
  939. name: "other-notice",
  940. for: ["libexample"],
  941. }`),
  942. },
  943. expectedErrors: []string{
  944. `module "nested-notice" references "//top:libexample" which is not visible to this module`,
  945. `module "other-notice" references "//top:libexample" which is not visible to this module`,
  946. },
  947. },
  948. {
  949. name: "//visibility:private mixed with other in defaults",
  950. fs: MockFS{
  951. "top/Android.bp": []byte(`
  952. mock_defaults {
  953. name: "libexample_defaults",
  954. visibility: ["//visibility:private", "//namespace"],
  955. }
  956. mock_library {
  957. name: "libexample",
  958. defaults: ["libexample_defaults"],
  959. }`),
  960. },
  961. expectedErrors: []string{
  962. `module "libexample_defaults": visibility: cannot mix "//visibility:private"` +
  963. ` with any other visibility rules`,
  964. },
  965. },
  966. {
  967. name: "//visibility:private overriding defaults",
  968. fs: MockFS{
  969. "top/Android.bp": []byte(`
  970. mock_defaults {
  971. name: "libexample_defaults",
  972. visibility: ["//namespace"],
  973. }
  974. mock_library {
  975. name: "libexample",
  976. visibility: ["//visibility:private"],
  977. defaults: ["libexample_defaults"],
  978. }`),
  979. },
  980. expectedErrors: []string{
  981. `module "libexample": visibility: cannot mix "//visibility:private"` +
  982. ` with any other visibility rules`,
  983. },
  984. },
  985. {
  986. name: "//visibility:private in defaults overridden",
  987. fs: MockFS{
  988. "top/Android.bp": []byte(`
  989. mock_defaults {
  990. name: "libexample_defaults",
  991. visibility: ["//visibility:private"],
  992. }
  993. mock_library {
  994. name: "libexample",
  995. visibility: ["//namespace"],
  996. defaults: ["libexample_defaults"],
  997. }`),
  998. },
  999. expectedErrors: []string{
  1000. `module "libexample": visibility: cannot mix "//visibility:private"` +
  1001. ` with any other visibility rules`,
  1002. },
  1003. },
  1004. {
  1005. name: "//visibility:private override //visibility:public",
  1006. fs: MockFS{
  1007. "top/Android.bp": []byte(`
  1008. mock_defaults {
  1009. name: "libexample_defaults",
  1010. visibility: ["//visibility:public"],
  1011. }
  1012. mock_library {
  1013. name: "libexample",
  1014. visibility: ["//visibility:private"],
  1015. defaults: ["libexample_defaults"],
  1016. }`),
  1017. },
  1018. expectedErrors: []string{
  1019. `module "libexample": visibility: cannot mix "//visibility:private" with any other visibility rules`,
  1020. },
  1021. },
  1022. {
  1023. name: "//visibility:public override //visibility:private",
  1024. fs: MockFS{
  1025. "top/Android.bp": []byte(`
  1026. mock_defaults {
  1027. name: "libexample_defaults",
  1028. visibility: ["//visibility:private"],
  1029. }
  1030. mock_library {
  1031. name: "libexample",
  1032. visibility: ["//visibility:public"],
  1033. defaults: ["libexample_defaults"],
  1034. }`),
  1035. },
  1036. expectedErrors: []string{
  1037. `module "libexample": visibility: cannot mix "//visibility:private" with any other visibility rules`,
  1038. },
  1039. },
  1040. {
  1041. name: "//visibility:override must be first in the list",
  1042. fs: MockFS{
  1043. "top/Android.bp": []byte(`
  1044. mock_library {
  1045. name: "libexample",
  1046. visibility: ["//other", "//visibility:override", "//namespace"],
  1047. }`),
  1048. },
  1049. expectedErrors: []string{
  1050. `module "libexample": visibility: "//visibility:override" may only be used at the start of the visibility rules`,
  1051. },
  1052. },
  1053. {
  1054. name: "//visibility:override discards //visibility:private",
  1055. fs: MockFS{
  1056. "top/Android.bp": []byte(`
  1057. mock_defaults {
  1058. name: "libexample_defaults",
  1059. visibility: ["//visibility:private"],
  1060. }
  1061. mock_library {
  1062. name: "libexample",
  1063. // Make this visibility to //other but not //visibility:private
  1064. visibility: ["//visibility:override", "//other"],
  1065. defaults: ["libexample_defaults"],
  1066. }`),
  1067. "other/Android.bp": []byte(`
  1068. mock_library {
  1069. name: "libother",
  1070. deps: ["libexample"],
  1071. }`),
  1072. },
  1073. },
  1074. {
  1075. name: "//visibility:override discards //visibility:private (notices)",
  1076. fs: MockFS{
  1077. "top/Android.bp": []byte(`
  1078. mock_defaults {
  1079. name: "libexample_defaults",
  1080. visibility: ["//visibility:private"],
  1081. }
  1082. mock_library {
  1083. name: "libexample",
  1084. // Make this visibility to //other but not //visibility:private
  1085. visibility: ["//visibility:override", "//other"],
  1086. defaults: ["libexample_defaults"],
  1087. }`),
  1088. "other/Android.bp": []byte(`
  1089. gen_notice {
  1090. name: "other-notice",
  1091. for: ["libexample"],
  1092. }`),
  1093. },
  1094. },
  1095. {
  1096. name: "//visibility:override discards //visibility:public",
  1097. fs: MockFS{
  1098. "top/Android.bp": []byte(`
  1099. mock_defaults {
  1100. name: "libexample_defaults",
  1101. visibility: ["//visibility:public"],
  1102. }
  1103. mock_library {
  1104. name: "libexample",
  1105. // Make this visibility to //other but not //visibility:public
  1106. visibility: ["//visibility:override", "//other"],
  1107. defaults: ["libexample_defaults"],
  1108. }`),
  1109. "other/Android.bp": []byte(`
  1110. mock_library {
  1111. name: "libother",
  1112. deps: ["libexample"],
  1113. }`),
  1114. "namespace/Android.bp": []byte(`
  1115. mock_library {
  1116. name: "libnamespace",
  1117. deps: ["libexample"],
  1118. }`),
  1119. },
  1120. expectedErrors: []string{
  1121. `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`,
  1122. },
  1123. },
  1124. {
  1125. name: "//visibility:override discards //visibility:public (notices)",
  1126. fs: MockFS{
  1127. "top/Android.bp": []byte(`
  1128. mock_defaults {
  1129. name: "libexample_defaults",
  1130. visibility: ["//visibility:public"],
  1131. }
  1132. mock_library {
  1133. name: "libexample",
  1134. // Make this visibility to //other but not //visibility:public
  1135. visibility: ["//visibility:override", "//other"],
  1136. defaults: ["libexample_defaults"],
  1137. }`),
  1138. "other/Android.bp": []byte(`
  1139. gen_notice {
  1140. name: "other-notice",
  1141. for: ["libexample"],
  1142. }`),
  1143. "namespace/Android.bp": []byte(`
  1144. gen_notice {
  1145. name: "namespace-notice",
  1146. for: ["libexample"],
  1147. }`),
  1148. },
  1149. expectedErrors: []string{
  1150. `module "namespace-notice" references "//top:libexample" which is not visible to this module\nYou may need to add "//namespace" to its visibility`,
  1151. },
  1152. },
  1153. {
  1154. name: "//visibility:override discards defaults supplied rules",
  1155. fs: MockFS{
  1156. "top/Android.bp": []byte(`
  1157. mock_defaults {
  1158. name: "libexample_defaults",
  1159. visibility: ["//namespace"],
  1160. }
  1161. mock_library {
  1162. name: "libexample",
  1163. // Make this visibility to //other but not //namespace
  1164. visibility: ["//visibility:override", "//other"],
  1165. defaults: ["libexample_defaults"],
  1166. }`),
  1167. "other/Android.bp": []byte(`
  1168. mock_library {
  1169. name: "libother",
  1170. deps: ["libexample"],
  1171. }`),
  1172. "namespace/Android.bp": []byte(`
  1173. mock_library {
  1174. name: "libnamespace",
  1175. deps: ["libexample"],
  1176. }`),
  1177. },
  1178. expectedErrors: []string{
  1179. `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`,
  1180. },
  1181. },
  1182. {
  1183. name: "//visibility:override discards defaults supplied rules (notices)",
  1184. fs: MockFS{
  1185. "top/Android.bp": []byte(`
  1186. mock_defaults {
  1187. name: "libexample_defaults",
  1188. visibility: ["//namespace"],
  1189. }
  1190. mock_library {
  1191. name: "libexample",
  1192. // Make this visibility to //other but not //namespace
  1193. visibility: ["//visibility:override", "//other"],
  1194. defaults: ["libexample_defaults"],
  1195. }`),
  1196. "other/Android.bp": []byte(`
  1197. gen_notice {
  1198. name: "other-notice",
  1199. for: ["libexample"],
  1200. }`),
  1201. "namespace/Android.bp": []byte(`
  1202. gen_notice {
  1203. name: "namespace-notice",
  1204. for: ["libexample"],
  1205. }`),
  1206. },
  1207. expectedErrors: []string{
  1208. `module "namespace-notice" references "//top:libexample" which is not visible to this module\nYou may need to add "//namespace" to its visibility`,
  1209. },
  1210. },
  1211. {
  1212. name: "//visibility:override can override //visibility:public with //visibility:private",
  1213. fs: MockFS{
  1214. "top/Android.bp": []byte(`
  1215. mock_defaults {
  1216. name: "libexample_defaults",
  1217. visibility: ["//visibility:public"],
  1218. }
  1219. mock_library {
  1220. name: "libexample",
  1221. visibility: ["//visibility:override", "//visibility:private"],
  1222. defaults: ["libexample_defaults"],
  1223. }`),
  1224. "namespace/Android.bp": []byte(`
  1225. mock_library {
  1226. name: "libnamespace",
  1227. deps: ["libexample"],
  1228. }`),
  1229. },
  1230. expectedErrors: []string{
  1231. `module "libnamespace" variant "android_common": depends on //top:libexample which is not visible to this module`,
  1232. },
  1233. },
  1234. {
  1235. name: "//visibility:override can override //visibility:public with //visibility:private (notices)",
  1236. fs: MockFS{
  1237. "top/Android.bp": []byte(`
  1238. mock_defaults {
  1239. name: "libexample_defaults",
  1240. visibility: ["//visibility:public"],
  1241. }
  1242. mock_library {
  1243. name: "libexample",
  1244. visibility: ["//visibility:override", "//visibility:private"],
  1245. defaults: ["libexample_defaults"],
  1246. }`),
  1247. "namespace/Android.bp": []byte(`
  1248. gen_notice {
  1249. name: "namespace-notice",
  1250. for: ["libexample"],
  1251. }`),
  1252. },
  1253. expectedErrors: []string{
  1254. `module "namespace-notice" references "//top:libexample" which is not visible to this module`,
  1255. },
  1256. },
  1257. {
  1258. name: "//visibility:override can override //visibility:private with //visibility:public",
  1259. fs: MockFS{
  1260. "top/Android.bp": []byte(`
  1261. mock_defaults {
  1262. name: "libexample_defaults",
  1263. visibility: ["//visibility:private"],
  1264. }
  1265. mock_library {
  1266. name: "libexample",
  1267. visibility: ["//visibility:override", "//visibility:public"],
  1268. defaults: ["libexample_defaults"],
  1269. }`),
  1270. "namespace/Android.bp": []byte(`
  1271. mock_library {
  1272. name: "libnamespace",
  1273. deps: ["libexample"],
  1274. }`),
  1275. },
  1276. },
  1277. {
  1278. name: "//visibility:override can override //visibility:private with //visibility:public (notices)",
  1279. fs: MockFS{
  1280. "top/Android.bp": []byte(`
  1281. mock_defaults {
  1282. name: "libexample_defaults",
  1283. visibility: ["//visibility:private"],
  1284. }
  1285. mock_library {
  1286. name: "libexample",
  1287. visibility: ["//visibility:override", "//visibility:public"],
  1288. defaults: ["libexample_defaults"],
  1289. }`),
  1290. "namespace/Android.bp": []byte(`
  1291. gen_notice {
  1292. name: "namespace-notice",
  1293. for: ["libexample"],
  1294. }`),
  1295. },
  1296. },
  1297. {
  1298. name: "//visibility:private mixed with itself",
  1299. fs: MockFS{
  1300. "top/Android.bp": []byte(`
  1301. mock_defaults {
  1302. name: "libexample_defaults_1",
  1303. visibility: ["//visibility:private"],
  1304. }
  1305. mock_defaults {
  1306. name: "libexample_defaults_2",
  1307. visibility: ["//visibility:private"],
  1308. }
  1309. mock_library {
  1310. name: "libexample",
  1311. visibility: ["//visibility:private"],
  1312. defaults: ["libexample_defaults_1", "libexample_defaults_2"],
  1313. }`),
  1314. "outsider/Android.bp": []byte(`
  1315. mock_library {
  1316. name: "liboutsider",
  1317. deps: ["libexample"],
  1318. }`),
  1319. },
  1320. expectedErrors: []string{
  1321. `module "liboutsider" variant "android_common": depends on //top:libexample which is not` +
  1322. ` visible to this module`,
  1323. },
  1324. },
  1325. {
  1326. name: "//visibility:private mixed with itself (notices)",
  1327. fs: MockFS{
  1328. "top/Android.bp": []byte(`
  1329. mock_defaults {
  1330. name: "libexample_defaults_1",
  1331. visibility: ["//visibility:private"],
  1332. }
  1333. mock_defaults {
  1334. name: "libexample_defaults_2",
  1335. visibility: ["//visibility:private"],
  1336. }
  1337. mock_library {
  1338. name: "libexample",
  1339. visibility: ["//visibility:private"],
  1340. defaults: ["libexample_defaults_1", "libexample_defaults_2"],
  1341. }`),
  1342. "outsider/Android.bp": []byte(`
  1343. gen_notice {
  1344. name: "outsider-notice",
  1345. for: ["libexample"],
  1346. }`),
  1347. },
  1348. expectedErrors: []string{
  1349. `module "outsider-notice" references "//top:libexample" which is not visible to this module`,
  1350. },
  1351. },
  1352. // Defaults module's defaults_visibility tests
  1353. {
  1354. name: "defaults_visibility invalid",
  1355. fs: MockFS{
  1356. "top/Android.bp": []byte(`
  1357. mock_defaults {
  1358. name: "top_defaults",
  1359. defaults_visibility: ["//visibility:invalid"],
  1360. }`),
  1361. },
  1362. expectedErrors: []string{
  1363. `defaults_visibility: unrecognized visibility rule "//visibility:invalid"`,
  1364. },
  1365. },
  1366. {
  1367. name: "defaults_visibility overrides package default",
  1368. fs: MockFS{
  1369. "top/Android.bp": []byte(`
  1370. package {
  1371. default_visibility: ["//visibility:private"],
  1372. }
  1373. mock_defaults {
  1374. name: "top_defaults",
  1375. defaults_visibility: ["//visibility:public"],
  1376. }`),
  1377. "outsider/Android.bp": []byte(`
  1378. mock_library {
  1379. name: "liboutsider",
  1380. defaults: ["top_defaults"],
  1381. }`),
  1382. },
  1383. },
  1384. // Package default_visibility tests
  1385. {
  1386. name: "package default_visibility property is checked",
  1387. fs: MockFS{
  1388. "top/Android.bp": []byte(`
  1389. package {
  1390. default_visibility: ["//visibility:invalid"],
  1391. }`),
  1392. },
  1393. expectedErrors: []string{`default_visibility: unrecognized visibility rule "//visibility:invalid"`},
  1394. },
  1395. {
  1396. // This test relies on the default visibility being legacy_public.
  1397. name: "package default_visibility property used when no visibility specified",
  1398. fs: MockFS{
  1399. "top/Android.bp": []byte(`
  1400. package {
  1401. default_visibility: ["//visibility:private"],
  1402. }
  1403. mock_library {
  1404. name: "libexample",
  1405. }`),
  1406. "outsider/Android.bp": []byte(`
  1407. mock_library {
  1408. name: "liboutsider",
  1409. deps: ["libexample"],
  1410. }`),
  1411. },
  1412. expectedErrors: []string{
  1413. `module "liboutsider" variant "android_common": depends on //top:libexample which is not` +
  1414. ` visible to this module`,
  1415. },
  1416. },
  1417. {
  1418. // This test relies on the default visibility being legacy_public.
  1419. name: "package default_visibility property used when no visibility specified (notices)",
  1420. fs: MockFS{
  1421. "top/Android.bp": []byte(`
  1422. package {
  1423. default_visibility: ["//visibility:private"],
  1424. }
  1425. mock_library {
  1426. name: "libexample",
  1427. }`),
  1428. "outsider/Android.bp": []byte(`
  1429. gen_notice {
  1430. name: "outsider-notice",
  1431. for: ["libexample"],
  1432. }`),
  1433. },
  1434. expectedErrors: []string{
  1435. `module "outsider-notice" references "//top:libexample" which is not visible to this module`,
  1436. },
  1437. },
  1438. {
  1439. name: "package default_visibility public does not override visibility private",
  1440. fs: MockFS{
  1441. "top/Android.bp": []byte(`
  1442. package {
  1443. default_visibility: ["//visibility:public"],
  1444. }
  1445. mock_library {
  1446. name: "libexample",
  1447. visibility: ["//visibility:private"],
  1448. }`),
  1449. "outsider/Android.bp": []byte(`
  1450. mock_library {
  1451. name: "liboutsider",
  1452. deps: ["libexample"],
  1453. }`),
  1454. },
  1455. expectedErrors: []string{
  1456. `module "liboutsider" variant "android_common": depends on //top:libexample which is not` +
  1457. ` visible to this module`,
  1458. },
  1459. },
  1460. {
  1461. name: "package default_visibility public does not override visibility private (notices)",
  1462. fs: MockFS{
  1463. "top/Android.bp": []byte(`
  1464. package {
  1465. default_visibility: ["//visibility:public"],
  1466. }
  1467. mock_library {
  1468. name: "libexample",
  1469. visibility: ["//visibility:private"],
  1470. }`),
  1471. "outsider/Android.bp": []byte(`
  1472. gen_notice {
  1473. name: "outsider-notice",
  1474. for: ["libexample"],
  1475. }`),
  1476. },
  1477. expectedErrors: []string{
  1478. `module "outsider-notice" references "//top:libexample" which is not visible to this module`,
  1479. },
  1480. },
  1481. {
  1482. name: "package default_visibility private does not override visibility public",
  1483. fs: MockFS{
  1484. "top/Android.bp": []byte(`
  1485. package {
  1486. default_visibility: ["//visibility:private"],
  1487. }
  1488. mock_library {
  1489. name: "libexample",
  1490. visibility: ["//visibility:public"],
  1491. }`),
  1492. "outsider/Android.bp": []byte(`
  1493. mock_library {
  1494. name: "liboutsider",
  1495. deps: ["libexample"],
  1496. }`),
  1497. },
  1498. },
  1499. {
  1500. name: "package default_visibility private does not override visibility public (notices)",
  1501. fs: MockFS{
  1502. "top/Android.bp": []byte(`
  1503. package {
  1504. default_visibility: ["//visibility:private"],
  1505. }
  1506. mock_library {
  1507. name: "libexample",
  1508. visibility: ["//visibility:public"],
  1509. }`),
  1510. "outsider/Android.bp": []byte(`
  1511. gen_notice {
  1512. name: "outsider-notice",
  1513. for: ["libexample"],
  1514. }`),
  1515. },
  1516. },
  1517. {
  1518. name: "package default_visibility :__subpackages__",
  1519. fs: MockFS{
  1520. "top/Android.bp": []byte(`
  1521. package {
  1522. default_visibility: [":__subpackages__"],
  1523. }
  1524. mock_library {
  1525. name: "libexample",
  1526. }`),
  1527. "top/nested/Android.bp": []byte(`
  1528. mock_library {
  1529. name: "libnested",
  1530. deps: ["libexample"],
  1531. }`),
  1532. "outsider/Android.bp": []byte(`
  1533. mock_library {
  1534. name: "liboutsider",
  1535. deps: ["libexample"],
  1536. }`),
  1537. },
  1538. expectedErrors: []string{
  1539. `module "liboutsider" variant "android_common": depends on //top:libexample which is not` +
  1540. ` visible to this module`,
  1541. },
  1542. },
  1543. {
  1544. name: "package default_visibility :__subpackages__ (notices)",
  1545. fs: MockFS{
  1546. "top/Android.bp": []byte(`
  1547. package {
  1548. default_visibility: [":__subpackages__"],
  1549. }
  1550. mock_library {
  1551. name: "libexample",
  1552. }`),
  1553. "top/nested/Android.bp": []byte(`
  1554. gen_notice {
  1555. name: "nested-notice",
  1556. for: ["libexample"],
  1557. }`),
  1558. "outsider/Android.bp": []byte(`
  1559. gen_notice {
  1560. name: "outsider-notice",
  1561. for: ["libexample"],
  1562. }`),
  1563. },
  1564. expectedErrors: []string{
  1565. `module "outsider-notice" references "//top:libexample" which is not visible to this module`,
  1566. },
  1567. },
  1568. {
  1569. name: "package default_visibility inherited to subpackages",
  1570. fs: MockFS{
  1571. "top/Android.bp": []byte(`
  1572. package {
  1573. default_visibility: ["//outsider"],
  1574. }
  1575. mock_library {
  1576. name: "libexample",
  1577. visibility: [":__subpackages__"],
  1578. }`),
  1579. "top/nested/Android.bp": []byte(`
  1580. mock_library {
  1581. name: "libnested",
  1582. deps: ["libexample"],
  1583. }`),
  1584. "outsider/Android.bp": []byte(`
  1585. mock_library {
  1586. name: "liboutsider",
  1587. deps: ["libexample", "libnested"],
  1588. }`),
  1589. },
  1590. expectedErrors: []string{
  1591. `module "liboutsider" variant "android_common": depends on //top:libexample which is not` +
  1592. ` visible to this module`,
  1593. },
  1594. },
  1595. {
  1596. name: "package default_visibility inherited to subpackages (notices)",
  1597. fs: MockFS{
  1598. "top/Android.bp": []byte(`
  1599. package {
  1600. default_visibility: ["//outsider"],
  1601. }
  1602. mock_library {
  1603. name: "libexample",
  1604. visibility: [":__subpackages__"],
  1605. }`),
  1606. "top/nested/Android.bp": []byte(`
  1607. mock_library {
  1608. name: "libnested",
  1609. deps: ["libexample"],
  1610. }
  1611. gen_notice {
  1612. name: "nested-notice",
  1613. for: ["libexample"],
  1614. }`),
  1615. "outsider/Android.bp": []byte(`
  1616. gen_notice {
  1617. name: "outsider-notice",
  1618. for: ["libexample", "libnested"],
  1619. }`),
  1620. },
  1621. expectedErrors: []string{
  1622. `module "outsider-notice" references "//top:libexample" which is not visible to this module`,
  1623. },
  1624. },
  1625. {
  1626. name: "package default_visibility inherited to subpackages",
  1627. fs: MockFS{
  1628. "top/Android.bp": []byte(`
  1629. package {
  1630. default_visibility: ["//visibility:private"],
  1631. }`),
  1632. "top/nested/Android.bp": []byte(`
  1633. package {
  1634. default_visibility: ["//outsider"],
  1635. }
  1636. mock_library {
  1637. name: "libnested",
  1638. }`),
  1639. "top/other/Android.bp": []byte(`
  1640. mock_library {
  1641. name: "libother",
  1642. }`),
  1643. "outsider/Android.bp": []byte(`
  1644. mock_library {
  1645. name: "liboutsider",
  1646. deps: ["libother", "libnested"],
  1647. }`),
  1648. },
  1649. expectedErrors: []string{
  1650. `module "liboutsider" variant "android_common": depends on //top/other:libother which is` +
  1651. ` not visible to this module`,
  1652. },
  1653. },
  1654. {
  1655. name: "package default_visibility inherited to subpackages (notices)",
  1656. fs: MockFS{
  1657. "top/Android.bp": []byte(`
  1658. package {
  1659. default_visibility: ["//visibility:private"],
  1660. }`),
  1661. "top/nested/Android.bp": []byte(`
  1662. package {
  1663. default_visibility: ["//outsider"],
  1664. }
  1665. mock_library {
  1666. name: "libnested",
  1667. }`),
  1668. "top/other/Android.bp": []byte(`
  1669. mock_library {
  1670. name: "libother",
  1671. }
  1672. gen_notice {
  1673. name: "other-notice",
  1674. for: ["libother"],
  1675. }`),
  1676. "outsider/Android.bp": []byte(`
  1677. gen_notice {
  1678. name: "outsider-notice",
  1679. for: ["libother", "libnested"],
  1680. }`),
  1681. },
  1682. expectedErrors: []string{
  1683. `module "outsider-notice" references "//top/other:libother" which is not visible to this` +
  1684. ` module\nYou may need to add "//outsider" to its visibility`,
  1685. },
  1686. },
  1687. {
  1688. name: "verify that prebuilt dependencies are ignored for visibility reasons (not preferred)",
  1689. fs: MockFS{
  1690. "prebuilts/Android.bp": []byte(`
  1691. prebuilt {
  1692. name: "module",
  1693. visibility: ["//top/other"],
  1694. }`),
  1695. "top/sources/source_file": nil,
  1696. "top/sources/Android.bp": []byte(`
  1697. source {
  1698. name: "module",
  1699. visibility: ["//top/other"],
  1700. }`),
  1701. "top/other/source_file": nil,
  1702. "top/other/Android.bp": []byte(`
  1703. source {
  1704. name: "other",
  1705. deps: [":module"],
  1706. }`),
  1707. },
  1708. },
  1709. {
  1710. name: "verify that prebuilt dependencies are ignored for visibility reasons (not preferred) (notices)",
  1711. fs: MockFS{
  1712. "prebuilts/Android.bp": []byte(`
  1713. prebuilt {
  1714. name: "module",
  1715. visibility: ["//top/other"],
  1716. }`),
  1717. "top/sources/source_file": nil,
  1718. "top/sources/Android.bp": []byte(`
  1719. source {
  1720. name: "module",
  1721. visibility: ["//top/other"],
  1722. }`),
  1723. "top/other/source_file": nil,
  1724. "top/other/Android.bp": []byte(`
  1725. gen_notice {
  1726. name: "other-notice",
  1727. for: ["module"],
  1728. }`),
  1729. },
  1730. },
  1731. {
  1732. name: "verify that prebuilt dependencies are ignored for visibility reasons (preferred)",
  1733. fs: MockFS{
  1734. "prebuilts/Android.bp": []byte(`
  1735. prebuilt {
  1736. name: "module",
  1737. visibility: ["//top/other"],
  1738. prefer: true,
  1739. }`),
  1740. "top/sources/source_file": nil,
  1741. "top/sources/Android.bp": []byte(`
  1742. source {
  1743. name: "module",
  1744. visibility: ["//top/other"],
  1745. }`),
  1746. "top/other/source_file": nil,
  1747. "top/other/Android.bp": []byte(`
  1748. source {
  1749. name: "other",
  1750. deps: [":module"],
  1751. }`),
  1752. },
  1753. },
  1754. {
  1755. name: "verify that prebuilt dependencies are ignored for visibility reasons (preferred) (notices)",
  1756. fs: MockFS{
  1757. "prebuilts/Android.bp": []byte(`
  1758. prebuilt {
  1759. name: "module",
  1760. visibility: ["//top/other"],
  1761. prefer: true,
  1762. }`),
  1763. "top/sources/source_file": nil,
  1764. "top/sources/Android.bp": []byte(`
  1765. source {
  1766. name: "module",
  1767. visibility: ["//top/other"],
  1768. }`),
  1769. "top/other/source_file": nil,
  1770. "top/other/Android.bp": []byte(`
  1771. gen_notice {
  1772. name: "other-notice",
  1773. for: ["module"],
  1774. }`),
  1775. },
  1776. },
  1777. {
  1778. name: "ensure visibility properties are checked for correctness",
  1779. fs: MockFS{
  1780. "top/Android.bp": []byte(`
  1781. mock_parent {
  1782. name: "parent",
  1783. visibility: ["//top/nested"],
  1784. child: {
  1785. name: "libchild",
  1786. visibility: ["top/other"],
  1787. },
  1788. }`),
  1789. },
  1790. expectedErrors: []string{
  1791. `module "parent": child.visibility: invalid visibility pattern "top/other"`,
  1792. },
  1793. },
  1794. {
  1795. name: "invalid visibility added to child detected during gather phase",
  1796. fs: MockFS{
  1797. "top/Android.bp": []byte(`
  1798. mock_parent {
  1799. name: "parent",
  1800. visibility: ["//top/nested"],
  1801. child: {
  1802. name: "libchild",
  1803. invalid_visibility: ["top/other"],
  1804. },
  1805. }`),
  1806. },
  1807. expectedErrors: []string{
  1808. // That this error is reported against the child not the parent shows it was
  1809. // not being detected in the parent which is correct as invalid_visibility is
  1810. // purposely not added to the list of visibility properties to check, and was
  1811. // in fact detected in the child in the gather phase. Contrast this error message
  1812. // with the preceding one.
  1813. `module "libchild" \(created by module "parent"\): visibility: invalid visibility pattern "top/other"`,
  1814. },
  1815. },
  1816. {
  1817. name: "automatic visibility inheritance enabled",
  1818. fs: MockFS{
  1819. "top/Android.bp": []byte(`
  1820. mock_parent {
  1821. name: "parent",
  1822. visibility: ["//top/nested"],
  1823. child: {
  1824. name: "libchild",
  1825. visibility: ["//top/other"],
  1826. },
  1827. }`),
  1828. "top/nested/Android.bp": []byte(`
  1829. mock_library {
  1830. name: "libnested",
  1831. deps: ["libchild"],
  1832. }`),
  1833. "top/other/Android.bp": []byte(`
  1834. mock_library {
  1835. name: "libother",
  1836. deps: ["libchild"],
  1837. }`),
  1838. },
  1839. },
  1840. {
  1841. name: "automatic visibility inheritance enabled (notices)",
  1842. fs: MockFS{
  1843. "top/Android.bp": []byte(`
  1844. mock_parent {
  1845. name: "parent",
  1846. visibility: ["//top/nested"],
  1847. child: {
  1848. name: "libchild",
  1849. visibility: ["//top/other"],
  1850. },
  1851. }`),
  1852. "top/nested/Android.bp": []byte(`
  1853. gen_notice {
  1854. name: "nested-notice",
  1855. for: ["libchild"],
  1856. }`),
  1857. "top/other/Android.bp": []byte(`
  1858. gen_notice {
  1859. name: "other-notice",
  1860. for: ["libchild"],
  1861. }`),
  1862. },
  1863. },
  1864. }
  1865. func TestVisibility(t *testing.T) {
  1866. for _, test := range visibilityTests {
  1867. t.Run(test.name, func(t *testing.T) {
  1868. result := GroupFixturePreparers(
  1869. // General preparers in alphabetical order as test infrastructure will enforce correct
  1870. // registration order.
  1871. PrepareForTestWithArchMutator,
  1872. PrepareForTestWithDefaults,
  1873. PrepareForTestWithGenNotice,
  1874. PrepareForTestWithOverrides,
  1875. PrepareForTestWithPackageModule,
  1876. PrepareForTestWithPrebuilts,
  1877. PrepareForTestWithVisibility,
  1878. // Additional test specific preparers.
  1879. FixtureRegisterWithContext(func(ctx RegistrationContext) {
  1880. ctx.RegisterModuleType("mock_library", newMockLibraryModule)
  1881. ctx.RegisterModuleType("mock_parent", newMockParentFactory)
  1882. ctx.RegisterModuleType("mock_defaults", defaultsFactory)
  1883. }),
  1884. prepareForTestWithFakePrebuiltModules,
  1885. // Add additional files to the mock filesystem
  1886. test.fs.AddToFixture(),
  1887. ).
  1888. ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
  1889. RunTest(t)
  1890. if test.effectiveVisibility != nil {
  1891. checkEffectiveVisibility(t, result, test.effectiveVisibility)
  1892. }
  1893. })
  1894. }
  1895. }
  1896. func checkEffectiveVisibility(t *testing.T, result *TestResult, effectiveVisibility map[qualifiedModuleName][]string) {
  1897. for moduleName, expectedRules := range effectiveVisibility {
  1898. rule := effectiveVisibilityRules(result.Config, moduleName)
  1899. stringRules := rule.Strings()
  1900. AssertDeepEquals(t, "effective rules mismatch", expectedRules, stringRules)
  1901. }
  1902. }
  1903. type mockLibraryProperties struct {
  1904. Deps []string
  1905. }
  1906. type mockLibraryModule struct {
  1907. ModuleBase
  1908. DefaultableModuleBase
  1909. properties mockLibraryProperties
  1910. }
  1911. func newMockLibraryModule() Module {
  1912. m := &mockLibraryModule{}
  1913. m.AddProperties(&m.properties)
  1914. InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon)
  1915. InitDefaultableModule(m)
  1916. return m
  1917. }
  1918. type dependencyTag struct {
  1919. blueprint.BaseDependencyTag
  1920. name string
  1921. }
  1922. func (j *mockLibraryModule) DepsMutator(ctx BottomUpMutatorContext) {
  1923. ctx.AddVariationDependencies(nil, dependencyTag{name: "mockdeps"}, j.properties.Deps...)
  1924. }
  1925. func (p *mockLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
  1926. }
  1927. type mockDefaults struct {
  1928. ModuleBase
  1929. DefaultsModuleBase
  1930. }
  1931. func defaultsFactory() Module {
  1932. m := &mockDefaults{}
  1933. InitDefaultsModule(m)
  1934. return m
  1935. }
  1936. type mockParentProperties struct {
  1937. Child struct {
  1938. Name *string
  1939. // Visibility to pass to the child module.
  1940. Visibility []string
  1941. // Purposely not validated visibility to pass to the child.
  1942. Invalid_visibility []string
  1943. }
  1944. }
  1945. type mockParent struct {
  1946. ModuleBase
  1947. DefaultableModuleBase
  1948. properties mockParentProperties
  1949. }
  1950. func (p *mockParent) GenerateAndroidBuildActions(ModuleContext) {
  1951. }
  1952. func newMockParentFactory() Module {
  1953. m := &mockParent{}
  1954. m.AddProperties(&m.properties)
  1955. InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon)
  1956. InitDefaultableModule(m)
  1957. AddVisibilityProperty(m, "child.visibility", &m.properties.Child.Visibility)
  1958. m.SetDefaultableHook(func(ctx DefaultableHookContext) {
  1959. visibility := m.properties.Child.Visibility
  1960. visibility = append(visibility, m.properties.Child.Invalid_visibility...)
  1961. ctx.CreateModule(newMockLibraryModule, &struct {
  1962. Name *string
  1963. Visibility []string
  1964. }{m.properties.Child.Name, visibility})
  1965. })
  1966. return m
  1967. }
  1968. func testVisibilityRuleSet(t *testing.T, rules, extra, expected []string) {
  1969. t.Helper()
  1970. set := &visibilityRuleSet{rules}
  1971. err := set.Widen(extra)
  1972. if err != nil {
  1973. t.Error(err)
  1974. return
  1975. }
  1976. actual := set.Strings()
  1977. if !reflect.DeepEqual(actual, expected) {
  1978. t.Errorf("mismatching rules after extend: expected %#v, actual %#v", expected, actual)
  1979. }
  1980. }
  1981. func TestVisibilityRuleSet(t *testing.T) {
  1982. t.Run("extend empty", func(t *testing.T) {
  1983. testVisibilityRuleSet(t, nil, []string{"//foo"}, []string{"//foo"})
  1984. })
  1985. t.Run("extend", func(t *testing.T) {
  1986. testVisibilityRuleSet(t, []string{"//foo"}, []string{"//bar"}, []string{"//bar", "//foo"})
  1987. })
  1988. t.Run("extend duplicate", func(t *testing.T) {
  1989. testVisibilityRuleSet(t, []string{"//foo"}, []string{"//bar", "//foo"}, []string{"//bar", "//foo"})
  1990. })
  1991. t.Run("extend public", func(t *testing.T) {
  1992. testVisibilityRuleSet(t, []string{"//visibility:public"}, []string{"//foo"}, []string{"//visibility:public"})
  1993. })
  1994. t.Run("extend private", func(t *testing.T) {
  1995. testVisibilityRuleSet(t, []string{"//visibility:private"}, []string{"//foo"}, []string{"//foo"})
  1996. })
  1997. t.Run("extend with public", func(t *testing.T) {
  1998. testVisibilityRuleSet(t, []string{"//foo"}, []string{"//visibility:public"}, []string{"//visibility:public"})
  1999. })
  2000. t.Run("extend with private", func(t *testing.T) {
  2001. t.Helper()
  2002. set := &visibilityRuleSet{[]string{"//foo"}}
  2003. err := set.Widen([]string{"//visibility:private"})
  2004. expectedError := `"//visibility:private" does not widen the visibility`
  2005. if err == nil {
  2006. t.Errorf("missing error")
  2007. } else if err.Error() != expectedError {
  2008. t.Errorf("expected error %q found error %q", expectedError, err)
  2009. }
  2010. })
  2011. }