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 15package bp2build 16 17import ( 18 "fmt" 19 "testing" 20 21 "android/soong/android" 22) 23 24func runFilegroupTestCase(t *testing.T, tc Bp2buildTestCase) { 25 t.Helper() 26 (&tc).ModuleTypeUnderTest = "filegroup" 27 (&tc).ModuleTypeUnderTestFactory = android.FileGroupFactory 28 RunBp2BuildTestCase(t, registerFilegroupModuleTypes, tc) 29} 30 31func registerFilegroupModuleTypes(ctx android.RegistrationContext) {} 32 33func TestFilegroupSameNameAsFile_OneFile(t *testing.T) { 34 runFilegroupTestCase(t, Bp2buildTestCase{ 35 Description: "filegroup - same name as file, with one file", 36 Filesystem: map[string]string{}, 37 Blueprint: ` 38filegroup { 39 name: "foo", 40 srcs: ["foo"], 41} 42`, 43 ExpectedBazelTargets: []string{}}) 44} 45 46func TestFilegroupSameNameAsFile_MultipleFiles(t *testing.T) { 47 runFilegroupTestCase(t, Bp2buildTestCase{ 48 Description: "filegroup - same name as file, with multiple files", 49 Filesystem: map[string]string{}, 50 Blueprint: ` 51filegroup { 52 name: "foo", 53 srcs: ["foo", "bar"], 54} 55`, 56 ExpectedErr: fmt.Errorf("filegroup 'foo' cannot contain a file with the same name"), 57 }) 58} 59 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 "tags": `["apex_available=//apex_available:anyapex"]`, 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"]`, 89 "tags": `["apex_available=//apex_available:anyapex"]`, 90 }, 91 }, 92 } 93 94 for _, test := range testcases { 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 }) 104 }) 105 } 106} 107 108func 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 144func 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} 164 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"]`, 178 "strip_import_prefix": `"proto"`, 179 "tags": `[ 180 "apex_available=//apex_available:anyapex", 181 "manual", 182 ]`, 183 }), 184 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} 209