blob: aaf98f595849d2b296653e92fc9ace8dc1c11e6d [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"
Logan Chienee97c3e2018-03-12 16:34:26 +080019 "regexp"
Colin Crosscec81712017-07-13 14:43:27 -070020 "strings"
Logan Chien42039712018-03-12 16:29:17 +080021 "testing"
Colin Crosscec81712017-07-13 14:43:27 -070022
23 "github.com/google/blueprint"
24)
25
26func NewTestContext() *TestContext {
Jeff Gaston088e29e2017-11-29 16:47:17 -080027 namespaceExportFilter := func(namespace *Namespace) bool {
28 return true
29 }
Jeff Gastonb274ed32017-12-01 17:10:33 -080030
31 nameResolver := NewNameResolver(namespaceExportFilter)
32 ctx := &TestContext{
Colin Cross4c83e5c2019-02-25 14:54:28 -080033 Context: &Context{blueprint.NewContext()},
Jeff Gastonb274ed32017-12-01 17:10:33 -080034 NameResolver: nameResolver,
35 }
36
37 ctx.SetNameInterface(nameResolver)
Jeff Gaston088e29e2017-11-29 16:47:17 -080038
Colin Crossf8b860a2019-04-16 14:43:28 -070039 ctx.preArch = append(ctx.preArch, registerLoadHookMutator)
40
Colin Cross1b488422019-03-04 22:33:56 -080041 ctx.postDeps = append(ctx.postDeps, registerPathDepsMutator)
42
Jeff Gaston088e29e2017-11-29 16:47:17 -080043 return ctx
Colin Crosscec81712017-07-13 14:43:27 -070044}
45
Colin Crossae4c6182017-09-15 17:33:55 -070046func NewTestArchContext() *TestContext {
47 ctx := NewTestContext()
48 ctx.preDeps = append(ctx.preDeps, registerArchMutator)
49 return ctx
50}
51
Colin Crosscec81712017-07-13 14:43:27 -070052type TestContext struct {
Colin Cross4c83e5c2019-02-25 14:54:28 -080053 *Context
Colin Crosscec81712017-07-13 14:43:27 -070054 preArch, preDeps, postDeps []RegisterMutatorFunc
Jeff Gastonb274ed32017-12-01 17:10:33 -080055 NameResolver *NameResolver
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)
78}
79
80func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) {
81 ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory))
82}
83
84func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) {
85 ctx.Context.RegisterSingletonType(name, SingletonFactoryAdaptor(factory))
Colin Crosscec81712017-07-13 14:43:27 -070086}
87
88func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
89 var module Module
90 ctx.VisitAllModules(func(m blueprint.Module) {
91 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
92 module = m.(Module)
93 }
94 })
95
96 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -070097 // find all the modules that do exist
98 allModuleNames := []string{}
99 ctx.VisitAllModules(func(m blueprint.Module) {
100 allModuleNames = append(allModuleNames, m.(Module).Name()+"("+ctx.ModuleSubDir(m)+")")
101 })
102
103 panic(fmt.Errorf("failed to find module %q variant %q."+
104 "\nall modules: %v", name, variant, allModuleNames))
Colin Crosscec81712017-07-13 14:43:27 -0700105 }
106
107 return TestingModule{module}
108}
109
Jiyong Park37b25202018-07-11 10:49:27 +0900110func (ctx *TestContext) ModuleVariantsForTests(name string) []string {
111 var variants []string
112 ctx.VisitAllModules(func(m blueprint.Module) {
113 if ctx.ModuleName(m) == name {
114 variants = append(variants, ctx.ModuleSubDir(m))
115 }
116 })
117 return variants
118}
119
Colin Cross4c83e5c2019-02-25 14:54:28 -0800120// SingletonForTests returns a TestingSingleton for the singleton registered with the given name.
121func (ctx *TestContext) SingletonForTests(name string) TestingSingleton {
122 allSingletonNames := []string{}
123 for _, s := range ctx.Singletons() {
124 n := ctx.SingletonName(s)
125 if n == name {
126 return TestingSingleton{
127 singleton: s.(*singletonAdaptor).Singleton,
128 provider: s.(testBuildProvider),
129 }
130 }
131 allSingletonNames = append(allSingletonNames, n)
132 }
133
134 panic(fmt.Errorf("failed to find singleton %q."+
135 "\nall singletons: %v", name, allSingletonNames))
136}
137
Colin Cross4c83e5c2019-02-25 14:54:28 -0800138type testBuildProvider interface {
139 BuildParamsForTests() []BuildParams
140 RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
141}
142
143type TestingBuildParams struct {
144 BuildParams
145 RuleParams blueprint.RuleParams
146}
147
148func newTestingBuildParams(provider testBuildProvider, bparams BuildParams) TestingBuildParams {
149 return TestingBuildParams{
150 BuildParams: bparams,
151 RuleParams: provider.RuleParamsForTests()[bparams.Rule],
152 }
153}
154
155func maybeBuildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
156 for _, p := range provider.BuildParamsForTests() {
157 if strings.Contains(p.Rule.String(), rule) {
158 return newTestingBuildParams(provider, p)
159 }
160 }
161 return TestingBuildParams{}
162}
163
164func buildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
165 p := maybeBuildParamsFromRule(provider, rule)
166 if p.Rule == nil {
167 panic(fmt.Errorf("couldn't find rule %q", rule))
168 }
169 return p
170}
171
172func maybeBuildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
173 for _, p := range provider.BuildParamsForTests() {
Colin Crossb88b3c52019-06-10 15:15:17 -0700174 if strings.Contains(p.Description, desc) {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800175 return newTestingBuildParams(provider, p)
176 }
177 }
178 return TestingBuildParams{}
179}
180
181func buildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
182 p := maybeBuildParamsFromDescription(provider, desc)
183 if p.Rule == nil {
184 panic(fmt.Errorf("couldn't find description %q", desc))
185 }
186 return p
187}
188
189func maybeBuildParamsFromOutput(provider testBuildProvider, file string) (TestingBuildParams, []string) {
190 var searchedOutputs []string
191 for _, p := range provider.BuildParamsForTests() {
192 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700193 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800194 if p.Output != nil {
195 outputs = append(outputs, p.Output)
196 }
197 for _, f := range outputs {
198 if f.String() == file || f.Rel() == file {
199 return newTestingBuildParams(provider, p), nil
200 }
201 searchedOutputs = append(searchedOutputs, f.Rel())
202 }
203 }
204 return TestingBuildParams{}, searchedOutputs
205}
206
207func buildParamsFromOutput(provider testBuildProvider, file string) TestingBuildParams {
208 p, searchedOutputs := maybeBuildParamsFromOutput(provider, file)
209 if p.Rule == nil {
210 panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
211 file, searchedOutputs))
212 }
213 return p
214}
215
216func allOutputs(provider testBuildProvider) []string {
217 var outputFullPaths []string
218 for _, p := range provider.BuildParamsForTests() {
219 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700220 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800221 if p.Output != nil {
222 outputs = append(outputs, p.Output)
223 }
224 outputFullPaths = append(outputFullPaths, outputs.Strings()...)
225 }
226 return outputFullPaths
227}
228
Colin Crossb77ffc42019-01-05 22:09:19 -0800229// TestingModule is wrapper around an android.Module that provides methods to find information about individual
230// ctx.Build parameters for verification in tests.
Colin Crosscec81712017-07-13 14:43:27 -0700231type TestingModule struct {
232 module Module
233}
234
Colin Crossb77ffc42019-01-05 22:09:19 -0800235// Module returns the Module wrapped by the TestingModule.
Colin Crosscec81712017-07-13 14:43:27 -0700236func (m TestingModule) Module() Module {
237 return m.module
238}
239
Colin Crossb77ffc42019-01-05 22:09:19 -0800240// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
241// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800242func (m TestingModule) MaybeRule(rule string) TestingBuildParams {
243 return maybeBuildParamsFromRule(m.module, rule)
Colin Crosscec81712017-07-13 14:43:27 -0700244}
245
Colin Crossb77ffc42019-01-05 22:09:19 -0800246// 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 -0800247func (m TestingModule) Rule(rule string) TestingBuildParams {
248 return buildParamsFromRule(m.module, rule)
Colin Crossb77ffc42019-01-05 22:09:19 -0800249}
250
251// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
252// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800253func (m TestingModule) MaybeDescription(desc string) TestingBuildParams {
254 return maybeBuildParamsFromDescription(m.module, desc)
Nan Zhanged19fc32017-10-19 13:06:22 -0700255}
256
Colin Crossb77ffc42019-01-05 22:09:19 -0800257// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
258// found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800259func (m TestingModule) Description(desc string) TestingBuildParams {
260 return buildParamsFromDescription(m.module, desc)
Colin Crossb77ffc42019-01-05 22:09:19 -0800261}
262
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800263// 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 -0800264// value matches the provided string. Returns an empty BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800265func (m TestingModule) MaybeOutput(file string) TestingBuildParams {
266 p, _ := maybeBuildParamsFromOutput(m.module, file)
Colin Crossb77ffc42019-01-05 22:09:19 -0800267 return p
268}
269
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800270// 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 -0800271// value matches the provided string. Panics if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800272func (m TestingModule) Output(file string) TestingBuildParams {
273 return buildParamsFromOutput(m.module, file)
Colin Crosscec81712017-07-13 14:43:27 -0700274}
Logan Chien42039712018-03-12 16:29:17 +0800275
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800276// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
277func (m TestingModule) AllOutputs() []string {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800278 return allOutputs(m.module)
279}
280
281// TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual
282// ctx.Build parameters for verification in tests.
283type TestingSingleton struct {
284 singleton Singleton
285 provider testBuildProvider
286}
287
288// Singleton returns the Singleton wrapped by the TestingSingleton.
289func (s TestingSingleton) Singleton() Singleton {
290 return s.singleton
291}
292
293// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
294// BuildParams if no rule is found.
295func (s TestingSingleton) MaybeRule(rule string) TestingBuildParams {
296 return maybeBuildParamsFromRule(s.provider, rule)
297}
298
299// Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found.
300func (s TestingSingleton) Rule(rule string) TestingBuildParams {
301 return buildParamsFromRule(s.provider, rule)
302}
303
304// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
305// BuildParams if no rule is found.
306func (s TestingSingleton) MaybeDescription(desc string) TestingBuildParams {
307 return maybeBuildParamsFromDescription(s.provider, desc)
308}
309
310// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
311// found.
312func (s TestingSingleton) Description(desc string) TestingBuildParams {
313 return buildParamsFromDescription(s.provider, desc)
314}
315
316// MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
317// value matches the provided string. Returns an empty BuildParams if no rule is found.
318func (s TestingSingleton) MaybeOutput(file string) TestingBuildParams {
319 p, _ := maybeBuildParamsFromOutput(s.provider, file)
320 return p
321}
322
323// Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
324// value matches the provided string. Panics if no rule is found.
325func (s TestingSingleton) Output(file string) TestingBuildParams {
326 return buildParamsFromOutput(s.provider, file)
327}
328
329// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
330func (s TestingSingleton) AllOutputs() []string {
331 return allOutputs(s.provider)
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800332}
333
Logan Chien42039712018-03-12 16:29:17 +0800334func FailIfErrored(t *testing.T, errs []error) {
335 t.Helper()
336 if len(errs) > 0 {
337 for _, err := range errs {
338 t.Error(err)
339 }
340 t.FailNow()
341 }
342}
Logan Chienee97c3e2018-03-12 16:34:26 +0800343
344func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) {
345 t.Helper()
346
347 matcher, err := regexp.Compile(pattern)
348 if err != nil {
349 t.Errorf("failed to compile regular expression %q because %s", pattern, err)
350 }
351
352 found := false
353 for _, err := range errs {
354 if matcher.FindStringIndex(err.Error()) != nil {
355 found = true
356 break
357 }
358 }
359 if !found {
360 t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs))
361 for i, err := range errs {
362 t.Errorf("errs[%d] = %s", i, err)
363 }
364 }
365}
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700366
Paul Duffin91e38192019-08-05 15:07:57 +0100367func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) {
368 t.Helper()
369
370 if expectedErrorPatterns == nil {
371 FailIfErrored(t, errs)
372 } else {
373 for _, expectedError := range expectedErrorPatterns {
374 FailIfNoMatchingErrors(t, expectedError, errs)
375 }
376 if len(errs) > len(expectedErrorPatterns) {
377 t.Errorf("additional errors found, expected %d, found %d",
378 len(expectedErrorPatterns), len(errs))
379 for i, expectedError := range expectedErrorPatterns {
380 t.Errorf("expectedErrors[%d] = %s", i, expectedError)
381 }
382 for i, err := range errs {
383 t.Errorf("errs[%d] = %s", i, err)
384 }
385 }
386 }
387
388}
389
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900390func AndroidMkEntriesForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) []AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700391 var p AndroidMkEntriesProvider
392 var ok bool
393 if p, ok = mod.(AndroidMkEntriesProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100394 t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name())
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700395 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900396
397 entriesList := p.AndroidMkEntries()
398 for i, _ := range entriesList {
399 entriesList[i].fillInEntries(config, bpPath, mod)
400 }
401 return entriesList
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700402}
Jooyung Han12df5fb2019-07-11 16:18:47 +0900403
404func AndroidMkDataForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) AndroidMkData {
405 var p AndroidMkDataProvider
406 var ok bool
407 if p, ok = mod.(AndroidMkDataProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100408 t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name())
Jooyung Han12df5fb2019-07-11 16:18:47 +0900409 }
410 data := p.AndroidMk()
411 data.fillInData(config, bpPath, mod)
412 return data
413}