package_conversion_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2022 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 bp2build
  15. import (
  16. "testing"
  17. "android/soong/android"
  18. "android/soong/genrule"
  19. )
  20. func registerDependentModules(ctx android.RegistrationContext) {
  21. ctx.RegisterModuleType("license", android.LicenseFactory)
  22. ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
  23. }
  24. func TestPackage(t *testing.T) {
  25. tests := []struct {
  26. description string
  27. modules string
  28. fs map[string]string
  29. expected []ExpectedRuleTarget
  30. }{
  31. {
  32. description: "with default applicable licenses",
  33. modules: `
  34. license {
  35. name: "my_license",
  36. visibility: [":__subpackages__"],
  37. license_kinds: ["SPDX-license-identifier-Apache-2.0"],
  38. license_text: ["NOTICE"],
  39. }
  40. package {
  41. default_applicable_licenses: ["my_license"],
  42. }
  43. `,
  44. expected: []ExpectedRuleTarget{
  45. {
  46. "package",
  47. "",
  48. AttrNameToString{
  49. "default_package_metadata": `[":my_license"]`,
  50. "default_visibility": `["//visibility:public"]`,
  51. },
  52. android.HostAndDeviceDefault,
  53. },
  54. {
  55. "android_license",
  56. "my_license",
  57. AttrNameToString{
  58. "license_kinds": `["SPDX-license-identifier-Apache-2.0"]`,
  59. "license_text": `"NOTICE"`,
  60. "visibility": `[":__subpackages__"]`,
  61. },
  62. android.HostAndDeviceDefault,
  63. },
  64. },
  65. },
  66. {
  67. description: "package has METADATA file",
  68. fs: map[string]string{
  69. "METADATA": ``,
  70. },
  71. modules: `
  72. license {
  73. name: "my_license",
  74. visibility: [":__subpackages__"],
  75. license_kinds: ["SPDX-license-identifier-Apache-2.0"],
  76. license_text: ["NOTICE"],
  77. }
  78. package {
  79. default_applicable_licenses: ["my_license"],
  80. }
  81. `,
  82. expected: []ExpectedRuleTarget{
  83. {
  84. "package",
  85. "",
  86. AttrNameToString{
  87. "default_package_metadata": `[
  88. ":my_license",
  89. ":default_metadata_file",
  90. ]`,
  91. "default_visibility": `["//visibility:public"]`,
  92. },
  93. android.HostAndDeviceDefault,
  94. },
  95. {
  96. "android_license",
  97. "my_license",
  98. AttrNameToString{
  99. "license_kinds": `["SPDX-license-identifier-Apache-2.0"]`,
  100. "license_text": `"NOTICE"`,
  101. "visibility": `[":__subpackages__"]`,
  102. },
  103. android.HostAndDeviceDefault,
  104. },
  105. {
  106. "filegroup",
  107. "default_metadata_file",
  108. AttrNameToString{
  109. "applicable_licenses": `[]`,
  110. "srcs": `["METADATA"]`,
  111. },
  112. android.HostAndDeviceDefault,
  113. },
  114. },
  115. },
  116. }
  117. for _, test := range tests {
  118. expected := make([]string, 0, len(test.expected))
  119. for _, e := range test.expected {
  120. expected = append(expected, e.String())
  121. }
  122. RunBp2BuildTestCase(t, registerDependentModules,
  123. Bp2buildTestCase{
  124. Description: test.description,
  125. ModuleTypeUnderTest: "package",
  126. ModuleTypeUnderTestFactory: android.PackageFactory,
  127. Blueprint: test.modules,
  128. ExpectedBazelTargets: expected,
  129. Filesystem: test.fs,
  130. })
  131. }
  132. }