Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 1 | // Copyright 2020 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 | "android/soong/android" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | func TestGenerateSoongModuleTargets(t *testing.T) { |
| 23 | testCases := []struct { |
| 24 | bp string |
| 25 | expectedBazelTarget string |
| 26 | }{ |
| 27 | { |
| 28 | bp: `custom { |
| 29 | name: "foo", |
| 30 | } |
| 31 | `, |
| 32 | expectedBazelTarget: `soong_module( |
| 33 | name = "foo", |
| 34 | module_name = "foo", |
| 35 | module_type = "custom", |
| 36 | module_variant = "", |
| 37 | module_deps = [ |
| 38 | ], |
| 39 | )`, |
| 40 | }, |
| 41 | { |
| 42 | bp: `custom { |
| 43 | name: "foo", |
| 44 | ramdisk: true, |
| 45 | } |
| 46 | `, |
| 47 | expectedBazelTarget: `soong_module( |
| 48 | name = "foo", |
| 49 | module_name = "foo", |
| 50 | module_type = "custom", |
| 51 | module_variant = "", |
| 52 | module_deps = [ |
| 53 | ], |
| 54 | ramdisk = True, |
| 55 | )`, |
| 56 | }, |
| 57 | { |
| 58 | bp: `custom { |
| 59 | name: "foo", |
| 60 | owner: "a_string_with\"quotes\"_and_\\backslashes\\\\", |
| 61 | } |
| 62 | `, |
| 63 | expectedBazelTarget: `soong_module( |
| 64 | name = "foo", |
| 65 | module_name = "foo", |
| 66 | module_type = "custom", |
| 67 | module_variant = "", |
| 68 | module_deps = [ |
| 69 | ], |
| 70 | owner = "a_string_with\"quotes\"_and_\\backslashes\\\\", |
| 71 | )`, |
| 72 | }, |
| 73 | { |
| 74 | bp: `custom { |
| 75 | name: "foo", |
| 76 | required: ["bar"], |
| 77 | } |
| 78 | `, |
| 79 | expectedBazelTarget: `soong_module( |
| 80 | name = "foo", |
| 81 | module_name = "foo", |
| 82 | module_type = "custom", |
| 83 | module_variant = "", |
| 84 | module_deps = [ |
| 85 | ], |
| 86 | required = [ |
| 87 | "bar", |
| 88 | ], |
| 89 | )`, |
| 90 | }, |
| 91 | { |
| 92 | bp: `custom { |
| 93 | name: "foo", |
| 94 | target_required: ["qux", "bazqux"], |
| 95 | } |
| 96 | `, |
| 97 | expectedBazelTarget: `soong_module( |
| 98 | name = "foo", |
| 99 | module_name = "foo", |
| 100 | module_type = "custom", |
| 101 | module_variant = "", |
| 102 | module_deps = [ |
| 103 | ], |
| 104 | target_required = [ |
| 105 | "qux", |
| 106 | "bazqux", |
| 107 | ], |
| 108 | )`, |
| 109 | }, |
| 110 | { |
| 111 | bp: `custom { |
| 112 | name: "foo", |
| 113 | dist: { |
| 114 | targets: ["goal_foo"], |
| 115 | tag: ".foo", |
| 116 | }, |
| 117 | dists: [ |
| 118 | { |
| 119 | targets: ["goal_bar"], |
| 120 | tag: ".bar", |
| 121 | }, |
| 122 | ], |
| 123 | } |
| 124 | `, |
| 125 | expectedBazelTarget: `soong_module( |
| 126 | name = "foo", |
| 127 | module_name = "foo", |
| 128 | module_type = "custom", |
| 129 | module_variant = "", |
| 130 | module_deps = [ |
| 131 | ], |
| 132 | dist = { |
| 133 | "tag": ".foo", |
| 134 | "targets": [ |
| 135 | "goal_foo", |
| 136 | ], |
| 137 | }, |
| 138 | dists = [ |
| 139 | { |
| 140 | "tag": ".bar", |
| 141 | "targets": [ |
| 142 | "goal_bar", |
| 143 | ], |
| 144 | }, |
| 145 | ], |
| 146 | )`, |
| 147 | }, |
| 148 | { |
| 149 | bp: `custom { |
| 150 | name: "foo", |
| 151 | required: ["bar"], |
| 152 | target_required: ["qux", "bazqux"], |
| 153 | ramdisk: true, |
| 154 | owner: "custom_owner", |
| 155 | dists: [ |
| 156 | { |
| 157 | tag: ".tag", |
| 158 | targets: ["my_goal"], |
| 159 | }, |
| 160 | ], |
| 161 | } |
| 162 | `, |
| 163 | expectedBazelTarget: `soong_module( |
| 164 | name = "foo", |
| 165 | module_name = "foo", |
| 166 | module_type = "custom", |
| 167 | module_variant = "", |
| 168 | module_deps = [ |
| 169 | ], |
| 170 | dists = [ |
| 171 | { |
| 172 | "tag": ".tag", |
| 173 | "targets": [ |
| 174 | "my_goal", |
| 175 | ], |
| 176 | }, |
| 177 | ], |
| 178 | owner = "custom_owner", |
| 179 | ramdisk = True, |
| 180 | required = [ |
| 181 | "bar", |
| 182 | ], |
| 183 | target_required = [ |
| 184 | "qux", |
| 185 | "bazqux", |
| 186 | ], |
| 187 | )`, |
| 188 | }, |
| 189 | } |
| 190 | |
| 191 | dir := "." |
| 192 | for _, testCase := range testCases { |
| 193 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 194 | ctx := android.NewTestContext(config) |
| 195 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 196 | ctx.Register() |
| 197 | |
| 198 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 199 | android.FailIfErrored(t, errs) |
| 200 | _, errs = ctx.PrepareBuildActions(config) |
| 201 | android.FailIfErrored(t, errs) |
| 202 | |
| 203 | bp2BuildCtx := bp2buildBlueprintWrapContext{ |
| 204 | bpCtx: ctx.Context.Context, |
| 205 | } |
| 206 | |
| 207 | bazelTargets := GenerateSoongModuleTargets(&bp2BuildCtx)[dir] |
| 208 | if g, w := len(bazelTargets), 1; g != w { |
| 209 | t.Fatalf("Expected %d bazel target, got %d", w, g) |
| 210 | } |
| 211 | |
| 212 | actualBazelTarget := bazelTargets[0] |
| 213 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 214 | t.Errorf( |
| 215 | "Expected generated Bazel target to be '%s', got '%s'", |
| 216 | testCase.expectedBazelTarget, |
| 217 | actualBazelTarget, |
| 218 | ) |
| 219 | } |
| 220 | } |
| 221 | } |