blob: b134eae387ece16a02ad872ec1edffe8eed6d5a2 [file] [log] [blame]
Colin Crosscec81712017-07-13 14:43:27 -07001// 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
15package android
16
17import (
18 "fmt"
Paul Duffin9b478b02019-12-10 13:41:51 +000019 "path/filepath"
Logan Chienee97c3e2018-03-12 16:34:26 +080020 "regexp"
Martin Stjernholm4c021242020-05-13 01:13:50 +010021 "sort"
Colin Crosscec81712017-07-13 14:43:27 -070022 "strings"
Logan Chien42039712018-03-12 16:29:17 +080023 "testing"
Colin Crosscec81712017-07-13 14:43:27 -070024
25 "github.com/google/blueprint"
26)
27
Colin Crossae8600b2020-10-29 17:09:13 -070028func NewTestContext(config Config) *TestContext {
Jeff Gaston088e29e2017-11-29 16:47:17 -080029 namespaceExportFilter := func(namespace *Namespace) bool {
30 return true
31 }
Jeff Gastonb274ed32017-12-01 17:10:33 -080032
33 nameResolver := NewNameResolver(namespaceExportFilter)
34 ctx := &TestContext{
Colin Crossae8600b2020-10-29 17:09:13 -070035 Context: &Context{blueprint.NewContext(), config},
Jeff Gastonb274ed32017-12-01 17:10:33 -080036 NameResolver: nameResolver,
37 }
38
39 ctx.SetNameInterface(nameResolver)
Jeff Gaston088e29e2017-11-29 16:47:17 -080040
Colin Cross1b488422019-03-04 22:33:56 -080041 ctx.postDeps = append(ctx.postDeps, registerPathDepsMutator)
42
Colin Crossae8600b2020-10-29 17:09:13 -070043 ctx.SetFs(ctx.config.fs)
44 if ctx.config.mockBpList != "" {
45 ctx.SetModuleListFile(ctx.config.mockBpList)
46 }
47
Jeff Gaston088e29e2017-11-29 16:47:17 -080048 return ctx
Colin Crosscec81712017-07-13 14:43:27 -070049}
50
Paul Duffina560d5a2021-02-28 01:38:51 +000051var PrepareForTestWithArchMutator = GroupFixturePreparers(
Paul Duffin35816122021-02-24 01:49:52 +000052 // 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
61var PrepareForTestWithDefaults = FixtureRegisterWithContext(func(ctx RegistrationContext) {
62 ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
63})
64
65var PrepareForTestWithComponentsMutator = FixtureRegisterWithContext(func(ctx RegistrationContext) {
66 ctx.PreArchMutators(RegisterComponentsMutator)
67})
68
69var PrepareForTestWithPrebuilts = FixtureRegisterWithContext(RegisterPrebuiltMutators)
70
71var PrepareForTestWithOverrides = FixtureRegisterWithContext(func(ctx RegistrationContext) {
72 ctx.PostDepsMutators(RegisterOverridePostDepsMutators)
73})
74
75// Prepares an integration test with build components from the android package.
Paul Duffina560d5a2021-02-28 01:38:51 +000076var PrepareForIntegrationTestWithAndroid = GroupFixturePreparers(
Paul Duffin35816122021-02-24 01:49:52 +000077 // 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 Crossae8600b2020-10-29 17:09:13 -070088func NewTestArchContext(config Config) *TestContext {
89 ctx := NewTestContext(config)
Colin Crossae4c6182017-09-15 17:33:55 -070090 ctx.preDeps = append(ctx.preDeps, registerArchMutator)
91 return ctx
92}
93
Colin Crosscec81712017-07-13 14:43:27 -070094type TestContext struct {
Colin Cross4c83e5c2019-02-25 14:54:28 -080095 *Context
Liz Kammer356f7d42021-01-26 09:18:53 -050096 preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc
97 bp2buildPreArch, bp2buildDeps, bp2buildMutators []RegisterMutatorFunc
98 NameResolver *NameResolver
Colin Crosscec81712017-07-13 14:43:27 -070099}
100
101func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
102 ctx.preArch = append(ctx.preArch, f)
103}
104
Paul Duffina80ef842020-01-14 12:09:36 +0000105func (ctx *TestContext) HardCodedPreArchMutators(f RegisterMutatorFunc) {
106 // Register mutator function as normal for testing.
107 ctx.PreArchMutators(f)
108}
109
Colin Crosscec81712017-07-13 14:43:27 -0700110func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
111 ctx.preDeps = append(ctx.preDeps, f)
112}
113
114func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
115 ctx.postDeps = append(ctx.postDeps, f)
116}
117
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000118func (ctx *TestContext) FinalDepsMutators(f RegisterMutatorFunc) {
119 ctx.finalDeps = append(ctx.finalDeps, f)
120}
121
Jingwen Chen73850672020-12-14 08:25:34 -0500122// RegisterBp2BuildMutator registers a BazelTargetModule mutator for converting a module
123// type to the equivalent Bazel target.
124func (ctx *TestContext) RegisterBp2BuildMutator(moduleType string, m func(TopDownMutatorContext)) {
Jingwen Chen73850672020-12-14 08:25:34 -0500125 f := func(ctx RegisterMutatorsContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500126 ctx.TopDown(moduleType, m)
Jingwen Chen73850672020-12-14 08:25:34 -0500127 }
Jingwen Chena42d6412021-01-26 21:57:27 -0500128 ctx.bp2buildMutators = append(ctx.bp2buildMutators, f)
Jingwen Chen73850672020-12-14 08:25:34 -0500129}
130
Liz Kammer356f7d42021-01-26 09:18:53 -0500131// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
132// into Bazel BUILD targets that should run prior to deps and conversion.
133func (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.
139func (ctx *TestContext) DepsBp2BuildMutators(f RegisterMutatorFunc) {
140 ctx.bp2buildDeps = append(ctx.bp2buildDeps, f)
141}
142
Colin Crossae8600b2020-10-29 17:09:13 -0700143func (ctx *TestContext) Register() {
Paul Duffinc05b0342021-03-06 13:28:13 +0000144 mutators := collateRegisteredMutators(ctx.preArch, ctx.preDeps, ctx.postDeps, ctx.finalDeps)
145 mutators.registerAll(ctx.Context)
Colin Crosscec81712017-07-13 14:43:27 -0700146
Colin Cross4b49b762019-11-22 15:25:03 -0800147 ctx.RegisterSingletonType("env", EnvSingleton)
Colin Cross31a738b2019-12-30 18:45:15 -0800148}
149
Jingwen Chen73850672020-12-14 08:25:34 -0500150// RegisterForBazelConversion prepares a test context for bp2build conversion.
151func (ctx *TestContext) RegisterForBazelConversion() {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000152 RegisterMutatorsForBazelConversion(ctx.Context, ctx.bp2buildPreArch, ctx.bp2buildDeps, ctx.bp2buildMutators)
Jingwen Chen73850672020-12-14 08:25:34 -0500153}
154
Colin Cross31a738b2019-12-30 18:45:15 -0800155func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) {
156 // This function adapts the old style ParseFileList calls that are spread throughout the tests
157 // to the new style that takes a config.
158 return ctx.Context.ParseFileList(rootDir, filePaths, ctx.config)
159}
160
161func (ctx *TestContext) ParseBlueprintsFiles(rootDir string) (deps []string, errs []error) {
162 // This function adapts the old style ParseBlueprintsFiles calls that are spread throughout the
163 // tests to the new style that takes a config.
164 return ctx.Context.ParseBlueprintsFiles(rootDir, ctx.config)
Colin Cross4b49b762019-11-22 15:25:03 -0800165}
166
167func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) {
168 ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory))
169}
170
Colin Cross9aed5bc2020-12-28 15:15:34 -0800171func (ctx *TestContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) {
172 s, m := SingletonModuleFactoryAdaptor(name, factory)
173 ctx.RegisterSingletonType(name, s)
174 ctx.RegisterModuleType(name, m)
175}
176
Colin Cross4b49b762019-11-22 15:25:03 -0800177func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) {
Colin Cross06fa5882020-10-29 18:21:38 -0700178 ctx.Context.RegisterSingletonType(name, SingletonFactoryAdaptor(ctx.Context, factory))
Colin Crosscec81712017-07-13 14:43:27 -0700179}
180
Paul Duffineafc16b2021-02-24 01:43:18 +0000181func (ctx *TestContext) RegisterPreSingletonType(name string, factory SingletonFactory) {
182 ctx.Context.RegisterPreSingletonType(name, SingletonFactoryAdaptor(ctx.Context, factory))
183}
184
Colin Crosscec81712017-07-13 14:43:27 -0700185func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
186 var module Module
187 ctx.VisitAllModules(func(m blueprint.Module) {
188 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
189 module = m.(Module)
190 }
191 })
192
193 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -0700194 // find all the modules that do exist
Colin Crossbeae6ec2020-08-11 12:02:11 -0700195 var allModuleNames []string
196 var allVariants []string
Jeff Gaston294356f2017-09-27 17:05:30 -0700197 ctx.VisitAllModules(func(m blueprint.Module) {
Colin Crossbeae6ec2020-08-11 12:02:11 -0700198 allModuleNames = append(allModuleNames, ctx.ModuleName(m))
199 if ctx.ModuleName(m) == name {
200 allVariants = append(allVariants, ctx.ModuleSubDir(m))
201 }
Jeff Gaston294356f2017-09-27 17:05:30 -0700202 })
Martin Stjernholm4c021242020-05-13 01:13:50 +0100203 sort.Strings(allModuleNames)
Colin Crossbeae6ec2020-08-11 12:02:11 -0700204 sort.Strings(allVariants)
Jeff Gaston294356f2017-09-27 17:05:30 -0700205
Colin Crossbeae6ec2020-08-11 12:02:11 -0700206 if len(allVariants) == 0 {
207 panic(fmt.Errorf("failed to find module %q. All modules:\n %s",
208 name, strings.Join(allModuleNames, "\n ")))
209 } else {
210 panic(fmt.Errorf("failed to find module %q variant %q. All variants:\n %s",
211 name, variant, strings.Join(allVariants, "\n ")))
212 }
Colin Crosscec81712017-07-13 14:43:27 -0700213 }
214
215 return TestingModule{module}
216}
217
Jiyong Park37b25202018-07-11 10:49:27 +0900218func (ctx *TestContext) ModuleVariantsForTests(name string) []string {
219 var variants []string
220 ctx.VisitAllModules(func(m blueprint.Module) {
221 if ctx.ModuleName(m) == name {
222 variants = append(variants, ctx.ModuleSubDir(m))
223 }
224 })
225 return variants
226}
227
Colin Cross4c83e5c2019-02-25 14:54:28 -0800228// SingletonForTests returns a TestingSingleton for the singleton registered with the given name.
229func (ctx *TestContext) SingletonForTests(name string) TestingSingleton {
230 allSingletonNames := []string{}
231 for _, s := range ctx.Singletons() {
232 n := ctx.SingletonName(s)
233 if n == name {
234 return TestingSingleton{
235 singleton: s.(*singletonAdaptor).Singleton,
236 provider: s.(testBuildProvider),
237 }
238 }
239 allSingletonNames = append(allSingletonNames, n)
240 }
241
242 panic(fmt.Errorf("failed to find singleton %q."+
243 "\nall singletons: %v", name, allSingletonNames))
244}
245
Colin Crossaa255532020-07-03 13:18:24 -0700246func (ctx *TestContext) Config() Config {
247 return ctx.config
248}
249
Colin Cross4c83e5c2019-02-25 14:54:28 -0800250type testBuildProvider interface {
251 BuildParamsForTests() []BuildParams
252 RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
253}
254
255type TestingBuildParams struct {
256 BuildParams
257 RuleParams blueprint.RuleParams
258}
259
260func newTestingBuildParams(provider testBuildProvider, bparams BuildParams) TestingBuildParams {
261 return TestingBuildParams{
262 BuildParams: bparams,
263 RuleParams: provider.RuleParamsForTests()[bparams.Rule],
264 }
265}
266
Thiébaud Weksteen3600b802020-08-27 15:50:24 +0200267func maybeBuildParamsFromRule(provider testBuildProvider, rule string) (TestingBuildParams, []string) {
268 var searchedRules []string
Colin Cross4c83e5c2019-02-25 14:54:28 -0800269 for _, p := range provider.BuildParamsForTests() {
Thiébaud Weksteen3600b802020-08-27 15:50:24 +0200270 searchedRules = append(searchedRules, p.Rule.String())
Colin Cross4c83e5c2019-02-25 14:54:28 -0800271 if strings.Contains(p.Rule.String(), rule) {
Thiébaud Weksteen3600b802020-08-27 15:50:24 +0200272 return newTestingBuildParams(provider, p), searchedRules
Colin Cross4c83e5c2019-02-25 14:54:28 -0800273 }
274 }
Thiébaud Weksteen3600b802020-08-27 15:50:24 +0200275 return TestingBuildParams{}, searchedRules
Colin Cross4c83e5c2019-02-25 14:54:28 -0800276}
277
278func buildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
Thiébaud Weksteen3600b802020-08-27 15:50:24 +0200279 p, searchRules := maybeBuildParamsFromRule(provider, rule)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800280 if p.Rule == nil {
Thiébaud Weksteen3600b802020-08-27 15:50:24 +0200281 panic(fmt.Errorf("couldn't find rule %q.\nall rules: %v", rule, searchRules))
Colin Cross4c83e5c2019-02-25 14:54:28 -0800282 }
283 return p
284}
285
286func maybeBuildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
287 for _, p := range provider.BuildParamsForTests() {
Colin Crossb88b3c52019-06-10 15:15:17 -0700288 if strings.Contains(p.Description, desc) {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800289 return newTestingBuildParams(provider, p)
290 }
291 }
292 return TestingBuildParams{}
293}
294
295func buildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
296 p := maybeBuildParamsFromDescription(provider, desc)
297 if p.Rule == nil {
298 panic(fmt.Errorf("couldn't find description %q", desc))
299 }
300 return p
301}
302
303func maybeBuildParamsFromOutput(provider testBuildProvider, file string) (TestingBuildParams, []string) {
304 var searchedOutputs []string
305 for _, p := range provider.BuildParamsForTests() {
306 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700307 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800308 if p.Output != nil {
309 outputs = append(outputs, p.Output)
310 }
311 for _, f := range outputs {
312 if f.String() == file || f.Rel() == file {
313 return newTestingBuildParams(provider, p), nil
314 }
315 searchedOutputs = append(searchedOutputs, f.Rel())
316 }
317 }
318 return TestingBuildParams{}, searchedOutputs
319}
320
321func buildParamsFromOutput(provider testBuildProvider, file string) TestingBuildParams {
322 p, searchedOutputs := maybeBuildParamsFromOutput(provider, file)
323 if p.Rule == nil {
324 panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
325 file, searchedOutputs))
326 }
327 return p
328}
329
330func allOutputs(provider testBuildProvider) []string {
331 var outputFullPaths []string
332 for _, p := range provider.BuildParamsForTests() {
333 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700334 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800335 if p.Output != nil {
336 outputs = append(outputs, p.Output)
337 }
338 outputFullPaths = append(outputFullPaths, outputs.Strings()...)
339 }
340 return outputFullPaths
341}
342
Colin Crossb77ffc42019-01-05 22:09:19 -0800343// TestingModule is wrapper around an android.Module that provides methods to find information about individual
344// ctx.Build parameters for verification in tests.
Colin Crosscec81712017-07-13 14:43:27 -0700345type TestingModule struct {
346 module Module
347}
348
Colin Crossb77ffc42019-01-05 22:09:19 -0800349// Module returns the Module wrapped by the TestingModule.
Colin Crosscec81712017-07-13 14:43:27 -0700350func (m TestingModule) Module() Module {
351 return m.module
352}
353
Colin Crossb77ffc42019-01-05 22:09:19 -0800354// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
355// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800356func (m TestingModule) MaybeRule(rule string) TestingBuildParams {
Thiébaud Weksteen3600b802020-08-27 15:50:24 +0200357 r, _ := maybeBuildParamsFromRule(m.module, rule)
358 return r
Colin Crosscec81712017-07-13 14:43:27 -0700359}
360
Colin Crossb77ffc42019-01-05 22:09:19 -0800361// 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 Cross4c83e5c2019-02-25 14:54:28 -0800362func (m TestingModule) Rule(rule string) TestingBuildParams {
363 return buildParamsFromRule(m.module, rule)
Colin Crossb77ffc42019-01-05 22:09:19 -0800364}
365
366// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
367// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800368func (m TestingModule) MaybeDescription(desc string) TestingBuildParams {
369 return maybeBuildParamsFromDescription(m.module, desc)
Nan Zhanged19fc32017-10-19 13:06:22 -0700370}
371
Colin Crossb77ffc42019-01-05 22:09:19 -0800372// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
373// found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800374func (m TestingModule) Description(desc string) TestingBuildParams {
375 return buildParamsFromDescription(m.module, desc)
Colin Crossb77ffc42019-01-05 22:09:19 -0800376}
377
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800378// MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
Colin Crossb77ffc42019-01-05 22:09:19 -0800379// value matches the provided string. Returns an empty BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800380func (m TestingModule) MaybeOutput(file string) TestingBuildParams {
381 p, _ := maybeBuildParamsFromOutput(m.module, file)
Colin Crossb77ffc42019-01-05 22:09:19 -0800382 return p
383}
384
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800385// Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
Colin Crossb77ffc42019-01-05 22:09:19 -0800386// value matches the provided string. Panics if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800387func (m TestingModule) Output(file string) TestingBuildParams {
388 return buildParamsFromOutput(m.module, file)
Colin Crosscec81712017-07-13 14:43:27 -0700389}
Logan Chien42039712018-03-12 16:29:17 +0800390
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800391// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
392func (m TestingModule) AllOutputs() []string {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800393 return allOutputs(m.module)
394}
395
396// TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual
397// ctx.Build parameters for verification in tests.
398type TestingSingleton struct {
399 singleton Singleton
400 provider testBuildProvider
401}
402
403// Singleton returns the Singleton wrapped by the TestingSingleton.
404func (s TestingSingleton) Singleton() Singleton {
405 return s.singleton
406}
407
408// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
409// BuildParams if no rule is found.
410func (s TestingSingleton) MaybeRule(rule string) TestingBuildParams {
Thiébaud Weksteen3600b802020-08-27 15:50:24 +0200411 r, _ := maybeBuildParamsFromRule(s.provider, rule)
412 return r
Colin Cross4c83e5c2019-02-25 14:54:28 -0800413}
414
415// Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found.
416func (s TestingSingleton) Rule(rule string) TestingBuildParams {
417 return buildParamsFromRule(s.provider, rule)
418}
419
420// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
421// BuildParams if no rule is found.
422func (s TestingSingleton) MaybeDescription(desc string) TestingBuildParams {
423 return maybeBuildParamsFromDescription(s.provider, desc)
424}
425
426// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
427// found.
428func (s TestingSingleton) Description(desc string) TestingBuildParams {
429 return buildParamsFromDescription(s.provider, desc)
430}
431
432// MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
433// value matches the provided string. Returns an empty BuildParams if no rule is found.
434func (s TestingSingleton) MaybeOutput(file string) TestingBuildParams {
435 p, _ := maybeBuildParamsFromOutput(s.provider, file)
436 return p
437}
438
439// Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
440// value matches the provided string. Panics if no rule is found.
441func (s TestingSingleton) Output(file string) TestingBuildParams {
442 return buildParamsFromOutput(s.provider, file)
443}
444
445// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
446func (s TestingSingleton) AllOutputs() []string {
447 return allOutputs(s.provider)
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800448}
449
Logan Chien42039712018-03-12 16:29:17 +0800450func FailIfErrored(t *testing.T, errs []error) {
451 t.Helper()
452 if len(errs) > 0 {
453 for _, err := range errs {
454 t.Error(err)
455 }
456 t.FailNow()
457 }
458}
Logan Chienee97c3e2018-03-12 16:34:26 +0800459
Paul Duffinea8a3862021-03-04 17:58:33 +0000460// Fail if no errors that matched the regular expression were found.
461//
462// Returns true if a matching error was found, false otherwise.
463func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) bool {
Logan Chienee97c3e2018-03-12 16:34:26 +0800464 t.Helper()
465
466 matcher, err := regexp.Compile(pattern)
467 if err != nil {
Paul Duffinea8a3862021-03-04 17:58:33 +0000468 t.Fatalf("failed to compile regular expression %q because %s", pattern, err)
Logan Chienee97c3e2018-03-12 16:34:26 +0800469 }
470
471 found := false
472 for _, err := range errs {
473 if matcher.FindStringIndex(err.Error()) != nil {
474 found = true
475 break
476 }
477 }
478 if !found {
479 t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs))
480 for i, err := range errs {
Colin Crossaede88c2020-08-11 12:17:01 -0700481 t.Errorf("errs[%d] = %q", i, err)
Logan Chienee97c3e2018-03-12 16:34:26 +0800482 }
483 }
Paul Duffinea8a3862021-03-04 17:58:33 +0000484
485 return found
Logan Chienee97c3e2018-03-12 16:34:26 +0800486}
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700487
Paul Duffin91e38192019-08-05 15:07:57 +0100488func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) {
489 t.Helper()
490
491 if expectedErrorPatterns == nil {
492 FailIfErrored(t, errs)
493 } else {
494 for _, expectedError := range expectedErrorPatterns {
495 FailIfNoMatchingErrors(t, expectedError, errs)
496 }
497 if len(errs) > len(expectedErrorPatterns) {
498 t.Errorf("additional errors found, expected %d, found %d",
499 len(expectedErrorPatterns), len(errs))
500 for i, expectedError := range expectedErrorPatterns {
501 t.Errorf("expectedErrors[%d] = %s", i, expectedError)
502 }
503 for i, err := range errs {
504 t.Errorf("errs[%d] = %s", i, err)
505 }
Paul Duffinea8a3862021-03-04 17:58:33 +0000506 t.FailNow()
Paul Duffin91e38192019-08-05 15:07:57 +0100507 }
508 }
Paul Duffin91e38192019-08-05 15:07:57 +0100509}
510
Jingwen Chencda22c92020-11-23 00:22:30 -0500511func SetKatiEnabledForTests(config Config) {
512 config.katiEnabled = true
Paul Duffin8c3fec42020-03-04 20:15:08 +0000513}
514
Colin Crossaa255532020-07-03 13:18:24 -0700515func AndroidMkEntriesForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) []AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700516 var p AndroidMkEntriesProvider
517 var ok bool
518 if p, ok = mod.(AndroidMkEntriesProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100519 t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name())
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700520 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900521
522 entriesList := p.AndroidMkEntries()
523 for i, _ := range entriesList {
Colin Crossaa255532020-07-03 13:18:24 -0700524 entriesList[i].fillInEntries(ctx, mod)
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900525 }
526 return entriesList
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700527}
Jooyung Han12df5fb2019-07-11 16:18:47 +0900528
Colin Crossaa255532020-07-03 13:18:24 -0700529func AndroidMkDataForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) AndroidMkData {
Jooyung Han12df5fb2019-07-11 16:18:47 +0900530 var p AndroidMkDataProvider
531 var ok bool
532 if p, ok = mod.(AndroidMkDataProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100533 t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name())
Jooyung Han12df5fb2019-07-11 16:18:47 +0900534 }
535 data := p.AndroidMk()
Colin Crossaa255532020-07-03 13:18:24 -0700536 data.fillInData(ctx, mod)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900537 return data
538}
Paul Duffin9b478b02019-12-10 13:41:51 +0000539
540// Normalize the path for testing.
541//
542// If the path is relative to the build directory then return the relative path
543// to avoid tests having to deal with the dynamically generated build directory.
544//
545// Otherwise, return the supplied path as it is almost certainly a source path
546// that is relative to the root of the source tree.
547//
548// The build and source paths should be distinguishable based on their contents.
549func NormalizePathForTesting(path Path) string {
Paul Duffin064b70c2020-11-02 17:32:38 +0000550 if path == nil {
551 return "<nil path>"
552 }
Paul Duffin9b478b02019-12-10 13:41:51 +0000553 p := path.String()
Paul Duffindb170e42020-12-08 17:48:25 +0000554 // Allow absolute paths to /dev/
555 if strings.HasPrefix(p, "/dev/") {
556 return p
557 }
Paul Duffin9b478b02019-12-10 13:41:51 +0000558 if w, ok := path.(WritablePath); ok {
559 rel, err := filepath.Rel(w.buildDir(), p)
560 if err != nil {
561 panic(err)
562 }
563 return rel
564 }
565 return p
566}
567
568func NormalizePathsForTesting(paths Paths) []string {
569 var result []string
570 for _, path := range paths {
571 relative := NormalizePathForTesting(path)
572 result = append(result, relative)
573 }
574 return result
575}