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 { |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 27 | Deps_missing_deps []string |
| 28 | Mutator_missing_deps []string |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 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) { |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 51 | ctx.AddDependency(ctx.Module(), nil, m.props.Deps_missing_deps...) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | func addMissingDependenciesMutator(ctx TopDownMutatorContext) { |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 55 | ctx.AddMissingDependencies(ctx.Module().(*mutatorTestModule).props.Mutator_missing_deps) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | func TestMutatorAddMissingDependencies(t *testing.T) { |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 59 | config := TestConfig(buildDir, nil) |
| 60 | config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true) |
| 61 | |
| 62 | ctx := NewTestContext() |
| 63 | ctx.SetAllowMissingDependencies(true) |
| 64 | |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 65 | ctx.RegisterModuleType("test", mutatorTestModuleFactory) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 66 | ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { |
| 67 | ctx.TopDown("add_missing_dependencies", addMissingDependenciesMutator) |
| 68 | }) |
| 69 | |
| 70 | bp := ` |
| 71 | test { |
| 72 | name: "foo", |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 73 | deps_missing_deps: ["regular_missing_dep"], |
| 74 | mutator_missing_deps: ["added_missing_dep"], |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 75 | } |
| 76 | ` |
| 77 | |
| 78 | mockFS := map[string][]byte{ |
| 79 | "Android.bp": []byte(bp), |
| 80 | } |
| 81 | |
| 82 | ctx.MockFileSystem(mockFS) |
| 83 | |
| 84 | ctx.Register() |
| 85 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 86 | FailIfErrored(t, errs) |
| 87 | _, errs = ctx.PrepareBuildActions(config) |
| 88 | FailIfErrored(t, errs) |
| 89 | |
| 90 | foo := ctx.ModuleForTests("foo", "").Module().(*mutatorTestModule) |
| 91 | |
| 92 | if g, w := foo.missingDeps, []string{"added_missing_dep", "regular_missing_dep"}; !reflect.DeepEqual(g, w) { |
| 93 | t.Errorf("want foo missing deps %q, got %q", w, g) |
| 94 | } |
| 95 | } |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 96 | |
| 97 | func TestModuleString(t *testing.T) { |
| 98 | ctx := NewTestContext() |
| 99 | |
| 100 | var moduleStrings []string |
| 101 | |
| 102 | ctx.PreArchMutators(func(ctx RegisterMutatorsContext) { |
| 103 | ctx.BottomUp("pre_arch", func(ctx BottomUpMutatorContext) { |
| 104 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 105 | ctx.CreateVariations("a", "b") |
| 106 | }) |
| 107 | ctx.TopDown("rename_top_down", func(ctx TopDownMutatorContext) { |
| 108 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 109 | ctx.Rename(ctx.Module().base().Name() + "_renamed1") |
| 110 | }) |
| 111 | }) |
| 112 | |
| 113 | ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { |
| 114 | ctx.BottomUp("pre_deps", func(ctx BottomUpMutatorContext) { |
| 115 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 116 | ctx.CreateVariations("c", "d") |
| 117 | }) |
| 118 | }) |
| 119 | |
| 120 | ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) { |
| 121 | ctx.BottomUp("post_deps", func(ctx BottomUpMutatorContext) { |
| 122 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 123 | ctx.CreateLocalVariations("e", "f") |
| 124 | }) |
| 125 | ctx.BottomUp("rename_bottom_up", func(ctx BottomUpMutatorContext) { |
| 126 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 127 | ctx.Rename(ctx.Module().base().Name() + "_renamed2") |
| 128 | }) |
| 129 | ctx.BottomUp("final", func(ctx BottomUpMutatorContext) { |
| 130 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 131 | }) |
| 132 | }) |
| 133 | |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 134 | ctx.RegisterModuleType("test", mutatorTestModuleFactory) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 135 | |
| 136 | bp := ` |
| 137 | test { |
| 138 | name: "foo", |
| 139 | } |
| 140 | ` |
| 141 | |
| 142 | mockFS := map[string][]byte{ |
| 143 | "Android.bp": []byte(bp), |
| 144 | } |
| 145 | |
| 146 | ctx.MockFileSystem(mockFS) |
| 147 | |
| 148 | ctx.Register() |
| 149 | |
| 150 | config := TestConfig(buildDir, nil) |
| 151 | |
| 152 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 153 | FailIfErrored(t, errs) |
| 154 | _, errs = ctx.PrepareBuildActions(config) |
| 155 | FailIfErrored(t, errs) |
| 156 | |
| 157 | want := []string{ |
| 158 | // Initial name. |
| 159 | "foo{}", |
| 160 | |
| 161 | // After pre_arch (reversed because rename_top_down is TopDown so it visits in reverse order). |
| 162 | "foo{pre_arch:b}", |
| 163 | "foo{pre_arch:a}", |
| 164 | |
| 165 | // After rename_top_down. |
| 166 | "foo_renamed1{pre_arch:a}", |
| 167 | "foo_renamed1{pre_arch:b}", |
| 168 | |
| 169 | // After pre_deps. |
| 170 | "foo_renamed1{pre_arch:a,pre_deps:c}", |
| 171 | "foo_renamed1{pre_arch:a,pre_deps:d}", |
| 172 | "foo_renamed1{pre_arch:b,pre_deps:c}", |
| 173 | "foo_renamed1{pre_arch:b,pre_deps:d}", |
| 174 | |
| 175 | // After post_deps. |
| 176 | "foo_renamed1{pre_arch:a,pre_deps:c,post_deps:e}", |
| 177 | "foo_renamed1{pre_arch:a,pre_deps:c,post_deps:f}", |
| 178 | "foo_renamed1{pre_arch:a,pre_deps:d,post_deps:e}", |
| 179 | "foo_renamed1{pre_arch:a,pre_deps:d,post_deps:f}", |
| 180 | "foo_renamed1{pre_arch:b,pre_deps:c,post_deps:e}", |
| 181 | "foo_renamed1{pre_arch:b,pre_deps:c,post_deps:f}", |
| 182 | "foo_renamed1{pre_arch:b,pre_deps:d,post_deps:e}", |
| 183 | "foo_renamed1{pre_arch:b,pre_deps:d,post_deps:f}", |
| 184 | |
| 185 | // After rename_bottom_up. |
| 186 | "foo_renamed2{pre_arch:a,pre_deps:c,post_deps:e}", |
| 187 | "foo_renamed2{pre_arch:a,pre_deps:c,post_deps:f}", |
| 188 | "foo_renamed2{pre_arch:a,pre_deps:d,post_deps:e}", |
| 189 | "foo_renamed2{pre_arch:a,pre_deps:d,post_deps:f}", |
| 190 | "foo_renamed2{pre_arch:b,pre_deps:c,post_deps:e}", |
| 191 | "foo_renamed2{pre_arch:b,pre_deps:c,post_deps:f}", |
| 192 | "foo_renamed2{pre_arch:b,pre_deps:d,post_deps:e}", |
| 193 | "foo_renamed2{pre_arch:b,pre_deps:d,post_deps:f}", |
| 194 | } |
| 195 | |
| 196 | if !reflect.DeepEqual(moduleStrings, want) { |
| 197 | t.Errorf("want module String() values:\n%q\ngot:\n%q", want, moduleStrings) |
| 198 | } |
| 199 | } |