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{ |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 34 | Context: &Context{blueprint.NewContext()}, |
Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 35 | NameResolver: nameResolver, |
| 36 | } |
| 37 | |
| 38 | ctx.SetNameInterface(nameResolver) |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 39 | |
Colin Cross | f8b860a | 2019-04-16 14:43:28 -0700 | [diff] [blame] | 40 | ctx.preArch = append(ctx.preArch, registerLoadHookMutator) |
| 41 | |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 42 | ctx.postDeps = append(ctx.postDeps, registerPathDepsMutator) |
| 43 | |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 44 | return ctx |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 47 | func NewTestArchContext() *TestContext { |
| 48 | ctx := NewTestContext() |
| 49 | ctx.preDeps = append(ctx.preDeps, registerArchMutator) |
| 50 | return ctx |
| 51 | } |
| 52 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 53 | type TestContext struct { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 54 | *Context |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 55 | preArch, preDeps, postDeps []RegisterMutatorFunc |
Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 56 | NameResolver *NameResolver |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) { |
| 60 | ctx.preArch = append(ctx.preArch, f) |
| 61 | } |
| 62 | |
| 63 | func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) { |
| 64 | ctx.preDeps = append(ctx.preDeps, f) |
| 65 | } |
| 66 | |
| 67 | func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) { |
| 68 | ctx.postDeps = append(ctx.postDeps, f) |
| 69 | } |
| 70 | |
| 71 | func (ctx *TestContext) Register() { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 72 | registerMutators(ctx.Context.Context, ctx.preArch, ctx.preDeps, ctx.postDeps) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 73 | |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame^] | 74 | ctx.RegisterSingletonType("env", EnvSingleton) |
| 75 | } |
| 76 | |
| 77 | func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) { |
| 78 | ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory)) |
| 79 | } |
| 80 | |
| 81 | func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) { |
| 82 | ctx.Context.RegisterSingletonType(name, SingletonFactoryAdaptor(factory)) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule { |
| 86 | var module Module |
| 87 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 88 | if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant { |
| 89 | module = m.(Module) |
| 90 | } |
| 91 | }) |
| 92 | |
| 93 | if module == nil { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 94 | // find all the modules that do exist |
| 95 | allModuleNames := []string{} |
| 96 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 97 | allModuleNames = append(allModuleNames, m.(Module).Name()+"("+ctx.ModuleSubDir(m)+")") |
| 98 | }) |
| 99 | |
| 100 | panic(fmt.Errorf("failed to find module %q variant %q."+ |
| 101 | "\nall modules: %v", name, variant, allModuleNames)) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | return TestingModule{module} |
| 105 | } |
| 106 | |
Jiyong Park | 37b2520 | 2018-07-11 10:49:27 +0900 | [diff] [blame] | 107 | func (ctx *TestContext) ModuleVariantsForTests(name string) []string { |
| 108 | var variants []string |
| 109 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 110 | if ctx.ModuleName(m) == name { |
| 111 | variants = append(variants, ctx.ModuleSubDir(m)) |
| 112 | } |
| 113 | }) |
| 114 | return variants |
| 115 | } |
| 116 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 117 | // SingletonForTests returns a TestingSingleton for the singleton registered with the given name. |
| 118 | func (ctx *TestContext) SingletonForTests(name string) TestingSingleton { |
| 119 | allSingletonNames := []string{} |
| 120 | for _, s := range ctx.Singletons() { |
| 121 | n := ctx.SingletonName(s) |
| 122 | if n == name { |
| 123 | return TestingSingleton{ |
| 124 | singleton: s.(*singletonAdaptor).Singleton, |
| 125 | provider: s.(testBuildProvider), |
| 126 | } |
| 127 | } |
| 128 | allSingletonNames = append(allSingletonNames, n) |
| 129 | } |
| 130 | |
| 131 | panic(fmt.Errorf("failed to find singleton %q."+ |
| 132 | "\nall singletons: %v", name, allSingletonNames)) |
| 133 | } |
| 134 | |
Jeff Gaston | dea7e4d | 2017-11-17 13:29:40 -0800 | [diff] [blame] | 135 | // MockFileSystem causes the Context to replace all reads with accesses to the provided map of |
| 136 | // filenames to contents stored as a byte slice. |
| 137 | func (ctx *TestContext) MockFileSystem(files map[string][]byte) { |
| 138 | // no module list file specified; find every file named Blueprints or Android.bp |
| 139 | pathsToParse := []string{} |
| 140 | for candidate := range files { |
| 141 | base := filepath.Base(candidate) |
| 142 | if base == "Blueprints" || base == "Android.bp" { |
| 143 | pathsToParse = append(pathsToParse, candidate) |
| 144 | } |
| 145 | } |
| 146 | if len(pathsToParse) < 1 { |
| 147 | panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", files)) |
| 148 | } |
| 149 | files[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n")) |
| 150 | |
| 151 | ctx.Context.MockFileSystem(files) |
| 152 | } |
| 153 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 154 | type testBuildProvider interface { |
| 155 | BuildParamsForTests() []BuildParams |
| 156 | RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams |
| 157 | } |
| 158 | |
| 159 | type TestingBuildParams struct { |
| 160 | BuildParams |
| 161 | RuleParams blueprint.RuleParams |
| 162 | } |
| 163 | |
| 164 | func newTestingBuildParams(provider testBuildProvider, bparams BuildParams) TestingBuildParams { |
| 165 | return TestingBuildParams{ |
| 166 | BuildParams: bparams, |
| 167 | RuleParams: provider.RuleParamsForTests()[bparams.Rule], |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func maybeBuildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams { |
| 172 | for _, p := range provider.BuildParamsForTests() { |
| 173 | if strings.Contains(p.Rule.String(), rule) { |
| 174 | return newTestingBuildParams(provider, p) |
| 175 | } |
| 176 | } |
| 177 | return TestingBuildParams{} |
| 178 | } |
| 179 | |
| 180 | func buildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams { |
| 181 | p := maybeBuildParamsFromRule(provider, rule) |
| 182 | if p.Rule == nil { |
| 183 | panic(fmt.Errorf("couldn't find rule %q", rule)) |
| 184 | } |
| 185 | return p |
| 186 | } |
| 187 | |
| 188 | func maybeBuildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams { |
| 189 | for _, p := range provider.BuildParamsForTests() { |
Colin Cross | b88b3c5 | 2019-06-10 15:15:17 -0700 | [diff] [blame] | 190 | if strings.Contains(p.Description, desc) { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 191 | return newTestingBuildParams(provider, p) |
| 192 | } |
| 193 | } |
| 194 | return TestingBuildParams{} |
| 195 | } |
| 196 | |
| 197 | func buildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams { |
| 198 | p := maybeBuildParamsFromDescription(provider, desc) |
| 199 | if p.Rule == nil { |
| 200 | panic(fmt.Errorf("couldn't find description %q", desc)) |
| 201 | } |
| 202 | return p |
| 203 | } |
| 204 | |
| 205 | func maybeBuildParamsFromOutput(provider testBuildProvider, file string) (TestingBuildParams, []string) { |
| 206 | var searchedOutputs []string |
| 207 | for _, p := range provider.BuildParamsForTests() { |
| 208 | outputs := append(WritablePaths(nil), p.Outputs...) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 209 | outputs = append(outputs, p.ImplicitOutputs...) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 210 | if p.Output != nil { |
| 211 | outputs = append(outputs, p.Output) |
| 212 | } |
| 213 | for _, f := range outputs { |
| 214 | if f.String() == file || f.Rel() == file { |
| 215 | return newTestingBuildParams(provider, p), nil |
| 216 | } |
| 217 | searchedOutputs = append(searchedOutputs, f.Rel()) |
| 218 | } |
| 219 | } |
| 220 | return TestingBuildParams{}, searchedOutputs |
| 221 | } |
| 222 | |
| 223 | func buildParamsFromOutput(provider testBuildProvider, file string) TestingBuildParams { |
| 224 | p, searchedOutputs := maybeBuildParamsFromOutput(provider, file) |
| 225 | if p.Rule == nil { |
| 226 | panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v", |
| 227 | file, searchedOutputs)) |
| 228 | } |
| 229 | return p |
| 230 | } |
| 231 | |
| 232 | func allOutputs(provider testBuildProvider) []string { |
| 233 | var outputFullPaths []string |
| 234 | for _, p := range provider.BuildParamsForTests() { |
| 235 | outputs := append(WritablePaths(nil), p.Outputs...) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 236 | outputs = append(outputs, p.ImplicitOutputs...) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 237 | if p.Output != nil { |
| 238 | outputs = append(outputs, p.Output) |
| 239 | } |
| 240 | outputFullPaths = append(outputFullPaths, outputs.Strings()...) |
| 241 | } |
| 242 | return outputFullPaths |
| 243 | } |
| 244 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 245 | // TestingModule is wrapper around an android.Module that provides methods to find information about individual |
| 246 | // ctx.Build parameters for verification in tests. |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 247 | type TestingModule struct { |
| 248 | module Module |
| 249 | } |
| 250 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 251 | // Module returns the Module wrapped by the TestingModule. |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 252 | func (m TestingModule) Module() Module { |
| 253 | return m.module |
| 254 | } |
| 255 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 256 | // MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty |
| 257 | // BuildParams if no rule is found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 258 | func (m TestingModule) MaybeRule(rule string) TestingBuildParams { |
| 259 | return maybeBuildParamsFromRule(m.module, rule) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 262 | // Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 263 | func (m TestingModule) Rule(rule string) TestingBuildParams { |
| 264 | return buildParamsFromRule(m.module, rule) |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty |
| 268 | // BuildParams if no rule is found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 269 | func (m TestingModule) MaybeDescription(desc string) TestingBuildParams { |
| 270 | return maybeBuildParamsFromDescription(m.module, desc) |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 273 | // Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is |
| 274 | // found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 275 | func (m TestingModule) Description(desc string) TestingBuildParams { |
| 276 | return buildParamsFromDescription(m.module, desc) |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 279 | // 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] | 280 | // value matches the provided string. Returns an empty BuildParams if no rule is found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 281 | func (m TestingModule) MaybeOutput(file string) TestingBuildParams { |
| 282 | p, _ := maybeBuildParamsFromOutput(m.module, file) |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 283 | return p |
| 284 | } |
| 285 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 286 | // 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] | 287 | // value matches the provided string. Panics if no rule is found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 288 | func (m TestingModule) Output(file string) TestingBuildParams { |
| 289 | return buildParamsFromOutput(m.module, file) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 290 | } |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 291 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 292 | // AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms. |
| 293 | func (m TestingModule) AllOutputs() []string { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 294 | return allOutputs(m.module) |
| 295 | } |
| 296 | |
| 297 | // TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual |
| 298 | // ctx.Build parameters for verification in tests. |
| 299 | type TestingSingleton struct { |
| 300 | singleton Singleton |
| 301 | provider testBuildProvider |
| 302 | } |
| 303 | |
| 304 | // Singleton returns the Singleton wrapped by the TestingSingleton. |
| 305 | func (s TestingSingleton) Singleton() Singleton { |
| 306 | return s.singleton |
| 307 | } |
| 308 | |
| 309 | // MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty |
| 310 | // BuildParams if no rule is found. |
| 311 | func (s TestingSingleton) MaybeRule(rule string) TestingBuildParams { |
| 312 | return maybeBuildParamsFromRule(s.provider, rule) |
| 313 | } |
| 314 | |
| 315 | // Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found. |
| 316 | func (s TestingSingleton) Rule(rule string) TestingBuildParams { |
| 317 | return buildParamsFromRule(s.provider, rule) |
| 318 | } |
| 319 | |
| 320 | // MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty |
| 321 | // BuildParams if no rule is found. |
| 322 | func (s TestingSingleton) MaybeDescription(desc string) TestingBuildParams { |
| 323 | return maybeBuildParamsFromDescription(s.provider, desc) |
| 324 | } |
| 325 | |
| 326 | // Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is |
| 327 | // found. |
| 328 | func (s TestingSingleton) Description(desc string) TestingBuildParams { |
| 329 | return buildParamsFromDescription(s.provider, desc) |
| 330 | } |
| 331 | |
| 332 | // MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel() |
| 333 | // value matches the provided string. Returns an empty BuildParams if no rule is found. |
| 334 | func (s TestingSingleton) MaybeOutput(file string) TestingBuildParams { |
| 335 | p, _ := maybeBuildParamsFromOutput(s.provider, file) |
| 336 | return p |
| 337 | } |
| 338 | |
| 339 | // Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel() |
| 340 | // value matches the provided string. Panics if no rule is found. |
| 341 | func (s TestingSingleton) Output(file string) TestingBuildParams { |
| 342 | return buildParamsFromOutput(s.provider, file) |
| 343 | } |
| 344 | |
| 345 | // AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms. |
| 346 | func (s TestingSingleton) AllOutputs() []string { |
| 347 | return allOutputs(s.provider) |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 350 | func FailIfErrored(t *testing.T, errs []error) { |
| 351 | t.Helper() |
| 352 | if len(errs) > 0 { |
| 353 | for _, err := range errs { |
| 354 | t.Error(err) |
| 355 | } |
| 356 | t.FailNow() |
| 357 | } |
| 358 | } |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 359 | |
| 360 | func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) { |
| 361 | t.Helper() |
| 362 | |
| 363 | matcher, err := regexp.Compile(pattern) |
| 364 | if err != nil { |
| 365 | t.Errorf("failed to compile regular expression %q because %s", pattern, err) |
| 366 | } |
| 367 | |
| 368 | found := false |
| 369 | for _, err := range errs { |
| 370 | if matcher.FindStringIndex(err.Error()) != nil { |
| 371 | found = true |
| 372 | break |
| 373 | } |
| 374 | } |
| 375 | if !found { |
| 376 | t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs)) |
| 377 | for i, err := range errs { |
| 378 | t.Errorf("errs[%d] = %s", i, err) |
| 379 | } |
| 380 | } |
| 381 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 382 | |
Paul Duffin | 91e3819 | 2019-08-05 15:07:57 +0100 | [diff] [blame] | 383 | func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) { |
| 384 | t.Helper() |
| 385 | |
| 386 | if expectedErrorPatterns == nil { |
| 387 | FailIfErrored(t, errs) |
| 388 | } else { |
| 389 | for _, expectedError := range expectedErrorPatterns { |
| 390 | FailIfNoMatchingErrors(t, expectedError, errs) |
| 391 | } |
| 392 | if len(errs) > len(expectedErrorPatterns) { |
| 393 | t.Errorf("additional errors found, expected %d, found %d", |
| 394 | len(expectedErrorPatterns), len(errs)) |
| 395 | for i, expectedError := range expectedErrorPatterns { |
| 396 | t.Errorf("expectedErrors[%d] = %s", i, expectedError) |
| 397 | } |
| 398 | for i, err := range errs { |
| 399 | t.Errorf("errs[%d] = %s", i, err) |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | } |
| 405 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 406 | func AndroidMkEntriesForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) AndroidMkEntries { |
| 407 | var p AndroidMkEntriesProvider |
| 408 | var ok bool |
| 409 | if p, ok = mod.(AndroidMkEntriesProvider); !ok { |
Roland Levillain | dfe75b3 | 2019-07-23 16:53:32 +0100 | [diff] [blame] | 410 | t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name()) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 411 | } |
| 412 | entries := p.AndroidMkEntries() |
| 413 | entries.fillInEntries(config, bpPath, mod) |
| 414 | return entries |
| 415 | } |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 416 | |
| 417 | func AndroidMkDataForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) AndroidMkData { |
| 418 | var p AndroidMkDataProvider |
| 419 | var ok bool |
| 420 | if p, ok = mod.(AndroidMkDataProvider); !ok { |
Roland Levillain | dfe75b3 | 2019-07-23 16:53:32 +0100 | [diff] [blame] | 421 | t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name()) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 422 | } |
| 423 | data := p.AndroidMk() |
| 424 | data.fillInData(config, bpPath, mod) |
| 425 | return data |
| 426 | } |