| Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1 | // 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 |  | 
|  | 15 | package bp2build | 
|  | 16 |  | 
|  | 17 | import ( | 
| Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 18 | "fmt" | 
| Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 19 | "testing" | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 20 |  | 
|  | 21 | "android/soong/android" | 
| Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 22 | ) | 
|  | 23 |  | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 24 | func runFilegroupTestCase(t *testing.T, tc Bp2buildTestCase) { | 
| Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 25 | t.Helper() | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 26 | (&tc).ModuleTypeUnderTest = "filegroup" | 
|  | 27 | (&tc).ModuleTypeUnderTestFactory = android.FileGroupFactory | 
|  | 28 | RunBp2BuildTestCase(t, registerFilegroupModuleTypes, tc) | 
| Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 29 | } | 
|  | 30 |  | 
|  | 31 | func registerFilegroupModuleTypes(ctx android.RegistrationContext) {} | 
|  | 32 |  | 
|  | 33 | func TestFilegroupSameNameAsFile_OneFile(t *testing.T) { | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 34 | runFilegroupTestCase(t, Bp2buildTestCase{ | 
|  | 35 | Description: "filegroup - same name as file, with one file", | 
|  | 36 | Filesystem:  map[string]string{}, | 
|  | 37 | Blueprint: ` | 
| Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 38 | filegroup { | 
|  | 39 | name: "foo", | 
|  | 40 | srcs: ["foo"], | 
|  | 41 | } | 
|  | 42 | `, | 
| Chris Parsons | 2ef472b | 2023-09-27 22:39:45 +0000 | [diff] [blame] | 43 | ExpectedBazelTargets:       []string{}, | 
|  | 44 | ExpectedHandcraftedModules: []string{"foo"}}, | 
|  | 45 | ) | 
| Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 46 | } | 
|  | 47 |  | 
|  | 48 | func TestFilegroupSameNameAsFile_MultipleFiles(t *testing.T) { | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 49 | runFilegroupTestCase(t, Bp2buildTestCase{ | 
|  | 50 | Description: "filegroup - same name as file, with multiple files", | 
|  | 51 | Filesystem:  map[string]string{}, | 
|  | 52 | Blueprint: ` | 
| Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 53 | filegroup { | 
|  | 54 | name: "foo", | 
|  | 55 | srcs: ["foo", "bar"], | 
|  | 56 | } | 
|  | 57 | `, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 58 | ExpectedErr: fmt.Errorf("filegroup 'foo' cannot contain a file with the same name"), | 
| Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 59 | }) | 
|  | 60 | } | 
| Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 61 |  | 
|  | 62 | func 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 Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame] | 79 | "tags":                `["apex_available=//apex_available:anyapex"]`, | 
| Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 80 | }, | 
|  | 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 Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame] | 91 | "tags": `["apex_available=//apex_available:anyapex"]`, | 
| Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 92 | }, | 
|  | 93 | }, | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | for _, test := range testcases { | 
| Liz Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame] | 97 | 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 Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 106 | }) | 
|  | 107 | } | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | func TestFilegroupWithAidlAndNonAidlSrcs(t *testing.T) { | 
|  | 111 | runFilegroupTestCase(t, Bp2buildTestCase{ | 
|  | 112 | Description: "filegroup with aidl and non-aidl srcs", | 
|  | 113 | Filesystem:  map[string]string{}, | 
|  | 114 | Blueprint: ` | 
|  | 115 | filegroup { | 
|  | 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 Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 130 |  | 
|  | 131 | func TestFilegroupWithProtoSrcs(t *testing.T) { | 
|  | 132 | runFilegroupTestCase(t, Bp2buildTestCase{ | 
|  | 133 | Description: "filegroup with proto and non-proto srcs", | 
|  | 134 | Filesystem:  map[string]string{}, | 
|  | 135 | Blueprint: ` | 
|  | 136 | filegroup { | 
|  | 137 | name: "foo", | 
|  | 138 | srcs: ["proto/foo.proto"], | 
|  | 139 | path: "proto", | 
|  | 140 | }`, | 
|  | 141 | ExpectedBazelTargets: []string{ | 
| Spandan Das | df3ec82 | 2023-08-04 02:19:53 +0000 | [diff] [blame] | 142 | MakeBazelTargetNoRestrictions("proto_library", "foo_proto", AttrNameToString{ | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 143 | "srcs":                `["proto/foo.proto"]`, | 
| Yu Liu | 2a85fb1 | 2022-09-15 22:18:48 -0700 | [diff] [blame] | 144 | "strip_import_prefix": `"proto"`, | 
| Liz Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame] | 145 | "tags": `[ | 
|  | 146 | "apex_available=//apex_available:anyapex", | 
|  | 147 | "manual", | 
|  | 148 | ]`, | 
|  | 149 | }), | 
| Spandan Das | df3ec82 | 2023-08-04 02:19:53 +0000 | [diff] [blame] | 150 | MakeBazelTargetNoRestrictions("alias", "foo_bp2build_converted", AttrNameToString{ | 
|  | 151 | "actual": `"//.:foo_proto"`, | 
|  | 152 | "tags": `[ | 
|  | 153 | "apex_available=//apex_available:anyapex", | 
|  | 154 | "manual", | 
|  | 155 | ]`, | 
|  | 156 | }), | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 157 | MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{ | 
|  | 158 | "srcs": `["proto/foo.proto"]`}), | 
|  | 159 | }}) | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | func TestFilegroupWithProtoAndNonProtoSrcs(t *testing.T) { | 
|  | 163 | runFilegroupTestCase(t, Bp2buildTestCase{ | 
|  | 164 | Description: "filegroup with proto and non-proto srcs", | 
|  | 165 | Filesystem:  map[string]string{}, | 
|  | 166 | Blueprint: ` | 
|  | 167 | filegroup { | 
|  | 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 Das | df3ec82 | 2023-08-04 02:19:53 +0000 | [diff] [blame] | 182 |  | 
|  | 183 | func 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: ` | 
|  | 190 | filegroup { | 
|  | 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 | } |