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