blob: 0ad0fb85979e9d49da7b234813dcb5e30daed5ec [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 (
18 "testing"
Colin Cross69a680f2024-01-29 13:18:43 -080019
20 "github.com/google/blueprint"
Colin Cross0e991752019-06-10 15:41:28 -070021)
22
23type defaultsTestProperties struct {
Colin Cross69a680f2024-01-29 13:18:43 -080024 Foo []string
25 Path_prop []string `android:"path"`
Colin Cross0e991752019-06-10 15:41:28 -070026}
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
Paul Duffin7c166b42021-03-16 19:31:17 +000061var prepareForDefaultsTest = GroupFixturePreparers(
62 PrepareForTestWithDefaults,
63 FixtureRegisterWithContext(func(ctx RegistrationContext) {
64 ctx.RegisterModuleType("test", defaultsTestModuleFactory)
65 ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory)
66 }),
67)
68
Colin Crosseabaedd2020-02-06 17:01:55 -080069func TestDefaults(t *testing.T) {
70 bp := `
71 defaults {
72 name: "transitive",
73 foo: ["transitive"],
74 }
75
76 defaults {
77 name: "defaults",
78 defaults: ["transitive"],
79 foo: ["defaults"],
80 }
81
82 test {
83 name: "foo",
84 defaults: ["defaults"],
85 foo: ["module"],
86 }
87 `
88
Paul Duffin30ac3e72021-03-20 00:36:14 +000089 result := GroupFixturePreparers(
Paul Duffin7c166b42021-03-16 19:31:17 +000090 prepareForDefaultsTest,
91 FixtureWithRootAndroidBp(bp),
Paul Duffin30ac3e72021-03-20 00:36:14 +000092 ).RunTest(t)
Colin Crosseabaedd2020-02-06 17:01:55 -080093
Paul Duffin7c166b42021-03-16 19:31:17 +000094 foo := result.Module("foo", "").(*defaultsTestModule)
Colin Crosseabaedd2020-02-06 17:01:55 -080095
Paul Duffin7c166b42021-03-16 19:31:17 +000096 AssertDeepEquals(t, "foo", []string{"transitive", "defaults", "module"}, foo.properties.Foo)
Colin Crosseabaedd2020-02-06 17:01:55 -080097}
98
Colin Cross0e991752019-06-10 15:41:28 -070099func TestDefaultsAllowMissingDependencies(t *testing.T) {
Colin Cross0e991752019-06-10 15:41:28 -0700100 bp := `
101 defaults {
102 name: "defaults",
103 defaults: ["missing"],
104 foo: ["defaults"],
105 }
106
107 test {
108 name: "missing_defaults",
109 defaults: ["missing"],
110 foo: ["module"],
111 }
112
113 test {
114 name: "missing_transitive_defaults",
115 defaults: ["defaults"],
116 foo: ["module"],
117 }
118 `
119
Paul Duffin30ac3e72021-03-20 00:36:14 +0000120 result := GroupFixturePreparers(
Paul Duffin7c166b42021-03-16 19:31:17 +0000121 prepareForDefaultsTest,
122 PrepareForTestWithAllowMissingDependencies,
123 FixtureWithRootAndroidBp(bp),
Paul Duffin30ac3e72021-03-20 00:36:14 +0000124 ).RunTest(t)
Colin Cross98be1bb2019-12-13 20:41:13 -0800125
Paul Duffin7c166b42021-03-16 19:31:17 +0000126 missingDefaults := result.ModuleForTests("missing_defaults", "").Output("out")
127 missingTransitiveDefaults := result.ModuleForTests("missing_transitive_defaults", "").Output("out")
Colin Cross98be1bb2019-12-13 20:41:13 -0800128
Paul Duffin7c166b42021-03-16 19:31:17 +0000129 AssertSame(t, "missing_defaults rule", ErrorRule, missingDefaults.Rule)
Colin Cross98be1bb2019-12-13 20:41:13 -0800130
Paul Duffin7c166b42021-03-16 19:31:17 +0000131 AssertStringEquals(t, "missing_defaults", "module missing_defaults missing dependencies: missing\n", missingDefaults.Args["error"])
Colin Cross0e991752019-06-10 15:41:28 -0700132
133 // TODO: missing transitive defaults is currently not handled
134 _ = missingTransitiveDefaults
135}
Colin Cross69a680f2024-01-29 13:18:43 -0800136
137func TestDefaultsPathProperties(t *testing.T) {
138 bp := `
139 defaults {
140 name: "defaults",
141 path_prop: [":gen"],
142 }
143
144 test {
145 name: "foo",
146 defaults: ["defaults"],
147 }
148
149 test {
150 name: "gen",
151 }
152 `
153
154 result := GroupFixturePreparers(
155 prepareForDefaultsTest,
156 FixtureWithRootAndroidBp(bp),
157 ).RunTest(t)
158
159 collectDeps := func(m Module) []string {
160 var deps []string
161 result.VisitDirectDeps(m, func(dep blueprint.Module) {
162 deps = append(deps, result.ModuleName(dep))
163 })
164 return deps
165 }
166
167 foo := result.Module("foo", "")
168 defaults := result.Module("defaults", "")
169
170 AssertStringListContains(t, "foo should depend on gen", collectDeps(foo), "gen")
171 AssertStringListDoesNotContain(t, "defaults should not depend on gen", collectDeps(defaults), "gen")
172}