blob: e79562daec45b6c28ba1658182ce31a2f55679b7 [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"
19 "strings"
20
21 "github.com/google/blueprint"
22)
23
24func NewTestContext() *TestContext {
25 return &TestContext{
26 Context: blueprint.NewContext(),
27 }
28}
29
Colin Crossae4c6182017-09-15 17:33:55 -070030func NewTestArchContext() *TestContext {
31 ctx := NewTestContext()
32 ctx.preDeps = append(ctx.preDeps, registerArchMutator)
33 return ctx
34}
35
Colin Crosscec81712017-07-13 14:43:27 -070036type TestContext struct {
37 *blueprint.Context
38 preArch, preDeps, postDeps []RegisterMutatorFunc
39}
40
41func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
42 ctx.preArch = append(ctx.preArch, f)
43}
44
45func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
46 ctx.preDeps = append(ctx.preDeps, f)
47}
48
49func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
50 ctx.postDeps = append(ctx.postDeps, f)
51}
52
53func (ctx *TestContext) Register() {
54 registerMutators(ctx.Context, ctx.preArch, ctx.preDeps, ctx.postDeps)
55
56 ctx.RegisterSingletonType("env", EnvSingleton)
57}
58
59func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
60 var module Module
61 ctx.VisitAllModules(func(m blueprint.Module) {
62 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
63 module = m.(Module)
64 }
65 })
66
67 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -070068 // find all the modules that do exist
69 allModuleNames := []string{}
70 ctx.VisitAllModules(func(m blueprint.Module) {
71 allModuleNames = append(allModuleNames, m.(Module).Name()+"("+ctx.ModuleSubDir(m)+")")
72 })
73
74 panic(fmt.Errorf("failed to find module %q variant %q."+
75 "\nall modules: %v", name, variant, allModuleNames))
Colin Crosscec81712017-07-13 14:43:27 -070076 }
77
78 return TestingModule{module}
79}
80
81type TestingModule struct {
82 module Module
83}
84
85func (m TestingModule) Module() Module {
86 return m.module
87}
88
Colin Crossae887032017-10-23 17:16:14 -070089func (m TestingModule) Rule(rule string) BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -070090 for _, p := range m.module.BuildParamsForTests() {
91 if strings.Contains(p.Rule.String(), rule) {
92 return p
93 }
94 }
95 panic(fmt.Errorf("couldn't find rule %q", rule))
96}
97
Colin Crossae887032017-10-23 17:16:14 -070098func (m TestingModule) Description(desc string) BuildParams {
Nan Zhanged19fc32017-10-19 13:06:22 -070099 for _, p := range m.module.BuildParamsForTests() {
100 if p.Description == desc {
101 return p
102 }
103 }
104 panic(fmt.Errorf("couldn't find description %q", desc))
105}
106
Colin Crossae887032017-10-23 17:16:14 -0700107func (m TestingModule) Output(file string) BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700108 for _, p := range m.module.BuildParamsForTests() {
109 outputs := append(WritablePaths(nil), p.Outputs...)
110 if p.Output != nil {
111 outputs = append(outputs, p.Output)
112 }
113 for _, f := range outputs {
Colin Cross702e0f82017-10-18 17:27:54 -0700114 if f.Rel() == file {
Colin Crosscec81712017-07-13 14:43:27 -0700115 return p
116 }
117 }
118 }
119 panic(fmt.Errorf("couldn't find output %q", file))
120}