Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 android |
| 16 | |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 17 | import ( |
Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 18 | "bytes" |
| 19 | "path/filepath" |
| 20 | "runtime" |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 21 | "testing" |
Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 22 | |
| 23 | mkparser "android/soong/androidmk/parser" |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 24 | ) |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 25 | |
| 26 | func TestSrcIsModule(t *testing.T) { |
| 27 | type args struct { |
| 28 | s string |
| 29 | } |
| 30 | tests := []struct { |
| 31 | name string |
| 32 | args args |
| 33 | wantModule string |
| 34 | }{ |
| 35 | { |
| 36 | name: "file", |
| 37 | args: args{ |
| 38 | s: "foo", |
| 39 | }, |
| 40 | wantModule: "", |
| 41 | }, |
| 42 | { |
| 43 | name: "module", |
| 44 | args: args{ |
| 45 | s: ":foo", |
| 46 | }, |
| 47 | wantModule: "foo", |
| 48 | }, |
| 49 | { |
| 50 | name: "tag", |
| 51 | args: args{ |
| 52 | s: ":foo{.bar}", |
| 53 | }, |
| 54 | wantModule: "foo{.bar}", |
| 55 | }, |
| 56 | { |
| 57 | name: "extra colon", |
| 58 | args: args{ |
| 59 | s: ":foo:bar", |
| 60 | }, |
| 61 | wantModule: "foo:bar", |
| 62 | }, |
Paul Duffin | e6ba072 | 2021-07-12 20:12:12 +0100 | [diff] [blame] | 63 | { |
| 64 | name: "fully qualified", |
| 65 | args: args{ |
| 66 | s: "//foo:bar", |
| 67 | }, |
| 68 | wantModule: "//foo:bar", |
| 69 | }, |
| 70 | { |
| 71 | name: "fully qualified with tag", |
| 72 | args: args{ |
| 73 | s: "//foo:bar{.tag}", |
| 74 | }, |
| 75 | wantModule: "//foo:bar{.tag}", |
| 76 | }, |
| 77 | { |
| 78 | name: "invalid unqualified name", |
| 79 | args: args{ |
| 80 | s: ":foo/bar", |
| 81 | }, |
| 82 | wantModule: "", |
| 83 | }, |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 84 | } |
| 85 | for _, tt := range tests { |
| 86 | t.Run(tt.name, func(t *testing.T) { |
| 87 | if gotModule := SrcIsModule(tt.args.s); gotModule != tt.wantModule { |
| 88 | t.Errorf("SrcIsModule() = %v, want %v", gotModule, tt.wantModule) |
| 89 | } |
| 90 | }) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestSrcIsModuleWithTag(t *testing.T) { |
| 95 | type args struct { |
| 96 | s string |
| 97 | } |
| 98 | tests := []struct { |
| 99 | name string |
| 100 | args args |
| 101 | wantModule string |
| 102 | wantTag string |
| 103 | }{ |
| 104 | { |
| 105 | name: "file", |
| 106 | args: args{ |
| 107 | s: "foo", |
| 108 | }, |
| 109 | wantModule: "", |
| 110 | wantTag: "", |
| 111 | }, |
| 112 | { |
| 113 | name: "module", |
| 114 | args: args{ |
| 115 | s: ":foo", |
| 116 | }, |
| 117 | wantModule: "foo", |
| 118 | wantTag: "", |
| 119 | }, |
| 120 | { |
| 121 | name: "tag", |
| 122 | args: args{ |
| 123 | s: ":foo{.bar}", |
| 124 | }, |
| 125 | wantModule: "foo", |
| 126 | wantTag: ".bar", |
| 127 | }, |
| 128 | { |
| 129 | name: "empty tag", |
| 130 | args: args{ |
| 131 | s: ":foo{}", |
| 132 | }, |
| 133 | wantModule: "foo", |
| 134 | wantTag: "", |
| 135 | }, |
| 136 | { |
| 137 | name: "extra colon", |
| 138 | args: args{ |
| 139 | s: ":foo:bar", |
| 140 | }, |
| 141 | wantModule: "foo:bar", |
| 142 | }, |
| 143 | { |
| 144 | name: "invalid tag", |
| 145 | args: args{ |
| 146 | s: ":foo{.bar", |
| 147 | }, |
| 148 | wantModule: "foo{.bar", |
| 149 | }, |
| 150 | { |
| 151 | name: "invalid tag 2", |
| 152 | args: args{ |
| 153 | s: ":foo.bar}", |
| 154 | }, |
| 155 | wantModule: "foo.bar}", |
| 156 | }, |
Paul Duffin | e6ba072 | 2021-07-12 20:12:12 +0100 | [diff] [blame] | 157 | { |
| 158 | name: "fully qualified", |
| 159 | args: args{ |
| 160 | s: "//foo:bar", |
| 161 | }, |
| 162 | wantModule: "//foo:bar", |
| 163 | }, |
| 164 | { |
| 165 | name: "fully qualified with tag", |
| 166 | args: args{ |
| 167 | s: "//foo:bar{.tag}", |
| 168 | }, |
| 169 | wantModule: "//foo:bar", |
| 170 | wantTag: ".tag", |
| 171 | }, |
| 172 | { |
| 173 | name: "invalid unqualified name", |
| 174 | args: args{ |
| 175 | s: ":foo/bar", |
| 176 | }, |
| 177 | wantModule: "", |
| 178 | }, |
| 179 | { |
| 180 | name: "invalid unqualified name with tag", |
| 181 | args: args{ |
| 182 | s: ":foo/bar{.tag}", |
| 183 | }, |
| 184 | wantModule: "", |
| 185 | }, |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 186 | } |
| 187 | for _, tt := range tests { |
| 188 | t.Run(tt.name, func(t *testing.T) { |
| 189 | gotModule, gotTag := SrcIsModuleWithTag(tt.args.s) |
| 190 | if gotModule != tt.wantModule { |
| 191 | t.Errorf("SrcIsModuleWithTag() gotModule = %v, want %v", gotModule, tt.wantModule) |
| 192 | } |
| 193 | if gotTag != tt.wantTag { |
| 194 | t.Errorf("SrcIsModuleWithTag() gotTag = %v, want %v", gotTag, tt.wantTag) |
| 195 | } |
| 196 | }) |
| 197 | } |
| 198 | } |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 199 | |
| 200 | type depsModule struct { |
| 201 | ModuleBase |
| 202 | props struct { |
| 203 | Deps []string |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | func (m *depsModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 208 | outputFile := PathForModuleOut(ctx, ctx.ModuleName()) |
| 209 | ctx.Build(pctx, BuildParams{ |
| 210 | Rule: Touch, |
| 211 | Output: outputFile, |
| 212 | }) |
| 213 | installFile := ctx.InstallFile(PathForModuleInstall(ctx), ctx.ModuleName(), outputFile) |
| 214 | ctx.InstallSymlink(PathForModuleInstall(ctx, "symlinks"), ctx.ModuleName(), installFile) |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | func (m *depsModule) DepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 218 | ctx.AddDependency(ctx.Module(), installDepTag{}, m.props.Deps...) |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | func depsModuleFactory() Module { |
| 222 | m := &depsModule{} |
| 223 | m.AddProperties(&m.props) |
Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 224 | InitAndroidArchModule(m, HostAndDeviceDefault, MultilibCommon) |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 225 | return m |
| 226 | } |
| 227 | |
Paul Duffin | f62dc9b | 2021-03-16 19:44:51 +0000 | [diff] [blame] | 228 | var prepareForModuleTests = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 229 | ctx.RegisterModuleType("deps", depsModuleFactory) |
| 230 | }) |
| 231 | |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 232 | func TestErrorDependsOnDisabledModule(t *testing.T) { |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 233 | bp := ` |
| 234 | deps { |
| 235 | name: "foo", |
| 236 | deps: ["bar"], |
| 237 | } |
| 238 | deps { |
| 239 | name: "bar", |
| 240 | enabled: false, |
| 241 | } |
| 242 | ` |
| 243 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 244 | prepareForModuleTests. |
Paul Duffin | f62dc9b | 2021-03-16 19:44:51 +0000 | [diff] [blame] | 245 | ExtendWithErrorHandler(FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": depends on disabled module "bar"`)). |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 246 | RunTestWithBp(t, bp) |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 247 | } |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 248 | |
| 249 | func TestValidateCorrectBuildParams(t *testing.T) { |
Paul Duffin | f62dc9b | 2021-03-16 19:44:51 +0000 | [diff] [blame] | 250 | config := TestConfig(t.TempDir(), nil, "", nil) |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 251 | pathContext := PathContextForTesting(config) |
| 252 | bparams := convertBuildParams(BuildParams{ |
| 253 | // Test with Output |
| 254 | Output: PathForOutput(pathContext, "undeclared_symlink"), |
| 255 | SymlinkOutput: PathForOutput(pathContext, "undeclared_symlink"), |
| 256 | }) |
| 257 | |
| 258 | err := validateBuildParams(bparams) |
| 259 | if err != nil { |
| 260 | t.Error(err) |
| 261 | } |
| 262 | |
| 263 | bparams = convertBuildParams(BuildParams{ |
| 264 | // Test with ImplicitOutput |
| 265 | ImplicitOutput: PathForOutput(pathContext, "undeclared_symlink"), |
| 266 | SymlinkOutput: PathForOutput(pathContext, "undeclared_symlink"), |
| 267 | }) |
| 268 | |
| 269 | err = validateBuildParams(bparams) |
| 270 | if err != nil { |
| 271 | t.Error(err) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | func TestValidateIncorrectBuildParams(t *testing.T) { |
Paul Duffin | f62dc9b | 2021-03-16 19:44:51 +0000 | [diff] [blame] | 276 | config := TestConfig(t.TempDir(), nil, "", nil) |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 277 | pathContext := PathContextForTesting(config) |
| 278 | params := BuildParams{ |
| 279 | Output: PathForOutput(pathContext, "regular_output"), |
| 280 | Outputs: PathsForOutput(pathContext, []string{"out1", "out2"}), |
| 281 | ImplicitOutput: PathForOutput(pathContext, "implicit_output"), |
| 282 | ImplicitOutputs: PathsForOutput(pathContext, []string{"i_out1", "_out2"}), |
| 283 | SymlinkOutput: PathForOutput(pathContext, "undeclared_symlink"), |
| 284 | } |
| 285 | |
| 286 | bparams := convertBuildParams(params) |
| 287 | err := validateBuildParams(bparams) |
| 288 | if err != nil { |
| 289 | FailIfNoMatchingErrors(t, "undeclared_symlink is not a declared output or implicit output", []error{err}) |
| 290 | } else { |
| 291 | t.Errorf("Expected build params to fail validation: %+v", bparams) |
| 292 | } |
| 293 | } |
Paul Duffin | 89968e3 | 2020-11-23 18:17:03 +0000 | [diff] [blame] | 294 | |
| 295 | func TestDistErrorChecking(t *testing.T) { |
| 296 | bp := ` |
| 297 | deps { |
| 298 | name: "foo", |
| 299 | dist: { |
| 300 | dest: "../invalid-dest", |
| 301 | dir: "../invalid-dir", |
| 302 | suffix: "invalid/suffix", |
| 303 | }, |
| 304 | dists: [ |
| 305 | { |
| 306 | dest: "../invalid-dest0", |
| 307 | dir: "../invalid-dir0", |
| 308 | suffix: "invalid/suffix0", |
| 309 | }, |
| 310 | { |
| 311 | dest: "../invalid-dest1", |
| 312 | dir: "../invalid-dir1", |
| 313 | suffix: "invalid/suffix1", |
| 314 | }, |
| 315 | ], |
| 316 | } |
| 317 | ` |
| 318 | |
Paul Duffin | 89968e3 | 2020-11-23 18:17:03 +0000 | [diff] [blame] | 319 | expectedErrs := []string{ |
| 320 | "\\QAndroid.bp:5:13: module \"foo\": dist.dest: Path is outside directory: ../invalid-dest\\E", |
| 321 | "\\QAndroid.bp:6:12: module \"foo\": dist.dir: Path is outside directory: ../invalid-dir\\E", |
| 322 | "\\QAndroid.bp:7:15: module \"foo\": dist.suffix: Suffix may not contain a '/' character.\\E", |
| 323 | "\\QAndroid.bp:11:15: module \"foo\": dists[0].dest: Path is outside directory: ../invalid-dest0\\E", |
| 324 | "\\QAndroid.bp:12:14: module \"foo\": dists[0].dir: Path is outside directory: ../invalid-dir0\\E", |
| 325 | "\\QAndroid.bp:13:17: module \"foo\": dists[0].suffix: Suffix may not contain a '/' character.\\E", |
| 326 | "\\QAndroid.bp:16:15: module \"foo\": dists[1].dest: Path is outside directory: ../invalid-dest1\\E", |
| 327 | "\\QAndroid.bp:17:14: module \"foo\": dists[1].dir: Path is outside directory: ../invalid-dir1\\E", |
| 328 | "\\QAndroid.bp:18:17: module \"foo\": dists[1].suffix: Suffix may not contain a '/' character.\\E", |
| 329 | } |
Paul Duffin | f62dc9b | 2021-03-16 19:44:51 +0000 | [diff] [blame] | 330 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 331 | prepareForModuleTests. |
Paul Duffin | f62dc9b | 2021-03-16 19:44:51 +0000 | [diff] [blame] | 332 | ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(expectedErrs)). |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 333 | RunTestWithBp(t, bp) |
Paul Duffin | 89968e3 | 2020-11-23 18:17:03 +0000 | [diff] [blame] | 334 | } |
Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 335 | |
| 336 | func TestInstall(t *testing.T) { |
| 337 | if runtime.GOOS != "linux" { |
| 338 | t.Skip("requires linux") |
| 339 | } |
| 340 | bp := ` |
| 341 | deps { |
| 342 | name: "foo", |
| 343 | deps: ["bar"], |
| 344 | } |
| 345 | |
| 346 | deps { |
| 347 | name: "bar", |
| 348 | deps: ["baz", "qux"], |
| 349 | } |
| 350 | |
| 351 | deps { |
| 352 | name: "baz", |
| 353 | deps: ["qux"], |
| 354 | } |
| 355 | |
| 356 | deps { |
| 357 | name: "qux", |
| 358 | } |
| 359 | ` |
| 360 | |
| 361 | result := GroupFixturePreparers( |
| 362 | prepareForModuleTests, |
| 363 | PrepareForTestWithArchMutator, |
| 364 | ).RunTestWithBp(t, bp) |
| 365 | |
| 366 | module := func(name string, host bool) TestingModule { |
| 367 | variant := "android_common" |
| 368 | if host { |
| 369 | variant = result.Config.BuildOSCommonTarget.String() |
| 370 | } |
| 371 | return result.ModuleForTests(name, variant) |
| 372 | } |
| 373 | |
| 374 | outputRule := func(name string) TestingBuildParams { return module(name, false).Output(name) } |
| 375 | |
| 376 | installRule := func(name string) TestingBuildParams { |
| 377 | return module(name, false).Output(filepath.Join("out/soong/target/product/test_device/system", name)) |
| 378 | } |
| 379 | |
| 380 | symlinkRule := func(name string) TestingBuildParams { |
| 381 | return module(name, false).Output(filepath.Join("out/soong/target/product/test_device/system/symlinks", name)) |
| 382 | } |
| 383 | |
| 384 | hostOutputRule := func(name string) TestingBuildParams { return module(name, true).Output(name) } |
| 385 | |
| 386 | hostInstallRule := func(name string) TestingBuildParams { |
| 387 | return module(name, true).Output(filepath.Join("out/soong/host/linux-x86", name)) |
| 388 | } |
| 389 | |
| 390 | hostSymlinkRule := func(name string) TestingBuildParams { |
| 391 | return module(name, true).Output(filepath.Join("out/soong/host/linux-x86/symlinks", name)) |
| 392 | } |
| 393 | |
| 394 | assertInputs := func(params TestingBuildParams, inputs ...Path) { |
| 395 | t.Helper() |
| 396 | AssertArrayString(t, "expected inputs", Paths(inputs).Strings(), |
| 397 | append(PathsIfNonNil(params.Input), params.Inputs...).Strings()) |
| 398 | } |
| 399 | |
| 400 | assertImplicits := func(params TestingBuildParams, implicits ...Path) { |
| 401 | t.Helper() |
| 402 | AssertArrayString(t, "expected implicit dependencies", Paths(implicits).Strings(), |
| 403 | append(PathsIfNonNil(params.Implicit), params.Implicits...).Strings()) |
| 404 | } |
| 405 | |
| 406 | assertOrderOnlys := func(params TestingBuildParams, orderonlys ...Path) { |
| 407 | t.Helper() |
| 408 | AssertArrayString(t, "expected orderonly dependencies", Paths(orderonlys).Strings(), |
| 409 | params.OrderOnly.Strings()) |
| 410 | } |
| 411 | |
| 412 | // Check host install rule dependencies |
| 413 | assertInputs(hostInstallRule("foo"), hostOutputRule("foo").Output) |
| 414 | assertImplicits(hostInstallRule("foo"), |
| 415 | hostInstallRule("bar").Output, |
| 416 | hostSymlinkRule("bar").Output, |
| 417 | hostInstallRule("baz").Output, |
| 418 | hostSymlinkRule("baz").Output, |
| 419 | hostInstallRule("qux").Output, |
| 420 | hostSymlinkRule("qux").Output, |
| 421 | ) |
| 422 | assertOrderOnlys(hostInstallRule("foo")) |
| 423 | |
Colin Cross | 64002af | 2021-11-09 16:37:52 -0800 | [diff] [blame] | 424 | // Check host symlink rule dependencies. Host symlinks must use a normal dependency, not an |
| 425 | // order-only dependency, so that the tool gets updated when the symlink is depended on. |
Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 426 | assertInputs(hostSymlinkRule("foo"), hostInstallRule("foo").Output) |
| 427 | assertImplicits(hostSymlinkRule("foo")) |
| 428 | assertOrderOnlys(hostSymlinkRule("foo")) |
| 429 | |
| 430 | // Check device install rule dependencies |
| 431 | assertInputs(installRule("foo"), outputRule("foo").Output) |
| 432 | assertImplicits(installRule("foo")) |
| 433 | assertOrderOnlys(installRule("foo"), |
| 434 | installRule("bar").Output, |
| 435 | symlinkRule("bar").Output, |
| 436 | installRule("baz").Output, |
| 437 | symlinkRule("baz").Output, |
| 438 | installRule("qux").Output, |
| 439 | symlinkRule("qux").Output, |
| 440 | ) |
| 441 | |
Colin Cross | 64002af | 2021-11-09 16:37:52 -0800 | [diff] [blame] | 442 | // Check device symlink rule dependencies. Device symlinks could use an order-only dependency, |
| 443 | // but the current implementation uses a normal dependency. |
Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 444 | assertInputs(symlinkRule("foo"), installRule("foo").Output) |
| 445 | assertImplicits(symlinkRule("foo")) |
| 446 | assertOrderOnlys(symlinkRule("foo")) |
| 447 | } |
| 448 | |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 449 | func TestInstallKatiEnabled(t *testing.T) { |
Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 450 | if runtime.GOOS != "linux" { |
| 451 | t.Skip("requires linux") |
| 452 | } |
| 453 | bp := ` |
| 454 | deps { |
| 455 | name: "foo", |
| 456 | deps: ["bar"], |
| 457 | } |
| 458 | |
| 459 | deps { |
| 460 | name: "bar", |
| 461 | deps: ["baz", "qux"], |
| 462 | } |
| 463 | |
| 464 | deps { |
| 465 | name: "baz", |
| 466 | deps: ["qux"], |
| 467 | } |
| 468 | |
| 469 | deps { |
| 470 | name: "qux", |
| 471 | } |
| 472 | ` |
| 473 | |
| 474 | result := GroupFixturePreparers( |
| 475 | prepareForModuleTests, |
| 476 | PrepareForTestWithArchMutator, |
| 477 | FixtureModifyConfig(SetKatiEnabledForTests), |
| 478 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 479 | ctx.RegisterSingletonType("makevars", makeVarsSingletonFunc) |
| 480 | }), |
| 481 | ).RunTestWithBp(t, bp) |
| 482 | |
| 483 | installs := result.SingletonForTests("makevars").Singleton().(*makeVarsSingleton).installsForTesting |
| 484 | buf := bytes.NewBuffer(append([]byte(nil), installs...)) |
| 485 | parser := mkparser.NewParser("makevars", buf) |
| 486 | |
| 487 | nodes, errs := parser.Parse() |
| 488 | if len(errs) > 0 { |
| 489 | t.Fatalf("error parsing install rules: %s", errs[0]) |
| 490 | } |
| 491 | |
| 492 | rules := parseMkRules(t, result.Config, nodes) |
| 493 | |
| 494 | module := func(name string, host bool) TestingModule { |
| 495 | variant := "android_common" |
| 496 | if host { |
| 497 | variant = result.Config.BuildOSCommonTarget.String() |
| 498 | } |
| 499 | return result.ModuleForTests(name, variant) |
| 500 | } |
| 501 | |
| 502 | outputRule := func(name string) TestingBuildParams { return module(name, false).Output(name) } |
| 503 | |
| 504 | ruleForOutput := func(output string) installMakeRule { |
| 505 | for _, rule := range rules { |
| 506 | if rule.target == output { |
| 507 | return rule |
| 508 | } |
| 509 | } |
| 510 | t.Fatalf("no make install rule for %s", output) |
| 511 | return installMakeRule{} |
| 512 | } |
| 513 | |
| 514 | installRule := func(name string) installMakeRule { |
| 515 | return ruleForOutput(filepath.Join("out/target/product/test_device/system", name)) |
| 516 | } |
| 517 | |
| 518 | symlinkRule := func(name string) installMakeRule { |
| 519 | return ruleForOutput(filepath.Join("out/target/product/test_device/system/symlinks", name)) |
| 520 | } |
| 521 | |
| 522 | hostOutputRule := func(name string) TestingBuildParams { return module(name, true).Output(name) } |
| 523 | |
| 524 | hostInstallRule := func(name string) installMakeRule { |
| 525 | return ruleForOutput(filepath.Join("out/host/linux-x86", name)) |
| 526 | } |
| 527 | |
| 528 | hostSymlinkRule := func(name string) installMakeRule { |
| 529 | return ruleForOutput(filepath.Join("out/host/linux-x86/symlinks", name)) |
| 530 | } |
| 531 | |
| 532 | assertDeps := func(rule installMakeRule, deps ...string) { |
| 533 | t.Helper() |
| 534 | AssertArrayString(t, "expected inputs", deps, rule.deps) |
| 535 | } |
| 536 | |
| 537 | assertOrderOnlys := func(rule installMakeRule, orderonlys ...string) { |
| 538 | t.Helper() |
| 539 | AssertArrayString(t, "expected orderonly dependencies", orderonlys, rule.orderOnlyDeps) |
| 540 | } |
| 541 | |
| 542 | // Check host install rule dependencies |
| 543 | assertDeps(hostInstallRule("foo"), |
| 544 | hostOutputRule("foo").Output.String(), |
| 545 | hostInstallRule("bar").target, |
| 546 | hostSymlinkRule("bar").target, |
| 547 | hostInstallRule("baz").target, |
| 548 | hostSymlinkRule("baz").target, |
| 549 | hostInstallRule("qux").target, |
| 550 | hostSymlinkRule("qux").target, |
| 551 | ) |
| 552 | assertOrderOnlys(hostInstallRule("foo")) |
| 553 | |
Colin Cross | 64002af | 2021-11-09 16:37:52 -0800 | [diff] [blame] | 554 | // Check host symlink rule dependencies. Host symlinks must use a normal dependency, not an |
| 555 | // order-only dependency, so that the tool gets updated when the symlink is depended on. |
| 556 | assertDeps(hostSymlinkRule("foo"), hostInstallRule("foo").target) |
| 557 | assertOrderOnlys(hostSymlinkRule("foo")) |
Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 558 | |
| 559 | // Check device install rule dependencies |
| 560 | assertDeps(installRule("foo"), outputRule("foo").Output.String()) |
| 561 | assertOrderOnlys(installRule("foo"), |
| 562 | installRule("bar").target, |
| 563 | symlinkRule("bar").target, |
| 564 | installRule("baz").target, |
| 565 | symlinkRule("baz").target, |
| 566 | installRule("qux").target, |
| 567 | symlinkRule("qux").target, |
| 568 | ) |
| 569 | |
Colin Cross | 64002af | 2021-11-09 16:37:52 -0800 | [diff] [blame] | 570 | // Check device symlink rule dependencies. Device symlinks could use an order-only dependency, |
| 571 | // but the current implementation uses a normal dependency. |
| 572 | assertDeps(symlinkRule("foo"), installRule("foo").target) |
| 573 | assertOrderOnlys(symlinkRule("foo")) |
Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | type installMakeRule struct { |
| 577 | target string |
| 578 | deps []string |
| 579 | orderOnlyDeps []string |
| 580 | } |
| 581 | |
| 582 | func parseMkRules(t *testing.T, config Config, nodes []mkparser.Node) []installMakeRule { |
| 583 | var rules []installMakeRule |
| 584 | for _, node := range nodes { |
| 585 | if mkParserRule, ok := node.(*mkparser.Rule); ok { |
| 586 | var rule installMakeRule |
| 587 | |
| 588 | if targets := mkParserRule.Target.Words(); len(targets) == 0 { |
| 589 | t.Fatalf("no targets for rule %s", mkParserRule.Dump()) |
| 590 | } else if len(targets) > 1 { |
| 591 | t.Fatalf("unsupported multiple targets for rule %s", mkParserRule.Dump()) |
| 592 | } else if !targets[0].Const() { |
| 593 | t.Fatalf("unsupported non-const target for rule %s", mkParserRule.Dump()) |
| 594 | } else { |
| 595 | rule.target = normalizeStringRelativeToTop(config, targets[0].Value(nil)) |
| 596 | } |
| 597 | |
| 598 | prereqList := &rule.deps |
| 599 | for _, prereq := range mkParserRule.Prerequisites.Words() { |
| 600 | if !prereq.Const() { |
| 601 | t.Fatalf("unsupported non-const prerequisite for rule %s", mkParserRule.Dump()) |
| 602 | } |
| 603 | |
| 604 | if prereq.Value(nil) == "|" { |
| 605 | prereqList = &rule.orderOnlyDeps |
| 606 | continue |
| 607 | } |
| 608 | |
| 609 | *prereqList = append(*prereqList, normalizeStringRelativeToTop(config, prereq.Value(nil))) |
| 610 | } |
| 611 | |
| 612 | rules = append(rules, rule) |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | return rules |
| 617 | } |
Liz Kammer | 9525e71 | 2022-01-05 13:46:24 -0500 | [diff] [blame] | 618 | |
| 619 | type PropsTestModuleEmbedded struct { |
| 620 | Embedded_prop *string |
| 621 | } |
| 622 | |
| 623 | type propsTestModule struct { |
| 624 | ModuleBase |
| 625 | DefaultableModuleBase |
| 626 | props struct { |
| 627 | A string `android:"arch_variant"` |
| 628 | B *bool |
| 629 | C []string |
| 630 | } |
| 631 | otherProps struct { |
| 632 | PropsTestModuleEmbedded |
| 633 | |
| 634 | D *int64 |
| 635 | Nested struct { |
| 636 | E *string |
| 637 | } |
| 638 | F *string `blueprint:"mutated"` |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | func propsTestModuleFactory() Module { |
| 643 | module := &propsTestModule{} |
| 644 | module.AddProperties(&module.props, &module.otherProps) |
| 645 | InitAndroidArchModule(module, HostAndDeviceSupported, MultilibBoth) |
| 646 | InitDefaultableModule(module) |
| 647 | return module |
| 648 | } |
| 649 | |
| 650 | type propsTestModuleDefaults struct { |
| 651 | ModuleBase |
| 652 | DefaultsModuleBase |
| 653 | } |
| 654 | |
| 655 | func propsTestModuleDefaultsFactory() Module { |
| 656 | defaults := &propsTestModuleDefaults{} |
| 657 | module := propsTestModule{} |
| 658 | defaults.AddProperties(&module.props, &module.otherProps) |
| 659 | InitDefaultsModule(defaults) |
| 660 | return defaults |
| 661 | } |
| 662 | |
| 663 | func (p *propsTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 664 | str := "abc" |
| 665 | p.otherProps.F = &str |
| 666 | } |
| 667 | |
| 668 | func TestUsedProperties(t *testing.T) { |
| 669 | testCases := []struct { |
| 670 | desc string |
| 671 | bp string |
| 672 | expectedProps []propInfo |
| 673 | }{ |
| 674 | { |
| 675 | desc: "only name", |
| 676 | bp: `test { |
| 677 | name: "foo", |
| 678 | } |
| 679 | `, |
| 680 | expectedProps: []propInfo{ |
| 681 | propInfo{"Name", "string"}, |
| 682 | }, |
| 683 | }, |
| 684 | { |
| 685 | desc: "some props", |
| 686 | bp: `test { |
| 687 | name: "foo", |
| 688 | a: "abc", |
| 689 | b: true, |
| 690 | d: 123, |
| 691 | } |
| 692 | `, |
| 693 | expectedProps: []propInfo{ |
| 694 | propInfo{"A", "string"}, |
| 695 | propInfo{"B", "bool"}, |
| 696 | propInfo{"D", "int64"}, |
| 697 | propInfo{"Name", "string"}, |
| 698 | }, |
| 699 | }, |
| 700 | { |
| 701 | desc: "unused non-pointer prop", |
| 702 | bp: `test { |
| 703 | name: "foo", |
| 704 | b: true, |
| 705 | d: 123, |
| 706 | } |
| 707 | `, |
| 708 | expectedProps: []propInfo{ |
| 709 | // for non-pointer cannot distinguish between unused and intentionally set to empty |
| 710 | propInfo{"A", "string"}, |
| 711 | propInfo{"B", "bool"}, |
| 712 | propInfo{"D", "int64"}, |
| 713 | propInfo{"Name", "string"}, |
| 714 | }, |
| 715 | }, |
| 716 | { |
| 717 | desc: "nested props", |
| 718 | bp: `test { |
| 719 | name: "foo", |
| 720 | nested: { |
| 721 | e: "abc", |
| 722 | } |
| 723 | } |
| 724 | `, |
| 725 | expectedProps: []propInfo{ |
| 726 | propInfo{"Nested.E", "string"}, |
| 727 | propInfo{"Name", "string"}, |
| 728 | }, |
| 729 | }, |
| 730 | { |
| 731 | desc: "arch props", |
| 732 | bp: `test { |
| 733 | name: "foo", |
| 734 | arch: { |
| 735 | x86_64: { |
| 736 | a: "abc", |
| 737 | }, |
| 738 | } |
| 739 | } |
| 740 | `, |
| 741 | expectedProps: []propInfo{ |
| 742 | propInfo{"Name", "string"}, |
| 743 | propInfo{"Arch.X86_64.A", "string"}, |
| 744 | }, |
| 745 | }, |
| 746 | { |
| 747 | desc: "embedded props", |
| 748 | bp: `test { |
| 749 | name: "foo", |
| 750 | embedded_prop: "a", |
| 751 | } |
| 752 | `, |
| 753 | expectedProps: []propInfo{ |
| 754 | propInfo{"Embedded_prop", "string"}, |
| 755 | propInfo{"Name", "string"}, |
| 756 | }, |
| 757 | }, |
| 758 | { |
| 759 | desc: "defaults", |
| 760 | bp: ` |
| 761 | test_defaults { |
| 762 | name: "foo_defaults", |
| 763 | a: "a", |
| 764 | b: true, |
| 765 | embedded_prop:"a", |
| 766 | arch: { |
| 767 | x86_64: { |
| 768 | a: "a", |
| 769 | }, |
| 770 | }, |
| 771 | } |
| 772 | test { |
| 773 | name: "foo", |
| 774 | defaults: ["foo_defaults"], |
| 775 | c: ["a"], |
| 776 | nested: { |
| 777 | e: "d", |
| 778 | }, |
| 779 | target: { |
| 780 | linux: { |
| 781 | a: "a", |
| 782 | }, |
| 783 | }, |
| 784 | } |
| 785 | `, |
| 786 | expectedProps: []propInfo{ |
| 787 | propInfo{"A", "string"}, |
| 788 | propInfo{"B", "bool"}, |
| 789 | propInfo{"C", "string slice"}, |
| 790 | propInfo{"Embedded_prop", "string"}, |
| 791 | propInfo{"Nested.E", "string"}, |
| 792 | propInfo{"Name", "string"}, |
| 793 | propInfo{"Arch.X86_64.A", "string"}, |
| 794 | propInfo{"Target.Linux.A", "string"}, |
| 795 | propInfo{"Defaults", "string slice"}, |
| 796 | }, |
| 797 | }, |
| 798 | } |
| 799 | |
| 800 | for _, tc := range testCases { |
| 801 | t.Run(tc.desc, func(t *testing.T) { |
| 802 | result := GroupFixturePreparers( |
| 803 | PrepareForTestWithAllowMissingDependencies, |
| 804 | PrepareForTestWithDefaults, |
| 805 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 806 | ctx.RegisterModuleType("test", propsTestModuleFactory) |
| 807 | ctx.RegisterModuleType("test_defaults", propsTestModuleDefaultsFactory) |
| 808 | }), |
| 809 | FixtureWithRootAndroidBp(tc.bp), |
| 810 | ).RunTest(t) |
| 811 | |
| 812 | foo := result.ModuleForTests("foo", "").Module().base() |
| 813 | |
| 814 | AssertDeepEquals(t, "foo ", tc.expectedProps, foo.propertiesWithValues()) |
| 815 | |
| 816 | }) |
| 817 | } |
| 818 | } |
Bob Badour | 4101c71 | 2022-02-09 11:54:35 -0800 | [diff] [blame^] | 819 | |
| 820 | func TestSortedUniqueNamedPaths(t *testing.T) { |
| 821 | type np struct { |
| 822 | path, name string |
| 823 | } |
| 824 | makePaths := func(l []np) NamedPaths { |
| 825 | result := make(NamedPaths, 0, len(l)) |
| 826 | for _, p := range l { |
| 827 | result = append(result, NamedPath{PathForTesting(p.path), p.name}) |
| 828 | } |
| 829 | return result |
| 830 | } |
| 831 | |
| 832 | tests := []struct { |
| 833 | name string |
| 834 | in []np |
| 835 | expectedOut []np |
| 836 | }{ |
| 837 | { |
| 838 | name: "empty", |
| 839 | in: []np{}, |
| 840 | expectedOut: []np{}, |
| 841 | }, |
| 842 | { |
| 843 | name: "all_same", |
| 844 | in: []np{ |
| 845 | {"a.txt", "A"}, |
| 846 | {"a.txt", "A"}, |
| 847 | {"a.txt", "A"}, |
| 848 | {"a.txt", "A"}, |
| 849 | {"a.txt", "A"}, |
| 850 | }, |
| 851 | expectedOut: []np{ |
| 852 | {"a.txt", "A"}, |
| 853 | }, |
| 854 | }, |
| 855 | { |
| 856 | name: "same_path_different_names", |
| 857 | in: []np{ |
| 858 | {"a.txt", "C"}, |
| 859 | {"a.txt", "A"}, |
| 860 | {"a.txt", "D"}, |
| 861 | {"a.txt", "B"}, |
| 862 | {"a.txt", "E"}, |
| 863 | }, |
| 864 | expectedOut: []np{ |
| 865 | {"a.txt", "A"}, |
| 866 | {"a.txt", "B"}, |
| 867 | {"a.txt", "C"}, |
| 868 | {"a.txt", "D"}, |
| 869 | {"a.txt", "E"}, |
| 870 | }, |
| 871 | }, |
| 872 | { |
| 873 | name: "different_paths_same_name", |
| 874 | in: []np{ |
| 875 | {"b/b.txt", "A"}, |
| 876 | {"a/a.txt", "A"}, |
| 877 | {"a/txt", "A"}, |
| 878 | {"b", "A"}, |
| 879 | {"a/b/d", "A"}, |
| 880 | }, |
| 881 | expectedOut: []np{ |
| 882 | {"a/a.txt", "A"}, |
| 883 | {"a/b/d", "A"}, |
| 884 | {"a/txt", "A"}, |
| 885 | {"b/b.txt", "A"}, |
| 886 | {"b", "A"}, |
| 887 | }, |
| 888 | }, |
| 889 | { |
| 890 | name: "all_different", |
| 891 | in: []np{ |
| 892 | {"b/b.txt", "A"}, |
| 893 | {"a/a.txt", "B"}, |
| 894 | {"a/txt", "D"}, |
| 895 | {"b", "C"}, |
| 896 | {"a/b/d", "E"}, |
| 897 | }, |
| 898 | expectedOut: []np{ |
| 899 | {"a/a.txt", "B"}, |
| 900 | {"a/b/d", "E"}, |
| 901 | {"a/txt", "D"}, |
| 902 | {"b/b.txt", "A"}, |
| 903 | {"b", "C"}, |
| 904 | }, |
| 905 | }, |
| 906 | { |
| 907 | name: "some_different", |
| 908 | in: []np{ |
| 909 | {"b/b.txt", "A"}, |
| 910 | {"a/a.txt", "B"}, |
| 911 | {"a/txt", "D"}, |
| 912 | {"a/b/d", "E"}, |
| 913 | {"b", "C"}, |
| 914 | {"a/a.txt", "B"}, |
| 915 | {"a/b/d", "E"}, |
| 916 | }, |
| 917 | expectedOut: []np{ |
| 918 | {"a/a.txt", "B"}, |
| 919 | {"a/b/d", "E"}, |
| 920 | {"a/txt", "D"}, |
| 921 | {"b/b.txt", "A"}, |
| 922 | {"b", "C"}, |
| 923 | }, |
| 924 | }, |
| 925 | } |
| 926 | for _, tt := range tests { |
| 927 | t.Run(tt.name, func(t *testing.T) { |
| 928 | actual := SortedUniqueNamedPaths(makePaths(tt.in)) |
| 929 | expected := makePaths(tt.expectedOut) |
| 930 | t.Logf("actual: %v", actual) |
| 931 | t.Logf("expected: %v", expected) |
| 932 | AssertDeepEquals(t, "SortedUniqueNamedPaths ", expected, actual) |
| 933 | }) |
| 934 | } |
| 935 | } |