Colin Cross | dc35e21 | 2019-06-06 16:13:11 -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 | |
| 17 | import ( |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 18 | "reflect" |
| 19 | "testing" |
| 20 | |
| 21 | "github.com/google/blueprint/proptools" |
| 22 | ) |
| 23 | |
| 24 | type mutatorTestModule struct { |
| 25 | ModuleBase |
| 26 | props struct { |
| 27 | } |
| 28 | |
| 29 | missingDeps []string |
| 30 | } |
| 31 | |
| 32 | func mutatorTestModuleFactory() Module { |
| 33 | module := &mutatorTestModule{} |
| 34 | module.AddProperties(&module.props) |
| 35 | InitAndroidModule(module) |
| 36 | return module |
| 37 | } |
| 38 | |
| 39 | func (m *mutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 40 | ctx.Build(pctx, BuildParams{ |
| 41 | Rule: Touch, |
| 42 | Output: PathForModuleOut(ctx, "output"), |
| 43 | }) |
| 44 | |
| 45 | m.missingDeps = ctx.GetMissingDependencies() |
| 46 | } |
| 47 | |
| 48 | func (m *mutatorTestModule) DepsMutator(ctx BottomUpMutatorContext) { |
| 49 | ctx.AddDependency(ctx.Module(), nil, "regular_missing_dep") |
| 50 | } |
| 51 | |
| 52 | func addMissingDependenciesMutator(ctx TopDownMutatorContext) { |
| 53 | ctx.AddMissingDependencies([]string{"added_missing_dep"}) |
| 54 | } |
| 55 | |
| 56 | func TestMutatorAddMissingDependencies(t *testing.T) { |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 57 | config := TestConfig(buildDir, nil) |
| 58 | config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true) |
| 59 | |
| 60 | ctx := NewTestContext() |
| 61 | ctx.SetAllowMissingDependencies(true) |
| 62 | |
| 63 | ctx.RegisterModuleType("test", ModuleFactoryAdaptor(mutatorTestModuleFactory)) |
| 64 | ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { |
| 65 | ctx.TopDown("add_missing_dependencies", addMissingDependenciesMutator) |
| 66 | }) |
| 67 | |
| 68 | bp := ` |
| 69 | test { |
| 70 | name: "foo", |
| 71 | } |
| 72 | ` |
| 73 | |
| 74 | mockFS := map[string][]byte{ |
| 75 | "Android.bp": []byte(bp), |
| 76 | } |
| 77 | |
| 78 | ctx.MockFileSystem(mockFS) |
| 79 | |
| 80 | ctx.Register() |
| 81 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 82 | FailIfErrored(t, errs) |
| 83 | _, errs = ctx.PrepareBuildActions(config) |
| 84 | FailIfErrored(t, errs) |
| 85 | |
| 86 | foo := ctx.ModuleForTests("foo", "").Module().(*mutatorTestModule) |
| 87 | |
| 88 | if g, w := foo.missingDeps, []string{"added_missing_dep", "regular_missing_dep"}; !reflect.DeepEqual(g, w) { |
| 89 | t.Errorf("want foo missing deps %q, got %q", w, g) |
| 90 | } |
| 91 | } |