blob: 6e80c53266554401db9edb00b29da48b6468deec [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"
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{
33 Context: blueprint.NewContext(),
34 NameResolver: nameResolver,
35 }
36
37 ctx.SetNameInterface(nameResolver)
Jeff Gaston088e29e2017-11-29 16:47:17 -080038
39 return ctx
Colin Crosscec81712017-07-13 14:43:27 -070040}
41
Colin Crossae4c6182017-09-15 17:33:55 -070042func NewTestArchContext() *TestContext {
43 ctx := NewTestContext()
44 ctx.preDeps = append(ctx.preDeps, registerArchMutator)
45 return ctx
46}
47
Colin Crosscec81712017-07-13 14:43:27 -070048type TestContext struct {
49 *blueprint.Context
50 preArch, preDeps, postDeps []RegisterMutatorFunc
Jeff Gastonb274ed32017-12-01 17:10:33 -080051 NameResolver *NameResolver
Colin Crosscec81712017-07-13 14:43:27 -070052}
53
54func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
55 ctx.preArch = append(ctx.preArch, f)
56}
57
58func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
59 ctx.preDeps = append(ctx.preDeps, f)
60}
61
62func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
63 ctx.postDeps = append(ctx.postDeps, f)
64}
65
66func (ctx *TestContext) Register() {
67 registerMutators(ctx.Context, ctx.preArch, ctx.preDeps, ctx.postDeps)
68
Colin Cross54855dd2017-11-28 23:55:23 -080069 ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(EnvSingleton))
Colin Crosscec81712017-07-13 14:43:27 -070070}
71
72func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
73 var module Module
74 ctx.VisitAllModules(func(m blueprint.Module) {
75 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
76 module = m.(Module)
77 }
78 })
79
80 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -070081 // find all the modules that do exist
82 allModuleNames := []string{}
83 ctx.VisitAllModules(func(m blueprint.Module) {
84 allModuleNames = append(allModuleNames, m.(Module).Name()+"("+ctx.ModuleSubDir(m)+")")
85 })
86
87 panic(fmt.Errorf("failed to find module %q variant %q."+
88 "\nall modules: %v", name, variant, allModuleNames))
Colin Crosscec81712017-07-13 14:43:27 -070089 }
90
91 return TestingModule{module}
92}
93
Jeff Gastondea7e4d2017-11-17 13:29:40 -080094// MockFileSystem causes the Context to replace all reads with accesses to the provided map of
95// filenames to contents stored as a byte slice.
96func (ctx *TestContext) MockFileSystem(files map[string][]byte) {
97 // no module list file specified; find every file named Blueprints or Android.bp
98 pathsToParse := []string{}
99 for candidate := range files {
100 base := filepath.Base(candidate)
101 if base == "Blueprints" || base == "Android.bp" {
102 pathsToParse = append(pathsToParse, candidate)
103 }
104 }
105 if len(pathsToParse) < 1 {
106 panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", files))
107 }
108 files[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n"))
109
110 ctx.Context.MockFileSystem(files)
111}
112
Colin Crosscec81712017-07-13 14:43:27 -0700113type TestingModule struct {
114 module Module
115}
116
117func (m TestingModule) Module() Module {
118 return m.module
119}
120
Colin Crossae887032017-10-23 17:16:14 -0700121func (m TestingModule) Rule(rule string) BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700122 for _, p := range m.module.BuildParamsForTests() {
123 if strings.Contains(p.Rule.String(), rule) {
124 return p
125 }
126 }
127 panic(fmt.Errorf("couldn't find rule %q", rule))
128}
129
Colin Crossae887032017-10-23 17:16:14 -0700130func (m TestingModule) Description(desc string) BuildParams {
Nan Zhanged19fc32017-10-19 13:06:22 -0700131 for _, p := range m.module.BuildParamsForTests() {
132 if p.Description == desc {
133 return p
134 }
135 }
136 panic(fmt.Errorf("couldn't find description %q", desc))
137}
138
Colin Crossae887032017-10-23 17:16:14 -0700139func (m TestingModule) Output(file string) BuildParams {
Colin Cross9cf27db2017-12-05 09:26:15 -0800140 var searchedOutputs []string
Colin Crosscec81712017-07-13 14:43:27 -0700141 for _, p := range m.module.BuildParamsForTests() {
142 outputs := append(WritablePaths(nil), p.Outputs...)
143 if p.Output != nil {
144 outputs = append(outputs, p.Output)
145 }
146 for _, f := range outputs {
Colin Cross890ff552017-11-30 20:13:19 -0800147 if f.String() == file || f.Rel() == file {
Colin Crosscec81712017-07-13 14:43:27 -0700148 return p
149 }
Colin Cross9cf27db2017-12-05 09:26:15 -0800150 searchedOutputs = append(searchedOutputs, f.Rel())
Colin Crosscec81712017-07-13 14:43:27 -0700151 }
152 }
Colin Cross9cf27db2017-12-05 09:26:15 -0800153 panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
154 file, searchedOutputs))
Colin Crosscec81712017-07-13 14:43:27 -0700155}
Logan Chien42039712018-03-12 16:29:17 +0800156
157func FailIfErrored(t *testing.T, errs []error) {
158 t.Helper()
159 if len(errs) > 0 {
160 for _, err := range errs {
161 t.Error(err)
162 }
163 t.FailNow()
164 }
165}