blob: 273d55636846eaea9b4e26bfb2a2dcbc20d9ed8b [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
Vinh Tran25625852023-04-14 18:45:20 -0400108func TestFilegroupWithAidlDeps(t *testing.T) {
109 bp := `
110 filegroup {
111 name: "bar",
112 srcs: ["bar.aidl"],
113 }
114 filegroup {
115 name: "foo",
116 srcs: ["aidl/foo.aidl"],
117 path: "aidl",
118 aidl: {
119 deps: [":bar"],
120 }
121 }`
122
123 t.Run("filegroup with aidl deps", func(t *testing.T) {
124 expectedBazelTargets := []string{
125 MakeBazelTargetNoRestrictions("aidl_library", "bar", AttrNameToString{
126 "srcs": `["bar.aidl"]`,
127 "tags": `["apex_available=//apex_available:anyapex"]`,
128 }),
129 MakeBazelTargetNoRestrictions("aidl_library", "foo", AttrNameToString{
130 "srcs": `["aidl/foo.aidl"]`,
131 "strip_import_prefix": `"aidl"`,
132 "deps": `[":bar"]`,
133 "tags": `["apex_available=//apex_available:anyapex"]`,
134 }),
135 }
136 runFilegroupTestCase(t, Bp2buildTestCase{
137 Description: "filegroup with aidl deps",
138 Blueprint: bp,
139 ExpectedBazelTargets: expectedBazelTargets,
140 })
141 })
142}
143
Vinh Tran444154d2022-08-16 13:10:31 -0400144func TestFilegroupWithAidlAndNonAidlSrcs(t *testing.T) {
145 runFilegroupTestCase(t, Bp2buildTestCase{
146 Description: "filegroup with aidl and non-aidl srcs",
147 Filesystem: map[string]string{},
148 Blueprint: `
149filegroup {
150 name: "foo",
151 srcs: [
152 "aidl/foo.aidl",
153 "buf.proto",
154 ],
155}`,
156 ExpectedBazelTargets: []string{
157 MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
158 "srcs": `[
159 "aidl/foo.aidl",
160 "buf.proto",
161 ]`}),
162 }})
163}
Yu Liu2aa806b2022-09-01 11:54:47 -0700164
165func TestFilegroupWithProtoSrcs(t *testing.T) {
166 runFilegroupTestCase(t, Bp2buildTestCase{
167 Description: "filegroup with proto and non-proto srcs",
168 Filesystem: map[string]string{},
169 Blueprint: `
170filegroup {
171 name: "foo",
172 srcs: ["proto/foo.proto"],
173 path: "proto",
174}`,
175 ExpectedBazelTargets: []string{
176 MakeBazelTargetNoRestrictions("proto_library", "foo_bp2build_converted", AttrNameToString{
177 "srcs": `["proto/foo.proto"]`,
Yu Liu2a85fb12022-09-15 22:18:48 -0700178 "strip_import_prefix": `"proto"`,
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400179 "tags": `[
180 "apex_available=//apex_available:anyapex",
181 "manual",
182 ]`,
183 }),
Yu Liu2aa806b2022-09-01 11:54:47 -0700184 MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
185 "srcs": `["proto/foo.proto"]`}),
186 }})
187}
188
189func TestFilegroupWithProtoAndNonProtoSrcs(t *testing.T) {
190 runFilegroupTestCase(t, Bp2buildTestCase{
191 Description: "filegroup with proto and non-proto srcs",
192 Filesystem: map[string]string{},
193 Blueprint: `
194filegroup {
195 name: "foo",
196 srcs: [
197 "foo.proto",
198 "buf.cpp",
199 ],
200}`,
201 ExpectedBazelTargets: []string{
202 MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
203 "srcs": `[
204 "foo.proto",
205 "buf.cpp",
206 ]`}),
207 }})
208}