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" |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 20 | "regexp" |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 21 | "strings" |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 22 | "testing" |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 23 | |
| 24 | "github.com/google/blueprint" |
| 25 | ) |
| 26 | |
| 27 | func NewTestContext() *TestContext { |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 28 | namespaceExportFilter := func(namespace *Namespace) bool { |
| 29 | return true |
| 30 | } |
Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 31 | |
| 32 | nameResolver := NewNameResolver(namespaceExportFilter) |
| 33 | ctx := &TestContext{ |
| 34 | Context: blueprint.NewContext(), |
| 35 | NameResolver: nameResolver, |
| 36 | } |
| 37 | |
| 38 | ctx.SetNameInterface(nameResolver) |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 39 | |
| 40 | return ctx |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 43 | func NewTestArchContext() *TestContext { |
| 44 | ctx := NewTestContext() |
| 45 | ctx.preDeps = append(ctx.preDeps, registerArchMutator) |
| 46 | return ctx |
| 47 | } |
| 48 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 49 | type TestContext struct { |
| 50 | *blueprint.Context |
| 51 | preArch, preDeps, postDeps []RegisterMutatorFunc |
Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 52 | NameResolver *NameResolver |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) { |
| 56 | ctx.preArch = append(ctx.preArch, f) |
| 57 | } |
| 58 | |
| 59 | func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) { |
| 60 | ctx.preDeps = append(ctx.preDeps, f) |
| 61 | } |
| 62 | |
| 63 | func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) { |
| 64 | ctx.postDeps = append(ctx.postDeps, f) |
| 65 | } |
| 66 | |
| 67 | func (ctx *TestContext) Register() { |
| 68 | registerMutators(ctx.Context, ctx.preArch, ctx.preDeps, ctx.postDeps) |
| 69 | |
Colin Cross | 54855dd | 2017-11-28 23:55:23 -0800 | [diff] [blame] | 70 | ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(EnvSingleton)) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule { |
| 74 | var module Module |
| 75 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 76 | if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant { |
| 77 | module = m.(Module) |
| 78 | } |
| 79 | }) |
| 80 | |
| 81 | if module == nil { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 82 | // find all the modules that do exist |
| 83 | allModuleNames := []string{} |
| 84 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 85 | allModuleNames = append(allModuleNames, m.(Module).Name()+"("+ctx.ModuleSubDir(m)+")") |
| 86 | }) |
| 87 | |
| 88 | panic(fmt.Errorf("failed to find module %q variant %q."+ |
| 89 | "\nall modules: %v", name, variant, allModuleNames)) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | return TestingModule{module} |
| 93 | } |
| 94 | |
Jiyong Park | 37b2520 | 2018-07-11 10:49:27 +0900 | [diff] [blame] | 95 | func (ctx *TestContext) ModuleVariantsForTests(name string) []string { |
| 96 | var variants []string |
| 97 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 98 | if ctx.ModuleName(m) == name { |
| 99 | variants = append(variants, ctx.ModuleSubDir(m)) |
| 100 | } |
| 101 | }) |
| 102 | return variants |
| 103 | } |
| 104 | |
Jeff Gaston | dea7e4d | 2017-11-17 13:29:40 -0800 | [diff] [blame] | 105 | // MockFileSystem causes the Context to replace all reads with accesses to the provided map of |
| 106 | // filenames to contents stored as a byte slice. |
| 107 | func (ctx *TestContext) MockFileSystem(files map[string][]byte) { |
| 108 | // no module list file specified; find every file named Blueprints or Android.bp |
| 109 | pathsToParse := []string{} |
| 110 | for candidate := range files { |
| 111 | base := filepath.Base(candidate) |
| 112 | if base == "Blueprints" || base == "Android.bp" { |
| 113 | pathsToParse = append(pathsToParse, candidate) |
| 114 | } |
| 115 | } |
| 116 | if len(pathsToParse) < 1 { |
| 117 | panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", files)) |
| 118 | } |
| 119 | files[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n")) |
| 120 | |
| 121 | ctx.Context.MockFileSystem(files) |
| 122 | } |
| 123 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 124 | // TestingModule is wrapper around an android.Module that provides methods to find information about individual |
| 125 | // ctx.Build parameters for verification in tests. |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 126 | type TestingModule struct { |
| 127 | module Module |
| 128 | } |
| 129 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 130 | // Module returns the Module wrapped by the TestingModule. |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 131 | func (m TestingModule) Module() Module { |
| 132 | return m.module |
| 133 | } |
| 134 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 135 | // MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty |
| 136 | // BuildParams if no rule is found. |
| 137 | func (m TestingModule) MaybeRule(rule string) BuildParams { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 138 | for _, p := range m.module.BuildParamsForTests() { |
| 139 | if strings.Contains(p.Rule.String(), rule) { |
| 140 | return p |
| 141 | } |
| 142 | } |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 143 | return BuildParams{} |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 146 | // Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found. |
| 147 | func (m TestingModule) Rule(rule string) BuildParams { |
| 148 | p := m.MaybeRule(rule) |
| 149 | if p.Rule == nil { |
| 150 | panic(fmt.Errorf("couldn't find rule %q", rule)) |
| 151 | } |
| 152 | return p |
| 153 | } |
| 154 | |
| 155 | // MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty |
| 156 | // BuildParams if no rule is found. |
| 157 | func (m TestingModule) MaybeDescription(desc string) BuildParams { |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 158 | for _, p := range m.module.BuildParamsForTests() { |
| 159 | if p.Description == desc { |
| 160 | return p |
| 161 | } |
| 162 | } |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 163 | return BuildParams{} |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 166 | // Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is |
| 167 | // found. |
| 168 | func (m TestingModule) Description(desc string) BuildParams { |
| 169 | p := m.MaybeDescription(desc) |
| 170 | if p.Rule == nil { |
| 171 | panic(fmt.Errorf("couldn't find description %q", desc)) |
| 172 | } |
| 173 | return p |
| 174 | } |
| 175 | |
| 176 | func (m TestingModule) maybeOutput(file string) (BuildParams, []string) { |
Colin Cross | 9cf27db | 2017-12-05 09:26:15 -0800 | [diff] [blame] | 177 | var searchedOutputs []string |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 178 | for _, p := range m.module.BuildParamsForTests() { |
| 179 | outputs := append(WritablePaths(nil), p.Outputs...) |
| 180 | if p.Output != nil { |
| 181 | outputs = append(outputs, p.Output) |
| 182 | } |
| 183 | for _, f := range outputs { |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 184 | if f.String() == file || f.Rel() == file { |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 185 | return p, nil |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 186 | } |
Colin Cross | 9cf27db | 2017-12-05 09:26:15 -0800 | [diff] [blame] | 187 | searchedOutputs = append(searchedOutputs, f.Rel()) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 190 | return BuildParams{}, searchedOutputs |
| 191 | } |
| 192 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame^] | 193 | // MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel() |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 194 | // value matches the provided string. Returns an empty BuildParams if no rule is found. |
| 195 | func (m TestingModule) MaybeOutput(file string) BuildParams { |
| 196 | p, _ := m.maybeOutput(file) |
| 197 | return p |
| 198 | } |
| 199 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame^] | 200 | // Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel() |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 201 | // value matches the provided string. Panics if no rule is found. |
| 202 | func (m TestingModule) Output(file string) BuildParams { |
| 203 | p, searchedOutputs := m.maybeOutput(file) |
| 204 | if p.Rule == nil { |
| 205 | panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v", |
| 206 | file, searchedOutputs)) |
| 207 | } |
| 208 | return p |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 209 | } |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 210 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame^] | 211 | // AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms. |
| 212 | func (m TestingModule) AllOutputs() []string { |
| 213 | var outputFullPaths []string |
| 214 | for _, p := range m.module.BuildParamsForTests() { |
| 215 | outputs := append(WritablePaths(nil), p.Outputs...) |
| 216 | if p.Output != nil { |
| 217 | outputs = append(outputs, p.Output) |
| 218 | } |
| 219 | outputFullPaths = append(outputFullPaths, outputs.Strings()...) |
| 220 | } |
| 221 | return outputFullPaths |
| 222 | } |
| 223 | |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 224 | func FailIfErrored(t *testing.T, errs []error) { |
| 225 | t.Helper() |
| 226 | if len(errs) > 0 { |
| 227 | for _, err := range errs { |
| 228 | t.Error(err) |
| 229 | } |
| 230 | t.FailNow() |
| 231 | } |
| 232 | } |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 233 | |
| 234 | func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) { |
| 235 | t.Helper() |
| 236 | |
| 237 | matcher, err := regexp.Compile(pattern) |
| 238 | if err != nil { |
| 239 | t.Errorf("failed to compile regular expression %q because %s", pattern, err) |
| 240 | } |
| 241 | |
| 242 | found := false |
| 243 | for _, err := range errs { |
| 244 | if matcher.FindStringIndex(err.Error()) != nil { |
| 245 | found = true |
| 246 | break |
| 247 | } |
| 248 | } |
| 249 | if !found { |
| 250 | t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs)) |
| 251 | for i, err := range errs { |
| 252 | t.Errorf("errs[%d] = %s", i, err) |
| 253 | } |
| 254 | } |
| 255 | } |