blob: b4008af374bedf6d66fec833e6703b1cae24ac6e [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
40 return ctx
Colin Crosscec81712017-07-13 14:43:27 -070041}
42
Colin Crossae4c6182017-09-15 17:33:55 -070043func NewTestArchContext() *TestContext {
44 ctx := NewTestContext()
45 ctx.preDeps = append(ctx.preDeps, registerArchMutator)
46 return ctx
47}
48
Colin Crosscec81712017-07-13 14:43:27 -070049type TestContext struct {
Colin Cross4c83e5c2019-02-25 14:54:28 -080050 *Context
Colin Crosscec81712017-07-13 14:43:27 -070051 preArch, preDeps, postDeps []RegisterMutatorFunc
Jeff Gastonb274ed32017-12-01 17:10:33 -080052 NameResolver *NameResolver
Colin Crosscec81712017-07-13 14:43:27 -070053}
54
55func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
56 ctx.preArch = append(ctx.preArch, f)
57}
58
59func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
60 ctx.preDeps = append(ctx.preDeps, f)
61}
62
63func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
64 ctx.postDeps = append(ctx.postDeps, f)
65}
66
67func (ctx *TestContext) Register() {
Colin Cross4c83e5c2019-02-25 14:54:28 -080068 registerMutators(ctx.Context.Context, ctx.preArch, ctx.preDeps, ctx.postDeps)
Colin Crosscec81712017-07-13 14:43:27 -070069
Colin Cross54855dd2017-11-28 23:55:23 -080070 ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(EnvSingleton))
Colin Crosscec81712017-07-13 14:43:27 -070071}
72
73func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
74 var module Module
75 ctx.VisitAllModules(func(m blueprint.Module) {
76 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
77 module = m.(Module)
78 }
79 })
80
81 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -070082 // find all the modules that do exist
83 allModuleNames := []string{}
84 ctx.VisitAllModules(func(m blueprint.Module) {
85 allModuleNames = append(allModuleNames, m.(Module).Name()+"("+ctx.ModuleSubDir(m)+")")
86 })
87
88 panic(fmt.Errorf("failed to find module %q variant %q."+
89 "\nall modules: %v", name, variant, allModuleNames))
Colin Crosscec81712017-07-13 14:43:27 -070090 }
91
92 return TestingModule{module}
93}
94
Jiyong Park37b25202018-07-11 10:49:27 +090095func (ctx *TestContext) ModuleVariantsForTests(name string) []string {
96 var variants []string
97 ctx.VisitAllModules(func(m blueprint.Module) {
98 if ctx.ModuleName(m) == name {
99 variants = append(variants, ctx.ModuleSubDir(m))
100 }
101 })
102 return variants
103}
104
Colin Cross4c83e5c2019-02-25 14:54:28 -0800105// SingletonForTests returns a TestingSingleton for the singleton registered with the given name.
106func (ctx *TestContext) SingletonForTests(name string) TestingSingleton {
107 allSingletonNames := []string{}
108 for _, s := range ctx.Singletons() {
109 n := ctx.SingletonName(s)
110 if n == name {
111 return TestingSingleton{
112 singleton: s.(*singletonAdaptor).Singleton,
113 provider: s.(testBuildProvider),
114 }
115 }
116 allSingletonNames = append(allSingletonNames, n)
117 }
118
119 panic(fmt.Errorf("failed to find singleton %q."+
120 "\nall singletons: %v", name, allSingletonNames))
121}
122
Jeff Gastondea7e4d2017-11-17 13:29:40 -0800123// MockFileSystem causes the Context to replace all reads with accesses to the provided map of
124// filenames to contents stored as a byte slice.
125func (ctx *TestContext) MockFileSystem(files map[string][]byte) {
126 // no module list file specified; find every file named Blueprints or Android.bp
127 pathsToParse := []string{}
128 for candidate := range files {
129 base := filepath.Base(candidate)
130 if base == "Blueprints" || base == "Android.bp" {
131 pathsToParse = append(pathsToParse, candidate)
132 }
133 }
134 if len(pathsToParse) < 1 {
135 panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", files))
136 }
137 files[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n"))
138
139 ctx.Context.MockFileSystem(files)
140}
141
Colin Cross4c83e5c2019-02-25 14:54:28 -0800142type testBuildProvider interface {
143 BuildParamsForTests() []BuildParams
144 RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
145}
146
147type TestingBuildParams struct {
148 BuildParams
149 RuleParams blueprint.RuleParams
150}
151
152func newTestingBuildParams(provider testBuildProvider, bparams BuildParams) TestingBuildParams {
153 return TestingBuildParams{
154 BuildParams: bparams,
155 RuleParams: provider.RuleParamsForTests()[bparams.Rule],
156 }
157}
158
159func maybeBuildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
160 for _, p := range provider.BuildParamsForTests() {
161 if strings.Contains(p.Rule.String(), rule) {
162 return newTestingBuildParams(provider, p)
163 }
164 }
165 return TestingBuildParams{}
166}
167
168func buildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
169 p := maybeBuildParamsFromRule(provider, rule)
170 if p.Rule == nil {
171 panic(fmt.Errorf("couldn't find rule %q", rule))
172 }
173 return p
174}
175
176func maybeBuildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
177 for _, p := range provider.BuildParamsForTests() {
178 if p.Description == desc {
179 return newTestingBuildParams(provider, p)
180 }
181 }
182 return TestingBuildParams{}
183}
184
185func buildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
186 p := maybeBuildParamsFromDescription(provider, desc)
187 if p.Rule == nil {
188 panic(fmt.Errorf("couldn't find description %q", desc))
189 }
190 return p
191}
192
193func maybeBuildParamsFromOutput(provider testBuildProvider, file string) (TestingBuildParams, []string) {
194 var searchedOutputs []string
195 for _, p := range provider.BuildParamsForTests() {
196 outputs := append(WritablePaths(nil), p.Outputs...)
197 if p.Output != nil {
198 outputs = append(outputs, p.Output)
199 }
200 for _, f := range outputs {
201 if f.String() == file || f.Rel() == file {
202 return newTestingBuildParams(provider, p), nil
203 }
204 searchedOutputs = append(searchedOutputs, f.Rel())
205 }
206 }
207 return TestingBuildParams{}, searchedOutputs
208}
209
210func buildParamsFromOutput(provider testBuildProvider, file string) TestingBuildParams {
211 p, searchedOutputs := maybeBuildParamsFromOutput(provider, file)
212 if p.Rule == nil {
213 panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
214 file, searchedOutputs))
215 }
216 return p
217}
218
219func allOutputs(provider testBuildProvider) []string {
220 var outputFullPaths []string
221 for _, p := range provider.BuildParamsForTests() {
222 outputs := append(WritablePaths(nil), p.Outputs...)
223 if p.Output != nil {
224 outputs = append(outputs, p.Output)
225 }
226 outputFullPaths = append(outputFullPaths, outputs.Strings()...)
227 }
228 return outputFullPaths
229}
230
Colin Crossb77ffc42019-01-05 22:09:19 -0800231// TestingModule is wrapper around an android.Module that provides methods to find information about individual
232// ctx.Build parameters for verification in tests.
Colin Crosscec81712017-07-13 14:43:27 -0700233type TestingModule struct {
234 module Module
235}
236
Colin Crossb77ffc42019-01-05 22:09:19 -0800237// Module returns the Module wrapped by the TestingModule.
Colin Crosscec81712017-07-13 14:43:27 -0700238func (m TestingModule) Module() Module {
239 return m.module
240}
241
Colin Crossb77ffc42019-01-05 22:09:19 -0800242// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
243// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800244func (m TestingModule) MaybeRule(rule string) TestingBuildParams {
245 return maybeBuildParamsFromRule(m.module, rule)
Colin Crosscec81712017-07-13 14:43:27 -0700246}
247
Colin Crossb77ffc42019-01-05 22:09:19 -0800248// 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 -0800249func (m TestingModule) Rule(rule string) TestingBuildParams {
250 return buildParamsFromRule(m.module, rule)
Colin Crossb77ffc42019-01-05 22:09:19 -0800251}
252
253// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
254// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800255func (m TestingModule) MaybeDescription(desc string) TestingBuildParams {
256 return maybeBuildParamsFromDescription(m.module, desc)
Nan Zhanged19fc32017-10-19 13:06:22 -0700257}
258
Colin Crossb77ffc42019-01-05 22:09:19 -0800259// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
260// found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800261func (m TestingModule) Description(desc string) TestingBuildParams {
262 return buildParamsFromDescription(m.module, desc)
Colin Crossb77ffc42019-01-05 22:09:19 -0800263}
264
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800265// 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 -0800266// value matches the provided string. Returns an empty BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800267func (m TestingModule) MaybeOutput(file string) TestingBuildParams {
268 p, _ := maybeBuildParamsFromOutput(m.module, file)
Colin Crossb77ffc42019-01-05 22:09:19 -0800269 return p
270}
271
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800272// 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 -0800273// value matches the provided string. Panics if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800274func (m TestingModule) Output(file string) TestingBuildParams {
275 return buildParamsFromOutput(m.module, file)
Colin Crosscec81712017-07-13 14:43:27 -0700276}
Logan Chien42039712018-03-12 16:29:17 +0800277
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800278// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
279func (m TestingModule) AllOutputs() []string {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800280 return allOutputs(m.module)
281}
282
283// TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual
284// ctx.Build parameters for verification in tests.
285type TestingSingleton struct {
286 singleton Singleton
287 provider testBuildProvider
288}
289
290// Singleton returns the Singleton wrapped by the TestingSingleton.
291func (s TestingSingleton) Singleton() Singleton {
292 return s.singleton
293}
294
295// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
296// BuildParams if no rule is found.
297func (s TestingSingleton) MaybeRule(rule string) TestingBuildParams {
298 return maybeBuildParamsFromRule(s.provider, rule)
299}
300
301// Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found.
302func (s TestingSingleton) Rule(rule string) TestingBuildParams {
303 return buildParamsFromRule(s.provider, rule)
304}
305
306// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
307// BuildParams if no rule is found.
308func (s TestingSingleton) MaybeDescription(desc string) TestingBuildParams {
309 return maybeBuildParamsFromDescription(s.provider, desc)
310}
311
312// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
313// found.
314func (s TestingSingleton) Description(desc string) TestingBuildParams {
315 return buildParamsFromDescription(s.provider, desc)
316}
317
318// MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
319// value matches the provided string. Returns an empty BuildParams if no rule is found.
320func (s TestingSingleton) MaybeOutput(file string) TestingBuildParams {
321 p, _ := maybeBuildParamsFromOutput(s.provider, file)
322 return p
323}
324
325// Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
326// value matches the provided string. Panics if no rule is found.
327func (s TestingSingleton) Output(file string) TestingBuildParams {
328 return buildParamsFromOutput(s.provider, file)
329}
330
331// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
332func (s TestingSingleton) AllOutputs() []string {
333 return allOutputs(s.provider)
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800334}
335
Logan Chien42039712018-03-12 16:29:17 +0800336func FailIfErrored(t *testing.T, errs []error) {
337 t.Helper()
338 if len(errs) > 0 {
339 for _, err := range errs {
340 t.Error(err)
341 }
342 t.FailNow()
343 }
344}
Logan Chienee97c3e2018-03-12 16:34:26 +0800345
346func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) {
347 t.Helper()
348
349 matcher, err := regexp.Compile(pattern)
350 if err != nil {
351 t.Errorf("failed to compile regular expression %q because %s", pattern, err)
352 }
353
354 found := false
355 for _, err := range errs {
356 if matcher.FindStringIndex(err.Error()) != nil {
357 found = true
358 break
359 }
360 }
361 if !found {
362 t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs))
363 for i, err := range errs {
364 t.Errorf("errs[%d] = %s", i, err)
365 }
366 }
367}