blob: 4b55920c5a6437e5305c3246c81b2c6a0c6243be [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"
Jeff Gastondea7e4d2017-11-17 13:29:40 -080019 "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
71func (ctx *TestContext) Register() {
Colin Cross4c83e5c2019-02-25 14:54:28 -080072 registerMutators(ctx.Context.Context, ctx.preArch, ctx.preDeps, ctx.postDeps)
Colin Crosscec81712017-07-13 14:43:27 -070073
Colin Cross4b49b762019-11-22 15:25:03 -080074 ctx.RegisterSingletonType("env", EnvSingleton)
75}
76
77func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) {
78 ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory))
79}
80
81func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) {
82 ctx.Context.RegisterSingletonType(name, SingletonFactoryAdaptor(factory))
Colin Crosscec81712017-07-13 14:43:27 -070083}
84
85func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
86 var module Module
87 ctx.VisitAllModules(func(m blueprint.Module) {
88 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
89 module = m.(Module)
90 }
91 })
92
93 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -070094 // find all the modules that do exist
95 allModuleNames := []string{}
96 ctx.VisitAllModules(func(m blueprint.Module) {
97 allModuleNames = append(allModuleNames, m.(Module).Name()+"("+ctx.ModuleSubDir(m)+")")
98 })
99
100 panic(fmt.Errorf("failed to find module %q variant %q."+
101 "\nall modules: %v", name, variant, allModuleNames))
Colin Crosscec81712017-07-13 14:43:27 -0700102 }
103
104 return TestingModule{module}
105}
106
Jiyong Park37b25202018-07-11 10:49:27 +0900107func (ctx *TestContext) ModuleVariantsForTests(name string) []string {
108 var variants []string
109 ctx.VisitAllModules(func(m blueprint.Module) {
110 if ctx.ModuleName(m) == name {
111 variants = append(variants, ctx.ModuleSubDir(m))
112 }
113 })
114 return variants
115}
116
Colin Cross4c83e5c2019-02-25 14:54:28 -0800117// SingletonForTests returns a TestingSingleton for the singleton registered with the given name.
118func (ctx *TestContext) SingletonForTests(name string) TestingSingleton {
119 allSingletonNames := []string{}
120 for _, s := range ctx.Singletons() {
121 n := ctx.SingletonName(s)
122 if n == name {
123 return TestingSingleton{
124 singleton: s.(*singletonAdaptor).Singleton,
125 provider: s.(testBuildProvider),
126 }
127 }
128 allSingletonNames = append(allSingletonNames, n)
129 }
130
131 panic(fmt.Errorf("failed to find singleton %q."+
132 "\nall singletons: %v", name, allSingletonNames))
133}
134
Jeff Gastondea7e4d2017-11-17 13:29:40 -0800135// MockFileSystem causes the Context to replace all reads with accesses to the provided map of
136// filenames to contents stored as a byte slice.
137func (ctx *TestContext) MockFileSystem(files map[string][]byte) {
138 // no module list file specified; find every file named Blueprints or Android.bp
139 pathsToParse := []string{}
140 for candidate := range files {
141 base := filepath.Base(candidate)
142 if base == "Blueprints" || base == "Android.bp" {
143 pathsToParse = append(pathsToParse, candidate)
144 }
145 }
146 if len(pathsToParse) < 1 {
147 panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", files))
148 }
149 files[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n"))
150
151 ctx.Context.MockFileSystem(files)
152}
153
Colin Cross4c83e5c2019-02-25 14:54:28 -0800154type testBuildProvider interface {
155 BuildParamsForTests() []BuildParams
156 RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
157}
158
159type TestingBuildParams struct {
160 BuildParams
161 RuleParams blueprint.RuleParams
162}
163
164func newTestingBuildParams(provider testBuildProvider, bparams BuildParams) TestingBuildParams {
165 return TestingBuildParams{
166 BuildParams: bparams,
167 RuleParams: provider.RuleParamsForTests()[bparams.Rule],
168 }
169}
170
171func maybeBuildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
172 for _, p := range provider.BuildParamsForTests() {
173 if strings.Contains(p.Rule.String(), rule) {
174 return newTestingBuildParams(provider, p)
175 }
176 }
177 return TestingBuildParams{}
178}
179
180func buildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
181 p := maybeBuildParamsFromRule(provider, rule)
182 if p.Rule == nil {
183 panic(fmt.Errorf("couldn't find rule %q", rule))
184 }
185 return p
186}
187
188func maybeBuildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
189 for _, p := range provider.BuildParamsForTests() {
Colin Crossb88b3c52019-06-10 15:15:17 -0700190 if strings.Contains(p.Description, desc) {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800191 return newTestingBuildParams(provider, p)
192 }
193 }
194 return TestingBuildParams{}
195}
196
197func buildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
198 p := maybeBuildParamsFromDescription(provider, desc)
199 if p.Rule == nil {
200 panic(fmt.Errorf("couldn't find description %q", desc))
201 }
202 return p
203}
204
205func maybeBuildParamsFromOutput(provider testBuildProvider, file string) (TestingBuildParams, []string) {
206 var searchedOutputs []string
207 for _, p := range provider.BuildParamsForTests() {
208 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700209 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800210 if p.Output != nil {
211 outputs = append(outputs, p.Output)
212 }
213 for _, f := range outputs {
214 if f.String() == file || f.Rel() == file {
215 return newTestingBuildParams(provider, p), nil
216 }
217 searchedOutputs = append(searchedOutputs, f.Rel())
218 }
219 }
220 return TestingBuildParams{}, searchedOutputs
221}
222
223func buildParamsFromOutput(provider testBuildProvider, file string) TestingBuildParams {
224 p, searchedOutputs := maybeBuildParamsFromOutput(provider, file)
225 if p.Rule == nil {
226 panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
227 file, searchedOutputs))
228 }
229 return p
230}
231
232func allOutputs(provider testBuildProvider) []string {
233 var outputFullPaths []string
234 for _, p := range provider.BuildParamsForTests() {
235 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700236 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800237 if p.Output != nil {
238 outputs = append(outputs, p.Output)
239 }
240 outputFullPaths = append(outputFullPaths, outputs.Strings()...)
241 }
242 return outputFullPaths
243}
244
Colin Crossb77ffc42019-01-05 22:09:19 -0800245// TestingModule is wrapper around an android.Module that provides methods to find information about individual
246// ctx.Build parameters for verification in tests.
Colin Crosscec81712017-07-13 14:43:27 -0700247type TestingModule struct {
248 module Module
249}
250
Colin Crossb77ffc42019-01-05 22:09:19 -0800251// Module returns the Module wrapped by the TestingModule.
Colin Crosscec81712017-07-13 14:43:27 -0700252func (m TestingModule) Module() Module {
253 return m.module
254}
255
Colin Crossb77ffc42019-01-05 22:09:19 -0800256// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
257// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800258func (m TestingModule) MaybeRule(rule string) TestingBuildParams {
259 return maybeBuildParamsFromRule(m.module, rule)
Colin Crosscec81712017-07-13 14:43:27 -0700260}
261
Colin Crossb77ffc42019-01-05 22:09:19 -0800262// 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 -0800263func (m TestingModule) Rule(rule string) TestingBuildParams {
264 return buildParamsFromRule(m.module, rule)
Colin Crossb77ffc42019-01-05 22:09:19 -0800265}
266
267// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
268// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800269func (m TestingModule) MaybeDescription(desc string) TestingBuildParams {
270 return maybeBuildParamsFromDescription(m.module, desc)
Nan Zhanged19fc32017-10-19 13:06:22 -0700271}
272
Colin Crossb77ffc42019-01-05 22:09:19 -0800273// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
274// found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800275func (m TestingModule) Description(desc string) TestingBuildParams {
276 return buildParamsFromDescription(m.module, desc)
Colin Crossb77ffc42019-01-05 22:09:19 -0800277}
278
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800279// 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 -0800280// value matches the provided string. Returns an empty BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800281func (m TestingModule) MaybeOutput(file string) TestingBuildParams {
282 p, _ := maybeBuildParamsFromOutput(m.module, file)
Colin Crossb77ffc42019-01-05 22:09:19 -0800283 return p
284}
285
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800286// 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 -0800287// value matches the provided string. Panics if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800288func (m TestingModule) Output(file string) TestingBuildParams {
289 return buildParamsFromOutput(m.module, file)
Colin Crosscec81712017-07-13 14:43:27 -0700290}
Logan Chien42039712018-03-12 16:29:17 +0800291
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800292// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
293func (m TestingModule) AllOutputs() []string {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800294 return allOutputs(m.module)
295}
296
297// TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual
298// ctx.Build parameters for verification in tests.
299type TestingSingleton struct {
300 singleton Singleton
301 provider testBuildProvider
302}
303
304// Singleton returns the Singleton wrapped by the TestingSingleton.
305func (s TestingSingleton) Singleton() Singleton {
306 return s.singleton
307}
308
309// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
310// BuildParams if no rule is found.
311func (s TestingSingleton) MaybeRule(rule string) TestingBuildParams {
312 return maybeBuildParamsFromRule(s.provider, rule)
313}
314
315// Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found.
316func (s TestingSingleton) Rule(rule string) TestingBuildParams {
317 return buildParamsFromRule(s.provider, rule)
318}
319
320// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
321// BuildParams if no rule is found.
322func (s TestingSingleton) MaybeDescription(desc string) TestingBuildParams {
323 return maybeBuildParamsFromDescription(s.provider, desc)
324}
325
326// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
327// found.
328func (s TestingSingleton) Description(desc string) TestingBuildParams {
329 return buildParamsFromDescription(s.provider, desc)
330}
331
332// MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
333// value matches the provided string. Returns an empty BuildParams if no rule is found.
334func (s TestingSingleton) MaybeOutput(file string) TestingBuildParams {
335 p, _ := maybeBuildParamsFromOutput(s.provider, file)
336 return p
337}
338
339// Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
340// value matches the provided string. Panics if no rule is found.
341func (s TestingSingleton) Output(file string) TestingBuildParams {
342 return buildParamsFromOutput(s.provider, file)
343}
344
345// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
346func (s TestingSingleton) AllOutputs() []string {
347 return allOutputs(s.provider)
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800348}
349
Logan Chien42039712018-03-12 16:29:17 +0800350func FailIfErrored(t *testing.T, errs []error) {
351 t.Helper()
352 if len(errs) > 0 {
353 for _, err := range errs {
354 t.Error(err)
355 }
356 t.FailNow()
357 }
358}
Logan Chienee97c3e2018-03-12 16:34:26 +0800359
360func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) {
361 t.Helper()
362
363 matcher, err := regexp.Compile(pattern)
364 if err != nil {
365 t.Errorf("failed to compile regular expression %q because %s", pattern, err)
366 }
367
368 found := false
369 for _, err := range errs {
370 if matcher.FindStringIndex(err.Error()) != nil {
371 found = true
372 break
373 }
374 }
375 if !found {
376 t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs))
377 for i, err := range errs {
378 t.Errorf("errs[%d] = %s", i, err)
379 }
380 }
381}
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700382
Paul Duffin91e38192019-08-05 15:07:57 +0100383func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) {
384 t.Helper()
385
386 if expectedErrorPatterns == nil {
387 FailIfErrored(t, errs)
388 } else {
389 for _, expectedError := range expectedErrorPatterns {
390 FailIfNoMatchingErrors(t, expectedError, errs)
391 }
392 if len(errs) > len(expectedErrorPatterns) {
393 t.Errorf("additional errors found, expected %d, found %d",
394 len(expectedErrorPatterns), len(errs))
395 for i, expectedError := range expectedErrorPatterns {
396 t.Errorf("expectedErrors[%d] = %s", i, expectedError)
397 }
398 for i, err := range errs {
399 t.Errorf("errs[%d] = %s", i, err)
400 }
401 }
402 }
403
404}
405
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700406func AndroidMkEntriesForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) AndroidMkEntries {
407 var p AndroidMkEntriesProvider
408 var ok bool
409 if p, ok = mod.(AndroidMkEntriesProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100410 t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name())
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700411 }
412 entries := p.AndroidMkEntries()
413 entries.fillInEntries(config, bpPath, mod)
414 return entries
415}
Jooyung Han12df5fb2019-07-11 16:18:47 +0900416
417func AndroidMkDataForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) AndroidMkData {
418 var p AndroidMkDataProvider
419 var ok bool
420 if p, ok = mod.(AndroidMkDataProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100421 t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name())
Jooyung Han12df5fb2019-07-11 16:18:47 +0900422 }
423 data := p.AndroidMk()
424 data.fillInData(config, bpPath, mod)
425 return data
426}