blob: 3d5e9d9308d8e99624c4492755f3a952ec9bb1ad [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 {
26 return &TestContext{
27 Context: blueprint.NewContext(),
28 }
29}
30
Colin Crossae4c6182017-09-15 17:33:55 -070031func NewTestArchContext() *TestContext {
32 ctx := NewTestContext()
33 ctx.preDeps = append(ctx.preDeps, registerArchMutator)
34 return ctx
35}
36
Colin Crosscec81712017-07-13 14:43:27 -070037type TestContext struct {
38 *blueprint.Context
39 preArch, preDeps, postDeps []RegisterMutatorFunc
40}
41
42func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
43 ctx.preArch = append(ctx.preArch, f)
44}
45
46func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
47 ctx.preDeps = append(ctx.preDeps, f)
48}
49
50func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
51 ctx.postDeps = append(ctx.postDeps, f)
52}
53
54func (ctx *TestContext) Register() {
55 registerMutators(ctx.Context, ctx.preArch, ctx.preDeps, ctx.postDeps)
56
57 ctx.RegisterSingletonType("env", EnvSingleton)
58}
59
60func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
61 var module Module
62 ctx.VisitAllModules(func(m blueprint.Module) {
63 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
64 module = m.(Module)
65 }
66 })
67
68 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -070069 // find all the modules that do exist
70 allModuleNames := []string{}
71 ctx.VisitAllModules(func(m blueprint.Module) {
72 allModuleNames = append(allModuleNames, m.(Module).Name()+"("+ctx.ModuleSubDir(m)+")")
73 })
74
75 panic(fmt.Errorf("failed to find module %q variant %q."+
76 "\nall modules: %v", name, variant, allModuleNames))
Colin Crosscec81712017-07-13 14:43:27 -070077 }
78
79 return TestingModule{module}
80}
81
Jeff Gastondea7e4d2017-11-17 13:29:40 -080082// MockFileSystem causes the Context to replace all reads with accesses to the provided map of
83// filenames to contents stored as a byte slice.
84func (ctx *TestContext) MockFileSystem(files map[string][]byte) {
85 // no module list file specified; find every file named Blueprints or Android.bp
86 pathsToParse := []string{}
87 for candidate := range files {
88 base := filepath.Base(candidate)
89 if base == "Blueprints" || base == "Android.bp" {
90 pathsToParse = append(pathsToParse, candidate)
91 }
92 }
93 if len(pathsToParse) < 1 {
94 panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", files))
95 }
96 files[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n"))
97
98 ctx.Context.MockFileSystem(files)
99}
100
Colin Crosscec81712017-07-13 14:43:27 -0700101type TestingModule struct {
102 module Module
103}
104
105func (m TestingModule) Module() Module {
106 return m.module
107}
108
Colin Crossae887032017-10-23 17:16:14 -0700109func (m TestingModule) Rule(rule string) BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700110 for _, p := range m.module.BuildParamsForTests() {
111 if strings.Contains(p.Rule.String(), rule) {
112 return p
113 }
114 }
115 panic(fmt.Errorf("couldn't find rule %q", rule))
116}
117
Colin Crossae887032017-10-23 17:16:14 -0700118func (m TestingModule) Description(desc string) BuildParams {
Nan Zhanged19fc32017-10-19 13:06:22 -0700119 for _, p := range m.module.BuildParamsForTests() {
120 if p.Description == desc {
121 return p
122 }
123 }
124 panic(fmt.Errorf("couldn't find description %q", desc))
125}
126
Colin Crossae887032017-10-23 17:16:14 -0700127func (m TestingModule) Output(file string) BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700128 for _, p := range m.module.BuildParamsForTests() {
129 outputs := append(WritablePaths(nil), p.Outputs...)
130 if p.Output != nil {
131 outputs = append(outputs, p.Output)
132 }
133 for _, f := range outputs {
Colin Cross702e0f82017-10-18 17:27:54 -0700134 if f.Rel() == file {
Colin Crosscec81712017-07-13 14:43:27 -0700135 return p
136 }
137 }
138 }
139 panic(fmt.Errorf("couldn't find output %q", file))
140}