bazel_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // Copyright 2021 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. package android
  15. import (
  16. "fmt"
  17. "testing"
  18. "android/soong/android/allowlists"
  19. "android/soong/bazel"
  20. "github.com/google/blueprint"
  21. "github.com/google/blueprint/proptools"
  22. )
  23. func TestConvertAllModulesInPackage(t *testing.T) {
  24. testCases := []struct {
  25. prefixes allowlists.Bp2BuildConfig
  26. packageDir string
  27. }{
  28. {
  29. prefixes: allowlists.Bp2BuildConfig{
  30. "a": allowlists.Bp2BuildDefaultTrueRecursively,
  31. },
  32. packageDir: "a",
  33. },
  34. {
  35. prefixes: allowlists.Bp2BuildConfig{
  36. "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
  37. },
  38. packageDir: "a/b",
  39. },
  40. {
  41. prefixes: allowlists.Bp2BuildConfig{
  42. "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
  43. "a/b/c": allowlists.Bp2BuildDefaultTrueRecursively,
  44. },
  45. packageDir: "a/b",
  46. },
  47. {
  48. prefixes: allowlists.Bp2BuildConfig{
  49. "a": allowlists.Bp2BuildDefaultTrueRecursively,
  50. "d/e/f": allowlists.Bp2BuildDefaultTrueRecursively,
  51. },
  52. packageDir: "a/b",
  53. },
  54. {
  55. prefixes: allowlists.Bp2BuildConfig{
  56. "a": allowlists.Bp2BuildDefaultFalse,
  57. "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
  58. "a/b/c": allowlists.Bp2BuildDefaultFalse,
  59. },
  60. packageDir: "a/b",
  61. },
  62. {
  63. prefixes: allowlists.Bp2BuildConfig{
  64. "a": allowlists.Bp2BuildDefaultTrueRecursively,
  65. "a/b": allowlists.Bp2BuildDefaultFalse,
  66. "a/b/c": allowlists.Bp2BuildDefaultTrueRecursively,
  67. },
  68. packageDir: "a",
  69. },
  70. }
  71. for _, test := range testCases {
  72. if ok, _ := bp2buildDefaultTrueRecursively(test.packageDir, test.prefixes); !ok {
  73. t.Errorf("Expected to convert all modules in %s based on %v, but failed.", test.packageDir, test.prefixes)
  74. }
  75. }
  76. }
  77. func TestModuleOptIn(t *testing.T) {
  78. testCases := []struct {
  79. prefixes allowlists.Bp2BuildConfig
  80. packageDir string
  81. }{
  82. {
  83. prefixes: allowlists.Bp2BuildConfig{
  84. "a/b": allowlists.Bp2BuildDefaultFalse,
  85. },
  86. packageDir: "a/b",
  87. },
  88. {
  89. prefixes: allowlists.Bp2BuildConfig{
  90. "a": allowlists.Bp2BuildDefaultFalse,
  91. "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
  92. },
  93. packageDir: "a",
  94. },
  95. {
  96. prefixes: allowlists.Bp2BuildConfig{
  97. "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
  98. },
  99. packageDir: "a", // opt-in by default
  100. },
  101. {
  102. prefixes: allowlists.Bp2BuildConfig{
  103. "a/b/c": allowlists.Bp2BuildDefaultTrueRecursively,
  104. },
  105. packageDir: "a/b",
  106. },
  107. {
  108. prefixes: allowlists.Bp2BuildConfig{
  109. "a": allowlists.Bp2BuildDefaultTrueRecursively,
  110. "d/e/f": allowlists.Bp2BuildDefaultTrueRecursively,
  111. },
  112. packageDir: "foo/bar",
  113. },
  114. {
  115. prefixes: allowlists.Bp2BuildConfig{
  116. "a": allowlists.Bp2BuildDefaultTrueRecursively,
  117. "a/b": allowlists.Bp2BuildDefaultFalse,
  118. "a/b/c": allowlists.Bp2BuildDefaultTrueRecursively,
  119. },
  120. packageDir: "a/b",
  121. },
  122. {
  123. prefixes: allowlists.Bp2BuildConfig{
  124. "a": allowlists.Bp2BuildDefaultFalse,
  125. "a/b": allowlists.Bp2BuildDefaultTrueRecursively,
  126. "a/b/c": allowlists.Bp2BuildDefaultFalse,
  127. },
  128. packageDir: "a",
  129. },
  130. }
  131. for _, test := range testCases {
  132. if ok, _ := bp2buildDefaultTrueRecursively(test.packageDir, test.prefixes); ok {
  133. t.Errorf("Expected to allow module opt-in in %s based on %v, but failed.", test.packageDir, test.prefixes)
  134. }
  135. }
  136. }
  137. type TestBazelModule struct {
  138. bazel.TestModuleInfo
  139. BazelModuleBase
  140. }
  141. var _ blueprint.Module = TestBazelModule{}
  142. func (m TestBazelModule) Name() string {
  143. return m.TestModuleInfo.ModuleName
  144. }
  145. func (m TestBazelModule) GenerateBuildActions(blueprint.ModuleContext) {
  146. }
  147. type TestBazelConversionContext struct {
  148. omc bazel.OtherModuleTestContext
  149. allowlist bp2BuildConversionAllowlist
  150. errors []string
  151. }
  152. var _ bazelOtherModuleContext = &TestBazelConversionContext{}
  153. func (bcc *TestBazelConversionContext) OtherModuleType(m blueprint.Module) string {
  154. return bcc.omc.OtherModuleType(m)
  155. }
  156. func (bcc *TestBazelConversionContext) OtherModuleName(m blueprint.Module) string {
  157. return bcc.omc.OtherModuleName(m)
  158. }
  159. func (bcc *TestBazelConversionContext) OtherModuleDir(m blueprint.Module) string {
  160. return bcc.omc.OtherModuleDir(m)
  161. }
  162. func (bcc *TestBazelConversionContext) ModuleErrorf(format string, args ...interface{}) {
  163. bcc.errors = append(bcc.errors, fmt.Sprintf(format, args...))
  164. }
  165. func (bcc *TestBazelConversionContext) Config() Config {
  166. return Config{
  167. &config{
  168. bp2buildPackageConfig: bcc.allowlist,
  169. },
  170. }
  171. }
  172. var bazelableBazelModuleBase = BazelModuleBase{
  173. bazelProperties: properties{
  174. Bazel_module: bazelModuleProperties{
  175. CanConvertToBazel: true,
  176. },
  177. },
  178. }
  179. func TestBp2BuildAllowlist(t *testing.T) {
  180. testCases := []struct {
  181. description string
  182. shouldConvert bool
  183. expectedErrors []string
  184. module TestBazelModule
  185. allowlist bp2BuildConversionAllowlist
  186. }{
  187. {
  188. description: "allowlist enables module",
  189. shouldConvert: true,
  190. module: TestBazelModule{
  191. TestModuleInfo: bazel.TestModuleInfo{
  192. ModuleName: "foo",
  193. Typ: "rule1",
  194. Dir: "dir1",
  195. },
  196. BazelModuleBase: bazelableBazelModuleBase,
  197. },
  198. allowlist: bp2BuildConversionAllowlist{
  199. moduleAlwaysConvert: map[string]bool{
  200. "foo": true,
  201. },
  202. },
  203. },
  204. {
  205. description: "module in name allowlist and type allowlist fails",
  206. shouldConvert: false,
  207. expectedErrors: []string{"A module cannot be in moduleAlwaysConvert and also be in moduleTypeAlwaysConvert"},
  208. module: TestBazelModule{
  209. TestModuleInfo: bazel.TestModuleInfo{
  210. ModuleName: "foo",
  211. Typ: "rule1",
  212. Dir: "dir1",
  213. },
  214. BazelModuleBase: bazelableBazelModuleBase,
  215. },
  216. allowlist: bp2BuildConversionAllowlist{
  217. moduleAlwaysConvert: map[string]bool{
  218. "foo": true,
  219. },
  220. moduleTypeAlwaysConvert: map[string]bool{
  221. "rule1": true,
  222. },
  223. },
  224. },
  225. {
  226. description: "module in allowlist and denylist fails",
  227. shouldConvert: false,
  228. expectedErrors: []string{"a module cannot be in moduleDoNotConvert and also be in moduleAlwaysConvert"},
  229. module: TestBazelModule{
  230. TestModuleInfo: bazel.TestModuleInfo{
  231. ModuleName: "foo",
  232. Typ: "rule1",
  233. Dir: "dir1",
  234. },
  235. BazelModuleBase: bazelableBazelModuleBase,
  236. },
  237. allowlist: bp2BuildConversionAllowlist{
  238. moduleAlwaysConvert: map[string]bool{
  239. "foo": true,
  240. },
  241. moduleDoNotConvert: map[string]bool{
  242. "foo": true,
  243. },
  244. },
  245. },
  246. {
  247. description: "module in allowlist and existing BUILD file",
  248. shouldConvert: false,
  249. expectedErrors: []string{"A module cannot be in a directory listed in keepExistingBuildFile and also be in moduleAlwaysConvert. Directory: 'existing/build/dir'"},
  250. module: TestBazelModule{
  251. TestModuleInfo: bazel.TestModuleInfo{
  252. ModuleName: "foo",
  253. Typ: "rule1",
  254. Dir: "existing/build/dir",
  255. },
  256. BazelModuleBase: bazelableBazelModuleBase,
  257. },
  258. allowlist: bp2BuildConversionAllowlist{
  259. moduleAlwaysConvert: map[string]bool{
  260. "foo": true,
  261. },
  262. keepExistingBuildFile: map[string]bool{
  263. "existing/build/dir": true,
  264. },
  265. },
  266. },
  267. {
  268. description: "module allowlist and enabled directory",
  269. shouldConvert: false,
  270. expectedErrors: []string{"A module cannot be in a directory marked Bp2BuildDefaultTrue or Bp2BuildDefaultTrueRecursively and also be in moduleAlwaysConvert. Directory: 'existing/build/dir'"},
  271. module: TestBazelModule{
  272. TestModuleInfo: bazel.TestModuleInfo{
  273. ModuleName: "foo",
  274. Typ: "rule1",
  275. Dir: "existing/build/dir",
  276. },
  277. BazelModuleBase: bazelableBazelModuleBase,
  278. },
  279. allowlist: bp2BuildConversionAllowlist{
  280. moduleAlwaysConvert: map[string]bool{
  281. "foo": true,
  282. },
  283. defaultConfig: allowlists.Bp2BuildConfig{
  284. "existing/build/dir": allowlists.Bp2BuildDefaultTrue,
  285. },
  286. },
  287. },
  288. {
  289. description: "module allowlist and enabled subdirectory",
  290. shouldConvert: false,
  291. expectedErrors: []string{"A module cannot be in a directory marked Bp2BuildDefaultTrue or Bp2BuildDefaultTrueRecursively and also be in moduleAlwaysConvert. Directory: 'existing/build/dir'"},
  292. module: TestBazelModule{
  293. TestModuleInfo: bazel.TestModuleInfo{
  294. ModuleName: "foo",
  295. Typ: "rule1",
  296. Dir: "existing/build/dir/subdir",
  297. },
  298. BazelModuleBase: bazelableBazelModuleBase,
  299. },
  300. allowlist: bp2BuildConversionAllowlist{
  301. moduleAlwaysConvert: map[string]bool{
  302. "foo": true,
  303. },
  304. defaultConfig: allowlists.Bp2BuildConfig{
  305. "existing/build/dir": allowlists.Bp2BuildDefaultTrueRecursively,
  306. },
  307. },
  308. },
  309. {
  310. description: "module enabled in unit test short-circuits other allowlists",
  311. shouldConvert: true,
  312. module: TestBazelModule{
  313. TestModuleInfo: bazel.TestModuleInfo{
  314. ModuleName: "foo",
  315. Typ: "rule1",
  316. Dir: ".",
  317. },
  318. BazelModuleBase: BazelModuleBase{
  319. bazelProperties: properties{
  320. Bazel_module: bazelModuleProperties{
  321. CanConvertToBazel: true,
  322. Bp2build_available: proptools.BoolPtr(true),
  323. },
  324. },
  325. },
  326. },
  327. allowlist: bp2BuildConversionAllowlist{
  328. moduleAlwaysConvert: map[string]bool{
  329. "foo": true,
  330. },
  331. moduleDoNotConvert: map[string]bool{
  332. "foo": true,
  333. },
  334. },
  335. },
  336. }
  337. for _, test := range testCases {
  338. t.Run(test.description, func(t *testing.T) {
  339. bcc := &TestBazelConversionContext{
  340. omc: bazel.OtherModuleTestContext{
  341. Modules: []bazel.TestModuleInfo{
  342. test.module.TestModuleInfo,
  343. },
  344. },
  345. allowlist: test.allowlist,
  346. }
  347. shouldConvert := test.module.shouldConvertWithBp2build(bcc, test.module.TestModuleInfo)
  348. if test.shouldConvert != shouldConvert {
  349. t.Errorf("Module shouldConvert expected to be: %v, but was: %v", test.shouldConvert, shouldConvert)
  350. }
  351. errorsMatch := true
  352. if len(test.expectedErrors) != len(bcc.errors) {
  353. errorsMatch = false
  354. } else {
  355. for i, err := range test.expectedErrors {
  356. if err != bcc.errors[i] {
  357. errorsMatch = false
  358. }
  359. }
  360. }
  361. if !errorsMatch {
  362. t.Errorf("Expected errors to be: %v, but were: %v", test.expectedErrors, bcc.errors)
  363. }
  364. })
  365. }
  366. }
  367. func TestBp2buildAllowList(t *testing.T) {
  368. allowlist := getBp2BuildAllowList()
  369. for k, v := range allowlists.Bp2buildDefaultConfig {
  370. if allowlist.defaultConfig[k] != v {
  371. t.Errorf("bp2build default config of %s: expected: %v, got: %v", k, v, allowlist.defaultConfig[k])
  372. }
  373. }
  374. for k, v := range allowlists.Bp2buildKeepExistingBuildFile {
  375. if allowlist.keepExistingBuildFile[k] != v {
  376. t.Errorf("bp2build keep existing build file of %s: expected: %v, got: %v", k, v, allowlist.keepExistingBuildFile[k])
  377. }
  378. }
  379. for _, k := range allowlists.Bp2buildModuleTypeAlwaysConvertList {
  380. if !allowlist.moduleTypeAlwaysConvert[k] {
  381. t.Errorf("bp2build module type always convert of %s: expected: true, got: %v", k, allowlist.moduleTypeAlwaysConvert[k])
  382. }
  383. }
  384. for _, k := range allowlists.Bp2buildModuleDoNotConvertList {
  385. if !allowlist.moduleDoNotConvert[k] {
  386. t.Errorf("bp2build module do not convert of %s: expected: true, got: %v", k, allowlist.moduleDoNotConvert[k])
  387. }
  388. }
  389. for _, k := range allowlists.Bp2buildCcLibraryStaticOnlyList {
  390. if !allowlist.ccLibraryStaticOnly[k] {
  391. t.Errorf("bp2build cc library static only of %s: expected: true, got: %v", k, allowlist.ccLibraryStaticOnly[k])
  392. }
  393. }
  394. for _, k := range allowlists.MixedBuildsDisabledList {
  395. if !allowlist.mixedBuildsDisabled[k] {
  396. t.Errorf("bp2build mix build disabled of %s: expected: true, got: %v", k, allowlist.mixedBuildsDisabled[k])
  397. }
  398. }
  399. }