blob: cb2e2076a3bdd88b5c74eb71b2f479822cd7f427 [file] [log] [blame]
Jingwen Chen5146ac02021-09-02 11:44:42 +00001// 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
15package bp2build
16
17import (
Jingwen Chen5146ac02021-09-02 11:44:42 +000018 "fmt"
Jingwen Chen5146ac02021-09-02 11:44:42 +000019 "testing"
Yu Liu2aa806b2022-09-01 11:54:47 -070020
21 "android/soong/android"
Jingwen Chen5146ac02021-09-02 11:44:42 +000022)
23
Sam Delmerico3177a6e2022-06-21 19:28:33 +000024func runFilegroupTestCase(t *testing.T, tc Bp2buildTestCase) {
Jingwen Chen5146ac02021-09-02 11:44:42 +000025 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000026 (&tc).ModuleTypeUnderTest = "filegroup"
27 (&tc).ModuleTypeUnderTestFactory = android.FileGroupFactory
28 RunBp2BuildTestCase(t, registerFilegroupModuleTypes, tc)
Jingwen Chen5146ac02021-09-02 11:44:42 +000029}
30
31func registerFilegroupModuleTypes(ctx android.RegistrationContext) {}
32
33func TestFilegroupSameNameAsFile_OneFile(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000034 runFilegroupTestCase(t, Bp2buildTestCase{
35 Description: "filegroup - same name as file, with one file",
36 Filesystem: map[string]string{},
37 Blueprint: `
Jingwen Chen5146ac02021-09-02 11:44:42 +000038filegroup {
39 name: "foo",
40 srcs: ["foo"],
41}
42`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000043 ExpectedBazelTargets: []string{}})
Jingwen Chen5146ac02021-09-02 11:44:42 +000044}
45
46func TestFilegroupSameNameAsFile_MultipleFiles(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000047 runFilegroupTestCase(t, Bp2buildTestCase{
48 Description: "filegroup - same name as file, with multiple files",
49 Filesystem: map[string]string{},
50 Blueprint: `
Jingwen Chen5146ac02021-09-02 11:44:42 +000051filegroup {
52 name: "foo",
53 srcs: ["foo", "bar"],
54}
55`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000056 ExpectedErr: fmt.Errorf("filegroup 'foo' cannot contain a file with the same name"),
Jingwen Chen5146ac02021-09-02 11:44:42 +000057 })
58}
Vinh Tran444154d2022-08-16 13:10:31 -040059
60func TestFilegroupWithAidlSrcs(t *testing.T) {
61 testcases := []struct {
62 name string
63 bp string
64 expectedBazelAttrs AttrNameToString
65 }{
66 {
67 name: "filegroup with only aidl srcs",
68 bp: `
69 filegroup {
70 name: "foo",
71 srcs: ["aidl/foo.aidl"],
72 path: "aidl",
73 }`,
74 expectedBazelAttrs: AttrNameToString{
75 "srcs": `["aidl/foo.aidl"]`,
76 "strip_import_prefix": `"aidl"`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -040077 "tags": `["apex_available=//apex_available:anyapex"]`,
Vinh Tran444154d2022-08-16 13:10:31 -040078 },
79 },
80 {
81 name: "filegroup without path",
82 bp: `
83 filegroup {
84 name: "foo",
85 srcs: ["aidl/foo.aidl"],
86 }`,
87 expectedBazelAttrs: AttrNameToString{
88 "srcs": `["aidl/foo.aidl"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -040089 "tags": `["apex_available=//apex_available:anyapex"]`,
Vinh Tran444154d2022-08-16 13:10:31 -040090 },
91 },
92 }
93
94 for _, test := range testcases {
Liz Kammer2b3f56e2023-03-23 11:51:49 -040095 t.Run(test.name, func(t *testing.T) {
96 expectedBazelTargets := []string{
97 MakeBazelTargetNoRestrictions("aidl_library", "foo", test.expectedBazelAttrs),
98 }
99 runFilegroupTestCase(t, Bp2buildTestCase{
100 Description: test.name,
101 Blueprint: test.bp,
102 ExpectedBazelTargets: expectedBazelTargets,
103 })
Vinh Tran444154d2022-08-16 13:10:31 -0400104 })
105 }
106}
107
108func TestFilegroupWithAidlAndNonAidlSrcs(t *testing.T) {
109 runFilegroupTestCase(t, Bp2buildTestCase{
110 Description: "filegroup with aidl and non-aidl srcs",
111 Filesystem: map[string]string{},
112 Blueprint: `
113filegroup {
114 name: "foo",
115 srcs: [
116 "aidl/foo.aidl",
117 "buf.proto",
118 ],
119}`,
120 ExpectedBazelTargets: []string{
121 MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
122 "srcs": `[
123 "aidl/foo.aidl",
124 "buf.proto",
125 ]`}),
126 }})
127}
Yu Liu2aa806b2022-09-01 11:54:47 -0700128
129func TestFilegroupWithProtoSrcs(t *testing.T) {
130 runFilegroupTestCase(t, Bp2buildTestCase{
131 Description: "filegroup with proto and non-proto srcs",
132 Filesystem: map[string]string{},
133 Blueprint: `
134filegroup {
135 name: "foo",
136 srcs: ["proto/foo.proto"],
137 path: "proto",
138}`,
139 ExpectedBazelTargets: []string{
Spandan Dasdf3ec822023-08-04 02:19:53 +0000140 MakeBazelTargetNoRestrictions("proto_library", "foo_proto", AttrNameToString{
Yu Liu2aa806b2022-09-01 11:54:47 -0700141 "srcs": `["proto/foo.proto"]`,
Yu Liu2a85fb12022-09-15 22:18:48 -0700142 "strip_import_prefix": `"proto"`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400143 "tags": `[
144 "apex_available=//apex_available:anyapex",
145 "manual",
146 ]`,
147 }),
Spandan Dasdf3ec822023-08-04 02:19:53 +0000148 MakeBazelTargetNoRestrictions("alias", "foo_bp2build_converted", AttrNameToString{
149 "actual": `"//.:foo_proto"`,
150 "tags": `[
151 "apex_available=//apex_available:anyapex",
152 "manual",
153 ]`,
154 }),
Yu Liu2aa806b2022-09-01 11:54:47 -0700155 MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
156 "srcs": `["proto/foo.proto"]`}),
157 }})
158}
159
160func TestFilegroupWithProtoAndNonProtoSrcs(t *testing.T) {
161 runFilegroupTestCase(t, Bp2buildTestCase{
162 Description: "filegroup with proto and non-proto srcs",
163 Filesystem: map[string]string{},
164 Blueprint: `
165filegroup {
166 name: "foo",
167 srcs: [
168 "foo.proto",
169 "buf.cpp",
170 ],
171}`,
172 ExpectedBazelTargets: []string{
173 MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
174 "srcs": `[
175 "foo.proto",
176 "buf.cpp",
177 ]`}),
178 }})
179}
Spandan Dasdf3ec822023-08-04 02:19:53 +0000180
181func TestFilegroupWithProtoInDifferentPackage(t *testing.T) {
182 runFilegroupTestCase(t, Bp2buildTestCase{
183 Description: "filegroup with .proto in different package",
184 Filesystem: map[string]string{
185 "subdir/Android.bp": "",
186 },
187 Blueprint: `
188filegroup {
189 name: "foo",
190 srcs: ["subdir/foo.proto"],
191}`,
192 Dir: "subdir", // check in subdir
193 ExpectedBazelTargets: []string{
194 MakeBazelTargetNoRestrictions("proto_library", "foo_proto", AttrNameToString{
195 "srcs": `["//subdir:foo.proto"]`,
196 "import_prefix": `"subdir"`,
197 "strip_import_prefix": `""`,
198 "tags": `[
199 "apex_available=//apex_available:anyapex",
200 "manual",
201 ]`}),
202 }})
203}