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