blob: a6abe8f064275a22a2695b66afad4918e3001171 [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) {
Colin Cross323dc602020-09-18 14:25:31 -070062 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -080063 bp := `
64 defaults {
65 name: "transitive",
66 foo: ["transitive"],
67 }
68
69 defaults {
70 name: "defaults",
71 defaults: ["transitive"],
72 foo: ["defaults"],
73 }
74
75 test {
76 name: "foo",
77 defaults: ["defaults"],
78 foo: ["module"],
79 }
80 `
81
82 config := TestConfig(buildDir, nil, bp, nil)
83
84 ctx := NewTestContext()
85
86 ctx.RegisterModuleType("test", defaultsTestModuleFactory)
87 ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory)
88
89 ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
90
91 ctx.Register(config)
92
93 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
94 FailIfErrored(t, errs)
95 _, errs = ctx.PrepareBuildActions(config)
96 FailIfErrored(t, errs)
97
98 foo := ctx.ModuleForTests("foo", "").Module().(*defaultsTestModule)
99
100 if g, w := foo.properties.Foo, []string{"transitive", "defaults", "module"}; !reflect.DeepEqual(g, w) {
101 t.Errorf("expected foo %q, got %q", w, g)
102 }
103}
104
Colin Cross0e991752019-06-10 15:41:28 -0700105func TestDefaultsAllowMissingDependencies(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700106 t.Parallel()
Colin Cross0e991752019-06-10 15:41:28 -0700107 bp := `
108 defaults {
109 name: "defaults",
110 defaults: ["missing"],
111 foo: ["defaults"],
112 }
113
114 test {
115 name: "missing_defaults",
116 defaults: ["missing"],
117 foo: ["module"],
118 }
119
120 test {
121 name: "missing_transitive_defaults",
122 defaults: ["defaults"],
123 foo: ["module"],
124 }
125 `
126
Colin Cross98be1bb2019-12-13 20:41:13 -0800127 config := TestConfig(buildDir, nil, bp, nil)
128 config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
129
130 ctx := NewTestContext()
131 ctx.SetAllowMissingDependencies(true)
132
133 ctx.RegisterModuleType("test", defaultsTestModuleFactory)
134 ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory)
135
136 ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
137
138 ctx.Register(config)
Colin Cross0e991752019-06-10 15:41:28 -0700139
140 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
141 FailIfErrored(t, errs)
142 _, errs = ctx.PrepareBuildActions(config)
143 FailIfErrored(t, errs)
144
145 missingDefaults := ctx.ModuleForTests("missing_defaults", "").Output("out")
146 missingTransitiveDefaults := ctx.ModuleForTests("missing_transitive_defaults", "").Output("out")
147
148 if missingDefaults.Rule != ErrorRule {
149 t.Errorf("expected missing_defaults rule to be ErrorRule, got %#v", missingDefaults.Rule)
150 }
151
152 if g, w := missingDefaults.Args["error"], "module missing_defaults missing dependencies: missing\n"; g != w {
153 t.Errorf("want error %q, got %q", w, g)
154 }
155
156 // TODO: missing transitive defaults is currently not handled
157 _ = missingTransitiveDefaults
158}