blob: a7542abb35c6b0e97ed060f1f716b4e81c224211 [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 Cross0e991752019-06-10 15:41:28 -070019)
20
21type defaultsTestProperties struct {
22 Foo []string
23}
24
25type defaultsTestModule struct {
26 ModuleBase
27 DefaultableModuleBase
28 properties defaultsTestProperties
29}
30
31func (d *defaultsTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
32 ctx.Build(pctx, BuildParams{
33 Rule: Touch,
34 Output: PathForModuleOut(ctx, "out"),
35 })
36}
37
38func defaultsTestModuleFactory() Module {
39 module := &defaultsTestModule{}
40 module.AddProperties(&module.properties)
Colin Cross0e991752019-06-10 15:41:28 -070041 InitAndroidModule(module)
Colin Crosseabaedd2020-02-06 17:01:55 -080042 InitDefaultableModule(module)
Colin Cross0e991752019-06-10 15:41:28 -070043 return module
44}
45
46type defaultsTestDefaults struct {
47 ModuleBase
48 DefaultsModuleBase
49}
50
51func defaultsTestDefaultsFactory() Module {
52 defaults := &defaultsTestDefaults{}
53 defaults.AddProperties(&defaultsTestProperties{})
54 InitDefaultsModule(defaults)
55 return defaults
56}
57
Paul Duffin7c166b42021-03-16 19:31:17 +000058var prepareForDefaultsTest = GroupFixturePreparers(
59 PrepareForTestWithDefaults,
60 FixtureRegisterWithContext(func(ctx RegistrationContext) {
61 ctx.RegisterModuleType("test", defaultsTestModuleFactory)
62 ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory)
63 }),
64)
65
Colin Crosseabaedd2020-02-06 17:01:55 -080066func TestDefaults(t *testing.T) {
67 bp := `
68 defaults {
69 name: "transitive",
70 foo: ["transitive"],
71 }
72
73 defaults {
74 name: "defaults",
75 defaults: ["transitive"],
76 foo: ["defaults"],
77 }
78
79 test {
80 name: "foo",
81 defaults: ["defaults"],
82 foo: ["module"],
83 }
84 `
85
Paul Duffin30ac3e72021-03-20 00:36:14 +000086 result := GroupFixturePreparers(
Paul Duffin7c166b42021-03-16 19:31:17 +000087 prepareForDefaultsTest,
88 FixtureWithRootAndroidBp(bp),
Paul Duffin30ac3e72021-03-20 00:36:14 +000089 ).RunTest(t)
Colin Crosseabaedd2020-02-06 17:01:55 -080090
Paul Duffin7c166b42021-03-16 19:31:17 +000091 foo := result.Module("foo", "").(*defaultsTestModule)
Colin Crosseabaedd2020-02-06 17:01:55 -080092
Paul Duffin7c166b42021-03-16 19:31:17 +000093 AssertDeepEquals(t, "foo", []string{"transitive", "defaults", "module"}, foo.properties.Foo)
Colin Crosseabaedd2020-02-06 17:01:55 -080094}
95
Colin Cross0e991752019-06-10 15:41:28 -070096func TestDefaultsAllowMissingDependencies(t *testing.T) {
Colin Cross0e991752019-06-10 15:41:28 -070097 bp := `
98 defaults {
99 name: "defaults",
100 defaults: ["missing"],
101 foo: ["defaults"],
102 }
103
104 test {
105 name: "missing_defaults",
106 defaults: ["missing"],
107 foo: ["module"],
108 }
109
110 test {
111 name: "missing_transitive_defaults",
112 defaults: ["defaults"],
113 foo: ["module"],
114 }
115 `
116
Paul Duffin30ac3e72021-03-20 00:36:14 +0000117 result := GroupFixturePreparers(
Paul Duffin7c166b42021-03-16 19:31:17 +0000118 prepareForDefaultsTest,
119 PrepareForTestWithAllowMissingDependencies,
120 FixtureWithRootAndroidBp(bp),
Paul Duffin30ac3e72021-03-20 00:36:14 +0000121 ).RunTest(t)
Colin Cross98be1bb2019-12-13 20:41:13 -0800122
Paul Duffin7c166b42021-03-16 19:31:17 +0000123 missingDefaults := result.ModuleForTests("missing_defaults", "").Output("out")
124 missingTransitiveDefaults := result.ModuleForTests("missing_transitive_defaults", "").Output("out")
Colin Cross98be1bb2019-12-13 20:41:13 -0800125
Paul Duffin7c166b42021-03-16 19:31:17 +0000126 AssertSame(t, "missing_defaults rule", ErrorRule, missingDefaults.Rule)
Colin Cross98be1bb2019-12-13 20:41:13 -0800127
Paul Duffin7c166b42021-03-16 19:31:17 +0000128 AssertStringEquals(t, "missing_defaults", "module missing_defaults missing dependencies: missing\n", missingDefaults.Args["error"])
Colin Cross0e991752019-06-10 15:41:28 -0700129
130 // TODO: missing transitive defaults is currently not handled
131 _ = missingTransitiveDefaults
132}