| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [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 | // | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 7 | //	http://www.apache.org/licenses/LICENSE-2.0 | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 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 | package android | 
|  | 15 |  | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 16 | import ( | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 17 | "fmt" | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 18 | "testing" | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 19 |  | 
| Wei Li | d7736ec | 2022-05-12 23:37:53 -0700 | [diff] [blame] | 20 | "android/soong/android/allowlists" | 
|  | 21 | "android/soong/bazel" | 
|  | 22 |  | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 23 | "github.com/google/blueprint" | 
|  | 24 | "github.com/google/blueprint/proptools" | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 25 | ) | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 26 |  | 
|  | 27 | func TestConvertAllModulesInPackage(t *testing.T) { | 
|  | 28 | testCases := []struct { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 29 | prefixes   allowlists.Bp2BuildConfig | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 30 | packageDir string | 
|  | 31 | }{ | 
|  | 32 | { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 33 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 34 | "a": allowlists.Bp2BuildDefaultTrueRecursively, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 35 | }, | 
|  | 36 | packageDir: "a", | 
|  | 37 | }, | 
|  | 38 | { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 39 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 40 | "a/b": allowlists.Bp2BuildDefaultTrueRecursively, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 41 | }, | 
|  | 42 | packageDir: "a/b", | 
|  | 43 | }, | 
|  | 44 | { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 45 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 46 | "a/b":   allowlists.Bp2BuildDefaultTrueRecursively, | 
|  | 47 | "a/b/c": allowlists.Bp2BuildDefaultTrueRecursively, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 48 | }, | 
|  | 49 | packageDir: "a/b", | 
|  | 50 | }, | 
|  | 51 | { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 52 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 53 | "a":     allowlists.Bp2BuildDefaultTrueRecursively, | 
|  | 54 | "d/e/f": allowlists.Bp2BuildDefaultTrueRecursively, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 55 | }, | 
|  | 56 | packageDir: "a/b", | 
|  | 57 | }, | 
|  | 58 | { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 59 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 60 | "a":     allowlists.Bp2BuildDefaultFalse, | 
|  | 61 | "a/b":   allowlists.Bp2BuildDefaultTrueRecursively, | 
|  | 62 | "a/b/c": allowlists.Bp2BuildDefaultFalse, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 63 | }, | 
|  | 64 | packageDir: "a/b", | 
|  | 65 | }, | 
|  | 66 | { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 67 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 68 | "a":     allowlists.Bp2BuildDefaultTrueRecursively, | 
|  | 69 | "a/b":   allowlists.Bp2BuildDefaultFalse, | 
|  | 70 | "a/b/c": allowlists.Bp2BuildDefaultTrueRecursively, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 71 | }, | 
|  | 72 | packageDir: "a", | 
|  | 73 | }, | 
| MarkDacek | 756b296 | 2022-10-13 17:50:17 +0000 | [diff] [blame] | 74 | { | 
|  | 75 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 76 | "a":   allowlists.Bp2BuildDefaultFalseRecursively, | 
|  | 77 | "a/b": allowlists.Bp2BuildDefaultTrue, | 
|  | 78 | }, | 
|  | 79 | packageDir: "a/b", | 
|  | 80 | }, | 
|  | 81 | { | 
|  | 82 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 83 | "a":   allowlists.Bp2BuildDefaultFalseRecursively, | 
|  | 84 | "a/b": allowlists.Bp2BuildDefaultTrueRecursively, | 
|  | 85 | }, | 
|  | 86 | packageDir: "a/b/c", | 
|  | 87 | }, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
|  | 90 | for _, test := range testCases { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 91 | if ok, _ := bp2buildDefaultTrueRecursively(test.packageDir, test.prefixes); !ok { | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 92 | t.Errorf("Expected to convert all modules in %s based on %v, but failed.", test.packageDir, test.prefixes) | 
|  | 93 | } | 
|  | 94 | } | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | func TestModuleOptIn(t *testing.T) { | 
|  | 98 | testCases := []struct { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 99 | prefixes   allowlists.Bp2BuildConfig | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 100 | packageDir string | 
|  | 101 | }{ | 
|  | 102 | { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 103 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 104 | "a/b": allowlists.Bp2BuildDefaultFalse, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 105 | }, | 
|  | 106 | packageDir: "a/b", | 
|  | 107 | }, | 
|  | 108 | { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 109 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 110 | "a":   allowlists.Bp2BuildDefaultFalse, | 
|  | 111 | "a/b": allowlists.Bp2BuildDefaultTrueRecursively, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 112 | }, | 
|  | 113 | packageDir: "a", | 
|  | 114 | }, | 
|  | 115 | { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 116 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 117 | "a/b": allowlists.Bp2BuildDefaultTrueRecursively, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 118 | }, | 
|  | 119 | packageDir: "a", // opt-in by default | 
|  | 120 | }, | 
|  | 121 | { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 122 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 123 | "a/b/c": allowlists.Bp2BuildDefaultTrueRecursively, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 124 | }, | 
|  | 125 | packageDir: "a/b", | 
|  | 126 | }, | 
|  | 127 | { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 128 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 129 | "a":     allowlists.Bp2BuildDefaultTrueRecursively, | 
|  | 130 | "d/e/f": allowlists.Bp2BuildDefaultTrueRecursively, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 131 | }, | 
|  | 132 | packageDir: "foo/bar", | 
|  | 133 | }, | 
|  | 134 | { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 135 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 136 | "a":     allowlists.Bp2BuildDefaultTrueRecursively, | 
|  | 137 | "a/b":   allowlists.Bp2BuildDefaultFalse, | 
|  | 138 | "a/b/c": allowlists.Bp2BuildDefaultTrueRecursively, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 139 | }, | 
|  | 140 | packageDir: "a/b", | 
|  | 141 | }, | 
|  | 142 | { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 143 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 144 | "a":     allowlists.Bp2BuildDefaultFalse, | 
|  | 145 | "a/b":   allowlists.Bp2BuildDefaultTrueRecursively, | 
|  | 146 | "a/b/c": allowlists.Bp2BuildDefaultFalse, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 147 | }, | 
|  | 148 | packageDir: "a", | 
|  | 149 | }, | 
| MarkDacek | 756b296 | 2022-10-13 17:50:17 +0000 | [diff] [blame] | 150 | { | 
|  | 151 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 152 | "a":   allowlists.Bp2BuildDefaultFalseRecursively, | 
|  | 153 | "a/b": allowlists.Bp2BuildDefaultTrue, | 
|  | 154 | }, | 
|  | 155 | packageDir: "a/b/c", | 
|  | 156 | }, | 
|  | 157 | { | 
|  | 158 | prefixes: allowlists.Bp2BuildConfig{ | 
|  | 159 | "a":   allowlists.Bp2BuildDefaultTrueRecursively, | 
|  | 160 | "a/b": allowlists.Bp2BuildDefaultFalseRecursively, | 
|  | 161 | }, | 
|  | 162 | packageDir: "a/b/c", | 
|  | 163 | }, | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 164 | } | 
|  | 165 |  | 
|  | 166 | for _, test := range testCases { | 
| Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 167 | if ok, _ := bp2buildDefaultTrueRecursively(test.packageDir, test.prefixes); ok { | 
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 168 | t.Errorf("Expected to allow module opt-in in %s based on %v, but failed.", test.packageDir, test.prefixes) | 
|  | 169 | } | 
|  | 170 | } | 
|  | 171 | } | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 172 |  | 
|  | 173 | type TestBazelModule struct { | 
|  | 174 | bazel.TestModuleInfo | 
|  | 175 | BazelModuleBase | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | var _ blueprint.Module = TestBazelModule{} | 
|  | 179 |  | 
|  | 180 | func (m TestBazelModule) Name() string { | 
|  | 181 | return m.TestModuleInfo.ModuleName | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | func (m TestBazelModule) GenerateBuildActions(blueprint.ModuleContext) { | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | type TestBazelConversionContext struct { | 
|  | 188 | omc       bazel.OtherModuleTestContext | 
| Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 189 | allowlist Bp2BuildConversionAllowlist | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 190 | errors    []string | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | var _ bazelOtherModuleContext = &TestBazelConversionContext{} | 
|  | 194 |  | 
|  | 195 | func (bcc *TestBazelConversionContext) OtherModuleType(m blueprint.Module) string { | 
|  | 196 | return bcc.omc.OtherModuleType(m) | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | func (bcc *TestBazelConversionContext) OtherModuleName(m blueprint.Module) string { | 
|  | 200 | return bcc.omc.OtherModuleName(m) | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | func (bcc *TestBazelConversionContext) OtherModuleDir(m blueprint.Module) string { | 
|  | 204 | return bcc.omc.OtherModuleDir(m) | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | func (bcc *TestBazelConversionContext) ModuleErrorf(format string, args ...interface{}) { | 
|  | 208 | bcc.errors = append(bcc.errors, fmt.Sprintf(format, args...)) | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | func (bcc *TestBazelConversionContext) Config() Config { | 
|  | 212 | return Config{ | 
|  | 213 | &config{ | 
| Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 214 | Bp2buildPackageConfig: bcc.allowlist, | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 215 | }, | 
|  | 216 | } | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | var bazelableBazelModuleBase = BazelModuleBase{ | 
|  | 220 | bazelProperties: properties{ | 
| Romain Jobredeaux | 8242b43 | 2023-05-04 10:16:26 -0400 | [diff] [blame] | 221 | Bazel_module: BazelModuleProperties{ | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 222 | CanConvertToBazel: true, | 
|  | 223 | }, | 
|  | 224 | }, | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 | func TestBp2BuildAllowlist(t *testing.T) { | 
|  | 228 | testCases := []struct { | 
|  | 229 | description    string | 
|  | 230 | shouldConvert  bool | 
|  | 231 | expectedErrors []string | 
|  | 232 | module         TestBazelModule | 
| Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 233 | allowlist      Bp2BuildConversionAllowlist | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 234 | }{ | 
|  | 235 | { | 
|  | 236 | description:   "allowlist enables module", | 
|  | 237 | shouldConvert: true, | 
|  | 238 | module: TestBazelModule{ | 
|  | 239 | TestModuleInfo: bazel.TestModuleInfo{ | 
|  | 240 | ModuleName: "foo", | 
|  | 241 | Typ:        "rule1", | 
|  | 242 | Dir:        "dir1", | 
|  | 243 | }, | 
|  | 244 | BazelModuleBase: bazelableBazelModuleBase, | 
|  | 245 | }, | 
| Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 246 | allowlist: Bp2BuildConversionAllowlist{ | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 247 | moduleAlwaysConvert: map[string]bool{ | 
|  | 248 | "foo": true, | 
|  | 249 | }, | 
|  | 250 | }, | 
|  | 251 | }, | 
|  | 252 | { | 
|  | 253 | description:    "module in name allowlist and type allowlist fails", | 
|  | 254 | shouldConvert:  false, | 
| Sam Delmerico | e42edc3 | 2023-09-05 19:00:38 +0000 | [diff] [blame] | 255 | expectedErrors: []string{"A module \"foo\" of type \"rule1\" cannot be in moduleAlwaysConvert and also be in moduleTypeAlwaysConvert"}, | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 256 | module: TestBazelModule{ | 
|  | 257 | TestModuleInfo: bazel.TestModuleInfo{ | 
|  | 258 | ModuleName: "foo", | 
|  | 259 | Typ:        "rule1", | 
|  | 260 | Dir:        "dir1", | 
|  | 261 | }, | 
|  | 262 | BazelModuleBase: bazelableBazelModuleBase, | 
|  | 263 | }, | 
| Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 264 | allowlist: Bp2BuildConversionAllowlist{ | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 265 | moduleAlwaysConvert: map[string]bool{ | 
|  | 266 | "foo": true, | 
|  | 267 | }, | 
|  | 268 | moduleTypeAlwaysConvert: map[string]bool{ | 
|  | 269 | "rule1": true, | 
|  | 270 | }, | 
|  | 271 | }, | 
|  | 272 | }, | 
|  | 273 | { | 
|  | 274 | description:    "module in allowlist and denylist fails", | 
|  | 275 | shouldConvert:  false, | 
| Sam Delmerico | e42edc3 | 2023-09-05 19:00:38 +0000 | [diff] [blame] | 276 | expectedErrors: []string{"a module \"foo\" cannot be in moduleDoNotConvert and also be in moduleAlwaysConvert"}, | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 277 | module: TestBazelModule{ | 
|  | 278 | TestModuleInfo: bazel.TestModuleInfo{ | 
|  | 279 | ModuleName: "foo", | 
|  | 280 | Typ:        "rule1", | 
|  | 281 | Dir:        "dir1", | 
|  | 282 | }, | 
|  | 283 | BazelModuleBase: bazelableBazelModuleBase, | 
|  | 284 | }, | 
| Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 285 | allowlist: Bp2BuildConversionAllowlist{ | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 286 | moduleAlwaysConvert: map[string]bool{ | 
|  | 287 | "foo": true, | 
|  | 288 | }, | 
|  | 289 | moduleDoNotConvert: map[string]bool{ | 
|  | 290 | "foo": true, | 
|  | 291 | }, | 
|  | 292 | }, | 
|  | 293 | }, | 
|  | 294 | { | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 295 | description:    "module allowlist and enabled directory", | 
|  | 296 | shouldConvert:  false, | 
| Yu Liu | 10853f9 | 2022-09-14 16:05:22 -0700 | [diff] [blame] | 297 | expectedErrors: []string{"A module cannot be in a directory marked Bp2BuildDefaultTrue or Bp2BuildDefaultTrueRecursively and also be in moduleAlwaysConvert. Directory: 'existing/build/dir' Module: 'foo'"}, | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 298 | module: TestBazelModule{ | 
|  | 299 | TestModuleInfo: bazel.TestModuleInfo{ | 
|  | 300 | ModuleName: "foo", | 
|  | 301 | Typ:        "rule1", | 
|  | 302 | Dir:        "existing/build/dir", | 
|  | 303 | }, | 
|  | 304 | BazelModuleBase: bazelableBazelModuleBase, | 
|  | 305 | }, | 
| Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 306 | allowlist: Bp2BuildConversionAllowlist{ | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 307 | moduleAlwaysConvert: map[string]bool{ | 
|  | 308 | "foo": true, | 
|  | 309 | }, | 
|  | 310 | defaultConfig: allowlists.Bp2BuildConfig{ | 
|  | 311 | "existing/build/dir": allowlists.Bp2BuildDefaultTrue, | 
|  | 312 | }, | 
|  | 313 | }, | 
|  | 314 | }, | 
|  | 315 | { | 
|  | 316 | description:    "module allowlist and enabled subdirectory", | 
|  | 317 | shouldConvert:  false, | 
| Yu Liu | 10853f9 | 2022-09-14 16:05:22 -0700 | [diff] [blame] | 318 | expectedErrors: []string{"A module cannot be in a directory marked Bp2BuildDefaultTrue or Bp2BuildDefaultTrueRecursively and also be in moduleAlwaysConvert. Directory: 'existing/build/dir' Module: 'foo'"}, | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 319 | module: TestBazelModule{ | 
|  | 320 | TestModuleInfo: bazel.TestModuleInfo{ | 
|  | 321 | ModuleName: "foo", | 
|  | 322 | Typ:        "rule1", | 
|  | 323 | Dir:        "existing/build/dir/subdir", | 
|  | 324 | }, | 
|  | 325 | BazelModuleBase: bazelableBazelModuleBase, | 
|  | 326 | }, | 
| Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 327 | allowlist: Bp2BuildConversionAllowlist{ | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 328 | moduleAlwaysConvert: map[string]bool{ | 
|  | 329 | "foo": true, | 
|  | 330 | }, | 
|  | 331 | defaultConfig: allowlists.Bp2BuildConfig{ | 
|  | 332 | "existing/build/dir": allowlists.Bp2BuildDefaultTrueRecursively, | 
|  | 333 | }, | 
|  | 334 | }, | 
|  | 335 | }, | 
|  | 336 | { | 
|  | 337 | description:   "module enabled in unit test short-circuits other allowlists", | 
|  | 338 | shouldConvert: true, | 
|  | 339 | module: TestBazelModule{ | 
|  | 340 | TestModuleInfo: bazel.TestModuleInfo{ | 
|  | 341 | ModuleName: "foo", | 
|  | 342 | Typ:        "rule1", | 
|  | 343 | Dir:        ".", | 
|  | 344 | }, | 
|  | 345 | BazelModuleBase: BazelModuleBase{ | 
|  | 346 | bazelProperties: properties{ | 
| Romain Jobredeaux | 8242b43 | 2023-05-04 10:16:26 -0400 | [diff] [blame] | 347 | Bazel_module: BazelModuleProperties{ | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 348 | CanConvertToBazel:  true, | 
|  | 349 | Bp2build_available: proptools.BoolPtr(true), | 
|  | 350 | }, | 
|  | 351 | }, | 
|  | 352 | }, | 
|  | 353 | }, | 
| Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 354 | allowlist: Bp2BuildConversionAllowlist{ | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 355 | moduleAlwaysConvert: map[string]bool{ | 
|  | 356 | "foo": true, | 
|  | 357 | }, | 
|  | 358 | moduleDoNotConvert: map[string]bool{ | 
|  | 359 | "foo": true, | 
|  | 360 | }, | 
|  | 361 | }, | 
|  | 362 | }, | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | for _, test := range testCases { | 
|  | 366 | t.Run(test.description, func(t *testing.T) { | 
|  | 367 | bcc := &TestBazelConversionContext{ | 
|  | 368 | omc: bazel.OtherModuleTestContext{ | 
|  | 369 | Modules: []bazel.TestModuleInfo{ | 
|  | 370 | test.module.TestModuleInfo, | 
|  | 371 | }, | 
|  | 372 | }, | 
|  | 373 | allowlist: test.allowlist, | 
|  | 374 | } | 
|  | 375 |  | 
| Liz Kammer | c86e094 | 2023-08-11 16:15:12 -0400 | [diff] [blame] | 376 | shouldConvert := test.module.shouldConvertWithBp2build(bcc, | 
|  | 377 | shouldConvertParams{ | 
|  | 378 | module:     test.module.TestModuleInfo, | 
|  | 379 | moduleDir:  test.module.TestModuleInfo.Dir, | 
|  | 380 | moduleType: test.module.TestModuleInfo.Typ, | 
|  | 381 | moduleName: test.module.TestModuleInfo.ModuleName, | 
|  | 382 | }, | 
|  | 383 | ) | 
| Sam Delmerico | cc51843 | 2022-03-30 15:50:34 +0000 | [diff] [blame] | 384 | if test.shouldConvert != shouldConvert { | 
|  | 385 | t.Errorf("Module shouldConvert expected to be: %v, but was: %v", test.shouldConvert, shouldConvert) | 
|  | 386 | } | 
|  | 387 |  | 
|  | 388 | errorsMatch := true | 
|  | 389 | if len(test.expectedErrors) != len(bcc.errors) { | 
|  | 390 | errorsMatch = false | 
|  | 391 | } else { | 
|  | 392 | for i, err := range test.expectedErrors { | 
|  | 393 | if err != bcc.errors[i] { | 
|  | 394 | errorsMatch = false | 
|  | 395 | } | 
|  | 396 | } | 
|  | 397 | } | 
|  | 398 | if !errorsMatch { | 
|  | 399 | t.Errorf("Expected errors to be: %v, but were: %v", test.expectedErrors, bcc.errors) | 
|  | 400 | } | 
|  | 401 | }) | 
|  | 402 | } | 
|  | 403 | } | 
| Wei Li | d7736ec | 2022-05-12 23:37:53 -0700 | [diff] [blame] | 404 |  | 
|  | 405 | func TestBp2buildAllowList(t *testing.T) { | 
| Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 406 | allowlist := GetBp2BuildAllowList() | 
| Wei Li | d7736ec | 2022-05-12 23:37:53 -0700 | [diff] [blame] | 407 | for k, v := range allowlists.Bp2buildDefaultConfig { | 
|  | 408 | if allowlist.defaultConfig[k] != v { | 
|  | 409 | t.Errorf("bp2build default config of %s: expected: %v, got: %v", k, v, allowlist.defaultConfig[k]) | 
|  | 410 | } | 
|  | 411 | } | 
|  | 412 | for k, v := range allowlists.Bp2buildKeepExistingBuildFile { | 
|  | 413 | if allowlist.keepExistingBuildFile[k] != v { | 
|  | 414 | t.Errorf("bp2build keep existing build file of %s: expected: %v, got: %v", k, v, allowlist.keepExistingBuildFile[k]) | 
|  | 415 | } | 
|  | 416 | } | 
|  | 417 | for _, k := range allowlists.Bp2buildModuleTypeAlwaysConvertList { | 
|  | 418 | if !allowlist.moduleTypeAlwaysConvert[k] { | 
|  | 419 | t.Errorf("bp2build module type always convert of %s: expected: true, got: %v", k, allowlist.moduleTypeAlwaysConvert[k]) | 
|  | 420 | } | 
|  | 421 | } | 
|  | 422 | for _, k := range allowlists.Bp2buildModuleDoNotConvertList { | 
|  | 423 | if !allowlist.moduleDoNotConvert[k] { | 
|  | 424 | t.Errorf("bp2build module do not convert of %s: expected: true, got: %v", k, allowlist.moduleDoNotConvert[k]) | 
|  | 425 | } | 
|  | 426 | } | 
| Wei Li | d7736ec | 2022-05-12 23:37:53 -0700 | [diff] [blame] | 427 | } | 
| Usta Shrestha | 342f28f | 2022-11-02 00:59:16 -0400 | [diff] [blame] | 428 |  | 
|  | 429 | func TestShouldKeepExistingBuildFileForDir(t *testing.T) { | 
|  | 430 | allowlist := NewBp2BuildAllowlist() | 
|  | 431 | // entry "a/b2/c2" is moot because of its parent "a/b2" | 
|  | 432 | allowlist.SetKeepExistingBuildFile(map[string]bool{"a": false, "a/b1": false, "a/b2": true, "a/b1/c1": true, "a/b2/c2": false}) | 
|  | 433 | truths := []string{"a", "a/b1", "a/b2", "a/b1/c1", "a/b2/c", "a/b2/c2", "a/b2/c2/d"} | 
|  | 434 | falsities := []string{"a1", "a/b", "a/b1/c"} | 
|  | 435 | for _, dir := range truths { | 
|  | 436 | if !allowlist.ShouldKeepExistingBuildFileForDir(dir) { | 
|  | 437 | t.Errorf("%s expected TRUE but was FALSE", dir) | 
|  | 438 | } | 
|  | 439 | } | 
|  | 440 | for _, dir := range falsities { | 
|  | 441 | if allowlist.ShouldKeepExistingBuildFileForDir(dir) { | 
|  | 442 | t.Errorf("%s expected FALSE but was TRUE", dir) | 
|  | 443 | } | 
|  | 444 | } | 
|  | 445 | } | 
| Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 446 |  | 
|  | 447 | type mixedBuildModule struct { | 
|  | 448 | ModuleBase | 
|  | 449 | BazelModuleBase | 
|  | 450 | props struct { | 
|  | 451 | Deps                     []string | 
|  | 452 | Mixed_build_incompatible *bool | 
|  | 453 | QueuedBazelCall          bool `blueprint:"mutated"` | 
|  | 454 | } | 
|  | 455 | } | 
|  | 456 |  | 
|  | 457 | type mixedBuildModuleInfo struct { | 
|  | 458 | QueuedBazelCall bool | 
|  | 459 | } | 
|  | 460 |  | 
|  | 461 | var mixedBuildModuleProvider = blueprint.NewProvider(mixedBuildModuleInfo{}) | 
|  | 462 |  | 
|  | 463 | func mixedBuildModuleFactory() Module { | 
|  | 464 | m := &mixedBuildModule{} | 
|  | 465 | m.AddProperties(&m.props) | 
|  | 466 | InitAndroidArchModule(m, HostAndDeviceDefault, MultilibBoth) | 
|  | 467 | InitBazelModule(m) | 
|  | 468 |  | 
|  | 469 | return m | 
|  | 470 | } | 
|  | 471 |  | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 472 | func (m *mixedBuildModule) ConvertWithBp2build(ctx Bp2buildMutatorContext) { | 
| Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 473 | } | 
|  | 474 |  | 
|  | 475 | func (m *mixedBuildModule) DepsMutator(ctx BottomUpMutatorContext) { | 
|  | 476 | ctx.AddDependency(ctx.Module(), installDepTag{}, m.props.Deps...) | 
|  | 477 | } | 
|  | 478 |  | 
|  | 479 | func (m *mixedBuildModule) GenerateAndroidBuildActions(ctx ModuleContext) { | 
|  | 480 | } | 
|  | 481 |  | 
|  | 482 | func (m *mixedBuildModule) IsMixedBuildSupported(ctx BaseModuleContext) bool { | 
|  | 483 | return !proptools.Bool(m.props.Mixed_build_incompatible) | 
|  | 484 | } | 
|  | 485 |  | 
|  | 486 | func (m *mixedBuildModule) QueueBazelCall(ctx BaseModuleContext) { | 
|  | 487 | m.props.QueuedBazelCall = true | 
|  | 488 | } | 
|  | 489 |  | 
|  | 490 | func (m *mixedBuildModule) ProcessBazelQueryResponse(ctx ModuleContext) { | 
|  | 491 | ctx.SetProvider(mixedBuildModuleProvider, mixedBuildModuleInfo{ | 
|  | 492 | QueuedBazelCall: m.props.QueuedBazelCall, | 
|  | 493 | }) | 
|  | 494 | } | 
|  | 495 |  | 
|  | 496 | var prepareForMixedBuildTests = FixtureRegisterWithContext(func(ctx RegistrationContext) { | 
|  | 497 | ctx.RegisterModuleType("deps", mixedBuildModuleFactory) | 
|  | 498 | RegisterMixedBuildsMutator(ctx) | 
|  | 499 | }) | 
|  | 500 |  | 
|  | 501 | func TestMixedBuildsEnabledForType(t *testing.T) { | 
|  | 502 | baseBp := ` | 
|  | 503 | deps { | 
|  | 504 | name: "foo", | 
|  | 505 | deps: ["bar"], | 
|  | 506 | target: { windows: { enabled: true } }, | 
|  | 507 | %s | 
|  | 508 | } | 
|  | 509 | ` | 
|  | 510 | depBp := ` | 
|  | 511 | deps { | 
|  | 512 | name: "bar", | 
|  | 513 | target: { | 
|  | 514 | windows: { | 
|  | 515 | enabled: true, | 
|  | 516 | }, | 
|  | 517 | }, | 
|  | 518 | } | 
|  | 519 | ` | 
|  | 520 | testCases := []struct { | 
|  | 521 | desc               string | 
|  | 522 | variant            *string | 
|  | 523 | missingDeps        bool | 
|  | 524 | extraBpInfo        string | 
|  | 525 | mixedBuildsEnabled bool | 
|  | 526 | }{ | 
|  | 527 | { | 
|  | 528 | desc:               "mixed builds works", | 
|  | 529 | mixedBuildsEnabled: true, | 
|  | 530 | extraBpInfo:        `bazel_module: { bp2build_available: true },`, | 
|  | 531 | }, | 
|  | 532 | { | 
|  | 533 | desc:               "missing deps", | 
|  | 534 | missingDeps:        true, | 
|  | 535 | mixedBuildsEnabled: false, | 
|  | 536 | extraBpInfo:        `bazel_module: { bp2build_available: true },`, | 
|  | 537 | }, | 
|  | 538 | { | 
|  | 539 | desc:               "windows no mixed builds", | 
|  | 540 | mixedBuildsEnabled: false, | 
|  | 541 | variant:            proptools.StringPtr("windows_x86"), | 
|  | 542 | extraBpInfo:        `bazel_module: { bp2build_available: true },`, | 
|  | 543 | }, | 
|  | 544 | { | 
|  | 545 | desc:               "mixed builds disabled by type", | 
|  | 546 | mixedBuildsEnabled: false, | 
|  | 547 | extraBpInfo: `mixed_build_incompatible: true, | 
|  | 548 | bazel_module: { bp2build_available: true },`, | 
|  | 549 | }, | 
|  | 550 | { | 
|  | 551 | desc:               "mixed builds not bp2build available", | 
|  | 552 | mixedBuildsEnabled: false, | 
|  | 553 | extraBpInfo:        `bazel_module: { bp2build_available: false },`, | 
|  | 554 | }, | 
|  | 555 | } | 
|  | 556 |  | 
|  | 557 | for _, tc := range testCases { | 
|  | 558 | t.Run(tc.desc, func(t *testing.T) { | 
|  | 559 | handlers := GroupFixturePreparers( | 
|  | 560 | prepareForMixedBuildTests, | 
|  | 561 | PrepareForTestWithArchMutator, | 
|  | 562 | FixtureModifyConfig(func(config Config) { | 
|  | 563 | config.BazelContext = MockBazelContext{ | 
|  | 564 | OutputBaseDir: "base", | 
|  | 565 | } | 
|  | 566 | config.Targets[Windows] = []Target{ | 
|  | 567 | {Windows, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", true}, | 
|  | 568 | {Windows, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", true}, | 
|  | 569 | } | 
|  | 570 | }), | 
|  | 571 | ) | 
|  | 572 | bp := fmt.Sprintf(baseBp, tc.extraBpInfo) | 
|  | 573 | if tc.missingDeps { | 
|  | 574 | handlers = GroupFixturePreparers( | 
|  | 575 | handlers, | 
|  | 576 | PrepareForTestWithAllowMissingDependencies, | 
|  | 577 | ) | 
|  | 578 | } else { | 
|  | 579 | bp += depBp | 
|  | 580 | } | 
|  | 581 | result := handlers.RunTestWithBp(t, bp) | 
|  | 582 |  | 
|  | 583 | variant := proptools.StringDefault(tc.variant, "android_arm64_armv8-a") | 
|  | 584 |  | 
|  | 585 | m := result.ModuleForTests("foo", variant) | 
|  | 586 | mixedBuildModuleInfo := result.TestContext.ModuleProvider(m.Module(), mixedBuildModuleProvider).(mixedBuildModuleInfo) | 
|  | 587 | if w, g := tc.mixedBuildsEnabled, mixedBuildModuleInfo.QueuedBazelCall; w != g { | 
|  | 588 | t.Errorf("Expected mixed builds enabled %t, got mixed builds enabled %t", w, g) | 
|  | 589 | } | 
|  | 590 | }) | 
|  | 591 | } | 
|  | 592 | } |