blob: e978fb319dab8322d3b2bb32d373c00bc8af0365 [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"`,
77 },
78 },
79 {
80 name: "filegroup without path",
81 bp: `
82 filegroup {
83 name: "foo",
84 srcs: ["aidl/foo.aidl"],
85 }`,
86 expectedBazelAttrs: AttrNameToString{
87 "srcs": `["aidl/foo.aidl"]`,
88 },
89 },
90 }
91
92 for _, test := range testcases {
93 expectedBazelTargets := []string{
94 MakeBazelTargetNoRestrictions("aidl_library", "foo", test.expectedBazelAttrs),
95 }
96 runFilegroupTestCase(t, Bp2buildTestCase{
97 Description: test.name,
98 Blueprint: test.bp,
99 ExpectedBazelTargets: expectedBazelTargets,
100 })
101 }
102}
103
104func TestFilegroupWithAidlAndNonAidlSrcs(t *testing.T) {
105 runFilegroupTestCase(t, Bp2buildTestCase{
106 Description: "filegroup with aidl and non-aidl srcs",
107 Filesystem: map[string]string{},
108 Blueprint: `
109filegroup {
110 name: "foo",
111 srcs: [
112 "aidl/foo.aidl",
113 "buf.proto",
114 ],
115}`,
116 ExpectedBazelTargets: []string{
117 MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
118 "srcs": `[
119 "aidl/foo.aidl",
120 "buf.proto",
121 ]`}),
122 }})
123}
Yu Liu2aa806b2022-09-01 11:54:47 -0700124
125func TestFilegroupWithProtoSrcs(t *testing.T) {
126 runFilegroupTestCase(t, Bp2buildTestCase{
127 Description: "filegroup with proto and non-proto srcs",
128 Filesystem: map[string]string{},
129 Blueprint: `
130filegroup {
131 name: "foo",
132 srcs: ["proto/foo.proto"],
133 path: "proto",
134}`,
135 ExpectedBazelTargets: []string{
136 MakeBazelTargetNoRestrictions("proto_library", "foo_bp2build_converted", AttrNameToString{
137 "srcs": `["proto/foo.proto"]`,
Yu Liu2a85fb12022-09-15 22:18:48 -0700138 "strip_import_prefix": `"proto"`,
139 "tags": `["manual"]`}),
Yu Liu2aa806b2022-09-01 11:54:47 -0700140 MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
141 "srcs": `["proto/foo.proto"]`}),
142 }})
143}
144
145func TestFilegroupWithProtoAndNonProtoSrcs(t *testing.T) {
146 runFilegroupTestCase(t, Bp2buildTestCase{
147 Description: "filegroup with proto and non-proto srcs",
148 Filesystem: map[string]string{},
149 Blueprint: `
150filegroup {
151 name: "foo",
152 srcs: [
153 "foo.proto",
154 "buf.cpp",
155 ],
156}`,
157 ExpectedBazelTargets: []string{
158 MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
159 "srcs": `[
160 "foo.proto",
161 "buf.cpp",
162 ]`}),
163 }})
164}