blob: 4f0591b1d47304cb09bd6fd90d85d883fdb6a9e9 [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 Crossf8b860a2019-04-16 14:43:28 -070040 ctx.preArch = append(ctx.preArch, registerLoadHookMutator)
41
Colin Cross1b488422019-03-04 22:33:56 -080042 ctx.postDeps = append(ctx.postDeps, registerPathDepsMutator)
43
Jeff Gaston088e29e2017-11-29 16:47:17 -080044 return ctx
Colin Crosscec81712017-07-13 14:43:27 -070045}
46
Colin Crossae4c6182017-09-15 17:33:55 -070047func NewTestArchContext() *TestContext {
48 ctx := NewTestContext()
49 ctx.preDeps = append(ctx.preDeps, registerArchMutator)
50 return ctx
51}
52
Colin Crosscec81712017-07-13 14:43:27 -070053type TestContext struct {
Colin Cross4c83e5c2019-02-25 14:54:28 -080054 *Context
Colin Crosscec81712017-07-13 14:43:27 -070055 preArch, preDeps, postDeps []RegisterMutatorFunc
Jeff Gastonb274ed32017-12-01 17:10:33 -080056 NameResolver *NameResolver
Colin Crosscec81712017-07-13 14:43:27 -070057}
58
59func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
60 ctx.preArch = append(ctx.preArch, f)
61}
62
63func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
64 ctx.preDeps = append(ctx.preDeps, f)
65}
66
67func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
68 ctx.postDeps = append(ctx.postDeps, f)
69}
70
Colin Cross98be1bb2019-12-13 20:41:13 -080071func (ctx *TestContext) Register(config Config) {
72 ctx.SetFs(config.fs)
73 if config.mockBpList != "" {
74 ctx.SetModuleListFile(config.mockBpList)
75 }
Colin Cross4c83e5c2019-02-25 14:54:28 -080076 registerMutators(ctx.Context.Context, ctx.preArch, ctx.preDeps, ctx.postDeps)
Colin Crosscec81712017-07-13 14:43:27 -070077
Colin Cross4b49b762019-11-22 15:25:03 -080078 ctx.RegisterSingletonType("env", EnvSingleton)
79}
80
81func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) {
82 ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory))
83}
84
85func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) {
86 ctx.Context.RegisterSingletonType(name, SingletonFactoryAdaptor(factory))
Colin Crosscec81712017-07-13 14:43:27 -070087}
88
89func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
90 var module Module
91 ctx.VisitAllModules(func(m blueprint.Module) {
92 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
93 module = m.(Module)
94 }
95 })
96
97 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -070098 // find all the modules that do exist
99 allModuleNames := []string{}
100 ctx.VisitAllModules(func(m blueprint.Module) {
101 allModuleNames = append(allModuleNames, m.(Module).Name()+"("+ctx.ModuleSubDir(m)+")")
102 })
103
104 panic(fmt.Errorf("failed to find module %q variant %q."+
105 "\nall modules: %v", name, variant, allModuleNames))
Colin Crosscec81712017-07-13 14:43:27 -0700106 }
107
108 return TestingModule{module}
109}
110
Jiyong Park37b25202018-07-11 10:49:27 +0900111func (ctx *TestContext) ModuleVariantsForTests(name string) []string {
112 var variants []string
113 ctx.VisitAllModules(func(m blueprint.Module) {
114 if ctx.ModuleName(m) == name {
115 variants = append(variants, ctx.ModuleSubDir(m))
116 }
117 })
118 return variants
119}
120
Colin Cross4c83e5c2019-02-25 14:54:28 -0800121// SingletonForTests returns a TestingSingleton for the singleton registered with the given name.
122func (ctx *TestContext) SingletonForTests(name string) TestingSingleton {
123 allSingletonNames := []string{}
124 for _, s := range ctx.Singletons() {
125 n := ctx.SingletonName(s)
126 if n == name {
127 return TestingSingleton{
128 singleton: s.(*singletonAdaptor).Singleton,
129 provider: s.(testBuildProvider),
130 }
131 }
132 allSingletonNames = append(allSingletonNames, n)
133 }
134
135 panic(fmt.Errorf("failed to find singleton %q."+
136 "\nall singletons: %v", name, allSingletonNames))
137}
138
Colin Cross4c83e5c2019-02-25 14:54:28 -0800139type testBuildProvider interface {
140 BuildParamsForTests() []BuildParams
141 RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
142}
143
144type TestingBuildParams struct {
145 BuildParams
146 RuleParams blueprint.RuleParams
147}
148
149func newTestingBuildParams(provider testBuildProvider, bparams BuildParams) TestingBuildParams {
150 return TestingBuildParams{
151 BuildParams: bparams,
152 RuleParams: provider.RuleParamsForTests()[bparams.Rule],
153 }
154}
155
156func maybeBuildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
157 for _, p := range provider.BuildParamsForTests() {
158 if strings.Contains(p.Rule.String(), rule) {
159 return newTestingBuildParams(provider, p)
160 }
161 }
162 return TestingBuildParams{}
163}
164
165func buildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
166 p := maybeBuildParamsFromRule(provider, rule)
167 if p.Rule == nil {
168 panic(fmt.Errorf("couldn't find rule %q", rule))
169 }
170 return p
171}
172
173func maybeBuildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
174 for _, p := range provider.BuildParamsForTests() {
Colin Crossb88b3c52019-06-10 15:15:17 -0700175 if strings.Contains(p.Description, desc) {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800176 return newTestingBuildParams(provider, p)
177 }
178 }
179 return TestingBuildParams{}
180}
181
182func buildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
183 p := maybeBuildParamsFromDescription(provider, desc)
184 if p.Rule == nil {
185 panic(fmt.Errorf("couldn't find description %q", desc))
186 }
187 return p
188}
189
190func maybeBuildParamsFromOutput(provider testBuildProvider, file string) (TestingBuildParams, []string) {
191 var searchedOutputs []string
192 for _, p := range provider.BuildParamsForTests() {
193 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700194 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800195 if p.Output != nil {
196 outputs = append(outputs, p.Output)
197 }
198 for _, f := range outputs {
199 if f.String() == file || f.Rel() == file {
200 return newTestingBuildParams(provider, p), nil
201 }
202 searchedOutputs = append(searchedOutputs, f.Rel())
203 }
204 }
205 return TestingBuildParams{}, searchedOutputs
206}
207
208func buildParamsFromOutput(provider testBuildProvider, file string) TestingBuildParams {
209 p, searchedOutputs := maybeBuildParamsFromOutput(provider, file)
210 if p.Rule == nil {
211 panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
212 file, searchedOutputs))
213 }
214 return p
215}
216
217func allOutputs(provider testBuildProvider) []string {
218 var outputFullPaths []string
219 for _, p := range provider.BuildParamsForTests() {
220 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700221 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800222 if p.Output != nil {
223 outputs = append(outputs, p.Output)
224 }
225 outputFullPaths = append(outputFullPaths, outputs.Strings()...)
226 }
227 return outputFullPaths
228}
229
Colin Crossb77ffc42019-01-05 22:09:19 -0800230// TestingModule is wrapper around an android.Module that provides methods to find information about individual
231// ctx.Build parameters for verification in tests.
Colin Crosscec81712017-07-13 14:43:27 -0700232type TestingModule struct {
233 module Module
234}
235
Colin Crossb77ffc42019-01-05 22:09:19 -0800236// Module returns the Module wrapped by the TestingModule.
Colin Crosscec81712017-07-13 14:43:27 -0700237func (m TestingModule) Module() Module {
238 return m.module
239}
240
Colin Crossb77ffc42019-01-05 22:09:19 -0800241// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
242// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800243func (m TestingModule) MaybeRule(rule string) TestingBuildParams {
244 return maybeBuildParamsFromRule(m.module, rule)
Colin Crosscec81712017-07-13 14:43:27 -0700245}
246
Colin Crossb77ffc42019-01-05 22:09:19 -0800247// 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 -0800248func (m TestingModule) Rule(rule string) TestingBuildParams {
249 return buildParamsFromRule(m.module, rule)
Colin Crossb77ffc42019-01-05 22:09:19 -0800250}
251
252// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
253// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800254func (m TestingModule) MaybeDescription(desc string) TestingBuildParams {
255 return maybeBuildParamsFromDescription(m.module, desc)
Nan Zhanged19fc32017-10-19 13:06:22 -0700256}
257
Colin Crossb77ffc42019-01-05 22:09:19 -0800258// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
259// found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800260func (m TestingModule) Description(desc string) TestingBuildParams {
261 return buildParamsFromDescription(m.module, desc)
Colin Crossb77ffc42019-01-05 22:09:19 -0800262}
263
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800264// 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 -0800265// value matches the provided string. Returns an empty BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800266func (m TestingModule) MaybeOutput(file string) TestingBuildParams {
267 p, _ := maybeBuildParamsFromOutput(m.module, file)
Colin Crossb77ffc42019-01-05 22:09:19 -0800268 return p
269}
270
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800271// 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 -0800272// value matches the provided string. Panics if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800273func (m TestingModule) Output(file string) TestingBuildParams {
274 return buildParamsFromOutput(m.module, file)
Colin Crosscec81712017-07-13 14:43:27 -0700275}
Logan Chien42039712018-03-12 16:29:17 +0800276
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800277// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
278func (m TestingModule) AllOutputs() []string {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800279 return allOutputs(m.module)
280}
281
282// TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual
283// ctx.Build parameters for verification in tests.
284type TestingSingleton struct {
285 singleton Singleton
286 provider testBuildProvider
287}
288
289// Singleton returns the Singleton wrapped by the TestingSingleton.
290func (s TestingSingleton) Singleton() Singleton {
291 return s.singleton
292}
293
294// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
295// BuildParams if no rule is found.
296func (s TestingSingleton) MaybeRule(rule string) TestingBuildParams {
297 return maybeBuildParamsFromRule(s.provider, rule)
298}
299
300// Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found.
301func (s TestingSingleton) Rule(rule string) TestingBuildParams {
302 return buildParamsFromRule(s.provider, rule)
303}
304
305// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
306// BuildParams if no rule is found.
307func (s TestingSingleton) MaybeDescription(desc string) TestingBuildParams {
308 return maybeBuildParamsFromDescription(s.provider, desc)
309}
310
311// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
312// found.
313func (s TestingSingleton) Description(desc string) TestingBuildParams {
314 return buildParamsFromDescription(s.provider, desc)
315}
316
317// MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
318// value matches the provided string. Returns an empty BuildParams if no rule is found.
319func (s TestingSingleton) MaybeOutput(file string) TestingBuildParams {
320 p, _ := maybeBuildParamsFromOutput(s.provider, file)
321 return p
322}
323
324// Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
325// value matches the provided string. Panics if no rule is found.
326func (s TestingSingleton) Output(file string) TestingBuildParams {
327 return buildParamsFromOutput(s.provider, file)
328}
329
330// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
331func (s TestingSingleton) AllOutputs() []string {
332 return allOutputs(s.provider)
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800333}
334
Logan Chien42039712018-03-12 16:29:17 +0800335func FailIfErrored(t *testing.T, errs []error) {
336 t.Helper()
337 if len(errs) > 0 {
338 for _, err := range errs {
339 t.Error(err)
340 }
341 t.FailNow()
342 }
343}
Logan Chienee97c3e2018-03-12 16:34:26 +0800344
345func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) {
346 t.Helper()
347
348 matcher, err := regexp.Compile(pattern)
349 if err != nil {
350 t.Errorf("failed to compile regular expression %q because %s", pattern, err)
351 }
352
353 found := false
354 for _, err := range errs {
355 if matcher.FindStringIndex(err.Error()) != nil {
356 found = true
357 break
358 }
359 }
360 if !found {
361 t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs))
362 for i, err := range errs {
363 t.Errorf("errs[%d] = %s", i, err)
364 }
365 }
366}
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700367
Paul Duffin91e38192019-08-05 15:07:57 +0100368func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) {
369 t.Helper()
370
371 if expectedErrorPatterns == nil {
372 FailIfErrored(t, errs)
373 } else {
374 for _, expectedError := range expectedErrorPatterns {
375 FailIfNoMatchingErrors(t, expectedError, errs)
376 }
377 if len(errs) > len(expectedErrorPatterns) {
378 t.Errorf("additional errors found, expected %d, found %d",
379 len(expectedErrorPatterns), len(errs))
380 for i, expectedError := range expectedErrorPatterns {
381 t.Errorf("expectedErrors[%d] = %s", i, expectedError)
382 }
383 for i, err := range errs {
384 t.Errorf("errs[%d] = %s", i, err)
385 }
386 }
387 }
388
389}
390
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900391func AndroidMkEntriesForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) []AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700392 var p AndroidMkEntriesProvider
393 var ok bool
394 if p, ok = mod.(AndroidMkEntriesProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100395 t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name())
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700396 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900397
398 entriesList := p.AndroidMkEntries()
399 for i, _ := range entriesList {
400 entriesList[i].fillInEntries(config, bpPath, mod)
401 }
402 return entriesList
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700403}
Jooyung Han12df5fb2019-07-11 16:18:47 +0900404
405func AndroidMkDataForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) AndroidMkData {
406 var p AndroidMkDataProvider
407 var ok bool
408 if p, ok = mod.(AndroidMkDataProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100409 t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name())
Jooyung Han12df5fb2019-07-11 16:18:47 +0900410 }
411 data := p.AndroidMk()
412 data.fillInData(config, bpPath, mod)
413 return data
414}
Paul Duffin9b478b02019-12-10 13:41:51 +0000415
416// Normalize the path for testing.
417//
418// If the path is relative to the build directory then return the relative path
419// to avoid tests having to deal with the dynamically generated build directory.
420//
421// Otherwise, return the supplied path as it is almost certainly a source path
422// that is relative to the root of the source tree.
423//
424// The build and source paths should be distinguishable based on their contents.
425func NormalizePathForTesting(path Path) string {
426 p := path.String()
427 if w, ok := path.(WritablePath); ok {
428 rel, err := filepath.Rel(w.buildDir(), p)
429 if err != nil {
430 panic(err)
431 }
432 return rel
433 }
434 return p
435}
436
437func NormalizePathsForTesting(paths Paths) []string {
438 var result []string
439 for _, path := range paths {
440 relative := NormalizePathForTesting(path)
441 result = append(result, relative)
442 }
443 return result
444}