Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 1 | // Copyright 2022 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 ( |
| 18 | "fmt" |
| 19 | "testing" |
| 20 | |
| 21 | "android/soong/cc" |
| 22 | ) |
| 23 | |
| 24 | func TestNdkHeaderFilepaths(t *testing.T) { |
| 25 | bpTemplate := ` |
| 26 | ndk_headers { |
| 27 | name: "foo", |
| 28 | srcs: %v, |
| 29 | exclude_srcs: %v, |
| 30 | } |
| 31 | ` |
| 32 | testCases := []struct { |
| 33 | desc string |
| 34 | srcs string |
| 35 | excludeSrcs string |
| 36 | expectedHdrs string |
| 37 | }{ |
| 38 | { |
| 39 | desc: "Single header file", |
| 40 | srcs: `["foo.h"]`, |
| 41 | excludeSrcs: `[]`, |
| 42 | expectedHdrs: `["foo.h"]`, |
| 43 | }, |
| 44 | { |
| 45 | desc: "Multiple header files", |
| 46 | srcs: `["foo.h", "foo_other.h"]`, |
| 47 | excludeSrcs: `[]`, |
| 48 | expectedHdrs: `[ |
| 49 | "foo.h", |
| 50 | "foo_other.h", |
| 51 | ]`, |
| 52 | }, |
| 53 | { |
| 54 | desc: "Multiple header files with excludes", |
| 55 | srcs: `["foo.h", "foo_other.h"]`, |
| 56 | excludeSrcs: `["foo_other.h"]`, |
| 57 | expectedHdrs: `["foo.h"]`, |
| 58 | }, |
| 59 | { |
| 60 | desc: "Multiple header files via Soong-supported globs", |
| 61 | srcs: `["*.h"]`, |
| 62 | excludeSrcs: `[]`, |
| 63 | expectedHdrs: `[ |
| 64 | "foo.h", |
| 65 | "foo_other.h", |
| 66 | ]`, |
| 67 | }, |
| 68 | } |
| 69 | for _, testCase := range testCases { |
| 70 | fs := map[string]string{ |
| 71 | "foo.h": "", |
| 72 | "foo_other.h": "", |
| 73 | } |
| 74 | expectedApiContributionTargetName := "foo.contribution" |
| 75 | expectedBazelTarget := MakeBazelTargetNoRestrictions( |
| 76 | "cc_api_headers", |
| 77 | expectedApiContributionTargetName, |
| 78 | AttrNameToString{ |
| 79 | "hdrs": testCase.expectedHdrs, |
| 80 | }, |
| 81 | ) |
Spandan Das | 8b4a5f3 | 2022-09-29 00:28:24 +0000 | [diff] [blame] | 82 | RunApiBp2BuildTestCase(t, cc.RegisterNdkModuleTypes, Bp2buildTestCase{ |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 83 | Description: testCase.desc, |
| 84 | Blueprint: fmt.Sprintf(bpTemplate, testCase.srcs, testCase.excludeSrcs), |
| 85 | ExpectedBazelTargets: []string{expectedBazelTarget}, |
| 86 | Filesystem: fs, |
| 87 | }) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func TestNdkHeaderIncludeDir(t *testing.T) { |
| 92 | bpTemplate := ` |
| 93 | ndk_headers { |
| 94 | name: "foo", |
| 95 | from: %v, |
| 96 | to: "this/value/is/ignored", |
| 97 | } |
| 98 | ` |
| 99 | testCases := []struct { |
| 100 | desc string |
| 101 | from string |
| 102 | expectedIncludeDir string |
| 103 | }{ |
| 104 | { |
| 105 | desc: "Empty `from` value", |
| 106 | from: `""`, |
| 107 | expectedIncludeDir: `""`, |
| 108 | }, |
| 109 | { |
| 110 | desc: "Non-Empty `from` value", |
| 111 | from: `"include"`, |
| 112 | expectedIncludeDir: `"include"`, |
| 113 | }, |
| 114 | } |
| 115 | for _, testCase := range testCases { |
| 116 | expectedApiContributionTargetName := "foo.contribution" |
| 117 | expectedBazelTarget := MakeBazelTargetNoRestrictions( |
| 118 | "cc_api_headers", |
| 119 | expectedApiContributionTargetName, |
| 120 | AttrNameToString{ |
| 121 | "include_dir": testCase.expectedIncludeDir, |
| 122 | }, |
| 123 | ) |
Spandan Das | 8b4a5f3 | 2022-09-29 00:28:24 +0000 | [diff] [blame] | 124 | RunApiBp2BuildTestCase(t, cc.RegisterNdkModuleTypes, Bp2buildTestCase{ |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 125 | Description: testCase.desc, |
| 126 | Blueprint: fmt.Sprintf(bpTemplate, testCase.from), |
| 127 | ExpectedBazelTargets: []string{expectedBazelTarget}, |
| 128 | }) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestVersionedNdkHeaderFilepaths(t *testing.T) { |
| 133 | bp := ` |
| 134 | versioned_ndk_headers { |
| 135 | name: "common_libc", |
| 136 | from: "include" |
| 137 | } |
| 138 | ` |
| 139 | fs := map[string]string{ |
| 140 | "include/math.h": "", |
| 141 | "include/stdio.h": "", |
| 142 | "include/arm/arm.h": "", |
| 143 | "include/x86/x86.h": "", |
| 144 | } |
| 145 | expectedApiContributionTargetName := "common_libc.contribution" |
| 146 | expectedBazelTarget := MakeBazelTargetNoRestrictions( |
| 147 | "cc_api_headers", |
| 148 | expectedApiContributionTargetName, |
| 149 | AttrNameToString{ |
| 150 | "include_dir": `"include"`, |
| 151 | "hdrs": `[ |
| 152 | "include/math.h", |
| 153 | "include/stdio.h", |
| 154 | "include/arm/arm.h", |
| 155 | "include/x86/x86.h", |
| 156 | ]`, |
| 157 | }, |
| 158 | ) |
Spandan Das | 8b4a5f3 | 2022-09-29 00:28:24 +0000 | [diff] [blame] | 159 | RunApiBp2BuildTestCase(t, cc.RegisterNdkModuleTypes, Bp2buildTestCase{ |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 160 | Blueprint: bp, |
| 161 | Filesystem: fs, |
| 162 | ExpectedBazelTargets: []string{expectedBazelTarget}, |
| 163 | }) |
| 164 | } |