blob: 6663728e3bab6f712aec33939b8e05ca854d6e2f [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
Colin Crosscec81712017-07-13 14:43:27 -070053 preArch, preDeps, postDeps []RegisterMutatorFunc
Jeff Gastonb274ed32017-12-01 17:10:33 -080054 NameResolver *NameResolver
Colin Cross31a738b2019-12-30 18:45:15 -080055 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
62func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
63 ctx.preDeps = append(ctx.preDeps, f)
64}
65
66func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
67 ctx.postDeps = append(ctx.postDeps, f)
68}
69
Colin Cross98be1bb2019-12-13 20:41:13 -080070func (ctx *TestContext) Register(config Config) {
71 ctx.SetFs(config.fs)
72 if config.mockBpList != "" {
73 ctx.SetModuleListFile(config.mockBpList)
74 }
Colin Cross4c83e5c2019-02-25 14:54:28 -080075 registerMutators(ctx.Context.Context, ctx.preArch, ctx.preDeps, ctx.postDeps)
Colin Crosscec81712017-07-13 14:43:27 -070076
Colin Cross4b49b762019-11-22 15:25:03 -080077 ctx.RegisterSingletonType("env", EnvSingleton)
Colin Cross31a738b2019-12-30 18:45:15 -080078
79 ctx.config = config
80}
81
82func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) {
83 // This function adapts the old style ParseFileList calls that are spread throughout the tests
84 // to the new style that takes a config.
85 return ctx.Context.ParseFileList(rootDir, filePaths, ctx.config)
86}
87
88func (ctx *TestContext) ParseBlueprintsFiles(rootDir string) (deps []string, errs []error) {
89 // This function adapts the old style ParseBlueprintsFiles calls that are spread throughout the
90 // tests to the new style that takes a config.
91 return ctx.Context.ParseBlueprintsFiles(rootDir, ctx.config)
Colin Cross4b49b762019-11-22 15:25:03 -080092}
93
94func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) {
95 ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory))
96}
97
98func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) {
99 ctx.Context.RegisterSingletonType(name, SingletonFactoryAdaptor(factory))
Colin Crosscec81712017-07-13 14:43:27 -0700100}
101
102func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
103 var module Module
104 ctx.VisitAllModules(func(m blueprint.Module) {
105 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
106 module = m.(Module)
107 }
108 })
109
110 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -0700111 // find all the modules that do exist
112 allModuleNames := []string{}
113 ctx.VisitAllModules(func(m blueprint.Module) {
114 allModuleNames = append(allModuleNames, m.(Module).Name()+"("+ctx.ModuleSubDir(m)+")")
115 })
116
117 panic(fmt.Errorf("failed to find module %q variant %q."+
118 "\nall modules: %v", name, variant, allModuleNames))
Colin Crosscec81712017-07-13 14:43:27 -0700119 }
120
121 return TestingModule{module}
122}
123
Jiyong Park37b25202018-07-11 10:49:27 +0900124func (ctx *TestContext) ModuleVariantsForTests(name string) []string {
125 var variants []string
126 ctx.VisitAllModules(func(m blueprint.Module) {
127 if ctx.ModuleName(m) == name {
128 variants = append(variants, ctx.ModuleSubDir(m))
129 }
130 })
131 return variants
132}
133
Colin Cross4c83e5c2019-02-25 14:54:28 -0800134// SingletonForTests returns a TestingSingleton for the singleton registered with the given name.
135func (ctx *TestContext) SingletonForTests(name string) TestingSingleton {
136 allSingletonNames := []string{}
137 for _, s := range ctx.Singletons() {
138 n := ctx.SingletonName(s)
139 if n == name {
140 return TestingSingleton{
141 singleton: s.(*singletonAdaptor).Singleton,
142 provider: s.(testBuildProvider),
143 }
144 }
145 allSingletonNames = append(allSingletonNames, n)
146 }
147
148 panic(fmt.Errorf("failed to find singleton %q."+
149 "\nall singletons: %v", name, allSingletonNames))
150}
151
Colin Cross4c83e5c2019-02-25 14:54:28 -0800152type testBuildProvider interface {
153 BuildParamsForTests() []BuildParams
154 RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
155}
156
157type TestingBuildParams struct {
158 BuildParams
159 RuleParams blueprint.RuleParams
160}
161
162func newTestingBuildParams(provider testBuildProvider, bparams BuildParams) TestingBuildParams {
163 return TestingBuildParams{
164 BuildParams: bparams,
165 RuleParams: provider.RuleParamsForTests()[bparams.Rule],
166 }
167}
168
169func maybeBuildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
170 for _, p := range provider.BuildParamsForTests() {
171 if strings.Contains(p.Rule.String(), rule) {
172 return newTestingBuildParams(provider, p)
173 }
174 }
175 return TestingBuildParams{}
176}
177
178func buildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
179 p := maybeBuildParamsFromRule(provider, rule)
180 if p.Rule == nil {
181 panic(fmt.Errorf("couldn't find rule %q", rule))
182 }
183 return p
184}
185
186func maybeBuildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
187 for _, p := range provider.BuildParamsForTests() {
Colin Crossb88b3c52019-06-10 15:15:17 -0700188 if strings.Contains(p.Description, desc) {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800189 return newTestingBuildParams(provider, p)
190 }
191 }
192 return TestingBuildParams{}
193}
194
195func buildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
196 p := maybeBuildParamsFromDescription(provider, desc)
197 if p.Rule == nil {
198 panic(fmt.Errorf("couldn't find description %q", desc))
199 }
200 return p
201}
202
203func maybeBuildParamsFromOutput(provider testBuildProvider, file string) (TestingBuildParams, []string) {
204 var searchedOutputs []string
205 for _, p := range provider.BuildParamsForTests() {
206 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700207 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800208 if p.Output != nil {
209 outputs = append(outputs, p.Output)
210 }
211 for _, f := range outputs {
212 if f.String() == file || f.Rel() == file {
213 return newTestingBuildParams(provider, p), nil
214 }
215 searchedOutputs = append(searchedOutputs, f.Rel())
216 }
217 }
218 return TestingBuildParams{}, searchedOutputs
219}
220
221func buildParamsFromOutput(provider testBuildProvider, file string) TestingBuildParams {
222 p, searchedOutputs := maybeBuildParamsFromOutput(provider, file)
223 if p.Rule == nil {
224 panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
225 file, searchedOutputs))
226 }
227 return p
228}
229
230func allOutputs(provider testBuildProvider) []string {
231 var outputFullPaths []string
232 for _, p := range provider.BuildParamsForTests() {
233 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700234 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800235 if p.Output != nil {
236 outputs = append(outputs, p.Output)
237 }
238 outputFullPaths = append(outputFullPaths, outputs.Strings()...)
239 }
240 return outputFullPaths
241}
242
Colin Crossb77ffc42019-01-05 22:09:19 -0800243// TestingModule is wrapper around an android.Module that provides methods to find information about individual
244// ctx.Build parameters for verification in tests.
Colin Crosscec81712017-07-13 14:43:27 -0700245type TestingModule struct {
246 module Module
247}
248
Colin Crossb77ffc42019-01-05 22:09:19 -0800249// Module returns the Module wrapped by the TestingModule.
Colin Crosscec81712017-07-13 14:43:27 -0700250func (m TestingModule) Module() Module {
251 return m.module
252}
253
Colin Crossb77ffc42019-01-05 22:09:19 -0800254// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
255// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800256func (m TestingModule) MaybeRule(rule string) TestingBuildParams {
257 return maybeBuildParamsFromRule(m.module, rule)
Colin Crosscec81712017-07-13 14:43:27 -0700258}
259
Colin Crossb77ffc42019-01-05 22:09:19 -0800260// 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 -0800261func (m TestingModule) Rule(rule string) TestingBuildParams {
262 return buildParamsFromRule(m.module, rule)
Colin Crossb77ffc42019-01-05 22:09:19 -0800263}
264
265// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
266// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800267func (m TestingModule) MaybeDescription(desc string) TestingBuildParams {
268 return maybeBuildParamsFromDescription(m.module, desc)
Nan Zhanged19fc32017-10-19 13:06:22 -0700269}
270
Colin Crossb77ffc42019-01-05 22:09:19 -0800271// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
272// found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800273func (m TestingModule) Description(desc string) TestingBuildParams {
274 return buildParamsFromDescription(m.module, desc)
Colin Crossb77ffc42019-01-05 22:09:19 -0800275}
276
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800277// 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 -0800278// value matches the provided string. Returns an empty BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800279func (m TestingModule) MaybeOutput(file string) TestingBuildParams {
280 p, _ := maybeBuildParamsFromOutput(m.module, file)
Colin Crossb77ffc42019-01-05 22:09:19 -0800281 return p
282}
283
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800284// 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 -0800285// value matches the provided string. Panics if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800286func (m TestingModule) Output(file string) TestingBuildParams {
287 return buildParamsFromOutput(m.module, file)
Colin Crosscec81712017-07-13 14:43:27 -0700288}
Logan Chien42039712018-03-12 16:29:17 +0800289
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800290// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
291func (m TestingModule) AllOutputs() []string {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800292 return allOutputs(m.module)
293}
294
295// TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual
296// ctx.Build parameters for verification in tests.
297type TestingSingleton struct {
298 singleton Singleton
299 provider testBuildProvider
300}
301
302// Singleton returns the Singleton wrapped by the TestingSingleton.
303func (s TestingSingleton) Singleton() Singleton {
304 return s.singleton
305}
306
307// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
308// BuildParams if no rule is found.
309func (s TestingSingleton) MaybeRule(rule string) TestingBuildParams {
310 return maybeBuildParamsFromRule(s.provider, rule)
311}
312
313// Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found.
314func (s TestingSingleton) Rule(rule string) TestingBuildParams {
315 return buildParamsFromRule(s.provider, rule)
316}
317
318// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
319// BuildParams if no rule is found.
320func (s TestingSingleton) MaybeDescription(desc string) TestingBuildParams {
321 return maybeBuildParamsFromDescription(s.provider, desc)
322}
323
324// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
325// found.
326func (s TestingSingleton) Description(desc string) TestingBuildParams {
327 return buildParamsFromDescription(s.provider, desc)
328}
329
330// MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
331// value matches the provided string. Returns an empty BuildParams if no rule is found.
332func (s TestingSingleton) MaybeOutput(file string) TestingBuildParams {
333 p, _ := maybeBuildParamsFromOutput(s.provider, file)
334 return p
335}
336
337// Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
338// value matches the provided string. Panics if no rule is found.
339func (s TestingSingleton) Output(file string) TestingBuildParams {
340 return buildParamsFromOutput(s.provider, file)
341}
342
343// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
344func (s TestingSingleton) AllOutputs() []string {
345 return allOutputs(s.provider)
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800346}
347
Logan Chien42039712018-03-12 16:29:17 +0800348func FailIfErrored(t *testing.T, errs []error) {
349 t.Helper()
350 if len(errs) > 0 {
351 for _, err := range errs {
352 t.Error(err)
353 }
354 t.FailNow()
355 }
356}
Logan Chienee97c3e2018-03-12 16:34:26 +0800357
358func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) {
359 t.Helper()
360
361 matcher, err := regexp.Compile(pattern)
362 if err != nil {
363 t.Errorf("failed to compile regular expression %q because %s", pattern, err)
364 }
365
366 found := false
367 for _, err := range errs {
368 if matcher.FindStringIndex(err.Error()) != nil {
369 found = true
370 break
371 }
372 }
373 if !found {
374 t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs))
375 for i, err := range errs {
376 t.Errorf("errs[%d] = %s", i, err)
377 }
378 }
379}
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700380
Paul Duffin91e38192019-08-05 15:07:57 +0100381func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) {
382 t.Helper()
383
384 if expectedErrorPatterns == nil {
385 FailIfErrored(t, errs)
386 } else {
387 for _, expectedError := range expectedErrorPatterns {
388 FailIfNoMatchingErrors(t, expectedError, errs)
389 }
390 if len(errs) > len(expectedErrorPatterns) {
391 t.Errorf("additional errors found, expected %d, found %d",
392 len(expectedErrorPatterns), len(errs))
393 for i, expectedError := range expectedErrorPatterns {
394 t.Errorf("expectedErrors[%d] = %s", i, expectedError)
395 }
396 for i, err := range errs {
397 t.Errorf("errs[%d] = %s", i, err)
398 }
399 }
400 }
401
402}
403
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900404func AndroidMkEntriesForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) []AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700405 var p AndroidMkEntriesProvider
406 var ok bool
407 if p, ok = mod.(AndroidMkEntriesProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100408 t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name())
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700409 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900410
411 entriesList := p.AndroidMkEntries()
412 for i, _ := range entriesList {
413 entriesList[i].fillInEntries(config, bpPath, mod)
414 }
415 return entriesList
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700416}
Jooyung Han12df5fb2019-07-11 16:18:47 +0900417
418func AndroidMkDataForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) AndroidMkData {
419 var p AndroidMkDataProvider
420 var ok bool
421 if p, ok = mod.(AndroidMkDataProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100422 t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name())
Jooyung Han12df5fb2019-07-11 16:18:47 +0900423 }
424 data := p.AndroidMk()
425 data.fillInData(config, bpPath, mod)
426 return data
427}
Paul Duffin9b478b02019-12-10 13:41:51 +0000428
429// Normalize the path for testing.
430//
431// If the path is relative to the build directory then return the relative path
432// to avoid tests having to deal with the dynamically generated build directory.
433//
434// Otherwise, return the supplied path as it is almost certainly a source path
435// that is relative to the root of the source tree.
436//
437// The build and source paths should be distinguishable based on their contents.
438func NormalizePathForTesting(path Path) string {
439 p := path.String()
440 if w, ok := path.(WritablePath); ok {
441 rel, err := filepath.Rel(w.buildDir(), p)
442 if err != nil {
443 panic(err)
444 }
445 return rel
446 }
447 return p
448}
449
450func NormalizePathsForTesting(paths Paths) []string {
451 var result []string
452 for _, path := range paths {
453 relative := NormalizePathForTesting(path)
454 result = append(result, relative)
455 }
456 return result
457}