blob: ae012b06e7506e5a416385ccd4015fccc89cf049 [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"
21
22 "github.com/google/blueprint"
23)
24
25func NewTestContext() *TestContext {
Jeff Gaston088e29e2017-11-29 16:47:17 -080026 namespaceExportFilter := func(namespace *Namespace) bool {
27 return true
28 }
Jeff Gastonb274ed32017-12-01 17:10:33 -080029
30 nameResolver := NewNameResolver(namespaceExportFilter)
31 ctx := &TestContext{
32 Context: blueprint.NewContext(),
33 NameResolver: nameResolver,
34 }
35
36 ctx.SetNameInterface(nameResolver)
Jeff Gaston088e29e2017-11-29 16:47:17 -080037
38 return ctx
Colin Crosscec81712017-07-13 14:43:27 -070039}
40
Colin Crossae4c6182017-09-15 17:33:55 -070041func NewTestArchContext() *TestContext {
42 ctx := NewTestContext()
43 ctx.preDeps = append(ctx.preDeps, registerArchMutator)
44 return ctx
45}
46
Colin Crosscec81712017-07-13 14:43:27 -070047type TestContext struct {
48 *blueprint.Context
49 preArch, preDeps, postDeps []RegisterMutatorFunc
Jeff Gastonb274ed32017-12-01 17:10:33 -080050 NameResolver *NameResolver
Colin Crosscec81712017-07-13 14:43:27 -070051}
52
53func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
54 ctx.preArch = append(ctx.preArch, f)
55}
56
57func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
58 ctx.preDeps = append(ctx.preDeps, f)
59}
60
61func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
62 ctx.postDeps = append(ctx.postDeps, f)
63}
64
65func (ctx *TestContext) Register() {
66 registerMutators(ctx.Context, ctx.preArch, ctx.preDeps, ctx.postDeps)
67
Colin Cross54855dd2017-11-28 23:55:23 -080068 ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(EnvSingleton))
Colin Crosscec81712017-07-13 14:43:27 -070069}
70
71func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
72 var module Module
73 ctx.VisitAllModules(func(m blueprint.Module) {
74 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
75 module = m.(Module)
76 }
77 })
78
79 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -070080 // find all the modules that do exist
81 allModuleNames := []string{}
82 ctx.VisitAllModules(func(m blueprint.Module) {
83 allModuleNames = append(allModuleNames, m.(Module).Name()+"("+ctx.ModuleSubDir(m)+")")
84 })
85
86 panic(fmt.Errorf("failed to find module %q variant %q."+
87 "\nall modules: %v", name, variant, allModuleNames))
Colin Crosscec81712017-07-13 14:43:27 -070088 }
89
90 return TestingModule{module}
91}
92
Jeff Gastondea7e4d2017-11-17 13:29:40 -080093// MockFileSystem causes the Context to replace all reads with accesses to the provided map of
94// filenames to contents stored as a byte slice.
95func (ctx *TestContext) MockFileSystem(files map[string][]byte) {
96 // no module list file specified; find every file named Blueprints or Android.bp
97 pathsToParse := []string{}
98 for candidate := range files {
99 base := filepath.Base(candidate)
100 if base == "Blueprints" || base == "Android.bp" {
101 pathsToParse = append(pathsToParse, candidate)
102 }
103 }
104 if len(pathsToParse) < 1 {
105 panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", files))
106 }
107 files[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n"))
108
109 ctx.Context.MockFileSystem(files)
110}
111
Colin Crosscec81712017-07-13 14:43:27 -0700112type TestingModule struct {
113 module Module
114}
115
116func (m TestingModule) Module() Module {
117 return m.module
118}
119
Colin Crossae887032017-10-23 17:16:14 -0700120func (m TestingModule) Rule(rule string) BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700121 for _, p := range m.module.BuildParamsForTests() {
122 if strings.Contains(p.Rule.String(), rule) {
123 return p
124 }
125 }
126 panic(fmt.Errorf("couldn't find rule %q", rule))
127}
128
Colin Crossae887032017-10-23 17:16:14 -0700129func (m TestingModule) Description(desc string) BuildParams {
Nan Zhanged19fc32017-10-19 13:06:22 -0700130 for _, p := range m.module.BuildParamsForTests() {
131 if p.Description == desc {
132 return p
133 }
134 }
135 panic(fmt.Errorf("couldn't find description %q", desc))
136}
137
Colin Crossae887032017-10-23 17:16:14 -0700138func (m TestingModule) Output(file string) BuildParams {
Colin Cross9cf27db2017-12-05 09:26:15 -0800139 var searchedOutputs []string
Colin Crosscec81712017-07-13 14:43:27 -0700140 for _, p := range m.module.BuildParamsForTests() {
141 outputs := append(WritablePaths(nil), p.Outputs...)
142 if p.Output != nil {
143 outputs = append(outputs, p.Output)
144 }
145 for _, f := range outputs {
Colin Cross890ff552017-11-30 20:13:19 -0800146 if f.String() == file || f.Rel() == file {
Colin Crosscec81712017-07-13 14:43:27 -0700147 return p
148 }
Colin Cross9cf27db2017-12-05 09:26:15 -0800149 searchedOutputs = append(searchedOutputs, f.Rel())
Colin Crosscec81712017-07-13 14:43:27 -0700150 }
151 }
Colin Cross9cf27db2017-12-05 09:26:15 -0800152 panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
153 file, searchedOutputs))
Colin Crosscec81712017-07-13 14:43:27 -0700154}