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