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" |
Jeff Gaston | dea7e4d | 2017-11-17 13:29:40 -0800 | [diff] [blame] | 19 | "path/filepath" |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 20 | "strings" |
| 21 | |
| 22 | "github.com/google/blueprint" |
| 23 | ) |
| 24 | |
| 25 | func NewTestContext() *TestContext { |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame^] | 26 | ctx := &TestContext{ |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 27 | Context: blueprint.NewContext(), |
| 28 | } |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame^] | 29 | |
| 30 | namespaceExportFilter := func(namespace *Namespace) bool { |
| 31 | return true |
| 32 | } |
| 33 | ctx.SetNameInterface(NewNameResolver(namespaceExportFilter)) |
| 34 | |
| 35 | return ctx |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 38 | func NewTestArchContext() *TestContext { |
| 39 | ctx := NewTestContext() |
| 40 | ctx.preDeps = append(ctx.preDeps, registerArchMutator) |
| 41 | return ctx |
| 42 | } |
| 43 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 44 | type TestContext struct { |
| 45 | *blueprint.Context |
| 46 | preArch, preDeps, postDeps []RegisterMutatorFunc |
| 47 | } |
| 48 | |
| 49 | func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) { |
| 50 | ctx.preArch = append(ctx.preArch, f) |
| 51 | } |
| 52 | |
| 53 | func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) { |
| 54 | ctx.preDeps = append(ctx.preDeps, f) |
| 55 | } |
| 56 | |
| 57 | func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) { |
| 58 | ctx.postDeps = append(ctx.postDeps, f) |
| 59 | } |
| 60 | |
| 61 | func (ctx *TestContext) Register() { |
| 62 | registerMutators(ctx.Context, ctx.preArch, ctx.preDeps, ctx.postDeps) |
| 63 | |
Colin Cross | 54855dd | 2017-11-28 23:55:23 -0800 | [diff] [blame] | 64 | ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(EnvSingleton)) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule { |
| 68 | var module Module |
| 69 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 70 | if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant { |
| 71 | module = m.(Module) |
| 72 | } |
| 73 | }) |
| 74 | |
| 75 | if module == nil { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 76 | // find all the modules that do exist |
| 77 | allModuleNames := []string{} |
| 78 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 79 | allModuleNames = append(allModuleNames, m.(Module).Name()+"("+ctx.ModuleSubDir(m)+")") |
| 80 | }) |
| 81 | |
| 82 | panic(fmt.Errorf("failed to find module %q variant %q."+ |
| 83 | "\nall modules: %v", name, variant, allModuleNames)) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | return TestingModule{module} |
| 87 | } |
| 88 | |
Jeff Gaston | dea7e4d | 2017-11-17 13:29:40 -0800 | [diff] [blame] | 89 | // MockFileSystem causes the Context to replace all reads with accesses to the provided map of |
| 90 | // filenames to contents stored as a byte slice. |
| 91 | func (ctx *TestContext) MockFileSystem(files map[string][]byte) { |
| 92 | // no module list file specified; find every file named Blueprints or Android.bp |
| 93 | pathsToParse := []string{} |
| 94 | for candidate := range files { |
| 95 | base := filepath.Base(candidate) |
| 96 | if base == "Blueprints" || base == "Android.bp" { |
| 97 | pathsToParse = append(pathsToParse, candidate) |
| 98 | } |
| 99 | } |
| 100 | if len(pathsToParse) < 1 { |
| 101 | panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", files)) |
| 102 | } |
| 103 | files[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n")) |
| 104 | |
| 105 | ctx.Context.MockFileSystem(files) |
| 106 | } |
| 107 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 108 | type TestingModule struct { |
| 109 | module Module |
| 110 | } |
| 111 | |
| 112 | func (m TestingModule) Module() Module { |
| 113 | return m.module |
| 114 | } |
| 115 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 116 | func (m TestingModule) Rule(rule string) BuildParams { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 117 | for _, p := range m.module.BuildParamsForTests() { |
| 118 | if strings.Contains(p.Rule.String(), rule) { |
| 119 | return p |
| 120 | } |
| 121 | } |
| 122 | panic(fmt.Errorf("couldn't find rule %q", rule)) |
| 123 | } |
| 124 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 125 | func (m TestingModule) Description(desc string) BuildParams { |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 126 | for _, p := range m.module.BuildParamsForTests() { |
| 127 | if p.Description == desc { |
| 128 | return p |
| 129 | } |
| 130 | } |
| 131 | panic(fmt.Errorf("couldn't find description %q", desc)) |
| 132 | } |
| 133 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 134 | func (m TestingModule) Output(file string) BuildParams { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 135 | for _, p := range m.module.BuildParamsForTests() { |
| 136 | outputs := append(WritablePaths(nil), p.Outputs...) |
| 137 | if p.Output != nil { |
| 138 | outputs = append(outputs, p.Output) |
| 139 | } |
| 140 | for _, f := range outputs { |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 141 | if f.String() == file || f.Rel() == file { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 142 | return p |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | panic(fmt.Errorf("couldn't find output %q", file)) |
| 147 | } |