blob: 667c1aa8dc9cb855b4c4f4f0058d19311391d4ae [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
89func (m TestingModule) Rule(rule string) ModuleBuildParams {
90 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
98func (m TestingModule) Output(file string) ModuleBuildParams {
99 for _, p := range m.module.BuildParamsForTests() {
100 outputs := append(WritablePaths(nil), p.Outputs...)
101 if p.Output != nil {
102 outputs = append(outputs, p.Output)
103 }
104 for _, f := range outputs {
105 if f.Base() == file {
106 return p
107 }
108 }
109 }
110 panic(fmt.Errorf("couldn't find output %q", file))
111}