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