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