Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 1 | // Copyright 2019 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 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | |
| 20 | "github.com/google/blueprint/proptools" |
| 21 | ) |
| 22 | |
| 23 | type defaultsTestProperties struct { |
| 24 | Foo []string |
| 25 | } |
| 26 | |
| 27 | type defaultsTestModule struct { |
| 28 | ModuleBase |
| 29 | DefaultableModuleBase |
| 30 | properties defaultsTestProperties |
| 31 | } |
| 32 | |
| 33 | func (d *defaultsTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 34 | ctx.Build(pctx, BuildParams{ |
| 35 | Rule: Touch, |
| 36 | Output: PathForModuleOut(ctx, "out"), |
| 37 | }) |
| 38 | } |
| 39 | |
| 40 | func defaultsTestModuleFactory() Module { |
| 41 | module := &defaultsTestModule{} |
| 42 | module.AddProperties(&module.properties) |
| 43 | InitDefaultableModule(module) |
| 44 | InitAndroidModule(module) |
| 45 | return module |
| 46 | } |
| 47 | |
| 48 | type defaultsTestDefaults struct { |
| 49 | ModuleBase |
| 50 | DefaultsModuleBase |
| 51 | } |
| 52 | |
| 53 | func defaultsTestDefaultsFactory() Module { |
| 54 | defaults := &defaultsTestDefaults{} |
| 55 | defaults.AddProperties(&defaultsTestProperties{}) |
| 56 | InitDefaultsModule(defaults) |
| 57 | return defaults |
| 58 | } |
| 59 | |
| 60 | func TestDefaultsAllowMissingDependencies(t *testing.T) { |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 61 | bp := ` |
| 62 | defaults { |
| 63 | name: "defaults", |
| 64 | defaults: ["missing"], |
| 65 | foo: ["defaults"], |
| 66 | } |
| 67 | |
| 68 | test { |
| 69 | name: "missing_defaults", |
| 70 | defaults: ["missing"], |
| 71 | foo: ["module"], |
| 72 | } |
| 73 | |
| 74 | test { |
| 75 | name: "missing_transitive_defaults", |
| 76 | defaults: ["defaults"], |
| 77 | foo: ["module"], |
| 78 | } |
| 79 | ` |
| 80 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 81 | config := TestConfig(buildDir, nil, bp, nil) |
| 82 | config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true) |
| 83 | |
| 84 | ctx := NewTestContext() |
| 85 | ctx.SetAllowMissingDependencies(true) |
| 86 | |
| 87 | ctx.RegisterModuleType("test", defaultsTestModuleFactory) |
| 88 | ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory) |
| 89 | |
| 90 | ctx.PreArchMutators(RegisterDefaultsPreArchMutators) |
| 91 | |
| 92 | ctx.Register(config) |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 93 | |
| 94 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 95 | FailIfErrored(t, errs) |
| 96 | _, errs = ctx.PrepareBuildActions(config) |
| 97 | FailIfErrored(t, errs) |
| 98 | |
| 99 | missingDefaults := ctx.ModuleForTests("missing_defaults", "").Output("out") |
| 100 | missingTransitiveDefaults := ctx.ModuleForTests("missing_transitive_defaults", "").Output("out") |
| 101 | |
| 102 | if missingDefaults.Rule != ErrorRule { |
| 103 | t.Errorf("expected missing_defaults rule to be ErrorRule, got %#v", missingDefaults.Rule) |
| 104 | } |
| 105 | |
| 106 | if g, w := missingDefaults.Args["error"], "module missing_defaults missing dependencies: missing\n"; g != w { |
| 107 | t.Errorf("want error %q, got %q", w, g) |
| 108 | } |
| 109 | |
| 110 | // TODO: missing transitive defaults is currently not handled |
| 111 | _ = missingTransitiveDefaults |
| 112 | } |