blob: 4f2a2da4fb9cfffc9125b60877a6480874b6728d [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 ctx := &TestContext{
Colin Crosscec81712017-07-13 14:43:27 -070027 Context: blueprint.NewContext(),
28 }
Jeff Gaston088e29e2017-11-29 16:47:17 -080029
30 namespaceExportFilter := func(namespace *Namespace) bool {
31 return true
32 }
33 ctx.SetNameInterface(NewNameResolver(namespaceExportFilter))
34
35 return ctx
Colin Crosscec81712017-07-13 14:43:27 -070036}
37
Colin Crossae4c6182017-09-15 17:33:55 -070038func NewTestArchContext() *TestContext {
39 ctx := NewTestContext()
40 ctx.preDeps = append(ctx.preDeps, registerArchMutator)
41 return ctx
42}
43
Colin Crosscec81712017-07-13 14:43:27 -070044type TestContext struct {
45 *blueprint.Context
46 preArch, preDeps, postDeps []RegisterMutatorFunc
47}
48
49func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
50 ctx.preArch = append(ctx.preArch, f)
51}
52
53func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
54 ctx.preDeps = append(ctx.preDeps, f)
55}
56
57func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
58 ctx.postDeps = append(ctx.postDeps, f)
59}
60
61func (ctx *TestContext) Register() {
62 registerMutators(ctx.Context, ctx.preArch, ctx.preDeps, ctx.postDeps)
63
Colin Cross54855dd2017-11-28 23:55:23 -080064 ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(EnvSingleton))
Colin Crosscec81712017-07-13 14:43:27 -070065}
66
67func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
68 var module Module
69 ctx.VisitAllModules(func(m blueprint.Module) {
70 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
71 module = m.(Module)
72 }
73 })
74
75 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -070076 // find all the modules that do exist
77 allModuleNames := []string{}
78 ctx.VisitAllModules(func(m blueprint.Module) {
79 allModuleNames = append(allModuleNames, m.(Module).Name()+"("+ctx.ModuleSubDir(m)+")")
80 })
81
82 panic(fmt.Errorf("failed to find module %q variant %q."+
83 "\nall modules: %v", name, variant, allModuleNames))
Colin Crosscec81712017-07-13 14:43:27 -070084 }
85
86 return TestingModule{module}
87}
88
Jeff Gastondea7e4d2017-11-17 13:29:40 -080089// MockFileSystem causes the Context to replace all reads with accesses to the provided map of
90// filenames to contents stored as a byte slice.
91func (ctx *TestContext) MockFileSystem(files map[string][]byte) {
92 // no module list file specified; find every file named Blueprints or Android.bp
93 pathsToParse := []string{}
94 for candidate := range files {
95 base := filepath.Base(candidate)
96 if base == "Blueprints" || base == "Android.bp" {
97 pathsToParse = append(pathsToParse, candidate)
98 }
99 }
100 if len(pathsToParse) < 1 {
101 panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", files))
102 }
103 files[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n"))
104
105 ctx.Context.MockFileSystem(files)
106}
107
Colin Crosscec81712017-07-13 14:43:27 -0700108type TestingModule struct {
109 module Module
110}
111
112func (m TestingModule) Module() Module {
113 return m.module
114}
115
Colin Crossae887032017-10-23 17:16:14 -0700116func (m TestingModule) Rule(rule string) BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700117 for _, p := range m.module.BuildParamsForTests() {
118 if strings.Contains(p.Rule.String(), rule) {
119 return p
120 }
121 }
122 panic(fmt.Errorf("couldn't find rule %q", rule))
123}
124
Colin Crossae887032017-10-23 17:16:14 -0700125func (m TestingModule) Description(desc string) BuildParams {
Nan Zhanged19fc32017-10-19 13:06:22 -0700126 for _, p := range m.module.BuildParamsForTests() {
127 if p.Description == desc {
128 return p
129 }
130 }
131 panic(fmt.Errorf("couldn't find description %q", desc))
132}
133
Colin Crossae887032017-10-23 17:16:14 -0700134func (m TestingModule) Output(file string) BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700135 for _, p := range m.module.BuildParamsForTests() {
136 outputs := append(WritablePaths(nil), p.Outputs...)
137 if p.Output != nil {
138 outputs = append(outputs, p.Output)
139 }
140 for _, f := range outputs {
Colin Cross890ff552017-11-30 20:13:19 -0800141 if f.String() == file || f.Rel() == file {
Colin Crosscec81712017-07-13 14:43:27 -0700142 return p
143 }
144 }
145 }
146 panic(fmt.Errorf("couldn't find output %q", file))
147}