blob: 76bb5c8c29e6f16cc38dd4ecfa7b084555556b6b [file] [log] [blame]
Colin Crossdc35e212019-06-06 16:13:11 -07001// 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
15package android
16
17import (
Colin Crossdc35e212019-06-06 16:13:11 -070018 "reflect"
19 "testing"
20
21 "github.com/google/blueprint/proptools"
22)
23
24type mutatorTestModule struct {
25 ModuleBase
26 props struct {
27 }
28
29 missingDeps []string
30}
31
32func mutatorTestModuleFactory() Module {
33 module := &mutatorTestModule{}
34 module.AddProperties(&module.props)
35 InitAndroidModule(module)
36 return module
37}
38
39func (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
48func (m *mutatorTestModule) DepsMutator(ctx BottomUpMutatorContext) {
49 ctx.AddDependency(ctx.Module(), nil, "regular_missing_dep")
50}
51
52func addMissingDependenciesMutator(ctx TopDownMutatorContext) {
53 ctx.AddMissingDependencies([]string{"added_missing_dep"})
54}
55
56func TestMutatorAddMissingDependencies(t *testing.T) {
Colin Crossdc35e212019-06-06 16:13:11 -070057 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}