blob: 1b1feb77447164d0688f0e2cd49c7194a08a881d [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
Colin Crossae8600b2020-10-29 17:09:13 -070051func NewTestArchContext(config Config) *TestContext {
52 ctx := NewTestContext(config)
Colin Crossae4c6182017-09-15 17:33:55 -070053 ctx.preDeps = append(ctx.preDeps, registerArchMutator)
54 return ctx
55}
56
Colin Crosscec81712017-07-13 14:43:27 -070057type TestContext struct {
Colin Cross4c83e5c2019-02-25 14:54:28 -080058 *Context
Liz Kammer356f7d42021-01-26 09:18:53 -050059 preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc
60 bp2buildPreArch, bp2buildDeps, bp2buildMutators []RegisterMutatorFunc
61 NameResolver *NameResolver
Colin Crosscec81712017-07-13 14:43:27 -070062}
63
64func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
65 ctx.preArch = append(ctx.preArch, f)
66}
67
Paul Duffina80ef842020-01-14 12:09:36 +000068func (ctx *TestContext) HardCodedPreArchMutators(f RegisterMutatorFunc) {
69 // Register mutator function as normal for testing.
70 ctx.PreArchMutators(f)
71}
72
Colin Crosscec81712017-07-13 14:43:27 -070073func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
74 ctx.preDeps = append(ctx.preDeps, f)
75}
76
77func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
78 ctx.postDeps = append(ctx.postDeps, f)
79}
80
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000081func (ctx *TestContext) FinalDepsMutators(f RegisterMutatorFunc) {
82 ctx.finalDeps = append(ctx.finalDeps, f)
83}
84
Jingwen Chen73850672020-12-14 08:25:34 -050085// RegisterBp2BuildMutator registers a BazelTargetModule mutator for converting a module
86// type to the equivalent Bazel target.
87func (ctx *TestContext) RegisterBp2BuildMutator(moduleType string, m func(TopDownMutatorContext)) {
Jingwen Chen73850672020-12-14 08:25:34 -050088 f := func(ctx RegisterMutatorsContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -050089 ctx.TopDown(moduleType, m)
Jingwen Chen73850672020-12-14 08:25:34 -050090 }
Jingwen Chena42d6412021-01-26 21:57:27 -050091 ctx.bp2buildMutators = append(ctx.bp2buildMutators, f)
Jingwen Chen73850672020-12-14 08:25:34 -050092}
93
Liz Kammer356f7d42021-01-26 09:18:53 -050094// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
95// into Bazel BUILD targets that should run prior to deps and conversion.
96func (ctx *TestContext) PreArchBp2BuildMutators(f RegisterMutatorFunc) {
97 ctx.bp2buildPreArch = append(ctx.bp2buildPreArch, f)
98}
99
100// DepsBp2BuildMutators adds mutators to be register for converting Android Blueprint modules into
101// Bazel BUILD targets that should run prior to conversion to resolve dependencies.
102func (ctx *TestContext) DepsBp2BuildMutators(f RegisterMutatorFunc) {
103 ctx.bp2buildDeps = append(ctx.bp2buildDeps, f)
104}
105
Colin Crossae8600b2020-10-29 17:09:13 -0700106func (ctx *TestContext) Register() {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000107 registerMutators(ctx.Context.Context, ctx.preArch, ctx.preDeps, ctx.postDeps, ctx.finalDeps)
Colin Crosscec81712017-07-13 14:43:27 -0700108
Colin Cross4b49b762019-11-22 15:25:03 -0800109 ctx.RegisterSingletonType("env", EnvSingleton)
Colin Cross31a738b2019-12-30 18:45:15 -0800110}
111
Jingwen Chen73850672020-12-14 08:25:34 -0500112// RegisterForBazelConversion prepares a test context for bp2build conversion.
113func (ctx *TestContext) RegisterForBazelConversion() {
Liz Kammer356f7d42021-01-26 09:18:53 -0500114 RegisterMutatorsForBazelConversion(ctx.Context.Context, ctx.bp2buildPreArch, ctx.bp2buildDeps, ctx.bp2buildMutators)
Jingwen Chen73850672020-12-14 08:25:34 -0500115}
116
Colin Cross31a738b2019-12-30 18:45:15 -0800117func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) {
118 // This function adapts the old style ParseFileList calls that are spread throughout the tests
119 // to the new style that takes a config.
120 return ctx.Context.ParseFileList(rootDir, filePaths, ctx.config)
121}
122
123func (ctx *TestContext) ParseBlueprintsFiles(rootDir string) (deps []string, errs []error) {
124 // This function adapts the old style ParseBlueprintsFiles calls that are spread throughout the
125 // tests to the new style that takes a config.
126 return ctx.Context.ParseBlueprintsFiles(rootDir, ctx.config)
Colin Cross4b49b762019-11-22 15:25:03 -0800127}
128
129func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) {
130 ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory))
131}
132
Colin Cross9aed5bc2020-12-28 15:15:34 -0800133func (ctx *TestContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) {
134 s, m := SingletonModuleFactoryAdaptor(name, factory)
135 ctx.RegisterSingletonType(name, s)
136 ctx.RegisterModuleType(name, m)
137}
138
Colin Cross4b49b762019-11-22 15:25:03 -0800139func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) {
Colin Cross06fa5882020-10-29 18:21:38 -0700140 ctx.Context.RegisterSingletonType(name, SingletonFactoryAdaptor(ctx.Context, factory))
Colin Crosscec81712017-07-13 14:43:27 -0700141}
142
Paul Duffineafc16b2021-02-24 01:43:18 +0000143func (ctx *TestContext) RegisterPreSingletonType(name string, factory SingletonFactory) {
144 ctx.Context.RegisterPreSingletonType(name, SingletonFactoryAdaptor(ctx.Context, factory))
145}
146
Colin Crosscec81712017-07-13 14:43:27 -0700147func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
148 var module Module
149 ctx.VisitAllModules(func(m blueprint.Module) {
150 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
151 module = m.(Module)
152 }
153 })
154
155 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -0700156 // find all the modules that do exist
Colin Crossbeae6ec2020-08-11 12:02:11 -0700157 var allModuleNames []string
158 var allVariants []string
Jeff Gaston294356f2017-09-27 17:05:30 -0700159 ctx.VisitAllModules(func(m blueprint.Module) {
Colin Crossbeae6ec2020-08-11 12:02:11 -0700160 allModuleNames = append(allModuleNames, ctx.ModuleName(m))
161 if ctx.ModuleName(m) == name {
162 allVariants = append(allVariants, ctx.ModuleSubDir(m))
163 }
Jeff Gaston294356f2017-09-27 17:05:30 -0700164 })
Martin Stjernholm4c021242020-05-13 01:13:50 +0100165 sort.Strings(allModuleNames)
Colin Crossbeae6ec2020-08-11 12:02:11 -0700166 sort.Strings(allVariants)
Jeff Gaston294356f2017-09-27 17:05:30 -0700167
Colin Crossbeae6ec2020-08-11 12:02:11 -0700168 if len(allVariants) == 0 {
169 panic(fmt.Errorf("failed to find module %q. All modules:\n %s",
170 name, strings.Join(allModuleNames, "\n ")))
171 } else {
172 panic(fmt.Errorf("failed to find module %q variant %q. All variants:\n %s",
173 name, variant, strings.Join(allVariants, "\n ")))
174 }
Colin Crosscec81712017-07-13 14:43:27 -0700175 }
176
177 return TestingModule{module}
178}
179
Jiyong Park37b25202018-07-11 10:49:27 +0900180func (ctx *TestContext) ModuleVariantsForTests(name string) []string {
181 var variants []string
182 ctx.VisitAllModules(func(m blueprint.Module) {
183 if ctx.ModuleName(m) == name {
184 variants = append(variants, ctx.ModuleSubDir(m))
185 }
186 })
187 return variants
188}
189
Colin Cross4c83e5c2019-02-25 14:54:28 -0800190// SingletonForTests returns a TestingSingleton for the singleton registered with the given name.
191func (ctx *TestContext) SingletonForTests(name string) TestingSingleton {
192 allSingletonNames := []string{}
193 for _, s := range ctx.Singletons() {
194 n := ctx.SingletonName(s)
195 if n == name {
196 return TestingSingleton{
197 singleton: s.(*singletonAdaptor).Singleton,
198 provider: s.(testBuildProvider),
199 }
200 }
201 allSingletonNames = append(allSingletonNames, n)
202 }
203
204 panic(fmt.Errorf("failed to find singleton %q."+
205 "\nall singletons: %v", name, allSingletonNames))
206}
207
Colin Crossaa255532020-07-03 13:18:24 -0700208func (ctx *TestContext) Config() Config {
209 return ctx.config
210}
211
Colin Cross4c83e5c2019-02-25 14:54:28 -0800212type testBuildProvider interface {
213 BuildParamsForTests() []BuildParams
214 RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
215}
216
217type TestingBuildParams struct {
218 BuildParams
219 RuleParams blueprint.RuleParams
220}
221
222func newTestingBuildParams(provider testBuildProvider, bparams BuildParams) TestingBuildParams {
223 return TestingBuildParams{
224 BuildParams: bparams,
225 RuleParams: provider.RuleParamsForTests()[bparams.Rule],
226 }
227}
228
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200229func maybeBuildParamsFromRule(provider testBuildProvider, rule string) (TestingBuildParams, []string) {
230 var searchedRules []string
Colin Cross4c83e5c2019-02-25 14:54:28 -0800231 for _, p := range provider.BuildParamsForTests() {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200232 searchedRules = append(searchedRules, p.Rule.String())
Colin Cross4c83e5c2019-02-25 14:54:28 -0800233 if strings.Contains(p.Rule.String(), rule) {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200234 return newTestingBuildParams(provider, p), searchedRules
Colin Cross4c83e5c2019-02-25 14:54:28 -0800235 }
236 }
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200237 return TestingBuildParams{}, searchedRules
Colin Cross4c83e5c2019-02-25 14:54:28 -0800238}
239
240func buildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200241 p, searchRules := maybeBuildParamsFromRule(provider, rule)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800242 if p.Rule == nil {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200243 panic(fmt.Errorf("couldn't find rule %q.\nall rules: %v", rule, searchRules))
Colin Cross4c83e5c2019-02-25 14:54:28 -0800244 }
245 return p
246}
247
248func maybeBuildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
249 for _, p := range provider.BuildParamsForTests() {
Colin Crossb88b3c52019-06-10 15:15:17 -0700250 if strings.Contains(p.Description, desc) {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800251 return newTestingBuildParams(provider, p)
252 }
253 }
254 return TestingBuildParams{}
255}
256
257func buildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
258 p := maybeBuildParamsFromDescription(provider, desc)
259 if p.Rule == nil {
260 panic(fmt.Errorf("couldn't find description %q", desc))
261 }
262 return p
263}
264
265func maybeBuildParamsFromOutput(provider testBuildProvider, file string) (TestingBuildParams, []string) {
266 var searchedOutputs []string
267 for _, p := range provider.BuildParamsForTests() {
268 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700269 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800270 if p.Output != nil {
271 outputs = append(outputs, p.Output)
272 }
273 for _, f := range outputs {
274 if f.String() == file || f.Rel() == file {
275 return newTestingBuildParams(provider, p), nil
276 }
277 searchedOutputs = append(searchedOutputs, f.Rel())
278 }
279 }
280 return TestingBuildParams{}, searchedOutputs
281}
282
283func buildParamsFromOutput(provider testBuildProvider, file string) TestingBuildParams {
284 p, searchedOutputs := maybeBuildParamsFromOutput(provider, file)
285 if p.Rule == nil {
286 panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
287 file, searchedOutputs))
288 }
289 return p
290}
291
292func allOutputs(provider testBuildProvider) []string {
293 var outputFullPaths []string
294 for _, p := range provider.BuildParamsForTests() {
295 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700296 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800297 if p.Output != nil {
298 outputs = append(outputs, p.Output)
299 }
300 outputFullPaths = append(outputFullPaths, outputs.Strings()...)
301 }
302 return outputFullPaths
303}
304
Colin Crossb77ffc42019-01-05 22:09:19 -0800305// TestingModule is wrapper around an android.Module that provides methods to find information about individual
306// ctx.Build parameters for verification in tests.
Colin Crosscec81712017-07-13 14:43:27 -0700307type TestingModule struct {
308 module Module
309}
310
Colin Crossb77ffc42019-01-05 22:09:19 -0800311// Module returns the Module wrapped by the TestingModule.
Colin Crosscec81712017-07-13 14:43:27 -0700312func (m TestingModule) Module() Module {
313 return m.module
314}
315
Colin Crossb77ffc42019-01-05 22:09:19 -0800316// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
317// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800318func (m TestingModule) MaybeRule(rule string) TestingBuildParams {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200319 r, _ := maybeBuildParamsFromRule(m.module, rule)
320 return r
Colin Crosscec81712017-07-13 14:43:27 -0700321}
322
Colin Crossb77ffc42019-01-05 22:09:19 -0800323// 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 -0800324func (m TestingModule) Rule(rule string) TestingBuildParams {
325 return buildParamsFromRule(m.module, rule)
Colin Crossb77ffc42019-01-05 22:09:19 -0800326}
327
328// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
329// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800330func (m TestingModule) MaybeDescription(desc string) TestingBuildParams {
331 return maybeBuildParamsFromDescription(m.module, desc)
Nan Zhanged19fc32017-10-19 13:06:22 -0700332}
333
Colin Crossb77ffc42019-01-05 22:09:19 -0800334// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
335// found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800336func (m TestingModule) Description(desc string) TestingBuildParams {
337 return buildParamsFromDescription(m.module, desc)
Colin Crossb77ffc42019-01-05 22:09:19 -0800338}
339
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800340// 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 -0800341// value matches the provided string. Returns an empty BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800342func (m TestingModule) MaybeOutput(file string) TestingBuildParams {
343 p, _ := maybeBuildParamsFromOutput(m.module, file)
Colin Crossb77ffc42019-01-05 22:09:19 -0800344 return p
345}
346
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800347// 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 -0800348// value matches the provided string. Panics if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800349func (m TestingModule) Output(file string) TestingBuildParams {
350 return buildParamsFromOutput(m.module, file)
Colin Crosscec81712017-07-13 14:43:27 -0700351}
Logan Chien42039712018-03-12 16:29:17 +0800352
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800353// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
354func (m TestingModule) AllOutputs() []string {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800355 return allOutputs(m.module)
356}
357
358// TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual
359// ctx.Build parameters for verification in tests.
360type TestingSingleton struct {
361 singleton Singleton
362 provider testBuildProvider
363}
364
365// Singleton returns the Singleton wrapped by the TestingSingleton.
366func (s TestingSingleton) Singleton() Singleton {
367 return s.singleton
368}
369
370// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
371// BuildParams if no rule is found.
372func (s TestingSingleton) MaybeRule(rule string) TestingBuildParams {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200373 r, _ := maybeBuildParamsFromRule(s.provider, rule)
374 return r
Colin Cross4c83e5c2019-02-25 14:54:28 -0800375}
376
377// Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found.
378func (s TestingSingleton) Rule(rule string) TestingBuildParams {
379 return buildParamsFromRule(s.provider, rule)
380}
381
382// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
383// BuildParams if no rule is found.
384func (s TestingSingleton) MaybeDescription(desc string) TestingBuildParams {
385 return maybeBuildParamsFromDescription(s.provider, desc)
386}
387
388// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
389// found.
390func (s TestingSingleton) Description(desc string) TestingBuildParams {
391 return buildParamsFromDescription(s.provider, desc)
392}
393
394// MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
395// value matches the provided string. Returns an empty BuildParams if no rule is found.
396func (s TestingSingleton) MaybeOutput(file string) TestingBuildParams {
397 p, _ := maybeBuildParamsFromOutput(s.provider, file)
398 return p
399}
400
401// Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
402// value matches the provided string. Panics if no rule is found.
403func (s TestingSingleton) Output(file string) TestingBuildParams {
404 return buildParamsFromOutput(s.provider, file)
405}
406
407// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
408func (s TestingSingleton) AllOutputs() []string {
409 return allOutputs(s.provider)
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800410}
411
Logan Chien42039712018-03-12 16:29:17 +0800412func FailIfErrored(t *testing.T, errs []error) {
413 t.Helper()
414 if len(errs) > 0 {
415 for _, err := range errs {
416 t.Error(err)
417 }
418 t.FailNow()
419 }
420}
Logan Chienee97c3e2018-03-12 16:34:26 +0800421
422func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) {
423 t.Helper()
424
425 matcher, err := regexp.Compile(pattern)
426 if err != nil {
427 t.Errorf("failed to compile regular expression %q because %s", pattern, err)
428 }
429
430 found := false
431 for _, err := range errs {
432 if matcher.FindStringIndex(err.Error()) != nil {
433 found = true
434 break
435 }
436 }
437 if !found {
438 t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs))
439 for i, err := range errs {
Colin Crossaede88c2020-08-11 12:17:01 -0700440 t.Errorf("errs[%d] = %q", i, err)
Logan Chienee97c3e2018-03-12 16:34:26 +0800441 }
442 }
443}
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700444
Paul Duffin91e38192019-08-05 15:07:57 +0100445func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) {
446 t.Helper()
447
448 if expectedErrorPatterns == nil {
449 FailIfErrored(t, errs)
450 } else {
451 for _, expectedError := range expectedErrorPatterns {
452 FailIfNoMatchingErrors(t, expectedError, errs)
453 }
454 if len(errs) > len(expectedErrorPatterns) {
455 t.Errorf("additional errors found, expected %d, found %d",
456 len(expectedErrorPatterns), len(errs))
457 for i, expectedError := range expectedErrorPatterns {
458 t.Errorf("expectedErrors[%d] = %s", i, expectedError)
459 }
460 for i, err := range errs {
461 t.Errorf("errs[%d] = %s", i, err)
462 }
463 }
464 }
465
466}
467
Jingwen Chencda22c92020-11-23 00:22:30 -0500468func SetKatiEnabledForTests(config Config) {
469 config.katiEnabled = true
Paul Duffin8c3fec42020-03-04 20:15:08 +0000470}
471
Colin Crossaa255532020-07-03 13:18:24 -0700472func AndroidMkEntriesForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) []AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700473 var p AndroidMkEntriesProvider
474 var ok bool
475 if p, ok = mod.(AndroidMkEntriesProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100476 t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name())
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700477 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900478
479 entriesList := p.AndroidMkEntries()
480 for i, _ := range entriesList {
Colin Crossaa255532020-07-03 13:18:24 -0700481 entriesList[i].fillInEntries(ctx, mod)
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900482 }
483 return entriesList
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700484}
Jooyung Han12df5fb2019-07-11 16:18:47 +0900485
Colin Crossaa255532020-07-03 13:18:24 -0700486func AndroidMkDataForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) AndroidMkData {
Jooyung Han12df5fb2019-07-11 16:18:47 +0900487 var p AndroidMkDataProvider
488 var ok bool
489 if p, ok = mod.(AndroidMkDataProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100490 t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name())
Jooyung Han12df5fb2019-07-11 16:18:47 +0900491 }
492 data := p.AndroidMk()
Colin Crossaa255532020-07-03 13:18:24 -0700493 data.fillInData(ctx, mod)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900494 return data
495}
Paul Duffin9b478b02019-12-10 13:41:51 +0000496
497// Normalize the path for testing.
498//
499// If the path is relative to the build directory then return the relative path
500// to avoid tests having to deal with the dynamically generated build directory.
501//
502// Otherwise, return the supplied path as it is almost certainly a source path
503// that is relative to the root of the source tree.
504//
505// The build and source paths should be distinguishable based on their contents.
506func NormalizePathForTesting(path Path) string {
Paul Duffin064b70c2020-11-02 17:32:38 +0000507 if path == nil {
508 return "<nil path>"
509 }
Paul Duffin9b478b02019-12-10 13:41:51 +0000510 p := path.String()
Paul Duffindb170e42020-12-08 17:48:25 +0000511 // Allow absolute paths to /dev/
512 if strings.HasPrefix(p, "/dev/") {
513 return p
514 }
Paul Duffin9b478b02019-12-10 13:41:51 +0000515 if w, ok := path.(WritablePath); ok {
516 rel, err := filepath.Rel(w.buildDir(), p)
517 if err != nil {
518 panic(err)
519 }
520 return rel
521 }
522 return p
523}
524
525func NormalizePathsForTesting(paths Paths) []string {
526 var result []string
527 for _, path := range paths {
528 relative := NormalizePathForTesting(path)
529 result = append(result, relative)
530 }
531 return result
532}