blob: 9c49dac6fc50de7c2a5d082e6dad62781f6f956d [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`,
Chris Parsons2ef472b2023-09-27 22:39:45 +000043 ExpectedBazelTargets: []string{},
44 ExpectedHandcraftedModules: []string{"foo"}},
45 )
Jingwen Chen5146ac02021-09-02 11:44:42 +000046}
47
48func TestFilegroupSameNameAsFile_MultipleFiles(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000049 runFilegroupTestCase(t, Bp2buildTestCase{
50 Description: "filegroup - same name as file, with multiple files",
51 Filesystem: map[string]string{},
52 Blueprint: `
Jingwen Chen5146ac02021-09-02 11:44:42 +000053filegroup {
54 name: "foo",
55 srcs: ["foo", "bar"],
56}
57`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000058 ExpectedErr: fmt.Errorf("filegroup 'foo' cannot contain a file with the same name"),
Jingwen Chen5146ac02021-09-02 11:44:42 +000059 })
60}
Vinh Tran444154d2022-08-16 13:10:31 -040061
62func TestFilegroupWithAidlSrcs(t *testing.T) {
63 testcases := []struct {
64 name string
65 bp string
66 expectedBazelAttrs AttrNameToString
67 }{
68 {
69 name: "filegroup with only aidl srcs",
70 bp: `
71 filegroup {
72 name: "foo",
73 srcs: ["aidl/foo.aidl"],
74 path: "aidl",
75 }`,
76 expectedBazelAttrs: AttrNameToString{
77 "srcs": `["aidl/foo.aidl"]`,
78 "strip_import_prefix": `"aidl"`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -040079 "tags": `["apex_available=//apex_available:anyapex"]`,
Vinh Tran444154d2022-08-16 13:10:31 -040080 },
81 },
82 {
83 name: "filegroup without path",
84 bp: `
85 filegroup {
86 name: "foo",
87 srcs: ["aidl/foo.aidl"],
88 }`,
89 expectedBazelAttrs: AttrNameToString{
90 "srcs": `["aidl/foo.aidl"]`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -040091 "tags": `["apex_available=//apex_available:anyapex"]`,
Vinh Tran444154d2022-08-16 13:10:31 -040092 },
93 },
94 }
95
96 for _, test := range testcases {
Liz Kammer2b3f56e2023-03-23 11:51:49 -040097 t.Run(test.name, func(t *testing.T) {
98 expectedBazelTargets := []string{
99 MakeBazelTargetNoRestrictions("aidl_library", "foo", test.expectedBazelAttrs),
100 }
101 runFilegroupTestCase(t, Bp2buildTestCase{
102 Description: test.name,
103 Blueprint: test.bp,
104 ExpectedBazelTargets: expectedBazelTargets,
105 })
Vinh Tran444154d2022-08-16 13:10:31 -0400106 })
107 }
108}
109
110func TestFilegroupWithAidlAndNonAidlSrcs(t *testing.T) {
111 runFilegroupTestCase(t, Bp2buildTestCase{
112 Description: "filegroup with aidl and non-aidl srcs",
113 Filesystem: map[string]string{},
114 Blueprint: `
115filegroup {
116 name: "foo",
117 srcs: [
118 "aidl/foo.aidl",
119 "buf.proto",
120 ],
121}`,
122 ExpectedBazelTargets: []string{
123 MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
124 "srcs": `[
125 "aidl/foo.aidl",
126 "buf.proto",
127 ]`}),
128 }})
129}
Yu Liu2aa806b2022-09-01 11:54:47 -0700130
131func TestFilegroupWithProtoSrcs(t *testing.T) {
132 runFilegroupTestCase(t, Bp2buildTestCase{
133 Description: "filegroup with proto and non-proto srcs",
134 Filesystem: map[string]string{},
135 Blueprint: `
136filegroup {
137 name: "foo",
138 srcs: ["proto/foo.proto"],
139 path: "proto",
140}`,
141 ExpectedBazelTargets: []string{
Spandan Dasdf3ec822023-08-04 02:19:53 +0000142 MakeBazelTargetNoRestrictions("proto_library", "foo_proto", AttrNameToString{
Yu Liu2aa806b2022-09-01 11:54:47 -0700143 "srcs": `["proto/foo.proto"]`,
Yu Liu2a85fb12022-09-15 22:18:48 -0700144 "strip_import_prefix": `"proto"`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400145 "tags": `[
146 "apex_available=//apex_available:anyapex",
147 "manual",
148 ]`,
149 }),
Spandan Dasdf3ec822023-08-04 02:19:53 +0000150 MakeBazelTargetNoRestrictions("alias", "foo_bp2build_converted", AttrNameToString{
151 "actual": `"//.:foo_proto"`,
152 "tags": `[
153 "apex_available=//apex_available:anyapex",
154 "manual",
155 ]`,
156 }),
Yu Liu2aa806b2022-09-01 11:54:47 -0700157 MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
158 "srcs": `["proto/foo.proto"]`}),
159 }})
160}
161
162func TestFilegroupWithProtoAndNonProtoSrcs(t *testing.T) {
163 runFilegroupTestCase(t, Bp2buildTestCase{
164 Description: "filegroup with proto and non-proto srcs",
165 Filesystem: map[string]string{},
166 Blueprint: `
167filegroup {
168 name: "foo",
169 srcs: [
170 "foo.proto",
171 "buf.cpp",
172 ],
173}`,
174 ExpectedBazelTargets: []string{
175 MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
176 "srcs": `[
177 "foo.proto",
178 "buf.cpp",
179 ]`}),
180 }})
181}
Spandan Dasdf3ec822023-08-04 02:19:53 +0000182
183func TestFilegroupWithProtoInDifferentPackage(t *testing.T) {
184 runFilegroupTestCase(t, Bp2buildTestCase{
185 Description: "filegroup with .proto in different package",
186 Filesystem: map[string]string{
187 "subdir/Android.bp": "",
188 },
189 Blueprint: `
190filegroup {
191 name: "foo",
192 srcs: ["subdir/foo.proto"],
193}`,
194 Dir: "subdir", // check in subdir
195 ExpectedBazelTargets: []string{
196 MakeBazelTargetNoRestrictions("proto_library", "foo_proto", AttrNameToString{
197 "srcs": `["//subdir:foo.proto"]`,
198 "import_prefix": `"subdir"`,
199 "strip_import_prefix": `""`,
200 "tags": `[
201 "apex_available=//apex_available:anyapex",
202 "manual",
203 ]`}),
204 }})
205}