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 | `, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 43 | ExpectedBazelTargets: []string{}}) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | func TestFilegroupSameNameAsFile_MultipleFiles(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 47 | runFilegroupTestCase(t, Bp2buildTestCase{ |
| 48 | Description: "filegroup - same name as file, with multiple files", |
| 49 | Filesystem: map[string]string{}, |
| 50 | Blueprint: ` |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 51 | filegroup { |
| 52 | name: "foo", |
| 53 | srcs: ["foo", "bar"], |
| 54 | } |
| 55 | `, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 56 | 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] | 57 | }) |
| 58 | } |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 59 | |
| 60 | func 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 Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame^] | 77 | "tags": `["apex_available=//apex_available:anyapex"]`, |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 78 | }, |
| 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 Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame^] | 89 | "tags": `["apex_available=//apex_available:anyapex"]`, |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 90 | }, |
| 91 | }, |
| 92 | } |
| 93 | |
| 94 | for _, test := range testcases { |
Liz Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame^] | 95 | 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 Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 104 | }) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestFilegroupWithAidlAndNonAidlSrcs(t *testing.T) { |
| 109 | runFilegroupTestCase(t, Bp2buildTestCase{ |
| 110 | Description: "filegroup with aidl and non-aidl srcs", |
| 111 | Filesystem: map[string]string{}, |
| 112 | Blueprint: ` |
| 113 | filegroup { |
| 114 | name: "foo", |
| 115 | srcs: [ |
| 116 | "aidl/foo.aidl", |
| 117 | "buf.proto", |
| 118 | ], |
| 119 | }`, |
| 120 | ExpectedBazelTargets: []string{ |
| 121 | MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{ |
| 122 | "srcs": `[ |
| 123 | "aidl/foo.aidl", |
| 124 | "buf.proto", |
| 125 | ]`}), |
| 126 | }}) |
| 127 | } |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 128 | |
| 129 | func TestFilegroupWithProtoSrcs(t *testing.T) { |
| 130 | runFilegroupTestCase(t, Bp2buildTestCase{ |
| 131 | Description: "filegroup with proto and non-proto srcs", |
| 132 | Filesystem: map[string]string{}, |
| 133 | Blueprint: ` |
| 134 | filegroup { |
| 135 | name: "foo", |
| 136 | srcs: ["proto/foo.proto"], |
| 137 | path: "proto", |
| 138 | }`, |
| 139 | ExpectedBazelTargets: []string{ |
| 140 | MakeBazelTargetNoRestrictions("proto_library", "foo_bp2build_converted", AttrNameToString{ |
| 141 | "srcs": `["proto/foo.proto"]`, |
Yu Liu | 2a85fb1 | 2022-09-15 22:18:48 -0700 | [diff] [blame] | 142 | "strip_import_prefix": `"proto"`, |
Liz Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame^] | 143 | "tags": `[ |
| 144 | "apex_available=//apex_available:anyapex", |
| 145 | "manual", |
| 146 | ]`, |
| 147 | }), |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 148 | MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{ |
| 149 | "srcs": `["proto/foo.proto"]`}), |
| 150 | }}) |
| 151 | } |
| 152 | |
| 153 | func TestFilegroupWithProtoAndNonProtoSrcs(t *testing.T) { |
| 154 | runFilegroupTestCase(t, Bp2buildTestCase{ |
| 155 | Description: "filegroup with proto and non-proto srcs", |
| 156 | Filesystem: map[string]string{}, |
| 157 | Blueprint: ` |
| 158 | filegroup { |
| 159 | name: "foo", |
| 160 | srcs: [ |
| 161 | "foo.proto", |
| 162 | "buf.cpp", |
| 163 | ], |
| 164 | }`, |
| 165 | ExpectedBazelTargets: []string{ |
| 166 | MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{ |
| 167 | "srcs": `[ |
| 168 | "foo.proto", |
| 169 | "buf.cpp", |
| 170 | ]`}), |
| 171 | }}) |
| 172 | } |