filegroup_conversion_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 bp2build
  15. import (
  16. "fmt"
  17. "testing"
  18. "android/soong/android"
  19. )
  20. func runFilegroupTestCase(t *testing.T, tc Bp2buildTestCase) {
  21. t.Helper()
  22. (&tc).ModuleTypeUnderTest = "filegroup"
  23. (&tc).ModuleTypeUnderTestFactory = android.FileGroupFactory
  24. RunBp2BuildTestCase(t, registerFilegroupModuleTypes, tc)
  25. }
  26. func registerFilegroupModuleTypes(ctx android.RegistrationContext) {}
  27. func TestFilegroupSameNameAsFile_OneFile(t *testing.T) {
  28. runFilegroupTestCase(t, Bp2buildTestCase{
  29. Description: "filegroup - same name as file, with one file",
  30. Filesystem: map[string]string{},
  31. Blueprint: `
  32. filegroup {
  33. name: "foo",
  34. srcs: ["foo"],
  35. }
  36. `,
  37. ExpectedBazelTargets: []string{}})
  38. }
  39. func TestFilegroupSameNameAsFile_MultipleFiles(t *testing.T) {
  40. runFilegroupTestCase(t, Bp2buildTestCase{
  41. Description: "filegroup - same name as file, with multiple files",
  42. Filesystem: map[string]string{},
  43. Blueprint: `
  44. filegroup {
  45. name: "foo",
  46. srcs: ["foo", "bar"],
  47. }
  48. `,
  49. ExpectedErr: fmt.Errorf("filegroup 'foo' cannot contain a file with the same name"),
  50. })
  51. }
  52. func TestFilegroupWithAidlSrcs(t *testing.T) {
  53. testcases := []struct {
  54. name string
  55. bp string
  56. expectedBazelAttrs AttrNameToString
  57. }{
  58. {
  59. name: "filegroup with only aidl srcs",
  60. bp: `
  61. filegroup {
  62. name: "foo",
  63. srcs: ["aidl/foo.aidl"],
  64. path: "aidl",
  65. }`,
  66. expectedBazelAttrs: AttrNameToString{
  67. "srcs": `["aidl/foo.aidl"]`,
  68. "strip_import_prefix": `"aidl"`,
  69. "tags": `["apex_available=//apex_available:anyapex"]`,
  70. },
  71. },
  72. {
  73. name: "filegroup without path",
  74. bp: `
  75. filegroup {
  76. name: "foo",
  77. srcs: ["aidl/foo.aidl"],
  78. }`,
  79. expectedBazelAttrs: AttrNameToString{
  80. "srcs": `["aidl/foo.aidl"]`,
  81. "tags": `["apex_available=//apex_available:anyapex"]`,
  82. },
  83. },
  84. }
  85. for _, test := range testcases {
  86. t.Run(test.name, func(t *testing.T) {
  87. expectedBazelTargets := []string{
  88. MakeBazelTargetNoRestrictions("aidl_library", "foo", test.expectedBazelAttrs),
  89. }
  90. runFilegroupTestCase(t, Bp2buildTestCase{
  91. Description: test.name,
  92. Blueprint: test.bp,
  93. ExpectedBazelTargets: expectedBazelTargets,
  94. })
  95. })
  96. }
  97. }
  98. func TestFilegroupWithAidlAndNonAidlSrcs(t *testing.T) {
  99. runFilegroupTestCase(t, Bp2buildTestCase{
  100. Description: "filegroup with aidl and non-aidl srcs",
  101. Filesystem: map[string]string{},
  102. Blueprint: `
  103. filegroup {
  104. name: "foo",
  105. srcs: [
  106. "aidl/foo.aidl",
  107. "buf.proto",
  108. ],
  109. }`,
  110. ExpectedBazelTargets: []string{
  111. MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
  112. "srcs": `[
  113. "aidl/foo.aidl",
  114. "buf.proto",
  115. ]`}),
  116. }})
  117. }
  118. func TestFilegroupWithProtoSrcs(t *testing.T) {
  119. runFilegroupTestCase(t, Bp2buildTestCase{
  120. Description: "filegroup with proto and non-proto srcs",
  121. Filesystem: map[string]string{},
  122. Blueprint: `
  123. filegroup {
  124. name: "foo",
  125. srcs: ["proto/foo.proto"],
  126. path: "proto",
  127. }`,
  128. ExpectedBazelTargets: []string{
  129. MakeBazelTargetNoRestrictions("proto_library", "foo_bp2build_converted", AttrNameToString{
  130. "srcs": `["proto/foo.proto"]`,
  131. "strip_import_prefix": `"proto"`,
  132. "tags": `[
  133. "apex_available=//apex_available:anyapex",
  134. "manual",
  135. ]`,
  136. }),
  137. MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
  138. "srcs": `["proto/foo.proto"]`}),
  139. }})
  140. }
  141. func TestFilegroupWithProtoAndNonProtoSrcs(t *testing.T) {
  142. runFilegroupTestCase(t, Bp2buildTestCase{
  143. Description: "filegroup with proto and non-proto srcs",
  144. Filesystem: map[string]string{},
  145. Blueprint: `
  146. filegroup {
  147. name: "foo",
  148. srcs: [
  149. "foo.proto",
  150. "buf.cpp",
  151. ],
  152. }`,
  153. ExpectedBazelTargets: []string{
  154. MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
  155. "srcs": `[
  156. "foo.proto",
  157. "buf.cpp",
  158. ]`}),
  159. }})
  160. }