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