blob: d84d4b3741ebbd6d53f9258f293b5b4b26f661d1 [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"]`,
138 "strip_import_prefix": `"proto"`}),
139 MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
140 "srcs": `["proto/foo.proto"]`}),
141 }})
142}
143
144func TestFilegroupWithProtoAndNonProtoSrcs(t *testing.T) {
145 runFilegroupTestCase(t, Bp2buildTestCase{
146 Description: "filegroup with proto and non-proto srcs",
147 Filesystem: map[string]string{},
148 Blueprint: `
149filegroup {
150 name: "foo",
151 srcs: [
152 "foo.proto",
153 "buf.cpp",
154 ],
155}`,
156 ExpectedBazelTargets: []string{
157 MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
158 "srcs": `[
159 "foo.proto",
160 "buf.cpp",
161 ]`}),
162 }})
163}