Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | ) |
| 23 | |
| 24 | func NewTestContext() *TestContext { |
| 25 | return &TestContext{ |
| 26 | Context: blueprint.NewContext(), |
| 27 | } |
| 28 | } |
| 29 | |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 30 | func NewTestArchContext() *TestContext { |
| 31 | ctx := NewTestContext() |
| 32 | ctx.preDeps = append(ctx.preDeps, registerArchMutator) |
| 33 | return ctx |
| 34 | } |
| 35 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 36 | type TestContext struct { |
| 37 | *blueprint.Context |
| 38 | preArch, preDeps, postDeps []RegisterMutatorFunc |
| 39 | } |
| 40 | |
| 41 | func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) { |
| 42 | ctx.preArch = append(ctx.preArch, f) |
| 43 | } |
| 44 | |
| 45 | func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) { |
| 46 | ctx.preDeps = append(ctx.preDeps, f) |
| 47 | } |
| 48 | |
| 49 | func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) { |
| 50 | ctx.postDeps = append(ctx.postDeps, f) |
| 51 | } |
| 52 | |
| 53 | func (ctx *TestContext) Register() { |
| 54 | registerMutators(ctx.Context, ctx.preArch, ctx.preDeps, ctx.postDeps) |
| 55 | |
| 56 | ctx.RegisterSingletonType("env", EnvSingleton) |
| 57 | } |
| 58 | |
| 59 | func (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 Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 68 | // 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 Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | return TestingModule{module} |
| 79 | } |
| 80 | |
| 81 | type TestingModule struct { |
| 82 | module Module |
| 83 | } |
| 84 | |
| 85 | func (m TestingModule) Module() Module { |
| 86 | return m.module |
| 87 | } |
| 88 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame^] | 89 | func (m TestingModule) Rule(rule string) BuildParams { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 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 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame^] | 98 | func (m TestingModule) Description(desc string) BuildParams { |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 99 | 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 Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame^] | 107 | func (m TestingModule) Output(file string) BuildParams { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 108 | 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 Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 114 | if f.Rel() == file { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 115 | return p |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | panic(fmt.Errorf("couldn't find output %q", file)) |
| 120 | } |