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" |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 23 | "sync" |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 24 | "testing" |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 25 | |
| 26 | "github.com/google/blueprint" |
Paul Duffin | 25259e9 | 2021-03-07 15:45:56 +0000 | [diff] [blame^] | 27 | "github.com/google/blueprint/proptools" |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 30 | func NewTestContext(config Config) *TestContext { |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 31 | namespaceExportFilter := func(namespace *Namespace) bool { |
| 32 | return true |
| 33 | } |
Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 34 | |
| 35 | nameResolver := NewNameResolver(namespaceExportFilter) |
| 36 | ctx := &TestContext{ |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 37 | Context: &Context{blueprint.NewContext(), config}, |
Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 38 | NameResolver: nameResolver, |
| 39 | } |
| 40 | |
| 41 | ctx.SetNameInterface(nameResolver) |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 42 | |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 43 | ctx.postDeps = append(ctx.postDeps, registerPathDepsMutator) |
| 44 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 45 | ctx.SetFs(ctx.config.fs) |
| 46 | if ctx.config.mockBpList != "" { |
| 47 | ctx.SetModuleListFile(ctx.config.mockBpList) |
| 48 | } |
| 49 | |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 50 | return ctx |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Paul Duffin | a560d5a | 2021-02-28 01:38:51 +0000 | [diff] [blame] | 53 | var PrepareForTestWithArchMutator = GroupFixturePreparers( |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 54 | // Configure architecture targets in the fixture config. |
| 55 | FixtureModifyConfig(modifyTestConfigToSupportArchMutator), |
| 56 | |
| 57 | // Add the arch mutator to the context. |
| 58 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 59 | ctx.PreDepsMutators(registerArchMutator) |
| 60 | }), |
| 61 | ) |
| 62 | |
| 63 | var PrepareForTestWithDefaults = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 64 | ctx.PreArchMutators(RegisterDefaultsPreArchMutators) |
| 65 | }) |
| 66 | |
| 67 | var PrepareForTestWithComponentsMutator = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 68 | ctx.PreArchMutators(RegisterComponentsMutator) |
| 69 | }) |
| 70 | |
| 71 | var PrepareForTestWithPrebuilts = FixtureRegisterWithContext(RegisterPrebuiltMutators) |
| 72 | |
| 73 | var PrepareForTestWithOverrides = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 74 | ctx.PostDepsMutators(RegisterOverridePostDepsMutators) |
| 75 | }) |
| 76 | |
Paul Duffin | ec3292b | 2021-03-09 01:01:31 +0000 | [diff] [blame] | 77 | // Test fixture preparer that will register most java build components. |
| 78 | // |
| 79 | // Singletons and mutators should only be added here if they are needed for a majority of java |
| 80 | // module types, otherwise they should be added under a separate preparer to allow them to be |
| 81 | // selected only when needed to reduce test execution time. |
| 82 | // |
| 83 | // Module types do not have much of an overhead unless they are used so this should include as many |
| 84 | // module types as possible. The exceptions are those module types that require mutators and/or |
| 85 | // singletons in order to function in which case they should be kept together in a separate |
| 86 | // preparer. |
| 87 | // |
| 88 | // The mutators in this group were chosen because they are needed by the vast majority of tests. |
| 89 | var PrepareForTestWithAndroidBuildComponents = GroupFixturePreparers( |
Paul Duffin | 530483c | 2021-03-07 13:20:38 +0000 | [diff] [blame] | 90 | // Sorted alphabetically as the actual order does not matter as tests automatically enforce the |
| 91 | // correct order. |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 92 | PrepareForTestWithArchMutator, |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 93 | PrepareForTestWithComponentsMutator, |
Paul Duffin | 530483c | 2021-03-07 13:20:38 +0000 | [diff] [blame] | 94 | PrepareForTestWithDefaults, |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 95 | PrepareForTestWithFilegroup, |
Paul Duffin | 530483c | 2021-03-07 13:20:38 +0000 | [diff] [blame] | 96 | PrepareForTestWithOverrides, |
| 97 | PrepareForTestWithPackageModule, |
| 98 | PrepareForTestWithPrebuilts, |
| 99 | PrepareForTestWithVisibility, |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 100 | ) |
| 101 | |
Paul Duffin | ec3292b | 2021-03-09 01:01:31 +0000 | [diff] [blame] | 102 | // Prepares an integration test with all build components from the android package. |
| 103 | // |
| 104 | // This should only be used by tests that want to run with as much of the build enabled as possible. |
| 105 | var PrepareForIntegrationTestWithAndroid = GroupFixturePreparers( |
| 106 | PrepareForTestWithAndroidBuildComponents, |
| 107 | ) |
| 108 | |
Paul Duffin | 25259e9 | 2021-03-07 15:45:56 +0000 | [diff] [blame^] | 109 | // Prepares a test that may be missing dependencies by setting allow_missing_dependencies to |
| 110 | // true. |
| 111 | var PrepareForTestWithAllowMissingDependencies = GroupFixturePreparers( |
| 112 | FixtureModifyProductVariables(func(variables FixtureProductVariables) { |
| 113 | variables.Allow_missing_dependencies = proptools.BoolPtr(true) |
| 114 | }), |
| 115 | FixtureModifyContext(func(ctx *TestContext) { |
| 116 | ctx.SetAllowMissingDependencies(true) |
| 117 | }), |
| 118 | ) |
| 119 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 120 | func NewTestArchContext(config Config) *TestContext { |
| 121 | ctx := NewTestContext(config) |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 122 | ctx.preDeps = append(ctx.preDeps, registerArchMutator) |
| 123 | return ctx |
| 124 | } |
| 125 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 126 | type TestContext struct { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 127 | *Context |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 128 | preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc |
| 129 | bp2buildPreArch, bp2buildDeps, bp2buildMutators []RegisterMutatorFunc |
| 130 | NameResolver *NameResolver |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 131 | |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 132 | // The list of pre-singletons and singletons registered for the test. |
| 133 | preSingletons, singletons sortableComponents |
| 134 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 135 | // The order in which the pre-singletons, mutators and singletons will be run in this test |
| 136 | // context; for debugging. |
| 137 | preSingletonOrder, mutatorOrder, singletonOrder []string |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) { |
| 141 | ctx.preArch = append(ctx.preArch, f) |
| 142 | } |
| 143 | |
Paul Duffin | a80ef84 | 2020-01-14 12:09:36 +0000 | [diff] [blame] | 144 | func (ctx *TestContext) HardCodedPreArchMutators(f RegisterMutatorFunc) { |
| 145 | // Register mutator function as normal for testing. |
| 146 | ctx.PreArchMutators(f) |
| 147 | } |
| 148 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 149 | func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) { |
| 150 | ctx.preDeps = append(ctx.preDeps, f) |
| 151 | } |
| 152 | |
| 153 | func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) { |
| 154 | ctx.postDeps = append(ctx.postDeps, f) |
| 155 | } |
| 156 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 157 | func (ctx *TestContext) FinalDepsMutators(f RegisterMutatorFunc) { |
| 158 | ctx.finalDeps = append(ctx.finalDeps, f) |
| 159 | } |
| 160 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 161 | // RegisterBp2BuildMutator registers a BazelTargetModule mutator for converting a module |
| 162 | // type to the equivalent Bazel target. |
| 163 | func (ctx *TestContext) RegisterBp2BuildMutator(moduleType string, m func(TopDownMutatorContext)) { |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 164 | f := func(ctx RegisterMutatorsContext) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 165 | ctx.TopDown(moduleType, m) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 166 | } |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 167 | ctx.bp2buildMutators = append(ctx.bp2buildMutators, f) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 168 | } |
| 169 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 170 | // PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules |
| 171 | // into Bazel BUILD targets that should run prior to deps and conversion. |
| 172 | func (ctx *TestContext) PreArchBp2BuildMutators(f RegisterMutatorFunc) { |
| 173 | ctx.bp2buildPreArch = append(ctx.bp2buildPreArch, f) |
| 174 | } |
| 175 | |
| 176 | // DepsBp2BuildMutators adds mutators to be register for converting Android Blueprint modules into |
| 177 | // Bazel BUILD targets that should run prior to conversion to resolve dependencies. |
| 178 | func (ctx *TestContext) DepsBp2BuildMutators(f RegisterMutatorFunc) { |
| 179 | ctx.bp2buildDeps = append(ctx.bp2buildDeps, f) |
| 180 | } |
| 181 | |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 182 | // registeredComponentOrder defines the order in which a sortableComponent type is registered at |
| 183 | // runtime and provides support for reordering the components registered for a test in the same |
| 184 | // way. |
| 185 | type registeredComponentOrder struct { |
| 186 | // The name of the component type, used for error messages. |
| 187 | componentType string |
| 188 | |
| 189 | // The names of the registered components in the order in which they were registered. |
| 190 | namesInOrder []string |
| 191 | |
| 192 | // Maps from the component name to its position in the runtime ordering. |
| 193 | namesToIndex map[string]int |
| 194 | |
| 195 | // A function that defines the order between two named components that can be used to sort a slice |
| 196 | // of component names into the same order as they appear in namesInOrder. |
| 197 | less func(string, string) bool |
| 198 | } |
| 199 | |
| 200 | // registeredComponentOrderFromExistingOrder takes an existing slice of sortableComponents and |
| 201 | // creates a registeredComponentOrder that contains a less function that can be used to sort a |
| 202 | // subset of that list of names so it is in the same order as the original sortableComponents. |
| 203 | func registeredComponentOrderFromExistingOrder(componentType string, existingOrder sortableComponents) registeredComponentOrder { |
| 204 | // Only the names from the existing order are needed for this so create a list of component names |
| 205 | // in the correct order. |
| 206 | namesInOrder := componentsToNames(existingOrder) |
| 207 | |
| 208 | // Populate the map from name to position in the list. |
| 209 | nameToIndex := make(map[string]int) |
| 210 | for i, n := range namesInOrder { |
| 211 | nameToIndex[n] = i |
| 212 | } |
| 213 | |
| 214 | // A function to use to map from a name to an index in the original order. |
| 215 | indexOf := func(name string) int { |
| 216 | index, ok := nameToIndex[name] |
| 217 | if !ok { |
| 218 | // Should never happen as tests that use components that are not known at runtime do not sort |
| 219 | // so should never use this function. |
| 220 | panic(fmt.Errorf("internal error: unknown %s %q should be one of %s", componentType, name, strings.Join(namesInOrder, ", "))) |
| 221 | } |
| 222 | return index |
| 223 | } |
| 224 | |
| 225 | // The less function. |
| 226 | less := func(n1, n2 string) bool { |
| 227 | i1 := indexOf(n1) |
| 228 | i2 := indexOf(n2) |
| 229 | return i1 < i2 |
| 230 | } |
| 231 | |
| 232 | return registeredComponentOrder{ |
| 233 | componentType: componentType, |
| 234 | namesInOrder: namesInOrder, |
| 235 | namesToIndex: nameToIndex, |
| 236 | less: less, |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // componentsToNames maps from the slice of components to a slice of their names. |
| 241 | func componentsToNames(components sortableComponents) []string { |
| 242 | names := make([]string, len(components)) |
| 243 | for i, c := range components { |
| 244 | names[i] = c.componentName() |
| 245 | } |
| 246 | return names |
| 247 | } |
| 248 | |
| 249 | // enforceOrdering enforces the supplied components are in the same order as is defined in this |
| 250 | // object. |
| 251 | // |
| 252 | // If the supplied components contains any components that are not registered at runtime, i.e. test |
| 253 | // specific components, then it is impossible to sort them into an order that both matches the |
| 254 | // runtime and also preserves the implicit ordering defined in the test. In that case it will not |
| 255 | // sort the components, instead it will just check that the components are in the correct order. |
| 256 | // |
| 257 | // Otherwise, this will sort the supplied components in place. |
| 258 | func (o *registeredComponentOrder) enforceOrdering(components sortableComponents) { |
| 259 | // Check to see if the list of components contains any components that are |
| 260 | // not registered at runtime. |
| 261 | var unknownComponents []string |
| 262 | testOrder := componentsToNames(components) |
| 263 | for _, name := range testOrder { |
| 264 | if _, ok := o.namesToIndex[name]; !ok { |
| 265 | unknownComponents = append(unknownComponents, name) |
| 266 | break |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // If the slice contains some unknown components then it is not possible to |
| 271 | // sort them into an order that matches the runtime while also preserving the |
| 272 | // order expected from the test, so in that case don't sort just check that |
| 273 | // the order of the known mutators does match. |
| 274 | if len(unknownComponents) > 0 { |
| 275 | // Check order. |
| 276 | o.checkTestOrder(testOrder, unknownComponents) |
| 277 | } else { |
| 278 | // Sort the components. |
| 279 | sort.Slice(components, func(i, j int) bool { |
| 280 | n1 := components[i].componentName() |
| 281 | n2 := components[j].componentName() |
| 282 | return o.less(n1, n2) |
| 283 | }) |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | // checkTestOrder checks that the supplied testOrder matches the one defined by this object, |
| 288 | // panicking if it does not. |
| 289 | func (o *registeredComponentOrder) checkTestOrder(testOrder []string, unknownComponents []string) { |
| 290 | lastMatchingTest := -1 |
| 291 | matchCount := 0 |
| 292 | // Take a copy of the runtime order as it is modified during the comparison. |
| 293 | runtimeOrder := append([]string(nil), o.namesInOrder...) |
| 294 | componentType := o.componentType |
| 295 | for i, j := 0, 0; i < len(testOrder) && j < len(runtimeOrder); { |
| 296 | test := testOrder[i] |
| 297 | runtime := runtimeOrder[j] |
| 298 | |
| 299 | if test == runtime { |
| 300 | testOrder[i] = test + fmt.Sprintf(" <-- matched with runtime %s %d", componentType, j) |
| 301 | runtimeOrder[j] = runtime + fmt.Sprintf(" <-- matched with test %s %d", componentType, i) |
| 302 | lastMatchingTest = i |
| 303 | i += 1 |
| 304 | j += 1 |
| 305 | matchCount += 1 |
| 306 | } else if _, ok := o.namesToIndex[test]; !ok { |
| 307 | // The test component is not registered globally so assume it is the correct place, treat it |
| 308 | // as having matched and skip it. |
| 309 | i += 1 |
| 310 | matchCount += 1 |
| 311 | } else { |
| 312 | // Assume that the test list is in the same order as the runtime list but the runtime list |
| 313 | // contains some components that are not present in the tests. So, skip the runtime component |
| 314 | // to try and find the next one that matches the current test component. |
| 315 | j += 1 |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // If every item in the test order was either test specific or matched one in the runtime then |
| 320 | // it is in the correct order. Otherwise, it was not so fail. |
| 321 | if matchCount != len(testOrder) { |
| 322 | // The test component names were not all matched with a runtime component name so there must |
| 323 | // either be a component present in the test that is not present in the runtime or they must be |
| 324 | // in the wrong order. |
| 325 | testOrder[lastMatchingTest+1] = testOrder[lastMatchingTest+1] + " <--- unmatched" |
| 326 | panic(fmt.Errorf("the tests uses test specific components %q and so cannot be automatically sorted."+ |
| 327 | " Unfortunately it uses %s components in the wrong order.\n"+ |
| 328 | "test order:\n %s\n"+ |
| 329 | "runtime order\n %s\n", |
| 330 | SortedUniqueStrings(unknownComponents), |
| 331 | componentType, |
| 332 | strings.Join(testOrder, "\n "), |
| 333 | strings.Join(runtimeOrder, "\n "))) |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // registrationSorter encapsulates the information needed to ensure that the test mutators are |
| 338 | // registered, and thereby executed, in the same order as they are at runtime. |
| 339 | // |
| 340 | // It MUST be populated lazily AFTER all package initialization has been done otherwise it will |
| 341 | // only define the order for a subset of all the registered build components that are available for |
| 342 | // the packages being tested. |
| 343 | // |
| 344 | // e.g if this is initialized during say the cc package initialization then any tests run in the |
| 345 | // java package will not sort build components registered by the java package's init() functions. |
| 346 | type registrationSorter struct { |
| 347 | // Used to ensure that this is only created once. |
| 348 | once sync.Once |
| 349 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 350 | // The order of pre-singletons |
| 351 | preSingletonOrder registeredComponentOrder |
| 352 | |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 353 | // The order of mutators |
| 354 | mutatorOrder registeredComponentOrder |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 355 | |
| 356 | // The order of singletons |
| 357 | singletonOrder registeredComponentOrder |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // populate initializes this structure from globally registered build components. |
| 361 | // |
| 362 | // Only the first call has any effect. |
| 363 | func (s *registrationSorter) populate() { |
| 364 | s.once.Do(func() { |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 365 | // Create an ordering from the globally registered pre-singletons. |
| 366 | s.preSingletonOrder = registeredComponentOrderFromExistingOrder("pre-singleton", preSingletons) |
| 367 | |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 368 | // Created an ordering from the globally registered mutators. |
| 369 | globallyRegisteredMutators := collateGloballyRegisteredMutators() |
| 370 | s.mutatorOrder = registeredComponentOrderFromExistingOrder("mutator", globallyRegisteredMutators) |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 371 | |
| 372 | // Create an ordering from the globally registered singletons. |
| 373 | globallyRegisteredSingletons := collateGloballyRegisteredSingletons() |
| 374 | s.singletonOrder = registeredComponentOrderFromExistingOrder("singleton", globallyRegisteredSingletons) |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 375 | }) |
| 376 | } |
| 377 | |
| 378 | // Provides support for enforcing the same order in which build components are registered globally |
| 379 | // to the order in which they are registered during tests. |
| 380 | // |
| 381 | // MUST only be accessed via the globallyRegisteredComponentsOrder func. |
| 382 | var globalRegistrationSorter registrationSorter |
| 383 | |
| 384 | // globallyRegisteredComponentsOrder returns the globalRegistrationSorter after ensuring it is |
| 385 | // correctly populated. |
| 386 | func globallyRegisteredComponentsOrder() *registrationSorter { |
| 387 | globalRegistrationSorter.populate() |
| 388 | return &globalRegistrationSorter |
| 389 | } |
| 390 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 391 | func (ctx *TestContext) Register() { |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 392 | globalOrder := globallyRegisteredComponentsOrder() |
| 393 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 394 | // Ensure that the pre-singletons used in the test are in the same order as they are used at |
| 395 | // runtime. |
| 396 | globalOrder.preSingletonOrder.enforceOrdering(ctx.preSingletons) |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 397 | ctx.preSingletons.registerAll(ctx.Context) |
| 398 | |
Paul Duffin | c05b034 | 2021-03-06 13:28:13 +0000 | [diff] [blame] | 399 | mutators := collateRegisteredMutators(ctx.preArch, ctx.preDeps, ctx.postDeps, ctx.finalDeps) |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 400 | // Ensure that the mutators used in the test are in the same order as they are used at runtime. |
| 401 | globalOrder.mutatorOrder.enforceOrdering(mutators) |
Paul Duffin | c05b034 | 2021-03-06 13:28:13 +0000 | [diff] [blame] | 402 | mutators.registerAll(ctx.Context) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 403 | |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 404 | // Register the env singleton with this context before sorting. |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 405 | ctx.RegisterSingletonType("env", EnvSingleton) |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 406 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 407 | // Ensure that the singletons used in the test are in the same order as they are used at runtime. |
| 408 | globalOrder.singletonOrder.enforceOrdering(ctx.singletons) |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 409 | ctx.singletons.registerAll(ctx.Context) |
| 410 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 411 | // Save the sorted components order away to make them easy to access while debugging. |
Paul Duffin | f5de668 | 2021-03-08 23:42:10 +0000 | [diff] [blame] | 412 | ctx.preSingletonOrder = componentsToNames(preSingletons) |
| 413 | ctx.mutatorOrder = componentsToNames(mutators) |
| 414 | ctx.singletonOrder = componentsToNames(singletons) |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 417 | // RegisterForBazelConversion prepares a test context for bp2build conversion. |
| 418 | func (ctx *TestContext) RegisterForBazelConversion() { |
Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 419 | RegisterMutatorsForBazelConversion(ctx.Context, ctx.bp2buildPreArch, ctx.bp2buildDeps, ctx.bp2buildMutators) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 420 | } |
| 421 | |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 422 | func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) { |
| 423 | // This function adapts the old style ParseFileList calls that are spread throughout the tests |
| 424 | // to the new style that takes a config. |
| 425 | return ctx.Context.ParseFileList(rootDir, filePaths, ctx.config) |
| 426 | } |
| 427 | |
| 428 | func (ctx *TestContext) ParseBlueprintsFiles(rootDir string) (deps []string, errs []error) { |
| 429 | // This function adapts the old style ParseBlueprintsFiles calls that are spread throughout the |
| 430 | // tests to the new style that takes a config. |
| 431 | return ctx.Context.ParseBlueprintsFiles(rootDir, ctx.config) |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) { |
| 435 | ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory)) |
| 436 | } |
| 437 | |
Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 438 | func (ctx *TestContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) { |
| 439 | s, m := SingletonModuleFactoryAdaptor(name, factory) |
| 440 | ctx.RegisterSingletonType(name, s) |
| 441 | ctx.RegisterModuleType(name, m) |
| 442 | } |
| 443 | |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 444 | func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) { |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 445 | ctx.singletons = append(ctx.singletons, newSingleton(name, factory)) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Paul Duffin | eafc16b | 2021-02-24 01:43:18 +0000 | [diff] [blame] | 448 | func (ctx *TestContext) RegisterPreSingletonType(name string, factory SingletonFactory) { |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 449 | ctx.preSingletons = append(ctx.preSingletons, newPreSingleton(name, factory)) |
Paul Duffin | eafc16b | 2021-02-24 01:43:18 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 452 | func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule { |
| 453 | var module Module |
| 454 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 455 | if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant { |
| 456 | module = m.(Module) |
| 457 | } |
| 458 | }) |
| 459 | |
| 460 | if module == nil { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 461 | // find all the modules that do exist |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 462 | var allModuleNames []string |
| 463 | var allVariants []string |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 464 | ctx.VisitAllModules(func(m blueprint.Module) { |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 465 | allModuleNames = append(allModuleNames, ctx.ModuleName(m)) |
| 466 | if ctx.ModuleName(m) == name { |
| 467 | allVariants = append(allVariants, ctx.ModuleSubDir(m)) |
| 468 | } |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 469 | }) |
Martin Stjernholm | 4c02124 | 2020-05-13 01:13:50 +0100 | [diff] [blame] | 470 | sort.Strings(allModuleNames) |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 471 | sort.Strings(allVariants) |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 472 | |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 473 | if len(allVariants) == 0 { |
| 474 | panic(fmt.Errorf("failed to find module %q. All modules:\n %s", |
| 475 | name, strings.Join(allModuleNames, "\n "))) |
| 476 | } else { |
| 477 | panic(fmt.Errorf("failed to find module %q variant %q. All variants:\n %s", |
| 478 | name, variant, strings.Join(allVariants, "\n "))) |
| 479 | } |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | return TestingModule{module} |
| 483 | } |
| 484 | |
Jiyong Park | 37b2520 | 2018-07-11 10:49:27 +0900 | [diff] [blame] | 485 | func (ctx *TestContext) ModuleVariantsForTests(name string) []string { |
| 486 | var variants []string |
| 487 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 488 | if ctx.ModuleName(m) == name { |
| 489 | variants = append(variants, ctx.ModuleSubDir(m)) |
| 490 | } |
| 491 | }) |
| 492 | return variants |
| 493 | } |
| 494 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 495 | // SingletonForTests returns a TestingSingleton for the singleton registered with the given name. |
| 496 | func (ctx *TestContext) SingletonForTests(name string) TestingSingleton { |
| 497 | allSingletonNames := []string{} |
| 498 | for _, s := range ctx.Singletons() { |
| 499 | n := ctx.SingletonName(s) |
| 500 | if n == name { |
| 501 | return TestingSingleton{ |
| 502 | singleton: s.(*singletonAdaptor).Singleton, |
| 503 | provider: s.(testBuildProvider), |
| 504 | } |
| 505 | } |
| 506 | allSingletonNames = append(allSingletonNames, n) |
| 507 | } |
| 508 | |
| 509 | panic(fmt.Errorf("failed to find singleton %q."+ |
| 510 | "\nall singletons: %v", name, allSingletonNames)) |
| 511 | } |
| 512 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 513 | func (ctx *TestContext) Config() Config { |
| 514 | return ctx.config |
| 515 | } |
| 516 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 517 | type testBuildProvider interface { |
| 518 | BuildParamsForTests() []BuildParams |
| 519 | RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams |
| 520 | } |
| 521 | |
| 522 | type TestingBuildParams struct { |
| 523 | BuildParams |
| 524 | RuleParams blueprint.RuleParams |
| 525 | } |
| 526 | |
| 527 | func newTestingBuildParams(provider testBuildProvider, bparams BuildParams) TestingBuildParams { |
| 528 | return TestingBuildParams{ |
| 529 | BuildParams: bparams, |
| 530 | RuleParams: provider.RuleParamsForTests()[bparams.Rule], |
| 531 | } |
| 532 | } |
| 533 | |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 534 | func maybeBuildParamsFromRule(provider testBuildProvider, rule string) (TestingBuildParams, []string) { |
| 535 | var searchedRules []string |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 536 | for _, p := range provider.BuildParamsForTests() { |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 537 | searchedRules = append(searchedRules, p.Rule.String()) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 538 | if strings.Contains(p.Rule.String(), rule) { |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 539 | return newTestingBuildParams(provider, p), searchedRules |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 540 | } |
| 541 | } |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 542 | return TestingBuildParams{}, searchedRules |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | func buildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams { |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 546 | p, searchRules := maybeBuildParamsFromRule(provider, rule) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 547 | if p.Rule == nil { |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 548 | panic(fmt.Errorf("couldn't find rule %q.\nall rules: %v", rule, searchRules)) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 549 | } |
| 550 | return p |
| 551 | } |
| 552 | |
| 553 | func maybeBuildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams { |
| 554 | for _, p := range provider.BuildParamsForTests() { |
Colin Cross | b88b3c5 | 2019-06-10 15:15:17 -0700 | [diff] [blame] | 555 | if strings.Contains(p.Description, desc) { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 556 | return newTestingBuildParams(provider, p) |
| 557 | } |
| 558 | } |
| 559 | return TestingBuildParams{} |
| 560 | } |
| 561 | |
| 562 | func buildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams { |
| 563 | p := maybeBuildParamsFromDescription(provider, desc) |
| 564 | if p.Rule == nil { |
| 565 | panic(fmt.Errorf("couldn't find description %q", desc)) |
| 566 | } |
| 567 | return p |
| 568 | } |
| 569 | |
| 570 | func maybeBuildParamsFromOutput(provider testBuildProvider, file string) (TestingBuildParams, []string) { |
| 571 | var searchedOutputs []string |
| 572 | for _, p := range provider.BuildParamsForTests() { |
| 573 | outputs := append(WritablePaths(nil), p.Outputs...) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 574 | outputs = append(outputs, p.ImplicitOutputs...) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 575 | if p.Output != nil { |
| 576 | outputs = append(outputs, p.Output) |
| 577 | } |
| 578 | for _, f := range outputs { |
| 579 | if f.String() == file || f.Rel() == file { |
| 580 | return newTestingBuildParams(provider, p), nil |
| 581 | } |
| 582 | searchedOutputs = append(searchedOutputs, f.Rel()) |
| 583 | } |
| 584 | } |
| 585 | return TestingBuildParams{}, searchedOutputs |
| 586 | } |
| 587 | |
| 588 | func buildParamsFromOutput(provider testBuildProvider, file string) TestingBuildParams { |
| 589 | p, searchedOutputs := maybeBuildParamsFromOutput(provider, file) |
| 590 | if p.Rule == nil { |
| 591 | panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v", |
| 592 | file, searchedOutputs)) |
| 593 | } |
| 594 | return p |
| 595 | } |
| 596 | |
| 597 | func allOutputs(provider testBuildProvider) []string { |
| 598 | var outputFullPaths []string |
| 599 | for _, p := range provider.BuildParamsForTests() { |
| 600 | outputs := append(WritablePaths(nil), p.Outputs...) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 601 | outputs = append(outputs, p.ImplicitOutputs...) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 602 | if p.Output != nil { |
| 603 | outputs = append(outputs, p.Output) |
| 604 | } |
| 605 | outputFullPaths = append(outputFullPaths, outputs.Strings()...) |
| 606 | } |
| 607 | return outputFullPaths |
| 608 | } |
| 609 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 610 | // TestingModule is wrapper around an android.Module that provides methods to find information about individual |
| 611 | // ctx.Build parameters for verification in tests. |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 612 | type TestingModule struct { |
| 613 | module Module |
| 614 | } |
| 615 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 616 | // Module returns the Module wrapped by the TestingModule. |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 617 | func (m TestingModule) Module() Module { |
| 618 | return m.module |
| 619 | } |
| 620 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 621 | // MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty |
| 622 | // BuildParams if no rule is found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 623 | func (m TestingModule) MaybeRule(rule string) TestingBuildParams { |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 624 | r, _ := maybeBuildParamsFromRule(m.module, rule) |
| 625 | return r |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 628 | // 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] | 629 | func (m TestingModule) Rule(rule string) TestingBuildParams { |
| 630 | return buildParamsFromRule(m.module, rule) |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | // MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty |
| 634 | // BuildParams if no rule is found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 635 | func (m TestingModule) MaybeDescription(desc string) TestingBuildParams { |
| 636 | return maybeBuildParamsFromDescription(m.module, desc) |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 639 | // Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is |
| 640 | // found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 641 | func (m TestingModule) Description(desc string) TestingBuildParams { |
| 642 | return buildParamsFromDescription(m.module, desc) |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 643 | } |
| 644 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 645 | // 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] | 646 | // 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] | 647 | func (m TestingModule) MaybeOutput(file string) TestingBuildParams { |
| 648 | p, _ := maybeBuildParamsFromOutput(m.module, file) |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 649 | return p |
| 650 | } |
| 651 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 652 | // 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] | 653 | // value matches the provided string. Panics if no rule is found. |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 654 | func (m TestingModule) Output(file string) TestingBuildParams { |
| 655 | return buildParamsFromOutput(m.module, file) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 656 | } |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 657 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 658 | // AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms. |
| 659 | func (m TestingModule) AllOutputs() []string { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 660 | return allOutputs(m.module) |
| 661 | } |
| 662 | |
| 663 | // TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual |
| 664 | // ctx.Build parameters for verification in tests. |
| 665 | type TestingSingleton struct { |
| 666 | singleton Singleton |
| 667 | provider testBuildProvider |
| 668 | } |
| 669 | |
| 670 | // Singleton returns the Singleton wrapped by the TestingSingleton. |
| 671 | func (s TestingSingleton) Singleton() Singleton { |
| 672 | return s.singleton |
| 673 | } |
| 674 | |
| 675 | // MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty |
| 676 | // BuildParams if no rule is found. |
| 677 | func (s TestingSingleton) MaybeRule(rule string) TestingBuildParams { |
ThiƩbaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 678 | r, _ := maybeBuildParamsFromRule(s.provider, rule) |
| 679 | return r |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | // Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found. |
| 683 | func (s TestingSingleton) Rule(rule string) TestingBuildParams { |
| 684 | return buildParamsFromRule(s.provider, rule) |
| 685 | } |
| 686 | |
| 687 | // MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty |
| 688 | // BuildParams if no rule is found. |
| 689 | func (s TestingSingleton) MaybeDescription(desc string) TestingBuildParams { |
| 690 | return maybeBuildParamsFromDescription(s.provider, desc) |
| 691 | } |
| 692 | |
| 693 | // Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is |
| 694 | // found. |
| 695 | func (s TestingSingleton) Description(desc string) TestingBuildParams { |
| 696 | return buildParamsFromDescription(s.provider, desc) |
| 697 | } |
| 698 | |
| 699 | // MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel() |
| 700 | // value matches the provided string. Returns an empty BuildParams if no rule is found. |
| 701 | func (s TestingSingleton) MaybeOutput(file string) TestingBuildParams { |
| 702 | p, _ := maybeBuildParamsFromOutput(s.provider, file) |
| 703 | return p |
| 704 | } |
| 705 | |
| 706 | // Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel() |
| 707 | // value matches the provided string. Panics if no rule is found. |
| 708 | func (s TestingSingleton) Output(file string) TestingBuildParams { |
| 709 | return buildParamsFromOutput(s.provider, file) |
| 710 | } |
| 711 | |
| 712 | // AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms. |
| 713 | func (s TestingSingleton) AllOutputs() []string { |
| 714 | return allOutputs(s.provider) |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 715 | } |
| 716 | |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 717 | func FailIfErrored(t *testing.T, errs []error) { |
| 718 | t.Helper() |
| 719 | if len(errs) > 0 { |
| 720 | for _, err := range errs { |
| 721 | t.Error(err) |
| 722 | } |
| 723 | t.FailNow() |
| 724 | } |
| 725 | } |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 726 | |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 727 | // Fail if no errors that matched the regular expression were found. |
| 728 | // |
| 729 | // Returns true if a matching error was found, false otherwise. |
| 730 | func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) bool { |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 731 | t.Helper() |
| 732 | |
| 733 | matcher, err := regexp.Compile(pattern) |
| 734 | if err != nil { |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 735 | t.Fatalf("failed to compile regular expression %q because %s", pattern, err) |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | found := false |
| 739 | for _, err := range errs { |
| 740 | if matcher.FindStringIndex(err.Error()) != nil { |
| 741 | found = true |
| 742 | break |
| 743 | } |
| 744 | } |
| 745 | if !found { |
| 746 | t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs)) |
| 747 | for i, err := range errs { |
Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 748 | t.Errorf("errs[%d] = %q", i, err) |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 749 | } |
| 750 | } |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 751 | |
| 752 | return found |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 753 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 754 | |
Paul Duffin | 91e3819 | 2019-08-05 15:07:57 +0100 | [diff] [blame] | 755 | func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) { |
| 756 | t.Helper() |
| 757 | |
| 758 | if expectedErrorPatterns == nil { |
| 759 | FailIfErrored(t, errs) |
| 760 | } else { |
| 761 | for _, expectedError := range expectedErrorPatterns { |
| 762 | FailIfNoMatchingErrors(t, expectedError, errs) |
| 763 | } |
| 764 | if len(errs) > len(expectedErrorPatterns) { |
| 765 | t.Errorf("additional errors found, expected %d, found %d", |
| 766 | len(expectedErrorPatterns), len(errs)) |
| 767 | for i, expectedError := range expectedErrorPatterns { |
| 768 | t.Errorf("expectedErrors[%d] = %s", i, expectedError) |
| 769 | } |
| 770 | for i, err := range errs { |
| 771 | t.Errorf("errs[%d] = %s", i, err) |
| 772 | } |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 773 | t.FailNow() |
Paul Duffin | 91e3819 | 2019-08-05 15:07:57 +0100 | [diff] [blame] | 774 | } |
| 775 | } |
Paul Duffin | 91e3819 | 2019-08-05 15:07:57 +0100 | [diff] [blame] | 776 | } |
| 777 | |
Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 778 | func SetKatiEnabledForTests(config Config) { |
| 779 | config.katiEnabled = true |
Paul Duffin | 8c3fec4 | 2020-03-04 20:15:08 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 782 | func AndroidMkEntriesForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) []AndroidMkEntries { |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 783 | var p AndroidMkEntriesProvider |
| 784 | var ok bool |
| 785 | if p, ok = mod.(AndroidMkEntriesProvider); !ok { |
Roland Levillain | dfe75b3 | 2019-07-23 16:53:32 +0100 | [diff] [blame] | 786 | t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name()) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 787 | } |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 788 | |
| 789 | entriesList := p.AndroidMkEntries() |
| 790 | for i, _ := range entriesList { |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 791 | entriesList[i].fillInEntries(ctx, mod) |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 792 | } |
| 793 | return entriesList |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 794 | } |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 795 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 796 | func AndroidMkDataForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) AndroidMkData { |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 797 | var p AndroidMkDataProvider |
| 798 | var ok bool |
| 799 | if p, ok = mod.(AndroidMkDataProvider); !ok { |
Roland Levillain | dfe75b3 | 2019-07-23 16:53:32 +0100 | [diff] [blame] | 800 | t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name()) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 801 | } |
| 802 | data := p.AndroidMk() |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 803 | data.fillInData(ctx, mod) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 804 | return data |
| 805 | } |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 806 | |
| 807 | // Normalize the path for testing. |
| 808 | // |
| 809 | // If the path is relative to the build directory then return the relative path |
| 810 | // to avoid tests having to deal with the dynamically generated build directory. |
| 811 | // |
| 812 | // Otherwise, return the supplied path as it is almost certainly a source path |
| 813 | // that is relative to the root of the source tree. |
| 814 | // |
| 815 | // The build and source paths should be distinguishable based on their contents. |
| 816 | func NormalizePathForTesting(path Path) string { |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 817 | if path == nil { |
| 818 | return "<nil path>" |
| 819 | } |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 820 | p := path.String() |
| 821 | if w, ok := path.(WritablePath); ok { |
| 822 | rel, err := filepath.Rel(w.buildDir(), p) |
| 823 | if err != nil { |
| 824 | panic(err) |
| 825 | } |
| 826 | return rel |
| 827 | } |
| 828 | return p |
| 829 | } |
| 830 | |
| 831 | func NormalizePathsForTesting(paths Paths) []string { |
| 832 | var result []string |
| 833 | for _, path := range paths { |
| 834 | relative := NormalizePathForTesting(path) |
| 835 | result = append(result, relative) |
| 836 | } |
| 837 | return result |
| 838 | } |