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