blob: d096b2f9216bdd846a47c1aeec1f6607a85179aa [file] [log] [blame]
Colin Cross0e991752019-06-10 15:41:28 -07001// 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
15package android
16
17import (
Colin Crosseabaedd2020-02-06 17:01:55 -080018 "reflect"
Colin Cross0e991752019-06-10 15:41:28 -070019 "testing"
20
21 "github.com/google/blueprint/proptools"
22)
23
24type defaultsTestProperties struct {
25 Foo []string
26}
27
28type defaultsTestModule struct {
29 ModuleBase
30 DefaultableModuleBase
31 properties defaultsTestProperties
32}
33
34func (d *defaultsTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
35 ctx.Build(pctx, BuildParams{
36 Rule: Touch,
37 Output: PathForModuleOut(ctx, "out"),
38 })
39}
40
41func defaultsTestModuleFactory() Module {
42 module := &defaultsTestModule{}
43 module.AddProperties(&module.properties)
Colin Cross0e991752019-06-10 15:41:28 -070044 InitAndroidModule(module)
Colin Crosseabaedd2020-02-06 17:01:55 -080045 InitDefaultableModule(module)
Colin Cross0e991752019-06-10 15:41:28 -070046 return module
47}
48
49type defaultsTestDefaults struct {
50 ModuleBase
51 DefaultsModuleBase
52}
53
54func defaultsTestDefaultsFactory() Module {
55 defaults := &defaultsTestDefaults{}
56 defaults.AddProperties(&defaultsTestProperties{})
57 InitDefaultsModule(defaults)
58 return defaults
59}
60
Colin Crosseabaedd2020-02-06 17:01:55 -080061func TestDefaults(t *testing.T) {
62 bp := `
63 defaults {
64 name: "transitive",
65 foo: ["transitive"],
66 }
67
68 defaults {
69 name: "defaults",
70 defaults: ["transitive"],
71 foo: ["defaults"],
72 }
73
74 test {
75 name: "foo",
76 defaults: ["defaults"],
77 foo: ["module"],
78 }
79 `
80
81 config := TestConfig(buildDir, nil, bp, nil)
82
83 ctx := NewTestContext()
84
85 ctx.RegisterModuleType("test", defaultsTestModuleFactory)
86 ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory)
87
88 ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
89
90 ctx.Register(config)
91
92 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
93 FailIfErrored(t, errs)
94 _, errs = ctx.PrepareBuildActions(config)
95 FailIfErrored(t, errs)
96
97 foo := ctx.ModuleForTests("foo", "").Module().(*defaultsTestModule)
98
99 if g, w := foo.properties.Foo, []string{"transitive", "defaults", "module"}; !reflect.DeepEqual(g, w) {
100 t.Errorf("expected foo %q, got %q", w, g)
101 }
102}
103
Colin Cross0e991752019-06-10 15:41:28 -0700104func TestDefaultsAllowMissingDependencies(t *testing.T) {
Colin Cross0e991752019-06-10 15:41:28 -0700105 bp := `
106 defaults {
107 name: "defaults",
108 defaults: ["missing"],
109 foo: ["defaults"],
110 }
111
112 test {
113 name: "missing_defaults",
114 defaults: ["missing"],
115 foo: ["module"],
116 }
117
118 test {
119 name: "missing_transitive_defaults",
120 defaults: ["defaults"],
121 foo: ["module"],
122 }
123 `
124
Colin Cross98be1bb2019-12-13 20:41:13 -0800125 config := TestConfig(buildDir, nil, bp, nil)
126 config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
127
128 ctx := NewTestContext()
129 ctx.SetAllowMissingDependencies(true)
130
131 ctx.RegisterModuleType("test", defaultsTestModuleFactory)
132 ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory)
133
134 ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
135
136 ctx.Register(config)
Colin Cross0e991752019-06-10 15:41:28 -0700137
138 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
139 FailIfErrored(t, errs)
140 _, errs = ctx.PrepareBuildActions(config)
141 FailIfErrored(t, errs)
142
143 missingDefaults := ctx.ModuleForTests("missing_defaults", "").Output("out")
144 missingTransitiveDefaults := ctx.ModuleForTests("missing_transitive_defaults", "").Output("out")
145
146 if missingDefaults.Rule != ErrorRule {
147 t.Errorf("expected missing_defaults rule to be ErrorRule, got %#v", missingDefaults.Rule)
148 }
149
150 if g, w := missingDefaults.Args["error"], "module missing_defaults missing dependencies: missing\n"; g != w {
151 t.Errorf("want error %q, got %q", w, g)
152 }
153
154 // TODO: missing transitive defaults is currently not handled
155 _ = missingTransitiveDefaults
156}