Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 1 | // Copyright 2017 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 19 | "path/filepath" |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 20 | "regexp" |
Martin Stjernholm | 4c02124 | 2020-05-13 01:13:50 +0100 | [diff] [blame] | 21 | "sort" |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 22 | "strings" |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 23 | "testing" |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 24 | |
| 25 | "github.com/google/blueprint" |
| 26 | ) |
| 27 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 28 | func NewTestContext(config Config) *TestContext { |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 29 | namespaceExportFilter := func(namespace *Namespace) bool { |
| 30 | return true |
| 31 | } |
Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 32 | |
| 33 | nameResolver := NewNameResolver(namespaceExportFilter) |
| 34 | ctx := &TestContext{ |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 35 | Context: &Context{blueprint.NewContext(), config}, |
Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 36 | NameResolver: nameResolver, |
| 37 | } |
| 38 | |
| 39 | ctx.SetNameInterface(nameResolver) |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 40 | |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 41 | ctx.postDeps = append(ctx.postDeps, registerPathDepsMutator) |
| 42 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 43 | ctx.SetFs(ctx.config.fs) |
| 44 | if ctx.config.mockBpList != "" { |
| 45 | ctx.SetModuleListFile(ctx.config.mockBpList) |
| 46 | } |
| 47 | |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 48 | return ctx |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Paul Duffin | a560d5a | 2021-02-28 01:38:51 +0000 | [diff] [blame^] | 51 | var PrepareForTestWithArchMutator = GroupFixturePreparers( |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 52 | // Configure architecture targets in the fixture config. |
| 53 | FixtureModifyConfig(modifyTestConfigToSupportArchMutator), |
| 54 | |
| 55 | // Add the arch mutator to the context. |
| 56 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 57 | ctx.PreDepsMutators(registerArchMutator) |
| 58 | }), |
| 59 | ) |
| 60 | |
| 61 | var PrepareForTestWithDefaults = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 62 | ctx.PreArchMutators(RegisterDefaultsPreArchMutators) |
| 63 | }) |
| 64 | |
| 65 | var PrepareForTestWithComponentsMutator = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 66 | ctx.PreArchMutators(RegisterComponentsMutator) |
| 67 | }) |
| 68 | |
| 69 | var PrepareForTestWithPrebuilts = FixtureRegisterWithContext(RegisterPrebuiltMutators) |
| 70 | |
| 71 | var PrepareForTestWithOverrides = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 72 | ctx.PostDepsMutators(RegisterOverridePostDepsMutators) |
| 73 | }) |
| 74 | |
| 75 | // Prepares an integration test with build components from the android package. |
Paul Duffin | a560d5a | 2021-02-28 01:38:51 +0000 | [diff] [blame^] | 76 | var PrepareForIntegrationTestWithAndroid = GroupFixturePreparers( |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 77 | // Mutators. Must match order in mutator.go. |
| 78 | PrepareForTestWithArchMutator, |
| 79 | PrepareForTestWithDefaults, |
| 80 | PrepareForTestWithComponentsMutator, |
| 81 | PrepareForTestWithPrebuilts, |
| 82 | PrepareForTestWithOverrides, |
| 83 | |
| 84 | // Modules |
| 85 | PrepareForTestWithFilegroup, |
| 86 | ) |
| 87 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 88 | func NewTestArchContext(config Config) *TestContext { |
| 89 | ctx := NewTestContext(config) |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 90 | ctx.preDeps = append(ctx.preDeps, registerArchMutator) |
| 91 | return ctx |
| 92 | } |
| 93 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 94 | type TestContext struct { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 95 | *Context |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 96 | preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc |
| 97 | bp2buildPreArch, bp2buildDeps, bp2buildMutators []RegisterMutatorFunc |
| 98 | NameResolver *NameResolver |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) { |
| 102 | ctx.preArch = append(ctx.preArch, f) |
| 103 | } |
| 104 | |
Paul Duffin | a80ef84 | 2020-01-14 12:09:36 +0000 | [diff] [blame] | 105 | func (ctx *TestContext) HardCodedPreArchMutators(f RegisterMutatorFunc) { |
| 106 | // Register mutator function as normal for testing. |
| 107 | ctx.PreArchMutators(f) |
| 108 | } |
| 109 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 110 | func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) { |
| 111 | ctx.preDeps = append(ctx.preDeps, f) |
| 112 | } |
| 113 | |
| 114 | func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) { |
| 115 | ctx.postDeps = append(ctx.postDeps, f) |
| 116 | } |
| 117 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 118 | func (ctx *TestContext) FinalDepsMutators(f RegisterMutatorFunc) { |
| 119 | ctx.finalDeps = append(ctx.finalDeps, f) |
| 120 | } |
| 121 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 122 | // RegisterBp2BuildMutator registers a BazelTargetModule mutator for converting a module |
| 123 | // type to the equivalent Bazel target. |
| 124 | func (ctx *TestContext) RegisterBp2BuildMutator(moduleType string, m func(TopDownMutatorContext)) { |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 125 | f := func(ctx RegisterMutatorsContext) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 126 | ctx.TopDown(moduleType, m) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 127 | } |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 128 | ctx.bp2buildMutators = append(ctx.bp2buildMutators, f) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 131 | // PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules |
| 132 | // into Bazel BUILD targets that should run prior to deps and conversion. |
| 133 | func (ctx *TestContext) PreArchBp2BuildMutators(f RegisterMutatorFunc) { |
| 134 | ctx.bp2buildPreArch = append(ctx.bp2buildPreArch, f) |
| 135 | } |
| 136 | |
| 137 | // DepsBp2BuildMutators adds mutators to be register for converting Android Blueprint modules into |
| 138 | // Bazel BUILD targets that should run prior to conversion to resolve dependencies. |
| 139 | func (ctx *TestContext) DepsBp2BuildMutators(f RegisterMutatorFunc) { |
| 140 | ctx.bp2buildDeps = append(ctx.bp2buildDeps, f) |
| 141 | } |
| 142 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 143 | func (ctx *TestContext) Register() { |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 144 | registerMutators(ctx.Context.Context, ctx.preArch, ctx.preDeps, ctx.postDeps, ctx.finalDeps) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 145 | |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 146 | ctx.RegisterSingletonType("env", EnvSingleton) |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 149 | // RegisterForBazelConversion prepares a test context for bp2build conversion. |
| 150 | func (ctx *TestContext) RegisterForBazelConversion() { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 151 | RegisterMutatorsForBazelConversion(ctx.Context.Context, ctx.bp2buildPreArch, ctx.bp2buildDeps, ctx.bp2buildMutators) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 154 | func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) { |
| 155 | // This function adapts the old style ParseFileList calls that are spread throughout the tests |
| 156 | // to the new style that takes a config. |
| 157 | return ctx.Context.ParseFileList(rootDir, filePaths, ctx.config) |
| 158 | } |
| 159 | |
| 160 | func (ctx *TestContext) ParseBlueprintsFiles(rootDir string) (deps []string, errs []error) { |
| 161 | // This function adapts the old style ParseBlueprintsFiles calls that are spread throughout the |
| 162 | // tests to the new style that takes a config. |
| 163 | return ctx.Context.ParseBlueprintsFiles(rootDir, ctx.config) |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) { |
| 167 | ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory)) |
| 168 | } |
| 169 | |
Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 170 | func (ctx *TestContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) { |
| 171 | s, m := SingletonModuleFactoryAdaptor(name, factory) |
| 172 | ctx.RegisterSingletonType(name, s) |
| 173 | ctx.RegisterModuleType(name, m) |
| 174 | } |
| 175 | |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 176 | func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) { |
Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 177 | ctx.Context.RegisterSingletonType(name, SingletonFactoryAdaptor(ctx.Context, factory)) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Paul Duffin | eafc16b | 2021-02-24 01:43:18 +0000 | [diff] [blame] | 180 | func (ctx *TestContext) RegisterPreSingletonType(name string, factory SingletonFactory) { |
| 181 | ctx.Context.RegisterPreSingletonType(name, SingletonFactoryAdaptor(ctx.Context, factory)) |
| 182 | } |
| 183 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 184 | func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule { |
| 185 | var module Module |
| 186 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 187 | if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant { |
| 188 | module = m.(Module) |
| 189 | } |
| 190 | }) |
| 191 | |
| 192 | if module == nil { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 193 | // find all the modules that do exist |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 194 | var allModuleNames []string |
| 195 | var allVariants []string |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 196 | ctx.VisitAllModules(func(m blueprint.Module) { |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 197 | allModuleNames = append(allModuleNames, ctx.ModuleName(m)) |
| 198 | if ctx.ModuleName(m) == name { |
| 199 | allVariants = append(allVariants, ctx.ModuleSubDir(m)) |
| 200 | } |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 201 | }) |
Martin Stjernholm | 4c02124 | 2020-05-13 01:13:50 +0100 | [diff] [blame] | 202 | sort.Strings(allModuleNames) |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 203 | sort.Strings(allVariants) |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 204 | |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 205 | if len(allVariants) == 0 { |
| 206 | panic(fmt.Errorf("failed to find module %q. All modules:\n %s", |
| 207 | name, strings.Join(allModuleNames, "\n "))) |
| 208 | } else { |
| 209 | panic(fmt.Errorf("failed to find module %q variant %q. All variants:\n %s", |
| 210 | name, variant, strings.Join(allVariants, "\n "))) |
| 211 | } |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | return TestingModule{module} |
| 215 | } |
| 216 | |
Jiyong Park | 37b2520 | 2018-07-11 10:49:27 +0900 | [diff] [blame] | 217 | func (ctx *TestContext) ModuleVariantsForTests(name string) []string { |
| 218 | var variants []string |
| 219 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 220 | if ctx.ModuleName(m) == name { |
| 221 | variants = append(variants, ctx.ModuleSubDir(m)) |
| 222 | } |
| 223 | }) |
| 224 | return variants |
| 225 | } |
| 226 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 227 | // SingletonForTests returns a TestingSingleton for the singleton registered with the given name. |
| 228 | func (ctx *TestContext) SingletonForTests(name string) TestingSingleton { |
| 229 | allSingletonNames := []string{} |
| 230 | for _, s := range ctx.Singletons() { |
| 231 | n := ctx.SingletonName(s) |
| 232 | if n == name { |
| 233 | return TestingSingleton{ |
| 234 | singleton: s.(*singletonAdaptor).Singleton, |
| 235 | provider: s.(testBuildProvider), |
| 236 | } |
| 237 | } |
| 238 | allSingletonNames = append(allSingletonNames, n) |
| 239 | } |
| 240 | |
| 241 | panic(fmt.Errorf("failed to find singleton %q."+ |
| 242 | "\nall singletons: %v", name, allSingletonNames)) |
| 243 | } |
| 244 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 245 | func (ctx *TestContext) Config() Config { |
| 246 | return ctx.config |
| 247 | } |
| 248 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 249 | type testBuildProvider interface { |
| 250 | BuildParamsForTests() []BuildParams |
| 251 | RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams |
| 252 | } |
| 253 | |
| 254 | type TestingBuildParams struct { |
| 255 | BuildParams |
| 256 | RuleParams blueprint.RuleParams |
| 257 | } |
| 258 | |
| 259 | func newTestingBuildParams(provider testBuildProvider, bparams BuildParams) TestingBuildParams { |
| 260 | return TestingBuildParams{ |
| 261 | BuildParams: bparams, |
| 262 | RuleParams: provider.RuleParamsForTests()[bparams.Rule], |
| 263 | } |
| 264 | } |
| 265 | |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 266 | func maybeBuildParamsFromRule(provider testBuildProvider, rule string) (TestingBuildParams, []string) { |
| 267 | var searchedRules []string |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 268 | for _, p := range provider.BuildParamsForTests() { |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 269 | searchedRules = append(searchedRules, p.Rule.String()) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 270 | if strings.Contains(p.Rule.String(), rule) { |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 271 | return newTestingBuildParams(provider, p), searchedRules |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 272 | } |
| 273 | } |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 274 | return TestingBuildParams{}, searchedRules |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | func buildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams { |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 278 | p, searchRules := maybeBuildParamsFromRule(provider, rule) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 279 | if p.Rule == nil { |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 280 | panic(fmt.Errorf("couldn't find rule %q.\nall rules: %v", rule, searchRules)) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 281 | } |
| 282 | return p |
| 283 | } |
| 284 | |
| 285 | func maybeBuildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams { |
| 286 | for _, p := range provider.BuildParamsForTests() { |
Colin Cross | b88b3c5 | 2019-06-10 15:15:17 -0700 | [diff] [blame] | 287 | if strings.Contains(p.Description, desc) { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 288 | return newTestingBuildParams(provider, p) |
| 289 | } |
| 290 | } |
| 291 | return TestingBuildParams{} |
| 292 | } |
| 293 | |
| 294 | func buildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams { |
| 295 | p := maybeBuildParamsFromDescription(provider, desc) |
| 296 | if p.Rule == nil { |
| 297 | panic(fmt.Errorf("couldn't find description %q", desc)) |
| 298 | } |
| 299 | return p |
| 300 | } |
| 301 | |
| 302 | func maybeBuildParamsFromOutput(provider testBuildProvider, file string) (TestingBuildParams, []string) { |
| 303 | var searchedOutputs []string |
| 304 | for _, p := range provider.BuildParamsForTests() { |
| 305 | outputs := append(WritablePaths(nil), p.Outputs...) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 306 | outputs = append(outputs, p.ImplicitOutputs...) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 307 | if p.Output != nil { |
| 308 | outputs = append(outputs, p.Output) |
| 309 | } |
| 310 | for _, f := range outputs { |
| 311 | if f.String() == file || f.Rel() == file { |
| 312 | return newTestingBuildParams(provider, p), nil |
| 313 | } |
| 314 | searchedOutputs = append(searchedOutputs, f.Rel()) |
| 315 | } |
| 316 | } |
| 317 | return TestingBuildParams{}, searchedOutputs |
| 318 | } |
| 319 | |
| 320 | func buildParamsFromOutput(provider testBuildProvider, file string) TestingBuildParams { |
| 321 | p, searchedOutputs := maybeBuildParamsFromOutput(provider, file) |
| 322 | if p.Rule == nil { |
| 323 | panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v", |
| 324 | file, searchedOutputs)) |
| 325 | } |
| 326 | return p |
| 327 | } |
| 328 | |
| 329 | func allOutputs(provider testBuildProvider) []string { |
| 330 | var outputFullPaths []string |
| 331 | for _, p := range provider.BuildParamsForTests() { |
| 332 | outputs := append(WritablePaths(nil), p.Outputs...) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 333 | outputs = append(outputs, p.ImplicitOutputs...) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 334 | if p.Output != nil { |
| 335 | outputs = append(outputs, p.Output) |
| 336 | } |
| 337 | outputFullPaths = append(outputFullPaths, outputs.Strings()...) |
| 338 | } |
| 339 | return outputFullPaths |
| 340 | } |
| 341 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 342 | // TestingModule is wrapper around an android.Module that provides methods to find information about individual |
| 343 | // ctx.Build parameters for verification in tests. |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 344 | type TestingModule struct { |
| 345 | module Module |
| 346 | } |
| 347 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 348 | // Module returns the Module wrapped by the TestingModule. |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 349 | func (m TestingModule) Module() Module { |
| 350 | return m.module |
| 351 | } |
| 352 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 353 | // MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty |
| 354 | // BuildParams if no rule is found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 355 | func (m TestingModule) MaybeRule(rule string) TestingBuildParams { |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 356 | r, _ := maybeBuildParamsFromRule(m.module, rule) |
| 357 | return r |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 360 | // Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 361 | func (m TestingModule) Rule(rule string) TestingBuildParams { |
| 362 | return buildParamsFromRule(m.module, rule) |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | // MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty |
| 366 | // BuildParams if no rule is found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 367 | func (m TestingModule) MaybeDescription(desc string) TestingBuildParams { |
| 368 | return maybeBuildParamsFromDescription(m.module, desc) |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 371 | // Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is |
| 372 | // found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 373 | func (m TestingModule) Description(desc string) TestingBuildParams { |
| 374 | return buildParamsFromDescription(m.module, desc) |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 377 | // MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel() |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 378 | // value matches the provided string. Returns an empty BuildParams if no rule is found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 379 | func (m TestingModule) MaybeOutput(file string) TestingBuildParams { |
| 380 | p, _ := maybeBuildParamsFromOutput(m.module, file) |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 381 | return p |
| 382 | } |
| 383 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 384 | // Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel() |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 385 | // value matches the provided string. Panics if no rule is found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 386 | func (m TestingModule) Output(file string) TestingBuildParams { |
| 387 | return buildParamsFromOutput(m.module, file) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 388 | } |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 389 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 390 | // AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms. |
| 391 | func (m TestingModule) AllOutputs() []string { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 392 | return allOutputs(m.module) |
| 393 | } |
| 394 | |
| 395 | // TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual |
| 396 | // ctx.Build parameters for verification in tests. |
| 397 | type TestingSingleton struct { |
| 398 | singleton Singleton |
| 399 | provider testBuildProvider |
| 400 | } |
| 401 | |
| 402 | // Singleton returns the Singleton wrapped by the TestingSingleton. |
| 403 | func (s TestingSingleton) Singleton() Singleton { |
| 404 | return s.singleton |
| 405 | } |
| 406 | |
| 407 | // MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty |
| 408 | // BuildParams if no rule is found. |
| 409 | func (s TestingSingleton) MaybeRule(rule string) TestingBuildParams { |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 410 | r, _ := maybeBuildParamsFromRule(s.provider, rule) |
| 411 | return r |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | // Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found. |
| 415 | func (s TestingSingleton) Rule(rule string) TestingBuildParams { |
| 416 | return buildParamsFromRule(s.provider, rule) |
| 417 | } |
| 418 | |
| 419 | // MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty |
| 420 | // BuildParams if no rule is found. |
| 421 | func (s TestingSingleton) MaybeDescription(desc string) TestingBuildParams { |
| 422 | return maybeBuildParamsFromDescription(s.provider, desc) |
| 423 | } |
| 424 | |
| 425 | // Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is |
| 426 | // found. |
| 427 | func (s TestingSingleton) Description(desc string) TestingBuildParams { |
| 428 | return buildParamsFromDescription(s.provider, desc) |
| 429 | } |
| 430 | |
| 431 | // MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel() |
| 432 | // value matches the provided string. Returns an empty BuildParams if no rule is found. |
| 433 | func (s TestingSingleton) MaybeOutput(file string) TestingBuildParams { |
| 434 | p, _ := maybeBuildParamsFromOutput(s.provider, file) |
| 435 | return p |
| 436 | } |
| 437 | |
| 438 | // Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel() |
| 439 | // value matches the provided string. Panics if no rule is found. |
| 440 | func (s TestingSingleton) Output(file string) TestingBuildParams { |
| 441 | return buildParamsFromOutput(s.provider, file) |
| 442 | } |
| 443 | |
| 444 | // AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms. |
| 445 | func (s TestingSingleton) AllOutputs() []string { |
| 446 | return allOutputs(s.provider) |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 449 | func FailIfErrored(t *testing.T, errs []error) { |
| 450 | t.Helper() |
| 451 | if len(errs) > 0 { |
| 452 | for _, err := range errs { |
| 453 | t.Error(err) |
| 454 | } |
| 455 | t.FailNow() |
| 456 | } |
| 457 | } |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 458 | |
| 459 | func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) { |
| 460 | t.Helper() |
| 461 | |
| 462 | matcher, err := regexp.Compile(pattern) |
| 463 | if err != nil { |
| 464 | t.Errorf("failed to compile regular expression %q because %s", pattern, err) |
| 465 | } |
| 466 | |
| 467 | found := false |
| 468 | for _, err := range errs { |
| 469 | if matcher.FindStringIndex(err.Error()) != nil { |
| 470 | found = true |
| 471 | break |
| 472 | } |
| 473 | } |
| 474 | if !found { |
| 475 | t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs)) |
| 476 | for i, err := range errs { |
Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 477 | t.Errorf("errs[%d] = %q", i, err) |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 481 | |
Paul Duffin | 91e3819 | 2019-08-05 15:07:57 +0100 | [diff] [blame] | 482 | func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) { |
| 483 | t.Helper() |
| 484 | |
| 485 | if expectedErrorPatterns == nil { |
| 486 | FailIfErrored(t, errs) |
| 487 | } else { |
| 488 | for _, expectedError := range expectedErrorPatterns { |
| 489 | FailIfNoMatchingErrors(t, expectedError, errs) |
| 490 | } |
| 491 | if len(errs) > len(expectedErrorPatterns) { |
| 492 | t.Errorf("additional errors found, expected %d, found %d", |
| 493 | len(expectedErrorPatterns), len(errs)) |
| 494 | for i, expectedError := range expectedErrorPatterns { |
| 495 | t.Errorf("expectedErrors[%d] = %s", i, expectedError) |
| 496 | } |
| 497 | for i, err := range errs { |
| 498 | t.Errorf("errs[%d] = %s", i, err) |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | } |
| 504 | |
Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 505 | func SetKatiEnabledForTests(config Config) { |
| 506 | config.katiEnabled = true |
Paul Duffin | 8c3fec4 | 2020-03-04 20:15:08 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 509 | func AndroidMkEntriesForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) []AndroidMkEntries { |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 510 | var p AndroidMkEntriesProvider |
| 511 | var ok bool |
| 512 | if p, ok = mod.(AndroidMkEntriesProvider); !ok { |
Roland Levillain | dfe75b3 | 2019-07-23 16:53:32 +0100 | [diff] [blame] | 513 | t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name()) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 514 | } |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 515 | |
| 516 | entriesList := p.AndroidMkEntries() |
| 517 | for i, _ := range entriesList { |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 518 | entriesList[i].fillInEntries(ctx, mod) |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 519 | } |
| 520 | return entriesList |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 521 | } |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 522 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 523 | func AndroidMkDataForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) AndroidMkData { |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 524 | var p AndroidMkDataProvider |
| 525 | var ok bool |
| 526 | if p, ok = mod.(AndroidMkDataProvider); !ok { |
Roland Levillain | dfe75b3 | 2019-07-23 16:53:32 +0100 | [diff] [blame] | 527 | t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name()) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 528 | } |
| 529 | data := p.AndroidMk() |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 530 | data.fillInData(ctx, mod) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 531 | return data |
| 532 | } |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 533 | |
| 534 | // Normalize the path for testing. |
| 535 | // |
| 536 | // If the path is relative to the build directory then return the relative path |
| 537 | // to avoid tests having to deal with the dynamically generated build directory. |
| 538 | // |
| 539 | // Otherwise, return the supplied path as it is almost certainly a source path |
| 540 | // that is relative to the root of the source tree. |
| 541 | // |
| 542 | // The build and source paths should be distinguishable based on their contents. |
| 543 | func NormalizePathForTesting(path Path) string { |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 544 | if path == nil { |
| 545 | return "<nil path>" |
| 546 | } |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 547 | p := path.String() |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 548 | // Allow absolute paths to /dev/ |
| 549 | if strings.HasPrefix(p, "/dev/") { |
| 550 | return p |
| 551 | } |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 552 | if w, ok := path.(WritablePath); ok { |
| 553 | rel, err := filepath.Rel(w.buildDir(), p) |
| 554 | if err != nil { |
| 555 | panic(err) |
| 556 | } |
| 557 | return rel |
| 558 | } |
| 559 | return p |
| 560 | } |
| 561 | |
| 562 | func NormalizePathsForTesting(paths Paths) []string { |
| 563 | var result []string |
| 564 | for _, path := range paths { |
| 565 | relative := NormalizePathForTesting(path) |
| 566 | result = append(result, relative) |
| 567 | } |
| 568 | return result |
| 569 | } |