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