1// 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" 19 20 "github.com/google/blueprint" 21) 22 23type defaultsTestProperties struct { 24 Foo []string 25 Path_prop []string `android:"path"` 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) 44 InitAndroidModule(module) 45 InitDefaultableModule(module) 46 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 61var prepareForDefaultsTest = GroupFixturePreparers( 62 PrepareForTestWithDefaults, 63 FixtureRegisterWithContext(func(ctx RegistrationContext) { 64 ctx.RegisterModuleType("test", defaultsTestModuleFactory) 65 ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory) 66 }), 67) 68 69func 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 89 result := GroupFixturePreparers( 90 prepareForDefaultsTest, 91 FixtureWithRootAndroidBp(bp), 92 ).RunTest(t) 93 94 foo := result.Module("foo", "").(*defaultsTestModule) 95 96 AssertDeepEquals(t, "foo", []string{"transitive", "defaults", "module"}, foo.properties.Foo) 97} 98 99func TestDefaultsAllowMissingDependencies(t *testing.T) { 100 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 120 result := GroupFixturePreparers( 121 prepareForDefaultsTest, 122 PrepareForTestWithAllowMissingDependencies, 123 FixtureWithRootAndroidBp(bp), 124 ).RunTest(t) 125 126 missingDefaults := result.ModuleForTests("missing_defaults", "").Output("out") 127 missingTransitiveDefaults := result.ModuleForTests("missing_transitive_defaults", "").Output("out") 128 129 AssertSame(t, "missing_defaults rule", ErrorRule, missingDefaults.Rule) 130 131 AssertStringEquals(t, "missing_defaults", "module missing_defaults missing dependencies: missing\n", missingDefaults.Args["error"]) 132 133 // TODO: missing transitive defaults is currently not handled 134 _ = missingTransitiveDefaults 135} 136 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} 173