blob: 76172d18e57ed170f5e0394db263096e65b46c24 [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
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000059 preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc
Jingwen Chen73850672020-12-14 08:25:34 -050060 bp2buildMutators []RegisterMutatorFunc
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000061 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)) {
88 mutatorName := moduleType + "_bp2build"
89 f := func(ctx RegisterMutatorsContext) {
90 ctx.TopDown(mutatorName, m)
91 }
92 bp2buildMutators = append(bp2buildMutators, f)
93}
94
Colin Crossae8600b2020-10-29 17:09:13 -070095func (ctx *TestContext) Register() {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000096 registerMutators(ctx.Context.Context, ctx.preArch, ctx.preDeps, ctx.postDeps, ctx.finalDeps)
Colin Crosscec81712017-07-13 14:43:27 -070097
Colin Cross4b49b762019-11-22 15:25:03 -080098 ctx.RegisterSingletonType("env", EnvSingleton)
Colin Cross31a738b2019-12-30 18:45:15 -080099}
100
Jingwen Chen73850672020-12-14 08:25:34 -0500101// RegisterForBazelConversion prepares a test context for bp2build conversion.
102func (ctx *TestContext) RegisterForBazelConversion() {
103 RegisterMutatorsForBazelConversion(ctx.Context.Context, bp2buildMutators)
104}
105
Colin Cross31a738b2019-12-30 18:45:15 -0800106func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) {
107 // This function adapts the old style ParseFileList calls that are spread throughout the tests
108 // to the new style that takes a config.
109 return ctx.Context.ParseFileList(rootDir, filePaths, ctx.config)
110}
111
112func (ctx *TestContext) ParseBlueprintsFiles(rootDir string) (deps []string, errs []error) {
113 // This function adapts the old style ParseBlueprintsFiles calls that are spread throughout the
114 // tests to the new style that takes a config.
115 return ctx.Context.ParseBlueprintsFiles(rootDir, ctx.config)
Colin Cross4b49b762019-11-22 15:25:03 -0800116}
117
118func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) {
119 ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory))
120}
121
Colin Cross9aed5bc2020-12-28 15:15:34 -0800122func (ctx *TestContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) {
123 s, m := SingletonModuleFactoryAdaptor(name, factory)
124 ctx.RegisterSingletonType(name, s)
125 ctx.RegisterModuleType(name, m)
126}
127
Colin Cross4b49b762019-11-22 15:25:03 -0800128func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) {
Colin Cross06fa5882020-10-29 18:21:38 -0700129 ctx.Context.RegisterSingletonType(name, SingletonFactoryAdaptor(ctx.Context, factory))
Colin Crosscec81712017-07-13 14:43:27 -0700130}
131
132func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
133 var module Module
134 ctx.VisitAllModules(func(m blueprint.Module) {
135 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
136 module = m.(Module)
137 }
138 })
139
140 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -0700141 // find all the modules that do exist
Colin Crossbeae6ec2020-08-11 12:02:11 -0700142 var allModuleNames []string
143 var allVariants []string
Jeff Gaston294356f2017-09-27 17:05:30 -0700144 ctx.VisitAllModules(func(m blueprint.Module) {
Colin Crossbeae6ec2020-08-11 12:02:11 -0700145 allModuleNames = append(allModuleNames, ctx.ModuleName(m))
146 if ctx.ModuleName(m) == name {
147 allVariants = append(allVariants, ctx.ModuleSubDir(m))
148 }
Jeff Gaston294356f2017-09-27 17:05:30 -0700149 })
Martin Stjernholm4c021242020-05-13 01:13:50 +0100150 sort.Strings(allModuleNames)
Colin Crossbeae6ec2020-08-11 12:02:11 -0700151 sort.Strings(allVariants)
Jeff Gaston294356f2017-09-27 17:05:30 -0700152
Colin Crossbeae6ec2020-08-11 12:02:11 -0700153 if len(allVariants) == 0 {
154 panic(fmt.Errorf("failed to find module %q. All modules:\n %s",
155 name, strings.Join(allModuleNames, "\n ")))
156 } else {
157 panic(fmt.Errorf("failed to find module %q variant %q. All variants:\n %s",
158 name, variant, strings.Join(allVariants, "\n ")))
159 }
Colin Crosscec81712017-07-13 14:43:27 -0700160 }
161
162 return TestingModule{module}
163}
164
Jiyong Park37b25202018-07-11 10:49:27 +0900165func (ctx *TestContext) ModuleVariantsForTests(name string) []string {
166 var variants []string
167 ctx.VisitAllModules(func(m blueprint.Module) {
168 if ctx.ModuleName(m) == name {
169 variants = append(variants, ctx.ModuleSubDir(m))
170 }
171 })
172 return variants
173}
174
Colin Cross4c83e5c2019-02-25 14:54:28 -0800175// SingletonForTests returns a TestingSingleton for the singleton registered with the given name.
176func (ctx *TestContext) SingletonForTests(name string) TestingSingleton {
177 allSingletonNames := []string{}
178 for _, s := range ctx.Singletons() {
179 n := ctx.SingletonName(s)
180 if n == name {
181 return TestingSingleton{
182 singleton: s.(*singletonAdaptor).Singleton,
183 provider: s.(testBuildProvider),
184 }
185 }
186 allSingletonNames = append(allSingletonNames, n)
187 }
188
189 panic(fmt.Errorf("failed to find singleton %q."+
190 "\nall singletons: %v", name, allSingletonNames))
191}
192
Colin Cross4c83e5c2019-02-25 14:54:28 -0800193type testBuildProvider interface {
194 BuildParamsForTests() []BuildParams
195 RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
196}
197
198type TestingBuildParams struct {
199 BuildParams
200 RuleParams blueprint.RuleParams
201}
202
203func newTestingBuildParams(provider testBuildProvider, bparams BuildParams) TestingBuildParams {
204 return TestingBuildParams{
205 BuildParams: bparams,
206 RuleParams: provider.RuleParamsForTests()[bparams.Rule],
207 }
208}
209
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200210func maybeBuildParamsFromRule(provider testBuildProvider, rule string) (TestingBuildParams, []string) {
211 var searchedRules []string
Colin Cross4c83e5c2019-02-25 14:54:28 -0800212 for _, p := range provider.BuildParamsForTests() {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200213 searchedRules = append(searchedRules, p.Rule.String())
Colin Cross4c83e5c2019-02-25 14:54:28 -0800214 if strings.Contains(p.Rule.String(), rule) {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200215 return newTestingBuildParams(provider, p), searchedRules
Colin Cross4c83e5c2019-02-25 14:54:28 -0800216 }
217 }
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200218 return TestingBuildParams{}, searchedRules
Colin Cross4c83e5c2019-02-25 14:54:28 -0800219}
220
221func buildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200222 p, searchRules := maybeBuildParamsFromRule(provider, rule)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800223 if p.Rule == nil {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200224 panic(fmt.Errorf("couldn't find rule %q.\nall rules: %v", rule, searchRules))
Colin Cross4c83e5c2019-02-25 14:54:28 -0800225 }
226 return p
227}
228
229func maybeBuildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
230 for _, p := range provider.BuildParamsForTests() {
Colin Crossb88b3c52019-06-10 15:15:17 -0700231 if strings.Contains(p.Description, desc) {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800232 return newTestingBuildParams(provider, p)
233 }
234 }
235 return TestingBuildParams{}
236}
237
238func buildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
239 p := maybeBuildParamsFromDescription(provider, desc)
240 if p.Rule == nil {
241 panic(fmt.Errorf("couldn't find description %q", desc))
242 }
243 return p
244}
245
246func maybeBuildParamsFromOutput(provider testBuildProvider, file string) (TestingBuildParams, []string) {
247 var searchedOutputs []string
248 for _, p := range provider.BuildParamsForTests() {
249 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700250 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800251 if p.Output != nil {
252 outputs = append(outputs, p.Output)
253 }
254 for _, f := range outputs {
255 if f.String() == file || f.Rel() == file {
256 return newTestingBuildParams(provider, p), nil
257 }
258 searchedOutputs = append(searchedOutputs, f.Rel())
259 }
260 }
261 return TestingBuildParams{}, searchedOutputs
262}
263
264func buildParamsFromOutput(provider testBuildProvider, file string) TestingBuildParams {
265 p, searchedOutputs := maybeBuildParamsFromOutput(provider, file)
266 if p.Rule == nil {
267 panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
268 file, searchedOutputs))
269 }
270 return p
271}
272
273func allOutputs(provider testBuildProvider) []string {
274 var outputFullPaths []string
275 for _, p := range provider.BuildParamsForTests() {
276 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700277 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800278 if p.Output != nil {
279 outputs = append(outputs, p.Output)
280 }
281 outputFullPaths = append(outputFullPaths, outputs.Strings()...)
282 }
283 return outputFullPaths
284}
285
Colin Crossb77ffc42019-01-05 22:09:19 -0800286// TestingModule is wrapper around an android.Module that provides methods to find information about individual
287// ctx.Build parameters for verification in tests.
Colin Crosscec81712017-07-13 14:43:27 -0700288type TestingModule struct {
289 module Module
290}
291
Colin Crossb77ffc42019-01-05 22:09:19 -0800292// Module returns the Module wrapped by the TestingModule.
Colin Crosscec81712017-07-13 14:43:27 -0700293func (m TestingModule) Module() Module {
294 return m.module
295}
296
Colin Crossb77ffc42019-01-05 22:09:19 -0800297// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
298// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800299func (m TestingModule) MaybeRule(rule string) TestingBuildParams {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200300 r, _ := maybeBuildParamsFromRule(m.module, rule)
301 return r
Colin Crosscec81712017-07-13 14:43:27 -0700302}
303
Colin Crossb77ffc42019-01-05 22:09:19 -0800304// 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 -0800305func (m TestingModule) Rule(rule string) TestingBuildParams {
306 return buildParamsFromRule(m.module, rule)
Colin Crossb77ffc42019-01-05 22:09:19 -0800307}
308
309// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
310// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800311func (m TestingModule) MaybeDescription(desc string) TestingBuildParams {
312 return maybeBuildParamsFromDescription(m.module, desc)
Nan Zhanged19fc32017-10-19 13:06:22 -0700313}
314
Colin Crossb77ffc42019-01-05 22:09:19 -0800315// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
316// found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800317func (m TestingModule) Description(desc string) TestingBuildParams {
318 return buildParamsFromDescription(m.module, desc)
Colin Crossb77ffc42019-01-05 22:09:19 -0800319}
320
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800321// 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 -0800322// value matches the provided string. Returns an empty BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800323func (m TestingModule) MaybeOutput(file string) TestingBuildParams {
324 p, _ := maybeBuildParamsFromOutput(m.module, file)
Colin Crossb77ffc42019-01-05 22:09:19 -0800325 return p
326}
327
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800328// 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 -0800329// value matches the provided string. Panics if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800330func (m TestingModule) Output(file string) TestingBuildParams {
331 return buildParamsFromOutput(m.module, file)
Colin Crosscec81712017-07-13 14:43:27 -0700332}
Logan Chien42039712018-03-12 16:29:17 +0800333
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800334// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
335func (m TestingModule) AllOutputs() []string {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800336 return allOutputs(m.module)
337}
338
339// TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual
340// ctx.Build parameters for verification in tests.
341type TestingSingleton struct {
342 singleton Singleton
343 provider testBuildProvider
344}
345
346// Singleton returns the Singleton wrapped by the TestingSingleton.
347func (s TestingSingleton) Singleton() Singleton {
348 return s.singleton
349}
350
351// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
352// BuildParams if no rule is found.
353func (s TestingSingleton) MaybeRule(rule string) TestingBuildParams {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200354 r, _ := maybeBuildParamsFromRule(s.provider, rule)
355 return r
Colin Cross4c83e5c2019-02-25 14:54:28 -0800356}
357
358// Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found.
359func (s TestingSingleton) Rule(rule string) TestingBuildParams {
360 return buildParamsFromRule(s.provider, rule)
361}
362
363// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
364// BuildParams if no rule is found.
365func (s TestingSingleton) MaybeDescription(desc string) TestingBuildParams {
366 return maybeBuildParamsFromDescription(s.provider, desc)
367}
368
369// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
370// found.
371func (s TestingSingleton) Description(desc string) TestingBuildParams {
372 return buildParamsFromDescription(s.provider, desc)
373}
374
375// MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
376// value matches the provided string. Returns an empty BuildParams if no rule is found.
377func (s TestingSingleton) MaybeOutput(file string) TestingBuildParams {
378 p, _ := maybeBuildParamsFromOutput(s.provider, file)
379 return p
380}
381
382// Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
383// value matches the provided string. Panics if no rule is found.
384func (s TestingSingleton) Output(file string) TestingBuildParams {
385 return buildParamsFromOutput(s.provider, file)
386}
387
388// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
389func (s TestingSingleton) AllOutputs() []string {
390 return allOutputs(s.provider)
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800391}
392
Logan Chien42039712018-03-12 16:29:17 +0800393func FailIfErrored(t *testing.T, errs []error) {
394 t.Helper()
395 if len(errs) > 0 {
396 for _, err := range errs {
397 t.Error(err)
398 }
399 t.FailNow()
400 }
401}
Logan Chienee97c3e2018-03-12 16:34:26 +0800402
403func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) {
404 t.Helper()
405
406 matcher, err := regexp.Compile(pattern)
407 if err != nil {
408 t.Errorf("failed to compile regular expression %q because %s", pattern, err)
409 }
410
411 found := false
412 for _, err := range errs {
413 if matcher.FindStringIndex(err.Error()) != nil {
414 found = true
415 break
416 }
417 }
418 if !found {
419 t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs))
420 for i, err := range errs {
Colin Crossaede88c2020-08-11 12:17:01 -0700421 t.Errorf("errs[%d] = %q", i, err)
Logan Chienee97c3e2018-03-12 16:34:26 +0800422 }
423 }
424}
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700425
Paul Duffin91e38192019-08-05 15:07:57 +0100426func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) {
427 t.Helper()
428
429 if expectedErrorPatterns == nil {
430 FailIfErrored(t, errs)
431 } else {
432 for _, expectedError := range expectedErrorPatterns {
433 FailIfNoMatchingErrors(t, expectedError, errs)
434 }
435 if len(errs) > len(expectedErrorPatterns) {
436 t.Errorf("additional errors found, expected %d, found %d",
437 len(expectedErrorPatterns), len(errs))
438 for i, expectedError := range expectedErrorPatterns {
439 t.Errorf("expectedErrors[%d] = %s", i, expectedError)
440 }
441 for i, err := range errs {
442 t.Errorf("errs[%d] = %s", i, err)
443 }
444 }
445 }
446
447}
448
Jingwen Chencda22c92020-11-23 00:22:30 -0500449func SetKatiEnabledForTests(config Config) {
450 config.katiEnabled = true
Paul Duffin8c3fec42020-03-04 20:15:08 +0000451}
452
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900453func AndroidMkEntriesForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) []AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700454 var p AndroidMkEntriesProvider
455 var ok bool
456 if p, ok = mod.(AndroidMkEntriesProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100457 t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name())
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700458 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900459
460 entriesList := p.AndroidMkEntries()
461 for i, _ := range entriesList {
462 entriesList[i].fillInEntries(config, bpPath, mod)
463 }
464 return entriesList
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700465}
Jooyung Han12df5fb2019-07-11 16:18:47 +0900466
467func AndroidMkDataForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) AndroidMkData {
468 var p AndroidMkDataProvider
469 var ok bool
470 if p, ok = mod.(AndroidMkDataProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100471 t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name())
Jooyung Han12df5fb2019-07-11 16:18:47 +0900472 }
473 data := p.AndroidMk()
474 data.fillInData(config, bpPath, mod)
475 return data
476}
Paul Duffin9b478b02019-12-10 13:41:51 +0000477
478// Normalize the path for testing.
479//
480// If the path is relative to the build directory then return the relative path
481// to avoid tests having to deal with the dynamically generated build directory.
482//
483// Otherwise, return the supplied path as it is almost certainly a source path
484// that is relative to the root of the source tree.
485//
486// The build and source paths should be distinguishable based on their contents.
487func NormalizePathForTesting(path Path) string {
Paul Duffin064b70c2020-11-02 17:32:38 +0000488 if path == nil {
489 return "<nil path>"
490 }
Paul Duffin9b478b02019-12-10 13:41:51 +0000491 p := path.String()
Paul Duffindb170e42020-12-08 17:48:25 +0000492 // Allow absolute paths to /dev/
493 if strings.HasPrefix(p, "/dev/") {
494 return p
495 }
Paul Duffin9b478b02019-12-10 13:41:51 +0000496 if w, ok := path.(WritablePath); ok {
497 rel, err := filepath.Rel(w.buildDir(), p)
498 if err != nil {
499 panic(err)
500 }
501 return rel
502 }
503 return p
504}
505
506func NormalizePathsForTesting(paths Paths) []string {
507 var result []string
508 for _, path := range paths {
509 relative := NormalizePathForTesting(path)
510 result = append(result, relative)
511 }
512 return result
513}