blob: 90989ef53757ec630f209f19698c84367bf7b1bf [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"
Colin Crosscec81712017-07-13 14:43:27 -070021 "strings"
Logan Chien42039712018-03-12 16:29:17 +080022 "testing"
Colin Crosscec81712017-07-13 14:43:27 -070023
24 "github.com/google/blueprint"
25)
26
27func NewTestContext() *TestContext {
Jeff Gaston088e29e2017-11-29 16:47:17 -080028 namespaceExportFilter := func(namespace *Namespace) bool {
29 return true
30 }
Jeff Gastonb274ed32017-12-01 17:10:33 -080031
32 nameResolver := NewNameResolver(namespaceExportFilter)
33 ctx := &TestContext{
Colin Cross4c83e5c2019-02-25 14:54:28 -080034 Context: &Context{blueprint.NewContext()},
Jeff Gastonb274ed32017-12-01 17:10:33 -080035 NameResolver: nameResolver,
36 }
37
38 ctx.SetNameInterface(nameResolver)
Jeff Gaston088e29e2017-11-29 16:47:17 -080039
Colin Cross1b488422019-03-04 22:33:56 -080040 ctx.postDeps = append(ctx.postDeps, registerPathDepsMutator)
41
Jeff Gaston088e29e2017-11-29 16:47:17 -080042 return ctx
Colin Crosscec81712017-07-13 14:43:27 -070043}
44
Colin Crossae4c6182017-09-15 17:33:55 -070045func NewTestArchContext() *TestContext {
46 ctx := NewTestContext()
47 ctx.preDeps = append(ctx.preDeps, registerArchMutator)
48 return ctx
49}
50
Colin Crosscec81712017-07-13 14:43:27 -070051type TestContext struct {
Colin Cross4c83e5c2019-02-25 14:54:28 -080052 *Context
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000053 preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc
54 NameResolver *NameResolver
55 config Config
Colin Crosscec81712017-07-13 14:43:27 -070056}
57
58func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
59 ctx.preArch = append(ctx.preArch, f)
60}
61
Paul Duffina80ef842020-01-14 12:09:36 +000062func (ctx *TestContext) HardCodedPreArchMutators(f RegisterMutatorFunc) {
63 // Register mutator function as normal for testing.
64 ctx.PreArchMutators(f)
65}
66
Colin Crosscec81712017-07-13 14:43:27 -070067func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
68 ctx.preDeps = append(ctx.preDeps, f)
69}
70
71func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
72 ctx.postDeps = append(ctx.postDeps, f)
73}
74
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000075func (ctx *TestContext) FinalDepsMutators(f RegisterMutatorFunc) {
76 ctx.finalDeps = append(ctx.finalDeps, f)
77}
78
Colin Cross98be1bb2019-12-13 20:41:13 -080079func (ctx *TestContext) Register(config Config) {
80 ctx.SetFs(config.fs)
81 if config.mockBpList != "" {
82 ctx.SetModuleListFile(config.mockBpList)
83 }
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000084 registerMutators(ctx.Context.Context, ctx.preArch, ctx.preDeps, ctx.postDeps, ctx.finalDeps)
Colin Crosscec81712017-07-13 14:43:27 -070085
Colin Cross4b49b762019-11-22 15:25:03 -080086 ctx.RegisterSingletonType("env", EnvSingleton)
Colin Cross31a738b2019-12-30 18:45:15 -080087
88 ctx.config = config
89}
90
91func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) {
92 // This function adapts the old style ParseFileList calls that are spread throughout the tests
93 // to the new style that takes a config.
94 return ctx.Context.ParseFileList(rootDir, filePaths, ctx.config)
95}
96
97func (ctx *TestContext) ParseBlueprintsFiles(rootDir string) (deps []string, errs []error) {
98 // This function adapts the old style ParseBlueprintsFiles calls that are spread throughout the
99 // tests to the new style that takes a config.
100 return ctx.Context.ParseBlueprintsFiles(rootDir, ctx.config)
Colin Cross4b49b762019-11-22 15:25:03 -0800101}
102
103func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) {
104 ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory))
105}
106
107func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) {
108 ctx.Context.RegisterSingletonType(name, SingletonFactoryAdaptor(factory))
Colin Crosscec81712017-07-13 14:43:27 -0700109}
110
111func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
112 var module Module
113 ctx.VisitAllModules(func(m blueprint.Module) {
114 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
115 module = m.(Module)
116 }
117 })
118
119 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -0700120 // find all the modules that do exist
121 allModuleNames := []string{}
122 ctx.VisitAllModules(func(m blueprint.Module) {
123 allModuleNames = append(allModuleNames, m.(Module).Name()+"("+ctx.ModuleSubDir(m)+")")
124 })
125
126 panic(fmt.Errorf("failed to find module %q variant %q."+
127 "\nall modules: %v", name, variant, allModuleNames))
Colin Crosscec81712017-07-13 14:43:27 -0700128 }
129
130 return TestingModule{module}
131}
132
Jiyong Park37b25202018-07-11 10:49:27 +0900133func (ctx *TestContext) ModuleVariantsForTests(name string) []string {
134 var variants []string
135 ctx.VisitAllModules(func(m blueprint.Module) {
136 if ctx.ModuleName(m) == name {
137 variants = append(variants, ctx.ModuleSubDir(m))
138 }
139 })
140 return variants
141}
142
Colin Cross4c83e5c2019-02-25 14:54:28 -0800143// SingletonForTests returns a TestingSingleton for the singleton registered with the given name.
144func (ctx *TestContext) SingletonForTests(name string) TestingSingleton {
145 allSingletonNames := []string{}
146 for _, s := range ctx.Singletons() {
147 n := ctx.SingletonName(s)
148 if n == name {
149 return TestingSingleton{
150 singleton: s.(*singletonAdaptor).Singleton,
151 provider: s.(testBuildProvider),
152 }
153 }
154 allSingletonNames = append(allSingletonNames, n)
155 }
156
157 panic(fmt.Errorf("failed to find singleton %q."+
158 "\nall singletons: %v", name, allSingletonNames))
159}
160
Colin Cross4c83e5c2019-02-25 14:54:28 -0800161type testBuildProvider interface {
162 BuildParamsForTests() []BuildParams
163 RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
164}
165
166type TestingBuildParams struct {
167 BuildParams
168 RuleParams blueprint.RuleParams
169}
170
171func newTestingBuildParams(provider testBuildProvider, bparams BuildParams) TestingBuildParams {
172 return TestingBuildParams{
173 BuildParams: bparams,
174 RuleParams: provider.RuleParamsForTests()[bparams.Rule],
175 }
176}
177
178func maybeBuildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
179 for _, p := range provider.BuildParamsForTests() {
180 if strings.Contains(p.Rule.String(), rule) {
181 return newTestingBuildParams(provider, p)
182 }
183 }
184 return TestingBuildParams{}
185}
186
187func buildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
188 p := maybeBuildParamsFromRule(provider, rule)
189 if p.Rule == nil {
190 panic(fmt.Errorf("couldn't find rule %q", rule))
191 }
192 return p
193}
194
195func maybeBuildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
196 for _, p := range provider.BuildParamsForTests() {
Colin Crossb88b3c52019-06-10 15:15:17 -0700197 if strings.Contains(p.Description, desc) {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800198 return newTestingBuildParams(provider, p)
199 }
200 }
201 return TestingBuildParams{}
202}
203
204func buildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
205 p := maybeBuildParamsFromDescription(provider, desc)
206 if p.Rule == nil {
207 panic(fmt.Errorf("couldn't find description %q", desc))
208 }
209 return p
210}
211
212func maybeBuildParamsFromOutput(provider testBuildProvider, file string) (TestingBuildParams, []string) {
213 var searchedOutputs []string
214 for _, p := range provider.BuildParamsForTests() {
215 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700216 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800217 if p.Output != nil {
218 outputs = append(outputs, p.Output)
219 }
220 for _, f := range outputs {
221 if f.String() == file || f.Rel() == file {
222 return newTestingBuildParams(provider, p), nil
223 }
224 searchedOutputs = append(searchedOutputs, f.Rel())
225 }
226 }
227 return TestingBuildParams{}, searchedOutputs
228}
229
230func buildParamsFromOutput(provider testBuildProvider, file string) TestingBuildParams {
231 p, searchedOutputs := maybeBuildParamsFromOutput(provider, file)
232 if p.Rule == nil {
233 panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
234 file, searchedOutputs))
235 }
236 return p
237}
238
239func allOutputs(provider testBuildProvider) []string {
240 var outputFullPaths []string
241 for _, p := range provider.BuildParamsForTests() {
242 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700243 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800244 if p.Output != nil {
245 outputs = append(outputs, p.Output)
246 }
247 outputFullPaths = append(outputFullPaths, outputs.Strings()...)
248 }
249 return outputFullPaths
250}
251
Colin Crossb77ffc42019-01-05 22:09:19 -0800252// TestingModule is wrapper around an android.Module that provides methods to find information about individual
253// ctx.Build parameters for verification in tests.
Colin Crosscec81712017-07-13 14:43:27 -0700254type TestingModule struct {
255 module Module
256}
257
Colin Crossb77ffc42019-01-05 22:09:19 -0800258// Module returns the Module wrapped by the TestingModule.
Colin Crosscec81712017-07-13 14:43:27 -0700259func (m TestingModule) Module() Module {
260 return m.module
261}
262
Colin Crossb77ffc42019-01-05 22:09:19 -0800263// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
264// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800265func (m TestingModule) MaybeRule(rule string) TestingBuildParams {
266 return maybeBuildParamsFromRule(m.module, rule)
Colin Crosscec81712017-07-13 14:43:27 -0700267}
268
Colin Crossb77ffc42019-01-05 22:09:19 -0800269// 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 -0800270func (m TestingModule) Rule(rule string) TestingBuildParams {
271 return buildParamsFromRule(m.module, rule)
Colin Crossb77ffc42019-01-05 22:09:19 -0800272}
273
274// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
275// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800276func (m TestingModule) MaybeDescription(desc string) TestingBuildParams {
277 return maybeBuildParamsFromDescription(m.module, desc)
Nan Zhanged19fc32017-10-19 13:06:22 -0700278}
279
Colin Crossb77ffc42019-01-05 22:09:19 -0800280// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
281// found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800282func (m TestingModule) Description(desc string) TestingBuildParams {
283 return buildParamsFromDescription(m.module, desc)
Colin Crossb77ffc42019-01-05 22:09:19 -0800284}
285
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800286// 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 -0800287// value matches the provided string. Returns an empty BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800288func (m TestingModule) MaybeOutput(file string) TestingBuildParams {
289 p, _ := maybeBuildParamsFromOutput(m.module, file)
Colin Crossb77ffc42019-01-05 22:09:19 -0800290 return p
291}
292
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800293// 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 -0800294// value matches the provided string. Panics if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800295func (m TestingModule) Output(file string) TestingBuildParams {
296 return buildParamsFromOutput(m.module, file)
Colin Crosscec81712017-07-13 14:43:27 -0700297}
Logan Chien42039712018-03-12 16:29:17 +0800298
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800299// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
300func (m TestingModule) AllOutputs() []string {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800301 return allOutputs(m.module)
302}
303
304// TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual
305// ctx.Build parameters for verification in tests.
306type TestingSingleton struct {
307 singleton Singleton
308 provider testBuildProvider
309}
310
311// Singleton returns the Singleton wrapped by the TestingSingleton.
312func (s TestingSingleton) Singleton() Singleton {
313 return s.singleton
314}
315
316// 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.
318func (s TestingSingleton) MaybeRule(rule string) TestingBuildParams {
319 return maybeBuildParamsFromRule(s.provider, rule)
320}
321
322// Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found.
323func (s TestingSingleton) Rule(rule string) TestingBuildParams {
324 return buildParamsFromRule(s.provider, rule)
325}
326
327// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
328// BuildParams if no rule is found.
329func (s TestingSingleton) MaybeDescription(desc string) TestingBuildParams {
330 return maybeBuildParamsFromDescription(s.provider, desc)
331}
332
333// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
334// found.
335func (s TestingSingleton) Description(desc string) TestingBuildParams {
336 return buildParamsFromDescription(s.provider, desc)
337}
338
339// MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
340// value matches the provided string. Returns an empty BuildParams if no rule is found.
341func (s TestingSingleton) MaybeOutput(file string) TestingBuildParams {
342 p, _ := maybeBuildParamsFromOutput(s.provider, file)
343 return p
344}
345
346// Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
347// value matches the provided string. Panics if no rule is found.
348func (s TestingSingleton) Output(file string) TestingBuildParams {
349 return buildParamsFromOutput(s.provider, file)
350}
351
352// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
353func (s TestingSingleton) AllOutputs() []string {
354 return allOutputs(s.provider)
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800355}
356
Logan Chien42039712018-03-12 16:29:17 +0800357func FailIfErrored(t *testing.T, errs []error) {
358 t.Helper()
359 if len(errs) > 0 {
360 for _, err := range errs {
361 t.Error(err)
362 }
363 t.FailNow()
364 }
365}
Logan Chienee97c3e2018-03-12 16:34:26 +0800366
367func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) {
368 t.Helper()
369
370 matcher, err := regexp.Compile(pattern)
371 if err != nil {
372 t.Errorf("failed to compile regular expression %q because %s", pattern, err)
373 }
374
375 found := false
376 for _, err := range errs {
377 if matcher.FindStringIndex(err.Error()) != nil {
378 found = true
379 break
380 }
381 }
382 if !found {
383 t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs))
384 for i, err := range errs {
385 t.Errorf("errs[%d] = %s", i, err)
386 }
387 }
388}
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700389
Paul Duffin91e38192019-08-05 15:07:57 +0100390func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) {
391 t.Helper()
392
393 if expectedErrorPatterns == nil {
394 FailIfErrored(t, errs)
395 } else {
396 for _, expectedError := range expectedErrorPatterns {
397 FailIfNoMatchingErrors(t, expectedError, errs)
398 }
399 if len(errs) > len(expectedErrorPatterns) {
400 t.Errorf("additional errors found, expected %d, found %d",
401 len(expectedErrorPatterns), len(errs))
402 for i, expectedError := range expectedErrorPatterns {
403 t.Errorf("expectedErrors[%d] = %s", i, expectedError)
404 }
405 for i, err := range errs {
406 t.Errorf("errs[%d] = %s", i, err)
407 }
408 }
409 }
410
411}
412
Paul Duffin8c3fec42020-03-04 20:15:08 +0000413func SetInMakeForTests(config Config) {
414 config.inMake = true
415}
416
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900417func AndroidMkEntriesForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) []AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700418 var p AndroidMkEntriesProvider
419 var ok bool
420 if p, ok = mod.(AndroidMkEntriesProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100421 t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name())
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700422 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900423
424 entriesList := p.AndroidMkEntries()
425 for i, _ := range entriesList {
426 entriesList[i].fillInEntries(config, bpPath, mod)
427 }
428 return entriesList
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700429}
Jooyung Han12df5fb2019-07-11 16:18:47 +0900430
431func AndroidMkDataForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) AndroidMkData {
432 var p AndroidMkDataProvider
433 var ok bool
434 if p, ok = mod.(AndroidMkDataProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100435 t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name())
Jooyung Han12df5fb2019-07-11 16:18:47 +0900436 }
437 data := p.AndroidMk()
438 data.fillInData(config, bpPath, mod)
439 return data
440}
Paul Duffin9b478b02019-12-10 13:41:51 +0000441
442// Normalize the path for testing.
443//
444// If the path is relative to the build directory then return the relative path
445// to avoid tests having to deal with the dynamically generated build directory.
446//
447// Otherwise, return the supplied path as it is almost certainly a source path
448// that is relative to the root of the source tree.
449//
450// The build and source paths should be distinguishable based on their contents.
451func NormalizePathForTesting(path Path) string {
452 p := path.String()
453 if w, ok := path.(WritablePath); ok {
454 rel, err := filepath.Rel(w.buildDir(), p)
455 if err != nil {
456 panic(err)
457 }
458 return rel
459 }
460 return p
461}
462
463func NormalizePathsForTesting(paths Paths) []string {
464 var result []string
465 for _, path := range paths {
466 relative := NormalizePathForTesting(path)
467 result = append(result, relative)
468 }
469 return result
470}