bpfix_test.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  1. // Copyright 2017 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // This file implements the logic of bpfix and also provides a programmatic interface
  15. package bpfix
  16. import (
  17. "bytes"
  18. "fmt"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. "github.com/google/blueprint/parser"
  23. "github.com/google/blueprint/pathtools"
  24. )
  25. // TODO(jeffrygaston) remove this when position is removed from ParseNode (in b/38325146) and we can directly do reflect.DeepEqual
  26. func printListOfStrings(items []string) (text string) {
  27. if len(items) == 0 {
  28. return "[]"
  29. }
  30. return fmt.Sprintf("[\"%s\"]", strings.Join(items, "\", \""))
  31. }
  32. func buildTree(local_include_dirs []string, export_include_dirs []string) (file *parser.File, errs []error) {
  33. // TODO(jeffrygaston) use the builder class when b/38325146 is done
  34. input := fmt.Sprintf(`cc_library_shared {
  35. name: "iAmAModule",
  36. local_include_dirs: %s,
  37. export_include_dirs: %s,
  38. }
  39. `,
  40. printListOfStrings(local_include_dirs), printListOfStrings(export_include_dirs))
  41. tree, errs := parser.Parse("", strings.NewReader(input), parser.NewScope(nil))
  42. if len(errs) > 0 {
  43. errs = append([]error{fmt.Errorf("failed to parse:\n%s", input)}, errs...)
  44. }
  45. return tree, errs
  46. }
  47. func implFilterListTest(t *testing.T, local_include_dirs []string, export_include_dirs []string, expectedResult []string) {
  48. // build tree
  49. tree, errs := buildTree(local_include_dirs, export_include_dirs)
  50. if len(errs) > 0 {
  51. t.Error("failed to build tree")
  52. for _, err := range errs {
  53. t.Error(err)
  54. }
  55. t.Fatalf("%d parse errors", len(errs))
  56. }
  57. fixer := NewFixer(tree)
  58. // apply simplifications
  59. err := runPatchListMod(simplifyKnownPropertiesDuplicatingEachOther)(fixer)
  60. if len(errs) > 0 {
  61. t.Fatal(err)
  62. }
  63. // lookup legacy property
  64. mod := fixer.tree.Defs[0].(*parser.Module)
  65. expectedResultString := fmt.Sprintf("%q", expectedResult)
  66. if expectedResult == nil {
  67. expectedResultString = "unset"
  68. }
  69. // check that the value for the legacy property was updated to the correct value
  70. errorHeader := fmt.Sprintf("\nFailed to correctly simplify key 'local_include_dirs' in the presence of 'export_include_dirs.'\n"+
  71. "original local_include_dirs: %q\n"+
  72. "original export_include_dirs: %q\n"+
  73. "expected result: %s\n"+
  74. "actual result: ",
  75. local_include_dirs, export_include_dirs, expectedResultString)
  76. result, found := mod.GetProperty("local_include_dirs")
  77. if !found {
  78. if expectedResult == nil {
  79. return
  80. }
  81. t.Fatal(errorHeader + "property not found")
  82. }
  83. listResult, ok := result.Value.(*parser.List)
  84. if !ok {
  85. t.Fatalf("%sproperty is not a list: %v", errorHeader, listResult)
  86. }
  87. if expectedResult == nil {
  88. t.Fatalf("%sproperty exists: %v", errorHeader, listResult)
  89. }
  90. actualExpressions := listResult.Values
  91. actualValues := make([]string, 0)
  92. for _, expr := range actualExpressions {
  93. str := expr.(*parser.String)
  94. actualValues = append(actualValues, str.Value)
  95. }
  96. if !reflect.DeepEqual(actualValues, expectedResult) {
  97. t.Fatalf("%s%q\nlists are different", errorHeader, actualValues)
  98. }
  99. }
  100. func TestSimplifyKnownVariablesDuplicatingEachOther(t *testing.T) {
  101. // TODO use []Expression{} once buildTree above can support it (which is after b/38325146 is done)
  102. implFilterListTest(t, []string{"include"}, []string{"include"}, nil)
  103. implFilterListTest(t, []string{"include1"}, []string{"include2"}, []string{"include1"})
  104. implFilterListTest(t, []string{"include1", "include2", "include3", "include4"}, []string{"include2"},
  105. []string{"include1", "include3", "include4"})
  106. implFilterListTest(t, []string{}, []string{"include"}, []string{})
  107. implFilterListTest(t, []string{}, []string{}, []string{})
  108. }
  109. func checkError(t *testing.T, in, expectedErr string, innerTest func(*Fixer) error) {
  110. expected := preProcessOutErr(expectedErr)
  111. runTestOnce(t, in, expected, innerTest)
  112. }
  113. func runTestOnce(t *testing.T, in, expected string, innerTest func(*Fixer) error) {
  114. fixer, err := preProcessIn(in)
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. out, err := runFixerOnce(fixer, innerTest)
  119. if err != nil {
  120. out = err.Error()
  121. }
  122. compareResult := compareOutExpected(in, out, expected)
  123. if len(compareResult) > 0 {
  124. t.Errorf(compareResult)
  125. }
  126. }
  127. func preProcessOutErr(expectedErr string) string {
  128. expected := strings.TrimSpace(expectedErr)
  129. return expected
  130. }
  131. func preProcessOut(out string) (expected string, err error) {
  132. expected, err = Reformat(out)
  133. if err != nil {
  134. return expected, err
  135. }
  136. return expected, nil
  137. }
  138. func preProcessIn(in string) (fixer *Fixer, err error) {
  139. in, err = Reformat(in)
  140. if err != nil {
  141. return fixer, err
  142. }
  143. tree, errs := parser.Parse("<testcase>", bytes.NewBufferString(in), parser.NewScope(nil))
  144. if errs != nil {
  145. return fixer, err
  146. }
  147. fixer = NewFixer(tree)
  148. return fixer, nil
  149. }
  150. func runFixerOnce(fixer *Fixer, innerTest func(*Fixer) error) (string, error) {
  151. err := innerTest(fixer)
  152. if err != nil {
  153. return "", err
  154. }
  155. out, err := parser.Print(fixer.tree)
  156. if err != nil {
  157. return "", err
  158. }
  159. return string(out), nil
  160. }
  161. func compareOutExpected(in, out, expected string) string {
  162. if out != expected {
  163. return fmt.Sprintf("output didn't match:\ninput:\n%s\n\nexpected:\n%s\ngot:\n%s\n",
  164. in, expected, out)
  165. }
  166. return ""
  167. }
  168. func runPassOnce(t *testing.T, in, out string, innerTest func(*Fixer) error) {
  169. expected, err := preProcessOut(out)
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. runTestOnce(t, in, expected, innerTest)
  174. }
  175. func runPass(t *testing.T, in, out string, innerTest func(*Fixer) error) {
  176. expected, err := preProcessOut(out)
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. fixer, err := preProcessIn(in)
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. got := ""
  185. prev := "foo"
  186. passes := 0
  187. for got != prev && passes < 10 {
  188. out, err = runFixerOnce(fixer, innerTest)
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. prev = got
  193. got = string(out)
  194. passes++
  195. }
  196. compareResult := compareOutExpected(in, out, expected)
  197. if len(compareResult) > 0 {
  198. t.Errorf(compareResult)
  199. }
  200. }
  201. func TestMergeMatchingProperties(t *testing.T) {
  202. tests := []struct {
  203. name string
  204. in string
  205. out string
  206. }{
  207. {
  208. name: "empty",
  209. in: `
  210. java_library {
  211. name: "foo",
  212. static_libs: [],
  213. static_libs: [],
  214. }
  215. `,
  216. out: `
  217. java_library {
  218. name: "foo",
  219. static_libs: [],
  220. }
  221. `,
  222. },
  223. {
  224. name: "single line into multiline",
  225. in: `
  226. java_library {
  227. name: "foo",
  228. static_libs: [
  229. "a",
  230. "b",
  231. ],
  232. //c1
  233. static_libs: ["c" /*c2*/],
  234. }
  235. `,
  236. out: `
  237. java_library {
  238. name: "foo",
  239. static_libs: [
  240. "a",
  241. "b",
  242. "c", /*c2*/
  243. ],
  244. //c1
  245. }
  246. `,
  247. },
  248. {
  249. name: "multiline into multiline",
  250. in: `
  251. java_library {
  252. name: "foo",
  253. static_libs: [
  254. "a",
  255. "b",
  256. ],
  257. //c1
  258. static_libs: [
  259. //c2
  260. "c", //c3
  261. "d",
  262. ],
  263. }
  264. `,
  265. out: `
  266. java_library {
  267. name: "foo",
  268. static_libs: [
  269. "a",
  270. "b",
  271. //c2
  272. "c", //c3
  273. "d",
  274. ],
  275. //c1
  276. }
  277. `,
  278. },
  279. }
  280. for _, test := range tests {
  281. t.Run(test.name, func(t *testing.T) {
  282. runPass(t, test.in, test.out, func(fixer *Fixer) error {
  283. return runPatchListMod(mergeMatchingModuleProperties)(fixer)
  284. })
  285. })
  286. }
  287. }
  288. func TestReorderCommonProperties(t *testing.T) {
  289. var tests = []struct {
  290. name string
  291. in string
  292. out string
  293. }{
  294. {
  295. name: "empty",
  296. in: `cc_library {}`,
  297. out: `cc_library {}`,
  298. },
  299. {
  300. name: "only priority",
  301. in: `
  302. cc_library {
  303. name: "foo",
  304. }
  305. `,
  306. out: `
  307. cc_library {
  308. name: "foo",
  309. }
  310. `,
  311. },
  312. {
  313. name: "already in order",
  314. in: `
  315. cc_library {
  316. name: "foo",
  317. defaults: ["bar"],
  318. }
  319. `,
  320. out: `
  321. cc_library {
  322. name: "foo",
  323. defaults: ["bar"],
  324. }
  325. `,
  326. },
  327. {
  328. name: "reorder only priority",
  329. in: `
  330. cc_library {
  331. defaults: ["bar"],
  332. name: "foo",
  333. }
  334. `,
  335. out: `
  336. cc_library {
  337. name: "foo",
  338. defaults: ["bar"],
  339. }
  340. `,
  341. },
  342. {
  343. name: "reorder",
  344. in: `
  345. cc_library {
  346. name: "foo",
  347. srcs: ["a.c"],
  348. host_supported: true,
  349. defaults: ["bar"],
  350. shared_libs: ["baz"],
  351. }
  352. `,
  353. out: `
  354. cc_library {
  355. name: "foo",
  356. defaults: ["bar"],
  357. host_supported: true,
  358. srcs: ["a.c"],
  359. shared_libs: ["baz"],
  360. }
  361. `,
  362. },
  363. }
  364. for _, test := range tests {
  365. t.Run(test.name, func(t *testing.T) {
  366. runPass(t, test.in, test.out, func(fixer *Fixer) error {
  367. return runPatchListMod(reorderCommonProperties)(fixer)
  368. })
  369. })
  370. }
  371. }
  372. func TestRemoveMatchingModuleListProperties(t *testing.T) {
  373. var tests = []struct {
  374. name string
  375. in string
  376. out string
  377. }{
  378. {
  379. name: "simple",
  380. in: `
  381. cc_library {
  382. name: "foo",
  383. foo: ["a"],
  384. bar: ["a"],
  385. }
  386. `,
  387. out: `
  388. cc_library {
  389. name: "foo",
  390. bar: ["a"],
  391. }
  392. `,
  393. },
  394. {
  395. name: "long",
  396. in: `
  397. cc_library {
  398. name: "foo",
  399. foo: [
  400. "a",
  401. "b",
  402. ],
  403. bar: ["a"],
  404. }
  405. `,
  406. out: `
  407. cc_library {
  408. name: "foo",
  409. foo: [
  410. "b",
  411. ],
  412. bar: ["a"],
  413. }
  414. `,
  415. },
  416. {
  417. name: "long fully removed",
  418. in: `
  419. cc_library {
  420. name: "foo",
  421. foo: [
  422. "a",
  423. ],
  424. bar: ["a"],
  425. }
  426. `,
  427. out: `
  428. cc_library {
  429. name: "foo",
  430. bar: ["a"],
  431. }
  432. `,
  433. },
  434. {
  435. name: "comment",
  436. in: `
  437. cc_library {
  438. name: "foo",
  439. // comment
  440. foo: ["a"],
  441. bar: ["a"],
  442. }
  443. `,
  444. out: `
  445. cc_library {
  446. name: "foo",
  447. // comment
  448. bar: ["a"],
  449. }
  450. `,
  451. },
  452. {
  453. name: "inner comment",
  454. in: `
  455. cc_library {
  456. name: "foo",
  457. foo: [
  458. // comment
  459. "a",
  460. ],
  461. bar: ["a"],
  462. }
  463. `,
  464. out: `
  465. cc_library {
  466. name: "foo",
  467. bar: ["a"],
  468. }
  469. `,
  470. },
  471. {
  472. name: "eol comment",
  473. in: `
  474. cc_library {
  475. name: "foo",
  476. foo: ["a"], // comment
  477. bar: ["a"],
  478. }
  479. `,
  480. out: `
  481. cc_library {
  482. name: "foo",
  483. // comment
  484. bar: ["a"],
  485. }
  486. `,
  487. },
  488. {
  489. name: "eol comment with blank lines",
  490. in: `
  491. cc_library {
  492. name: "foo",
  493. foo: ["a"], // comment
  494. // bar
  495. bar: ["a"],
  496. }
  497. `,
  498. out: `
  499. cc_library {
  500. name: "foo",
  501. // comment
  502. // bar
  503. bar: ["a"],
  504. }
  505. `,
  506. },
  507. }
  508. for _, test := range tests {
  509. t.Run(test.name, func(t *testing.T) {
  510. runPass(t, test.in, test.out, func(fixer *Fixer) error {
  511. return runPatchListMod(func(mod *parser.Module, buf []byte, patchList *parser.PatchList) error {
  512. return removeMatchingModuleListProperties(mod, patchList, "bar", "foo")
  513. })(fixer)
  514. })
  515. })
  516. }
  517. }
  518. func TestReplaceJavaStaticLibs(t *testing.T) {
  519. tests := []struct {
  520. name string
  521. in string
  522. out string
  523. }{
  524. {
  525. name: "static lib",
  526. in: `
  527. java_library_static {
  528. name: "foo",
  529. }
  530. `,
  531. out: `
  532. java_library {
  533. name: "foo",
  534. }
  535. `,
  536. },
  537. {
  538. name: "java lib",
  539. in: `
  540. java_library {
  541. name: "foo",
  542. }
  543. `,
  544. out: `
  545. java_library {
  546. name: "foo",
  547. }
  548. `,
  549. },
  550. {
  551. name: "java installable lib",
  552. in: `
  553. java_library {
  554. name: "foo",
  555. installable: true,
  556. }
  557. `,
  558. out: `
  559. java_library {
  560. name: "foo",
  561. installable: true,
  562. }
  563. `,
  564. },
  565. }
  566. for _, test := range tests {
  567. t.Run(test.name, func(t *testing.T) {
  568. runPass(t, test.in, test.out, func(fixer *Fixer) error {
  569. return rewriteJavaStaticLibs(fixer)
  570. })
  571. })
  572. }
  573. }
  574. func TestRewritePrebuilts(t *testing.T) {
  575. tests := []struct {
  576. name string
  577. in string
  578. out string
  579. }{
  580. {
  581. name: "jar srcs",
  582. in: `
  583. java_import {
  584. name: "foo",
  585. srcs: ["foo.jar"],
  586. }
  587. `,
  588. out: `
  589. java_import {
  590. name: "foo",
  591. jars: ["foo.jar"],
  592. }
  593. `,
  594. },
  595. {
  596. name: "aar srcs",
  597. in: `
  598. java_import {
  599. name: "foo",
  600. srcs: ["foo.aar"],
  601. installable: true,
  602. }
  603. `,
  604. out: `
  605. android_library_import {
  606. name: "foo",
  607. aars: ["foo.aar"],
  608. }
  609. `,
  610. },
  611. {
  612. name: "host prebuilt",
  613. in: `
  614. java_import {
  615. name: "foo",
  616. srcs: ["foo.jar"],
  617. host: true,
  618. }
  619. `,
  620. out: `
  621. java_import_host {
  622. name: "foo",
  623. jars: ["foo.jar"],
  624. }
  625. `,
  626. },
  627. }
  628. for _, test := range tests {
  629. t.Run(test.name, func(t *testing.T) {
  630. runPass(t, test.in, test.out, func(fixer *Fixer) error {
  631. return rewriteIncorrectAndroidmkPrebuilts(fixer)
  632. })
  633. })
  634. }
  635. }
  636. func TestRewriteCtsModuleTypes(t *testing.T) {
  637. tests := []struct {
  638. name string
  639. in string
  640. out string
  641. }{
  642. {
  643. name: "cts_support_package",
  644. in: `
  645. cts_support_package {
  646. name: "foo",
  647. }
  648. `,
  649. out: `
  650. android_test_helper_app {
  651. name: "foo",
  652. defaults: ["cts_support_defaults"],
  653. }
  654. `,
  655. },
  656. {
  657. name: "cts_package",
  658. in: `
  659. cts_package {
  660. name: "foo",
  661. }
  662. `,
  663. out: `
  664. android_test {
  665. name: "foo",
  666. defaults: ["cts_defaults"],
  667. }
  668. `,
  669. },
  670. {
  671. name: "cts_target_java_library",
  672. in: `
  673. cts_target_java_library {
  674. name: "foo",
  675. }
  676. `,
  677. out: `
  678. java_library {
  679. name: "foo",
  680. defaults: ["cts_defaults"],
  681. }
  682. `,
  683. },
  684. {
  685. name: "cts_host_java_library",
  686. in: `
  687. cts_host_java_library {
  688. name: "foo",
  689. }
  690. `,
  691. out: `
  692. java_library_host {
  693. name: "foo",
  694. defaults: ["cts_defaults"],
  695. }
  696. `,
  697. },
  698. }
  699. for _, test := range tests {
  700. t.Run(test.name, func(t *testing.T) {
  701. runPass(t, test.in, test.out, rewriteCtsModuleTypes)
  702. })
  703. }
  704. }
  705. func TestRewritePrebuiltEtc(t *testing.T) {
  706. tests := []struct {
  707. name string
  708. in string
  709. out string
  710. }{
  711. {
  712. name: "prebuilt_etc src",
  713. in: `
  714. prebuilt_etc {
  715. name: "foo",
  716. srcs: ["bar"],
  717. }
  718. `,
  719. out: `prebuilt_etc {
  720. name: "foo",
  721. src: "bar",
  722. }
  723. `,
  724. },
  725. {
  726. name: "prebuilt_etc src",
  727. in: `
  728. prebuilt_etc {
  729. name: "foo",
  730. srcs: FOO,
  731. }
  732. `,
  733. out: `prebuilt_etc {
  734. name: "foo",
  735. src: FOO,
  736. }
  737. `,
  738. },
  739. {
  740. name: "prebuilt_etc src",
  741. in: `
  742. prebuilt_etc {
  743. name: "foo",
  744. srcs: ["bar", "baz"],
  745. }
  746. `,
  747. out: `prebuilt_etc {
  748. name: "foo",
  749. src: "ERROR: LOCAL_SRC_FILES should contain at most one item",
  750. }
  751. `,
  752. },
  753. {
  754. name: "prebuilt_etc sub_dir",
  755. in: `
  756. prebuilt_etc {
  757. name: "foo",
  758. src: "bar",
  759. sub_dir: "baz",
  760. }
  761. `,
  762. out: `prebuilt_etc {
  763. name: "foo",
  764. src: "bar",
  765. relative_install_path: "baz",
  766. }
  767. `,
  768. },
  769. {
  770. name: "prebuilt_etc sub_dir",
  771. in: `
  772. prebuilt_etc_host {
  773. name: "foo",
  774. src: "bar",
  775. local_module_path: {
  776. var: "HOST_OUT",
  777. fixed: "/etc/baz",
  778. },
  779. }
  780. `,
  781. out: `prebuilt_etc_host {
  782. name: "foo",
  783. src: "bar",
  784. relative_install_path: "baz",
  785. }
  786. `,
  787. },
  788. {
  789. name: "prebuilt_etc sub_dir",
  790. in: `
  791. prebuilt_etc_host {
  792. name: "foo",
  793. src: "bar",
  794. local_module_path: {
  795. var: "HOST_OUT",
  796. fixed: "/baz/sub",
  797. },
  798. }
  799. `,
  800. out: `prebuilt_root_host {
  801. name: "foo",
  802. src: "bar",
  803. relative_install_path: "baz/sub",
  804. }
  805. `,
  806. },
  807. }
  808. for _, test := range tests {
  809. t.Run(test.name, func(t *testing.T) {
  810. runPass(t, test.in, test.out, func(fixer *Fixer) error {
  811. return rewriteAndroidmkPrebuiltEtc(fixer)
  812. })
  813. })
  814. }
  815. }
  816. func TestRewriteAndroidTest(t *testing.T) {
  817. tests := []struct {
  818. name string
  819. in string
  820. out string
  821. }{
  822. {
  823. name: "android_test valid module path",
  824. in: `
  825. android_test {
  826. name: "foo",
  827. local_module_path: {
  828. var: "TARGET_OUT_DATA_APPS",
  829. },
  830. }
  831. `,
  832. out: `
  833. android_test {
  834. name: "foo",
  835. }
  836. `,
  837. },
  838. }
  839. for _, test := range tests {
  840. t.Run(test.name, func(t *testing.T) {
  841. runPass(t, test.in, test.out, func(fixer *Fixer) error {
  842. return rewriteAndroidTest(fixer)
  843. })
  844. })
  845. }
  846. }
  847. func TestRewriteAndroidAppImport(t *testing.T) {
  848. tests := []struct {
  849. name string
  850. in string
  851. out string
  852. }{
  853. {
  854. name: "android_app_import apk",
  855. in: `
  856. android_app_import {
  857. name: "foo",
  858. srcs: ["package.apk"],
  859. }
  860. `,
  861. out: `
  862. android_app_import {
  863. name: "foo",
  864. apk: "package.apk",
  865. }
  866. `,
  867. },
  868. {
  869. name: "android_app_import presigned",
  870. in: `
  871. android_app_import {
  872. name: "foo",
  873. apk: "package.apk",
  874. certificate: "PRESIGNED",
  875. }
  876. `,
  877. out: `
  878. android_app_import {
  879. name: "foo",
  880. apk: "package.apk",
  881. presigned: true,
  882. }
  883. `,
  884. },
  885. }
  886. for _, test := range tests {
  887. t.Run(test.name, func(t *testing.T) {
  888. runPass(t, test.in, test.out, func(fixer *Fixer) error {
  889. return rewriteAndroidAppImport(fixer)
  890. })
  891. })
  892. }
  893. }
  894. func TestRemoveEmptyLibDependencies(t *testing.T) {
  895. tests := []struct {
  896. name string
  897. in string
  898. out string
  899. }{
  900. {
  901. name: "remove sole shared lib",
  902. in: `
  903. cc_library {
  904. name: "foo",
  905. shared_libs: ["libhwbinder"],
  906. }
  907. `,
  908. out: `
  909. cc_library {
  910. name: "foo",
  911. }
  912. `,
  913. },
  914. {
  915. name: "remove a shared lib",
  916. in: `
  917. cc_library {
  918. name: "foo",
  919. shared_libs: [
  920. "libhwbinder",
  921. "libfoo",
  922. "libhidltransport",
  923. ],
  924. }
  925. `,
  926. out: `
  927. cc_library {
  928. name: "foo",
  929. shared_libs: [
  930. "libfoo",
  931. ],
  932. }
  933. `,
  934. },
  935. }
  936. for _, test := range tests {
  937. t.Run(test.name, func(t *testing.T) {
  938. runPass(t, test.in, test.out, func(fixer *Fixer) error {
  939. return removeEmptyLibDependencies(fixer)
  940. })
  941. })
  942. }
  943. }
  944. func TestRemoveHidlInterfaceTypes(t *testing.T) {
  945. tests := []struct {
  946. name string
  947. in string
  948. out string
  949. }{
  950. {
  951. name: "remove types",
  952. in: `
  953. hidl_interface {
  954. name: "foo@1.0",
  955. types: ["ParcelFooBar"],
  956. }
  957. `,
  958. out: `
  959. hidl_interface {
  960. name: "foo@1.0",
  961. }
  962. `,
  963. },
  964. }
  965. for _, test := range tests {
  966. t.Run(test.name, func(t *testing.T) {
  967. runPass(t, test.in, test.out, func(fixer *Fixer) error {
  968. return removeHidlInterfaceTypes(fixer)
  969. })
  970. })
  971. }
  972. }
  973. func TestRemoveSoongConfigBoolVariable(t *testing.T) {
  974. tests := []struct {
  975. name string
  976. in string
  977. out string
  978. }{
  979. {
  980. name: "remove bool",
  981. in: `
  982. soong_config_module_type {
  983. name: "foo",
  984. variables: ["bar", "baz"],
  985. }
  986. soong_config_bool_variable {
  987. name: "bar",
  988. }
  989. soong_config_string_variable {
  990. name: "baz",
  991. }
  992. `,
  993. out: `
  994. soong_config_module_type {
  995. name: "foo",
  996. variables: [
  997. "baz"
  998. ],
  999. bool_variables: ["bar"],
  1000. }
  1001. soong_config_string_variable {
  1002. name: "baz",
  1003. }
  1004. `,
  1005. },
  1006. {
  1007. name: "existing bool_variables",
  1008. in: `
  1009. soong_config_module_type {
  1010. name: "foo",
  1011. variables: ["baz"],
  1012. bool_variables: ["bar"],
  1013. }
  1014. soong_config_bool_variable {
  1015. name: "baz",
  1016. }
  1017. `,
  1018. out: `
  1019. soong_config_module_type {
  1020. name: "foo",
  1021. bool_variables: ["bar", "baz"],
  1022. }
  1023. `,
  1024. },
  1025. }
  1026. for _, test := range tests {
  1027. t.Run(test.name, func(t *testing.T) {
  1028. runPass(t, test.in, test.out, removeSoongConfigBoolVariable)
  1029. })
  1030. }
  1031. }
  1032. func TestRemoveNestedProperty(t *testing.T) {
  1033. tests := []struct {
  1034. name string
  1035. in string
  1036. out string
  1037. propertyName string
  1038. }{
  1039. {
  1040. name: "remove no nesting",
  1041. in: `
  1042. cc_library {
  1043. name: "foo",
  1044. foo: true,
  1045. }`,
  1046. out: `
  1047. cc_library {
  1048. name: "foo",
  1049. }
  1050. `,
  1051. propertyName: "foo",
  1052. },
  1053. {
  1054. name: "remove one nest",
  1055. in: `
  1056. cc_library {
  1057. name: "foo",
  1058. foo: {
  1059. bar: true,
  1060. },
  1061. }`,
  1062. out: `
  1063. cc_library {
  1064. name: "foo",
  1065. }
  1066. `,
  1067. propertyName: "foo.bar",
  1068. },
  1069. {
  1070. name: "remove one nest, multiple props",
  1071. in: `
  1072. cc_library {
  1073. name: "foo",
  1074. foo: {
  1075. bar: true,
  1076. baz: false,
  1077. },
  1078. }`,
  1079. out: `
  1080. cc_library {
  1081. name: "foo",
  1082. foo: {
  1083. baz: false,
  1084. },
  1085. }
  1086. `,
  1087. propertyName: "foo.bar",
  1088. },
  1089. {
  1090. name: "remove multiple nest",
  1091. in: `
  1092. cc_library {
  1093. name: "foo",
  1094. foo: {
  1095. bar: {
  1096. baz: {
  1097. a: true,
  1098. }
  1099. },
  1100. },
  1101. }`,
  1102. out: `
  1103. cc_library {
  1104. name: "foo",
  1105. }
  1106. `,
  1107. propertyName: "foo.bar.baz.a",
  1108. },
  1109. {
  1110. name: "remove multiple nest, outer non-empty",
  1111. in: `
  1112. cc_library {
  1113. name: "foo",
  1114. foo: {
  1115. bar: {
  1116. baz: {
  1117. a: true,
  1118. }
  1119. },
  1120. other: true,
  1121. },
  1122. }`,
  1123. out: `
  1124. cc_library {
  1125. name: "foo",
  1126. foo: {
  1127. other: true,
  1128. },
  1129. }
  1130. `,
  1131. propertyName: "foo.bar.baz.a",
  1132. },
  1133. {
  1134. name: "remove multiple nest, inner non-empty",
  1135. in: `
  1136. cc_library {
  1137. name: "foo",
  1138. foo: {
  1139. bar: {
  1140. baz: {
  1141. a: true,
  1142. },
  1143. other: true,
  1144. },
  1145. },
  1146. }`,
  1147. out: `
  1148. cc_library {
  1149. name: "foo",
  1150. foo: {
  1151. bar: {
  1152. other: true,
  1153. },
  1154. },
  1155. }
  1156. `,
  1157. propertyName: "foo.bar.baz.a",
  1158. },
  1159. {
  1160. name: "remove multiple nest, inner-most non-empty",
  1161. in: `
  1162. cc_library {
  1163. name: "foo",
  1164. foo: {
  1165. bar: {
  1166. baz: {
  1167. a: true,
  1168. other: true,
  1169. },
  1170. },
  1171. },
  1172. }`,
  1173. out: `
  1174. cc_library {
  1175. name: "foo",
  1176. foo: {
  1177. bar: {
  1178. baz: {
  1179. other: true,
  1180. },
  1181. },
  1182. },
  1183. }
  1184. `,
  1185. propertyName: "foo.bar.baz.a",
  1186. },
  1187. }
  1188. for _, test := range tests {
  1189. t.Run(test.name, func(t *testing.T) {
  1190. runPass(t, test.in, test.out, runPatchListMod(removeObsoleteProperty(test.propertyName)))
  1191. })
  1192. }
  1193. }
  1194. func TestRemoveObsoleteProperties(t *testing.T) {
  1195. tests := []struct {
  1196. name string
  1197. in string
  1198. out string
  1199. }{
  1200. {
  1201. name: "remove property",
  1202. in: `
  1203. cc_library_shared {
  1204. name: "foo",
  1205. product_variables: {
  1206. other: {
  1207. bar: true,
  1208. },
  1209. pdk: {
  1210. enabled: false,
  1211. },
  1212. },
  1213. }
  1214. `,
  1215. out: `
  1216. cc_library_shared {
  1217. name: "foo",
  1218. product_variables: {
  1219. other: {
  1220. bar: true,
  1221. },
  1222. },
  1223. }
  1224. `,
  1225. },
  1226. {
  1227. name: "remove property and empty product_variables",
  1228. in: `
  1229. cc_library_shared {
  1230. name: "foo",
  1231. product_variables: {
  1232. pdk: {
  1233. enabled: false,
  1234. },
  1235. },
  1236. }
  1237. `,
  1238. out: `
  1239. cc_library_shared {
  1240. name: "foo",
  1241. }
  1242. `,
  1243. },
  1244. }
  1245. for _, test := range tests {
  1246. t.Run(test.name, func(t *testing.T) {
  1247. runPass(t, test.in, test.out, runPatchListMod(removeObsoleteProperty("product_variables.pdk")))
  1248. })
  1249. }
  1250. }
  1251. func TestRewriteRuntimeResourceOverlay(t *testing.T) {
  1252. tests := []struct {
  1253. name string
  1254. in string
  1255. out string
  1256. }{
  1257. {
  1258. name: "product_specific runtime_resource_overlay",
  1259. in: `
  1260. runtime_resource_overlay {
  1261. name: "foo",
  1262. resource_dirs: ["res"],
  1263. product_specific: true,
  1264. }
  1265. `,
  1266. out: `
  1267. runtime_resource_overlay {
  1268. name: "foo",
  1269. resource_dirs: ["res"],
  1270. product_specific: true,
  1271. }
  1272. `,
  1273. },
  1274. {
  1275. // It's probably wrong for runtime_resource_overlay not to be product specific, but let's not
  1276. // debate it here.
  1277. name: "non-product_specific runtime_resource_overlay",
  1278. in: `
  1279. runtime_resource_overlay {
  1280. name: "foo",
  1281. resource_dirs: ["res"],
  1282. product_specific: false,
  1283. }
  1284. `,
  1285. out: `
  1286. runtime_resource_overlay {
  1287. name: "foo",
  1288. resource_dirs: ["res"],
  1289. product_specific: false,
  1290. }
  1291. `,
  1292. },
  1293. {
  1294. name: "runtime_resource_overlay without product_specific value",
  1295. in: `
  1296. runtime_resource_overlay {
  1297. name: "foo",
  1298. resource_dirs: ["res"],
  1299. }
  1300. `,
  1301. out: `
  1302. runtime_resource_overlay {
  1303. name: "foo",
  1304. resource_dirs: ["res"],
  1305. product_specific: true,
  1306. }
  1307. `,
  1308. },
  1309. }
  1310. for _, test := range tests {
  1311. t.Run(test.name, func(t *testing.T) {
  1312. runPass(t, test.in, test.out, func(fixer *Fixer) error {
  1313. return RewriteRuntimeResourceOverlay(fixer)
  1314. })
  1315. })
  1316. }
  1317. }
  1318. func TestRewriteTestModuleTypes(t *testing.T) {
  1319. tests := []struct {
  1320. name string
  1321. in string
  1322. out string
  1323. }{
  1324. {
  1325. name: "cc_binary with test_suites",
  1326. in: `
  1327. cc_binary {
  1328. name: "foo",
  1329. srcs: ["srcs"],
  1330. test_suites: ["test_suite1"],
  1331. }
  1332. `,
  1333. out: `
  1334. cc_test {
  1335. name: "foo",
  1336. srcs: ["srcs"],
  1337. test_suites: ["test_suite1"],
  1338. }
  1339. `,
  1340. },
  1341. {
  1342. name: "cc_binary without test_suites",
  1343. in: `
  1344. cc_binary {
  1345. name: "foo",
  1346. srcs: ["srcs"],
  1347. }
  1348. `,
  1349. out: `
  1350. cc_binary {
  1351. name: "foo",
  1352. srcs: ["srcs"],
  1353. }
  1354. `,
  1355. },
  1356. {
  1357. name: "android_app with android_test",
  1358. in: `
  1359. android_app {
  1360. name: "foo",
  1361. srcs: ["srcs"],
  1362. test_suites: ["test_suite1"],
  1363. }
  1364. `,
  1365. out: `
  1366. android_test {
  1367. name: "foo",
  1368. srcs: ["srcs"],
  1369. test_suites: ["test_suite1"],
  1370. }
  1371. `,
  1372. },
  1373. {
  1374. name: "android_app without test_suites",
  1375. in: `
  1376. android_app {
  1377. name: "foo",
  1378. srcs: ["srcs"],
  1379. }
  1380. `,
  1381. out: `
  1382. android_app {
  1383. name: "foo",
  1384. srcs: ["srcs"],
  1385. }
  1386. `,
  1387. },
  1388. }
  1389. for _, test := range tests {
  1390. t.Run(test.name, func(t *testing.T) {
  1391. runPass(t, test.in, test.out, func(fixer *Fixer) error {
  1392. return rewriteTestModuleTypes(fixer)
  1393. })
  1394. })
  1395. }
  1396. }
  1397. func TestFormatFlagProperty(t *testing.T) {
  1398. tests := []struct {
  1399. name string
  1400. in string
  1401. out string
  1402. }{
  1403. {
  1404. name: "group options and values for apptflags, dxflags, javacflags, and kotlincflags",
  1405. in: `
  1406. android_test {
  1407. name: "foo",
  1408. aaptflags: [
  1409. // comment1_1
  1410. "--flag1",
  1411. // comment1_2
  1412. "1",
  1413. // comment2_1
  1414. // comment2_2
  1415. "--flag2",
  1416. // comment3_1
  1417. // comment3_2
  1418. // comment3_3
  1419. "--flag3",
  1420. // comment3_4
  1421. // comment3_5
  1422. // comment3_6
  1423. "3",
  1424. // other comment1_1
  1425. // other comment1_2
  1426. ],
  1427. dxflags: [
  1428. "--flag1",
  1429. // comment1_1
  1430. "1",
  1431. // comment2_1
  1432. "--flag2",
  1433. // comment3_1
  1434. "--flag3",
  1435. // comment3_2
  1436. "3",
  1437. ],
  1438. javacflags: [
  1439. "--flag1",
  1440. "1",
  1441. "--flag2",
  1442. "--flag3",
  1443. "3",
  1444. ],
  1445. kotlincflags: [
  1446. "--flag1",
  1447. "1",
  1448. "--flag2",
  1449. "--flag3",
  1450. "3",
  1451. ],
  1452. }
  1453. `,
  1454. out: `
  1455. android_test {
  1456. name: "foo",
  1457. aaptflags: [
  1458. // comment1_1
  1459. // comment1_2
  1460. "--flag1 1",
  1461. // comment2_1
  1462. // comment2_2
  1463. "--flag2",
  1464. // comment3_1
  1465. // comment3_2
  1466. // comment3_3
  1467. // comment3_4
  1468. // comment3_5
  1469. // comment3_6
  1470. "--flag3 3",
  1471. // other comment1_1
  1472. // other comment1_2
  1473. ],
  1474. dxflags: [
  1475. // comment1_1
  1476. "--flag1 1",
  1477. // comment2_1
  1478. "--flag2",
  1479. // comment3_1
  1480. // comment3_2
  1481. "--flag3 3",
  1482. ],
  1483. javacflags: [
  1484. "--flag1 1",
  1485. "--flag2",
  1486. "--flag3 3",
  1487. ],
  1488. kotlincflags: [
  1489. "--flag1 1",
  1490. "--flag2",
  1491. "--flag3 3",
  1492. ],
  1493. }
  1494. `,
  1495. },
  1496. {
  1497. name: "group options and values for asflags, cflags, clang_asflags, clang_cflags, conlyflags, cppflags, ldflags, and tidy_flags",
  1498. in: `
  1499. cc_test {
  1500. name: "foo",
  1501. asflags: [
  1502. // comment1_1
  1503. "--flag1",
  1504. "1",
  1505. // comment2_1
  1506. // comment2_2
  1507. "--flag2",
  1508. // comment2_3
  1509. "2",
  1510. // comment3_1
  1511. // comment3_2
  1512. "--flag3",
  1513. // comment3_3
  1514. // comment3_4
  1515. // comment3_4
  1516. "3",
  1517. // comment4_1
  1518. // comment4_2
  1519. // comment4_3
  1520. "--flag4",
  1521. ],
  1522. cflags: [
  1523. "--flag1",
  1524. "1",
  1525. "--flag2",
  1526. "2",
  1527. "--flag3",
  1528. "3",
  1529. "--flag4",
  1530. ],
  1531. clang_asflags: [
  1532. "--flag1",
  1533. "1",
  1534. "--flag2",
  1535. "2",
  1536. "--flag3",
  1537. "3",
  1538. "--flag4",
  1539. ],
  1540. clang_cflags: [
  1541. "--flag1",
  1542. "1",
  1543. "--flag2",
  1544. "2",
  1545. "--flag3",
  1546. "3",
  1547. "--flag4",
  1548. ],
  1549. conlyflags: [
  1550. "--flag1",
  1551. "1",
  1552. "--flag2",
  1553. "2",
  1554. "--flag3",
  1555. "3",
  1556. "--flag4",
  1557. ],
  1558. cppflags: [
  1559. "--flag1",
  1560. "1",
  1561. "--flag2",
  1562. "2",
  1563. "--flag3",
  1564. "3",
  1565. "--flag4",
  1566. ],
  1567. ldflags: [
  1568. "--flag1",
  1569. "1",
  1570. "--flag2",
  1571. "2",
  1572. "--flag3",
  1573. "3",
  1574. "--flag4",
  1575. ],
  1576. tidy_flags: [
  1577. "--flag1",
  1578. "1",
  1579. "--flag2",
  1580. "2",
  1581. "--flag3",
  1582. "3",
  1583. "--flag4",
  1584. ],
  1585. }
  1586. `,
  1587. out: `
  1588. cc_test {
  1589. name: "foo",
  1590. asflags: [
  1591. // comment1_1
  1592. "--flag1 1",
  1593. // comment2_1
  1594. // comment2_2
  1595. // comment2_3
  1596. "--flag2 2",
  1597. // comment3_1
  1598. // comment3_2
  1599. // comment3_3
  1600. // comment3_4
  1601. // comment3_4
  1602. "--flag3 3",
  1603. // comment4_1
  1604. // comment4_2
  1605. // comment4_3
  1606. "--flag4",
  1607. ],
  1608. cflags: [
  1609. "--flag1 1",
  1610. "--flag2 2",
  1611. "--flag3 3",
  1612. "--flag4",
  1613. ],
  1614. clang_asflags: [
  1615. "--flag1 1",
  1616. "--flag2 2",
  1617. "--flag3 3",
  1618. "--flag4",
  1619. ],
  1620. clang_cflags: [
  1621. "--flag1 1",
  1622. "--flag2 2",
  1623. "--flag3 3",
  1624. "--flag4",
  1625. ],
  1626. conlyflags: [
  1627. "--flag1 1",
  1628. "--flag2 2",
  1629. "--flag3 3",
  1630. "--flag4",
  1631. ],
  1632. cppflags: [
  1633. "--flag1 1",
  1634. "--flag2 2",
  1635. "--flag3 3",
  1636. "--flag4",
  1637. ],
  1638. ldflags: [
  1639. "--flag1 1",
  1640. "--flag2 2",
  1641. "--flag3 3",
  1642. "--flag4",
  1643. ],
  1644. tidy_flags: [
  1645. "--flag1 1",
  1646. "--flag2 2",
  1647. "--flag3 3",
  1648. "--flag4",
  1649. ],
  1650. }
  1651. `,
  1652. },
  1653. }
  1654. for _, test := range tests {
  1655. t.Run(test.name, func(t *testing.T) {
  1656. runPass(t, test.in, test.out, runPatchListMod(formatFlagProperties))
  1657. })
  1658. }
  1659. }
  1660. func TestRewriteLicenseProperty(t *testing.T) {
  1661. mockFs := pathtools.MockFs(map[string][]byte{
  1662. "a/b/c/d/Android.mk": []byte("this is not important."),
  1663. "a/b/LicenseFile1": []byte("LicenseFile1"),
  1664. "a/b/LicenseFile2": []byte("LicenseFile2"),
  1665. "a/b/Android.bp": []byte("license {\n\tname: \"reuse_a_b_license\",\n}\n"),
  1666. })
  1667. relativePath := "a/b/c/d"
  1668. relativePathErr := "a/b/c"
  1669. tests := []struct {
  1670. name string
  1671. in string
  1672. fs pathtools.FileSystem
  1673. path string
  1674. out string
  1675. }{
  1676. {
  1677. name: "license rewriting with one module",
  1678. in: `
  1679. android_test {
  1680. name: "foo",
  1681. android_license_kinds: ["license_kind"],
  1682. android_license_conditions: ["license_notice"],
  1683. }
  1684. `,
  1685. out: `
  1686. package {
  1687. // See: http://go/android-license-faq
  1688. default_applicable_licenses: [
  1689. "Android-Apache-2.0",
  1690. ],
  1691. }
  1692. android_test {
  1693. name: "foo",
  1694. android_license_kinds: ["license_kind"],
  1695. android_license_conditions: ["license_notice"],
  1696. }
  1697. `,
  1698. },
  1699. {
  1700. name: "license rewriting with two modules",
  1701. in: `
  1702. android_test {
  1703. name: "foo1",
  1704. android_license_kinds: ["license_kind1"],
  1705. android_license_conditions: ["license_notice1"],
  1706. }
  1707. android_test {
  1708. name: "foo2",
  1709. android_license_kinds: ["license_kind2"],
  1710. android_license_conditions: ["license_notice2"],
  1711. }
  1712. `,
  1713. out: `
  1714. package {
  1715. // See: http://go/android-license-faq
  1716. default_applicable_licenses: [
  1717. "Android-Apache-2.0",
  1718. ],
  1719. }
  1720. android_test {
  1721. name: "foo1",
  1722. android_license_kinds: ["license_kind1"],
  1723. android_license_conditions: ["license_notice1"],
  1724. }
  1725. android_test {
  1726. name: "foo2",
  1727. android_license_kinds: ["license_kind2"],
  1728. android_license_conditions: ["license_notice2"],
  1729. }
  1730. `,
  1731. },
  1732. {
  1733. name: "license rewriting with license files in the current directory",
  1734. in: `
  1735. android_test {
  1736. name: "foo",
  1737. android_license_kinds: ["license_kind"],
  1738. android_license_conditions: ["license_notice"],
  1739. android_license_files: ["LicenseFile1", "LicenseFile2",],
  1740. }
  1741. `,
  1742. fs: mockFs,
  1743. path: relativePath,
  1744. out: `
  1745. package {
  1746. // See: http://go/android-license-faq
  1747. default_applicable_licenses: [
  1748. "a_b_c_d_license",
  1749. ],
  1750. }
  1751. license {
  1752. name: "a_b_c_d_license",
  1753. visibility: [":__subpackages__"],
  1754. license_kinds: [
  1755. "license_kind",
  1756. ],
  1757. license_text: [
  1758. "LicenseFile1",
  1759. "LicenseFile2",
  1760. ],
  1761. }
  1762. android_test {
  1763. name: "foo",
  1764. android_license_kinds: ["license_kind"],
  1765. android_license_conditions: ["license_notice"],
  1766. android_license_files: [
  1767. "LicenseFile1",
  1768. "LicenseFile2",
  1769. ],
  1770. }
  1771. `,
  1772. },
  1773. {
  1774. name: "license rewriting with license files outside the current directory",
  1775. in: `
  1776. android_test {
  1777. name: "foo",
  1778. android_license_kinds: ["license_kind"],
  1779. android_license_conditions: ["license_notice"],
  1780. android_license_files: ["../../LicenseFile1", "../../LicenseFile2",],
  1781. }
  1782. `,
  1783. fs: mockFs,
  1784. path: relativePath,
  1785. out: `
  1786. package {
  1787. // See: http://go/android-license-faq
  1788. default_applicable_licenses: [
  1789. "reuse_a_b_license",
  1790. ],
  1791. }
  1792. android_test {
  1793. name: "foo",
  1794. android_license_kinds: ["license_kind"],
  1795. android_license_conditions: ["license_notice"],
  1796. android_license_files: [
  1797. "../../LicenseFile1",
  1798. "../../LicenseFile2",
  1799. ],
  1800. }
  1801. `,
  1802. },
  1803. {
  1804. name: "license rewriting with no Android.bp file in the expected location",
  1805. in: `
  1806. android_test {
  1807. name: "foo",
  1808. android_license_kinds: ["license_kind"],
  1809. android_license_conditions: ["license_notice"],
  1810. android_license_files: ["../../LicenseFile1", "../../LicenseFile2",],
  1811. }
  1812. `,
  1813. fs: pathtools.MockFs(map[string][]byte{
  1814. "a/b/c/d/Android.mk": []byte("this is not important."),
  1815. "a/b/LicenseFile1": []byte("LicenseFile1"),
  1816. "a/b/LicenseFile2": []byte("LicenseFile2"),
  1817. "a/Android.bp": []byte("license {\n\tname: \"reuse_a_b_license\",\n}\n"),
  1818. }),
  1819. path: relativePath,
  1820. out: `
  1821. // Error: No Android.bp file is found at path
  1822. // a/b
  1823. // Please add one there with the needed license module first.
  1824. // Then reset the default_applicable_licenses property below with the license module name.
  1825. package {
  1826. // See: http://go/android-license-faq
  1827. default_applicable_licenses: [
  1828. "",
  1829. ],
  1830. }
  1831. android_test {
  1832. name: "foo",
  1833. android_license_kinds: ["license_kind"],
  1834. android_license_conditions: ["license_notice"],
  1835. android_license_files: [
  1836. "../../LicenseFile1",
  1837. "../../LicenseFile2",
  1838. ],
  1839. }
  1840. `,
  1841. },
  1842. {
  1843. name: "license rewriting with an Android.bp file without a license module",
  1844. in: `
  1845. android_test {
  1846. name: "foo",
  1847. android_license_kinds: ["license_kind"],
  1848. android_license_conditions: ["license_notice"],
  1849. android_license_files: ["../../LicenseFile1", "../../LicenseFile2",],
  1850. }
  1851. `,
  1852. fs: pathtools.MockFs(map[string][]byte{
  1853. "a/b/c/d/Android.mk": []byte("this is not important."),
  1854. "a/b/LicenseFile1": []byte("LicenseFile1"),
  1855. "a/b/LicenseFile2": []byte("LicenseFile2"),
  1856. "a/b/Android.bp": []byte("non_license {\n\tname: \"reuse_a_b_license\",\n}\n"),
  1857. }),
  1858. path: relativePath,
  1859. out: `
  1860. // Error: Cannot get the name of the license module in the
  1861. // a/b/Android.bp file.
  1862. // If no such license module exists, please add one there first.
  1863. // Then reset the default_applicable_licenses property below with the license module name.
  1864. package {
  1865. // See: http://go/android-license-faq
  1866. default_applicable_licenses: [
  1867. "",
  1868. ],
  1869. }
  1870. android_test {
  1871. name: "foo",
  1872. android_license_kinds: ["license_kind"],
  1873. android_license_conditions: ["license_notice"],
  1874. android_license_files: [
  1875. "../../LicenseFile1",
  1876. "../../LicenseFile2",
  1877. ],
  1878. }
  1879. `,
  1880. },
  1881. }
  1882. for _, test := range tests {
  1883. t.Run(test.name, func(t *testing.T) {
  1884. runPassOnce(t, test.in, test.out, runPatchListMod(rewriteLicenseProperty(test.fs, test.path)))
  1885. })
  1886. }
  1887. testErrs := []struct {
  1888. name string
  1889. in string
  1890. fs pathtools.FileSystem
  1891. path string
  1892. expectedErr string
  1893. }{
  1894. {
  1895. name: "license rewriting with a wrong path",
  1896. in: `
  1897. android_test {
  1898. name: "foo",
  1899. android_license_kinds: ["license_kind"],
  1900. android_license_conditions: ["license_notice"],
  1901. android_license_files: ["../../LicenseFile1", "../../LicenseFile2",],
  1902. }
  1903. `,
  1904. fs: mockFs,
  1905. path: relativePathErr,
  1906. expectedErr: `
  1907. Cannot find an Android.mk file at path "a/b/c"
  1908. `,
  1909. },
  1910. }
  1911. for _, test := range testErrs {
  1912. t.Run(test.name, func(t *testing.T) {
  1913. checkError(t, test.in, test.expectedErr, runPatchListMod(rewriteLicenseProperty(test.fs, test.path)))
  1914. })
  1915. }
  1916. }
  1917. func TestHaveSameLicense(t *testing.T) {
  1918. tests := []struct {
  1919. name string
  1920. in string
  1921. out string
  1922. }{
  1923. {
  1924. name: "two modules with the same license",
  1925. in: `
  1926. android_test {
  1927. name: "foo1",
  1928. android_license_kinds: ["license_kind"],
  1929. android_license_conditions: ["license_notice"],
  1930. }
  1931. android_test {
  1932. name: "foo2",
  1933. android_license_kinds: ["license_kind"],
  1934. android_license_conditions: ["license_notice"],
  1935. }
  1936. `,
  1937. out: `
  1938. android_test {
  1939. name: "foo1",
  1940. android_license_kinds: ["license_kind"],
  1941. android_license_conditions: ["license_notice"],
  1942. }
  1943. android_test {
  1944. name: "foo2",
  1945. android_license_kinds: ["license_kind"],
  1946. android_license_conditions: ["license_notice"],
  1947. }
  1948. `,
  1949. },
  1950. }
  1951. for _, test := range tests {
  1952. t.Run(test.name, func(t *testing.T) {
  1953. runPassOnce(t, test.in, test.out, func(fixer *Fixer) error {
  1954. return haveSameLicense(fixer)
  1955. })
  1956. })
  1957. }
  1958. testErrs := []struct {
  1959. name string
  1960. in string
  1961. expectedErr string
  1962. }{
  1963. {
  1964. name: "two modules will different licenses",
  1965. in: `
  1966. android_test {
  1967. name: "foo1",
  1968. android_license_kinds: ["license_kind1"],
  1969. android_license_conditions: ["license_notice1"],
  1970. }
  1971. android_test {
  1972. name: "foo2",
  1973. android_license_kinds: ["license_kind2"],
  1974. android_license_conditions: ["license_notice2"],
  1975. }
  1976. `,
  1977. expectedErr: `
  1978. Modules foo1 and foo2 are expected to have the same android_license_kinds property.
  1979. `,
  1980. },
  1981. }
  1982. for _, test := range testErrs {
  1983. t.Run(test.name, func(t *testing.T) {
  1984. checkError(t, test.in, test.expectedErr, func(fixer *Fixer) error {
  1985. return haveSameLicense(fixer)
  1986. })
  1987. })
  1988. }
  1989. }
  1990. func TestRemoveResourceAndAssetsIfDefault(t *testing.T) {
  1991. tests := []struct {
  1992. name string
  1993. in string
  1994. out string
  1995. }{
  1996. {
  1997. name: "resource_dirs default",
  1998. in: `
  1999. android_app {
  2000. name: "foo",
  2001. resource_dirs: ["res"],
  2002. }
  2003. `,
  2004. out: `
  2005. android_app {
  2006. name: "foo",
  2007. }
  2008. `,
  2009. },
  2010. {
  2011. name: "resource_dirs not default",
  2012. in: `
  2013. android_app {
  2014. name: "foo",
  2015. resource_dirs: ["reso"],
  2016. }
  2017. `,
  2018. out: `
  2019. android_app {
  2020. name: "foo",
  2021. resource_dirs: ["reso"],
  2022. }
  2023. `,
  2024. },
  2025. {
  2026. name: "resource_dirs includes not default",
  2027. in: `
  2028. android_app {
  2029. name: "foo",
  2030. resource_dirs: ["res", "reso"],
  2031. }
  2032. `,
  2033. out: `
  2034. android_app {
  2035. name: "foo",
  2036. resource_dirs: ["res", "reso"],
  2037. }
  2038. `,
  2039. }, {
  2040. name: "asset_dirs default",
  2041. in: `
  2042. android_app {
  2043. name: "foo",
  2044. asset_dirs: ["assets"],
  2045. }
  2046. `,
  2047. out: `
  2048. android_app {
  2049. name: "foo",
  2050. }
  2051. `,
  2052. },
  2053. {
  2054. name: "asset_dirs not default",
  2055. in: `
  2056. android_app {
  2057. name: "foo",
  2058. asset_dirs: ["assety"],
  2059. }
  2060. `,
  2061. out: `
  2062. android_app {
  2063. name: "foo",
  2064. asset_dirs: ["assety"],
  2065. }
  2066. `,
  2067. },
  2068. {
  2069. name: "asset_dirs includes not default",
  2070. in: `
  2071. android_app {
  2072. name: "foo",
  2073. asset_dirs: ["assets", "assety"],
  2074. }
  2075. `,
  2076. out: `
  2077. android_app {
  2078. name: "foo",
  2079. asset_dirs: ["assets", "assety"],
  2080. }
  2081. `,
  2082. },
  2083. {
  2084. name: "resource_dirs and asset_dirs both default",
  2085. in: `
  2086. android_app {
  2087. name: "foo",
  2088. asset_dirs: ["assets"],
  2089. resource_dirs: ["res"],
  2090. }
  2091. `,
  2092. out: `
  2093. android_app {
  2094. name: "foo",
  2095. }
  2096. `,
  2097. },
  2098. }
  2099. for _, test := range tests {
  2100. t.Run(test.name, func(t *testing.T) {
  2101. runPassOnce(t, test.in, test.out, func(fixer *Fixer) error {
  2102. return removeResourceAndAssetsIfDefault(fixer)
  2103. })
  2104. })
  2105. }
  2106. }