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 { |
| 68 | panic(fmt.Errorf("failed to find module %q variant %q", name, variant)) |
| 69 | } |
| 70 | |
| 71 | return TestingModule{module} |
| 72 | } |
| 73 | |
| 74 | type TestingModule struct { |
| 75 | module Module |
| 76 | } |
| 77 | |
| 78 | func (m TestingModule) Module() Module { |
| 79 | return m.module |
| 80 | } |
| 81 | |
| 82 | func (m TestingModule) Rule(rule string) ModuleBuildParams { |
| 83 | for _, p := range m.module.BuildParamsForTests() { |
| 84 | if strings.Contains(p.Rule.String(), rule) { |
| 85 | return p |
| 86 | } |
| 87 | } |
| 88 | panic(fmt.Errorf("couldn't find rule %q", rule)) |
| 89 | } |
| 90 | |
| 91 | func (m TestingModule) Output(file string) ModuleBuildParams { |
| 92 | for _, p := range m.module.BuildParamsForTests() { |
| 93 | outputs := append(WritablePaths(nil), p.Outputs...) |
| 94 | if p.Output != nil { |
| 95 | outputs = append(outputs, p.Output) |
| 96 | } |
| 97 | for _, f := range outputs { |
| 98 | if f.Base() == file { |
| 99 | return p |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | panic(fmt.Errorf("couldn't find output %q", file)) |
| 104 | } |