blob: ca7e7ce6b502ca550cdc3c36fef82dd6b557bac6 [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{
34 Context: blueprint.NewContext(),
35 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 {
50 *blueprint.Context
51 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() {
68 registerMutators(ctx.Context, ctx.preArch, ctx.preDeps, ctx.postDeps)
69
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
Jeff Gastondea7e4d2017-11-17 13:29:40 -0800105// MockFileSystem causes the Context to replace all reads with accesses to the provided map of
106// filenames to contents stored as a byte slice.
107func (ctx *TestContext) MockFileSystem(files map[string][]byte) {
108 // no module list file specified; find every file named Blueprints or Android.bp
109 pathsToParse := []string{}
110 for candidate := range files {
111 base := filepath.Base(candidate)
112 if base == "Blueprints" || base == "Android.bp" {
113 pathsToParse = append(pathsToParse, candidate)
114 }
115 }
116 if len(pathsToParse) < 1 {
117 panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", files))
118 }
119 files[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n"))
120
121 ctx.Context.MockFileSystem(files)
122}
123
Colin Crosscec81712017-07-13 14:43:27 -0700124type TestingModule struct {
125 module Module
126}
127
128func (m TestingModule) Module() Module {
129 return m.module
130}
131
Colin Crossae887032017-10-23 17:16:14 -0700132func (m TestingModule) Rule(rule string) BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700133 for _, p := range m.module.BuildParamsForTests() {
134 if strings.Contains(p.Rule.String(), rule) {
135 return p
136 }
137 }
138 panic(fmt.Errorf("couldn't find rule %q", rule))
139}
140
Colin Crossae887032017-10-23 17:16:14 -0700141func (m TestingModule) Description(desc string) BuildParams {
Nan Zhanged19fc32017-10-19 13:06:22 -0700142 for _, p := range m.module.BuildParamsForTests() {
143 if p.Description == desc {
144 return p
145 }
146 }
147 panic(fmt.Errorf("couldn't find description %q", desc))
148}
149
Colin Crossae887032017-10-23 17:16:14 -0700150func (m TestingModule) Output(file string) BuildParams {
Colin Cross9cf27db2017-12-05 09:26:15 -0800151 var searchedOutputs []string
Colin Crosscec81712017-07-13 14:43:27 -0700152 for _, p := range m.module.BuildParamsForTests() {
153 outputs := append(WritablePaths(nil), p.Outputs...)
154 if p.Output != nil {
155 outputs = append(outputs, p.Output)
156 }
157 for _, f := range outputs {
Colin Cross890ff552017-11-30 20:13:19 -0800158 if f.String() == file || f.Rel() == file {
Colin Crosscec81712017-07-13 14:43:27 -0700159 return p
160 }
Colin Cross9cf27db2017-12-05 09:26:15 -0800161 searchedOutputs = append(searchedOutputs, f.Rel())
Colin Crosscec81712017-07-13 14:43:27 -0700162 }
163 }
Colin Cross9cf27db2017-12-05 09:26:15 -0800164 panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
165 file, searchedOutputs))
Colin Crosscec81712017-07-13 14:43:27 -0700166}
Logan Chien42039712018-03-12 16:29:17 +0800167
168func FailIfErrored(t *testing.T, errs []error) {
169 t.Helper()
170 if len(errs) > 0 {
171 for _, err := range errs {
172 t.Error(err)
173 }
174 t.FailNow()
175 }
176}
Logan Chienee97c3e2018-03-12 16:34:26 +0800177
178func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) {
179 t.Helper()
180
181 matcher, err := regexp.Compile(pattern)
182 if err != nil {
183 t.Errorf("failed to compile regular expression %q because %s", pattern, err)
184 }
185
186 found := false
187 for _, err := range errs {
188 if matcher.FindStringIndex(err.Error()) != nil {
189 found = true
190 break
191 }
192 }
193 if !found {
194 t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs))
195 for i, err := range errs {
196 t.Errorf("errs[%d] = %s", i, err)
197 }
198 }
199}