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 | |
Paul Duffin | 76e5c8a | 2021-03-20 14:19:46 +0000 | [diff] [blame] | 120 | // Prepares a test that disallows non-existent paths. |
| 121 | var PrepareForTestDisallowNonExistentPaths = FixtureModifyConfig(func(config Config) { |
| 122 | config.TestAllowNonExistentPaths = false |
| 123 | }) |
| 124 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 125 | func NewTestArchContext(config Config) *TestContext { |
| 126 | ctx := NewTestContext(config) |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 127 | ctx.preDeps = append(ctx.preDeps, registerArchMutator) |
| 128 | return ctx |
| 129 | } |
| 130 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 131 | type TestContext struct { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 132 | *Context |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 133 | preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc |
| 134 | bp2buildPreArch, bp2buildDeps, bp2buildMutators []RegisterMutatorFunc |
| 135 | NameResolver *NameResolver |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 136 | |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 137 | // The list of pre-singletons and singletons registered for the test. |
| 138 | preSingletons, singletons sortableComponents |
| 139 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 140 | // The order in which the pre-singletons, mutators and singletons will be run in this test |
| 141 | // context; for debugging. |
| 142 | preSingletonOrder, mutatorOrder, singletonOrder []string |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) { |
| 146 | ctx.preArch = append(ctx.preArch, f) |
| 147 | } |
| 148 | |
Paul Duffin | a80ef84 | 2020-01-14 12:09:36 +0000 | [diff] [blame] | 149 | func (ctx *TestContext) HardCodedPreArchMutators(f RegisterMutatorFunc) { |
| 150 | // Register mutator function as normal for testing. |
| 151 | ctx.PreArchMutators(f) |
| 152 | } |
| 153 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 154 | func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) { |
| 155 | ctx.preDeps = append(ctx.preDeps, f) |
| 156 | } |
| 157 | |
| 158 | func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) { |
| 159 | ctx.postDeps = append(ctx.postDeps, f) |
| 160 | } |
| 161 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 162 | func (ctx *TestContext) FinalDepsMutators(f RegisterMutatorFunc) { |
| 163 | ctx.finalDeps = append(ctx.finalDeps, f) |
| 164 | } |
| 165 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 166 | func (ctx *TestContext) RegisterBp2BuildConfig(config Bp2BuildConfig) { |
| 167 | ctx.config.bp2buildPackageConfig = config |
| 168 | } |
| 169 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 170 | // RegisterBp2BuildMutator registers a BazelTargetModule mutator for converting a module |
| 171 | // type to the equivalent Bazel target. |
| 172 | func (ctx *TestContext) RegisterBp2BuildMutator(moduleType string, m func(TopDownMutatorContext)) { |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 173 | f := func(ctx RegisterMutatorsContext) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 174 | ctx.TopDown(moduleType, m) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 175 | } |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 176 | ctx.config.bp2buildModuleTypeConfig[moduleType] = true |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 177 | ctx.bp2buildMutators = append(ctx.bp2buildMutators, f) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 180 | // PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules |
| 181 | // into Bazel BUILD targets that should run prior to deps and conversion. |
| 182 | func (ctx *TestContext) PreArchBp2BuildMutators(f RegisterMutatorFunc) { |
| 183 | ctx.bp2buildPreArch = append(ctx.bp2buildPreArch, f) |
| 184 | } |
| 185 | |
| 186 | // DepsBp2BuildMutators adds mutators to be register for converting Android Blueprint modules into |
| 187 | // Bazel BUILD targets that should run prior to conversion to resolve dependencies. |
| 188 | func (ctx *TestContext) DepsBp2BuildMutators(f RegisterMutatorFunc) { |
| 189 | ctx.bp2buildDeps = append(ctx.bp2buildDeps, f) |
| 190 | } |
| 191 | |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 192 | // registeredComponentOrder defines the order in which a sortableComponent type is registered at |
| 193 | // runtime and provides support for reordering the components registered for a test in the same |
| 194 | // way. |
| 195 | type registeredComponentOrder struct { |
| 196 | // The name of the component type, used for error messages. |
| 197 | componentType string |
| 198 | |
| 199 | // The names of the registered components in the order in which they were registered. |
| 200 | namesInOrder []string |
| 201 | |
| 202 | // Maps from the component name to its position in the runtime ordering. |
| 203 | namesToIndex map[string]int |
| 204 | |
| 205 | // A function that defines the order between two named components that can be used to sort a slice |
| 206 | // of component names into the same order as they appear in namesInOrder. |
| 207 | less func(string, string) bool |
| 208 | } |
| 209 | |
| 210 | // registeredComponentOrderFromExistingOrder takes an existing slice of sortableComponents and |
| 211 | // creates a registeredComponentOrder that contains a less function that can be used to sort a |
| 212 | // subset of that list of names so it is in the same order as the original sortableComponents. |
| 213 | func registeredComponentOrderFromExistingOrder(componentType string, existingOrder sortableComponents) registeredComponentOrder { |
| 214 | // Only the names from the existing order are needed for this so create a list of component names |
| 215 | // in the correct order. |
| 216 | namesInOrder := componentsToNames(existingOrder) |
| 217 | |
| 218 | // Populate the map from name to position in the list. |
| 219 | nameToIndex := make(map[string]int) |
| 220 | for i, n := range namesInOrder { |
| 221 | nameToIndex[n] = i |
| 222 | } |
| 223 | |
| 224 | // A function to use to map from a name to an index in the original order. |
| 225 | indexOf := func(name string) int { |
| 226 | index, ok := nameToIndex[name] |
| 227 | if !ok { |
| 228 | // Should never happen as tests that use components that are not known at runtime do not sort |
| 229 | // so should never use this function. |
| 230 | panic(fmt.Errorf("internal error: unknown %s %q should be one of %s", componentType, name, strings.Join(namesInOrder, ", "))) |
| 231 | } |
| 232 | return index |
| 233 | } |
| 234 | |
| 235 | // The less function. |
| 236 | less := func(n1, n2 string) bool { |
| 237 | i1 := indexOf(n1) |
| 238 | i2 := indexOf(n2) |
| 239 | return i1 < i2 |
| 240 | } |
| 241 | |
| 242 | return registeredComponentOrder{ |
| 243 | componentType: componentType, |
| 244 | namesInOrder: namesInOrder, |
| 245 | namesToIndex: nameToIndex, |
| 246 | less: less, |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // componentsToNames maps from the slice of components to a slice of their names. |
| 251 | func componentsToNames(components sortableComponents) []string { |
| 252 | names := make([]string, len(components)) |
| 253 | for i, c := range components { |
| 254 | names[i] = c.componentName() |
| 255 | } |
| 256 | return names |
| 257 | } |
| 258 | |
| 259 | // enforceOrdering enforces the supplied components are in the same order as is defined in this |
| 260 | // object. |
| 261 | // |
| 262 | // If the supplied components contains any components that are not registered at runtime, i.e. test |
| 263 | // specific components, then it is impossible to sort them into an order that both matches the |
| 264 | // runtime and also preserves the implicit ordering defined in the test. In that case it will not |
| 265 | // sort the components, instead it will just check that the components are in the correct order. |
| 266 | // |
| 267 | // Otherwise, this will sort the supplied components in place. |
| 268 | func (o *registeredComponentOrder) enforceOrdering(components sortableComponents) { |
| 269 | // Check to see if the list of components contains any components that are |
| 270 | // not registered at runtime. |
| 271 | var unknownComponents []string |
| 272 | testOrder := componentsToNames(components) |
| 273 | for _, name := range testOrder { |
| 274 | if _, ok := o.namesToIndex[name]; !ok { |
| 275 | unknownComponents = append(unknownComponents, name) |
| 276 | break |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // If the slice contains some unknown components then it is not possible to |
| 281 | // sort them into an order that matches the runtime while also preserving the |
| 282 | // order expected from the test, so in that case don't sort just check that |
| 283 | // the order of the known mutators does match. |
| 284 | if len(unknownComponents) > 0 { |
| 285 | // Check order. |
| 286 | o.checkTestOrder(testOrder, unknownComponents) |
| 287 | } else { |
| 288 | // Sort the components. |
| 289 | sort.Slice(components, func(i, j int) bool { |
| 290 | n1 := components[i].componentName() |
| 291 | n2 := components[j].componentName() |
| 292 | return o.less(n1, n2) |
| 293 | }) |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // checkTestOrder checks that the supplied testOrder matches the one defined by this object, |
| 298 | // panicking if it does not. |
| 299 | func (o *registeredComponentOrder) checkTestOrder(testOrder []string, unknownComponents []string) { |
| 300 | lastMatchingTest := -1 |
| 301 | matchCount := 0 |
| 302 | // Take a copy of the runtime order as it is modified during the comparison. |
| 303 | runtimeOrder := append([]string(nil), o.namesInOrder...) |
| 304 | componentType := o.componentType |
| 305 | for i, j := 0, 0; i < len(testOrder) && j < len(runtimeOrder); { |
| 306 | test := testOrder[i] |
| 307 | runtime := runtimeOrder[j] |
| 308 | |
| 309 | if test == runtime { |
| 310 | testOrder[i] = test + fmt.Sprintf(" <-- matched with runtime %s %d", componentType, j) |
| 311 | runtimeOrder[j] = runtime + fmt.Sprintf(" <-- matched with test %s %d", componentType, i) |
| 312 | lastMatchingTest = i |
| 313 | i += 1 |
| 314 | j += 1 |
| 315 | matchCount += 1 |
| 316 | } else if _, ok := o.namesToIndex[test]; !ok { |
| 317 | // The test component is not registered globally so assume it is the correct place, treat it |
| 318 | // as having matched and skip it. |
| 319 | i += 1 |
| 320 | matchCount += 1 |
| 321 | } else { |
| 322 | // Assume that the test list is in the same order as the runtime list but the runtime list |
| 323 | // contains some components that are not present in the tests. So, skip the runtime component |
| 324 | // to try and find the next one that matches the current test component. |
| 325 | j += 1 |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // If every item in the test order was either test specific or matched one in the runtime then |
| 330 | // it is in the correct order. Otherwise, it was not so fail. |
| 331 | if matchCount != len(testOrder) { |
| 332 | // The test component names were not all matched with a runtime component name so there must |
| 333 | // either be a component present in the test that is not present in the runtime or they must be |
| 334 | // in the wrong order. |
| 335 | testOrder[lastMatchingTest+1] = testOrder[lastMatchingTest+1] + " <--- unmatched" |
| 336 | panic(fmt.Errorf("the tests uses test specific components %q and so cannot be automatically sorted."+ |
| 337 | " Unfortunately it uses %s components in the wrong order.\n"+ |
| 338 | "test order:\n %s\n"+ |
| 339 | "runtime order\n %s\n", |
| 340 | SortedUniqueStrings(unknownComponents), |
| 341 | componentType, |
| 342 | strings.Join(testOrder, "\n "), |
| 343 | strings.Join(runtimeOrder, "\n "))) |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // registrationSorter encapsulates the information needed to ensure that the test mutators are |
| 348 | // registered, and thereby executed, in the same order as they are at runtime. |
| 349 | // |
| 350 | // It MUST be populated lazily AFTER all package initialization has been done otherwise it will |
| 351 | // only define the order for a subset of all the registered build components that are available for |
| 352 | // the packages being tested. |
| 353 | // |
| 354 | // e.g if this is initialized during say the cc package initialization then any tests run in the |
| 355 | // java package will not sort build components registered by the java package's init() functions. |
| 356 | type registrationSorter struct { |
| 357 | // Used to ensure that this is only created once. |
| 358 | once sync.Once |
| 359 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 360 | // The order of pre-singletons |
| 361 | preSingletonOrder registeredComponentOrder |
| 362 | |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 363 | // The order of mutators |
| 364 | mutatorOrder registeredComponentOrder |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 365 | |
| 366 | // The order of singletons |
| 367 | singletonOrder registeredComponentOrder |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | // populate initializes this structure from globally registered build components. |
| 371 | // |
| 372 | // Only the first call has any effect. |
| 373 | func (s *registrationSorter) populate() { |
| 374 | s.once.Do(func() { |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 375 | // Create an ordering from the globally registered pre-singletons. |
| 376 | s.preSingletonOrder = registeredComponentOrderFromExistingOrder("pre-singleton", preSingletons) |
| 377 | |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 378 | // Created an ordering from the globally registered mutators. |
| 379 | globallyRegisteredMutators := collateGloballyRegisteredMutators() |
| 380 | s.mutatorOrder = registeredComponentOrderFromExistingOrder("mutator", globallyRegisteredMutators) |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 381 | |
| 382 | // Create an ordering from the globally registered singletons. |
| 383 | globallyRegisteredSingletons := collateGloballyRegisteredSingletons() |
| 384 | s.singletonOrder = registeredComponentOrderFromExistingOrder("singleton", globallyRegisteredSingletons) |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 385 | }) |
| 386 | } |
| 387 | |
| 388 | // Provides support for enforcing the same order in which build components are registered globally |
| 389 | // to the order in which they are registered during tests. |
| 390 | // |
| 391 | // MUST only be accessed via the globallyRegisteredComponentsOrder func. |
| 392 | var globalRegistrationSorter registrationSorter |
| 393 | |
| 394 | // globallyRegisteredComponentsOrder returns the globalRegistrationSorter after ensuring it is |
| 395 | // correctly populated. |
| 396 | func globallyRegisteredComponentsOrder() *registrationSorter { |
| 397 | globalRegistrationSorter.populate() |
| 398 | return &globalRegistrationSorter |
| 399 | } |
| 400 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 401 | func (ctx *TestContext) Register() { |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 402 | globalOrder := globallyRegisteredComponentsOrder() |
| 403 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 404 | // Ensure that the pre-singletons used in the test are in the same order as they are used at |
| 405 | // runtime. |
| 406 | globalOrder.preSingletonOrder.enforceOrdering(ctx.preSingletons) |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 407 | ctx.preSingletons.registerAll(ctx.Context) |
| 408 | |
Paul Duffin | c05b034 | 2021-03-06 13:28:13 +0000 | [diff] [blame] | 409 | mutators := collateRegisteredMutators(ctx.preArch, ctx.preDeps, ctx.postDeps, ctx.finalDeps) |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 410 | // Ensure that the mutators used in the test are in the same order as they are used at runtime. |
| 411 | globalOrder.mutatorOrder.enforceOrdering(mutators) |
Paul Duffin | c05b034 | 2021-03-06 13:28:13 +0000 | [diff] [blame] | 412 | mutators.registerAll(ctx.Context) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 413 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 414 | // Ensure that the singletons used in the test are in the same order as they are used at runtime. |
| 415 | globalOrder.singletonOrder.enforceOrdering(ctx.singletons) |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 416 | ctx.singletons.registerAll(ctx.Context) |
| 417 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 418 | // 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] | 419 | ctx.preSingletonOrder = componentsToNames(preSingletons) |
| 420 | ctx.mutatorOrder = componentsToNames(mutators) |
| 421 | ctx.singletonOrder = componentsToNames(singletons) |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 424 | // RegisterForBazelConversion prepares a test context for bp2build conversion. |
| 425 | func (ctx *TestContext) RegisterForBazelConversion() { |
Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 426 | RegisterMutatorsForBazelConversion(ctx.Context, ctx.bp2buildPreArch, ctx.bp2buildDeps, ctx.bp2buildMutators) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 427 | } |
| 428 | |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 429 | func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) { |
| 430 | // This function adapts the old style ParseFileList calls that are spread throughout the tests |
| 431 | // to the new style that takes a config. |
| 432 | return ctx.Context.ParseFileList(rootDir, filePaths, ctx.config) |
| 433 | } |
| 434 | |
| 435 | func (ctx *TestContext) ParseBlueprintsFiles(rootDir string) (deps []string, errs []error) { |
| 436 | // This function adapts the old style ParseBlueprintsFiles calls that are spread throughout the |
| 437 | // tests to the new style that takes a config. |
| 438 | return ctx.Context.ParseBlueprintsFiles(rootDir, ctx.config) |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) { |
| 442 | ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory)) |
| 443 | } |
| 444 | |
Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 445 | func (ctx *TestContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) { |
| 446 | s, m := SingletonModuleFactoryAdaptor(name, factory) |
| 447 | ctx.RegisterSingletonType(name, s) |
| 448 | ctx.RegisterModuleType(name, m) |
| 449 | } |
| 450 | |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 451 | func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) { |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 452 | ctx.singletons = append(ctx.singletons, newSingleton(name, factory)) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Paul Duffin | eafc16b | 2021-02-24 01:43:18 +0000 | [diff] [blame] | 455 | func (ctx *TestContext) RegisterPreSingletonType(name string, factory SingletonFactory) { |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 456 | ctx.preSingletons = append(ctx.preSingletons, newPreSingleton(name, factory)) |
Paul Duffin | eafc16b | 2021-02-24 01:43:18 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 459 | func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule { |
| 460 | var module Module |
| 461 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 462 | if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant { |
| 463 | module = m.(Module) |
| 464 | } |
| 465 | }) |
| 466 | |
| 467 | if module == nil { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 468 | // find all the modules that do exist |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 469 | var allModuleNames []string |
| 470 | var allVariants []string |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 471 | ctx.VisitAllModules(func(m blueprint.Module) { |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 472 | allModuleNames = append(allModuleNames, ctx.ModuleName(m)) |
| 473 | if ctx.ModuleName(m) == name { |
| 474 | allVariants = append(allVariants, ctx.ModuleSubDir(m)) |
| 475 | } |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 476 | }) |
Martin Stjernholm | 4c02124 | 2020-05-13 01:13:50 +0100 | [diff] [blame] | 477 | sort.Strings(allModuleNames) |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 478 | sort.Strings(allVariants) |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 479 | |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 480 | if len(allVariants) == 0 { |
| 481 | panic(fmt.Errorf("failed to find module %q. All modules:\n %s", |
| 482 | name, strings.Join(allModuleNames, "\n "))) |
| 483 | } else { |
| 484 | panic(fmt.Errorf("failed to find module %q variant %q. All variants:\n %s", |
| 485 | name, variant, strings.Join(allVariants, "\n "))) |
| 486 | } |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 489 | return newTestingModule(ctx.config, module) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Jiyong Park | 37b2520 | 2018-07-11 10:49:27 +0900 | [diff] [blame] | 492 | func (ctx *TestContext) ModuleVariantsForTests(name string) []string { |
| 493 | var variants []string |
| 494 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 495 | if ctx.ModuleName(m) == name { |
| 496 | variants = append(variants, ctx.ModuleSubDir(m)) |
| 497 | } |
| 498 | }) |
| 499 | return variants |
| 500 | } |
| 501 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 502 | // SingletonForTests returns a TestingSingleton for the singleton registered with the given name. |
| 503 | func (ctx *TestContext) SingletonForTests(name string) TestingSingleton { |
| 504 | allSingletonNames := []string{} |
| 505 | for _, s := range ctx.Singletons() { |
| 506 | n := ctx.SingletonName(s) |
| 507 | if n == name { |
| 508 | return TestingSingleton{ |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 509 | baseTestingComponent: newBaseTestingComponent(ctx.config, s.(testBuildProvider)), |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 510 | singleton: s.(*singletonAdaptor).Singleton, |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | allSingletonNames = append(allSingletonNames, n) |
| 514 | } |
| 515 | |
| 516 | panic(fmt.Errorf("failed to find singleton %q."+ |
| 517 | "\nall singletons: %v", name, allSingletonNames)) |
| 518 | } |
| 519 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 520 | func (ctx *TestContext) Config() Config { |
| 521 | return ctx.config |
| 522 | } |
| 523 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 524 | type testBuildProvider interface { |
| 525 | BuildParamsForTests() []BuildParams |
| 526 | RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams |
| 527 | } |
| 528 | |
| 529 | type TestingBuildParams struct { |
| 530 | BuildParams |
| 531 | RuleParams blueprint.RuleParams |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 532 | |
| 533 | config Config |
| 534 | } |
| 535 | |
| 536 | // RelativeToTop creates a new instance of this which has had any usages of the current test's |
| 537 | // temporary and test specific build directory replaced with a path relative to the notional top. |
| 538 | // |
| 539 | // The parts of this structure which are changed are: |
| 540 | // * BuildParams |
| 541 | // * Args |
| 542 | // * Path instances are intentionally not modified, use AssertPathRelativeToTopEquals or |
| 543 | // AssertPathsRelativeToTopEquals instead which do something similar. |
| 544 | // |
| 545 | // * RuleParams |
| 546 | // * Command |
| 547 | // * Depfile |
| 548 | // * Rspfile |
| 549 | // * RspfileContent |
| 550 | // * SymlinkOutputs |
| 551 | // * CommandDeps |
| 552 | // * CommandOrderOnly |
| 553 | // |
| 554 | // See PathRelativeToTop for more details. |
| 555 | func (p TestingBuildParams) RelativeToTop() TestingBuildParams { |
| 556 | // If this is not a valid params then just return it back. That will make it easy to use with the |
| 557 | // Maybe...() methods. |
| 558 | if p.Rule == nil { |
| 559 | return p |
| 560 | } |
| 561 | if p.config.config == nil { |
| 562 | panic("cannot call RelativeToTop() on a TestingBuildParams previously returned by RelativeToTop()") |
| 563 | } |
| 564 | // Take a copy of the build params and replace any args that contains test specific temporary |
| 565 | // paths with paths relative to the top. |
| 566 | bparams := p.BuildParams |
| 567 | bparams.Args = normalizeStringMapRelativeToTop(p.config, bparams.Args) |
| 568 | |
| 569 | // Ditto for any fields in the RuleParams. |
| 570 | rparams := p.RuleParams |
| 571 | rparams.Command = normalizeStringRelativeToTop(p.config, rparams.Command) |
| 572 | rparams.Depfile = normalizeStringRelativeToTop(p.config, rparams.Depfile) |
| 573 | rparams.Rspfile = normalizeStringRelativeToTop(p.config, rparams.Rspfile) |
| 574 | rparams.RspfileContent = normalizeStringRelativeToTop(p.config, rparams.RspfileContent) |
| 575 | rparams.SymlinkOutputs = normalizeStringArrayRelativeToTop(p.config, rparams.SymlinkOutputs) |
| 576 | rparams.CommandDeps = normalizeStringArrayRelativeToTop(p.config, rparams.CommandDeps) |
| 577 | rparams.CommandOrderOnly = normalizeStringArrayRelativeToTop(p.config, rparams.CommandOrderOnly) |
| 578 | |
| 579 | return TestingBuildParams{ |
| 580 | BuildParams: bparams, |
| 581 | RuleParams: rparams, |
| 582 | } |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 583 | } |
| 584 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 585 | // baseTestingComponent provides functionality common to both TestingModule and TestingSingleton. |
| 586 | type baseTestingComponent struct { |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 587 | config Config |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 588 | provider testBuildProvider |
| 589 | } |
| 590 | |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 591 | func newBaseTestingComponent(config Config, provider testBuildProvider) baseTestingComponent { |
| 592 | return baseTestingComponent{config, provider} |
| 593 | } |
| 594 | |
| 595 | // A function that will normalize a string containing paths, e.g. ninja command, by replacing |
| 596 | // any references to the test specific temporary build directory that changes with each run to a |
| 597 | // fixed path relative to a notional top directory. |
| 598 | // |
| 599 | // This is similar to StringPathRelativeToTop except that assumes the string is a single path |
| 600 | // containing at most one instance of the temporary build directory at the start of the path while |
| 601 | // this assumes that there can be any number at any position. |
| 602 | func normalizeStringRelativeToTop(config Config, s string) string { |
| 603 | // The buildDir usually looks something like: /tmp/testFoo2345/001 |
| 604 | // |
| 605 | // Replace any usage of the buildDir with out/soong, e.g. replace "/tmp/testFoo2345/001" with |
| 606 | // "out/soong". |
| 607 | outSoongDir := filepath.Clean(config.buildDir) |
| 608 | re := regexp.MustCompile(`\Q` + outSoongDir + `\E\b`) |
| 609 | s = re.ReplaceAllString(s, "out/soong") |
| 610 | |
| 611 | // Replace any usage of the buildDir/.. with out, e.g. replace "/tmp/testFoo2345" with |
| 612 | // "out". This must come after the previous replacement otherwise this would replace |
| 613 | // "/tmp/testFoo2345/001" with "out/001" instead of "out/soong". |
| 614 | outDir := filepath.Dir(outSoongDir) |
| 615 | re = regexp.MustCompile(`\Q` + outDir + `\E\b`) |
| 616 | s = re.ReplaceAllString(s, "out") |
| 617 | |
| 618 | return s |
| 619 | } |
| 620 | |
| 621 | // normalizeStringArrayRelativeToTop creates a new slice constructed by applying |
| 622 | // normalizeStringRelativeToTop to each item in the slice. |
| 623 | func normalizeStringArrayRelativeToTop(config Config, slice []string) []string { |
| 624 | newSlice := make([]string, len(slice)) |
| 625 | for i, s := range slice { |
| 626 | newSlice[i] = normalizeStringRelativeToTop(config, s) |
| 627 | } |
| 628 | return newSlice |
| 629 | } |
| 630 | |
| 631 | // normalizeStringMapRelativeToTop creates a new map constructed by applying |
| 632 | // normalizeStringRelativeToTop to each value in the map. |
| 633 | func normalizeStringMapRelativeToTop(config Config, m map[string]string) map[string]string { |
| 634 | newMap := map[string]string{} |
| 635 | for k, v := range m { |
| 636 | newMap[k] = normalizeStringRelativeToTop(config, v) |
| 637 | } |
| 638 | return newMap |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | func (b baseTestingComponent) newTestingBuildParams(bparams BuildParams) TestingBuildParams { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 642 | return TestingBuildParams{ |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 643 | config: b.config, |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 644 | BuildParams: bparams, |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 645 | RuleParams: b.provider.RuleParamsForTests()[bparams.Rule], |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 649 | func (b baseTestingComponent) maybeBuildParamsFromRule(rule string) (TestingBuildParams, []string) { |
Thiébaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 650 | var searchedRules []string |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 651 | for _, p := range b.provider.BuildParamsForTests() { |
Thiébaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 652 | searchedRules = append(searchedRules, p.Rule.String()) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 653 | if strings.Contains(p.Rule.String(), rule) { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 654 | return b.newTestingBuildParams(p), searchedRules |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 655 | } |
| 656 | } |
Thiébaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 657 | return TestingBuildParams{}, searchedRules |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 658 | } |
| 659 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 660 | func (b baseTestingComponent) buildParamsFromRule(rule string) TestingBuildParams { |
| 661 | p, searchRules := b.maybeBuildParamsFromRule(rule) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 662 | if p.Rule == nil { |
Thiébaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 663 | 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] | 664 | } |
| 665 | return p |
| 666 | } |
| 667 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 668 | func (b baseTestingComponent) maybeBuildParamsFromDescription(desc string) TestingBuildParams { |
| 669 | for _, p := range b.provider.BuildParamsForTests() { |
Colin Cross | b88b3c5 | 2019-06-10 15:15:17 -0700 | [diff] [blame] | 670 | if strings.Contains(p.Description, desc) { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 671 | return b.newTestingBuildParams(p) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | return TestingBuildParams{} |
| 675 | } |
| 676 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 677 | func (b baseTestingComponent) buildParamsFromDescription(desc string) TestingBuildParams { |
| 678 | p := b.maybeBuildParamsFromDescription(desc) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 679 | if p.Rule == nil { |
| 680 | panic(fmt.Errorf("couldn't find description %q", desc)) |
| 681 | } |
| 682 | return p |
| 683 | } |
| 684 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 685 | func (b baseTestingComponent) maybeBuildParamsFromOutput(file string) (TestingBuildParams, []string) { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 686 | var searchedOutputs []string |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 687 | for _, p := range b.provider.BuildParamsForTests() { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 688 | outputs := append(WritablePaths(nil), p.Outputs...) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 689 | outputs = append(outputs, p.ImplicitOutputs...) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 690 | if p.Output != nil { |
| 691 | outputs = append(outputs, p.Output) |
| 692 | } |
| 693 | for _, f := range outputs { |
Paul Duffin | 4e6e35c | 2021-03-22 11:34:57 +0000 | [diff] [blame] | 694 | if f.String() == file || f.Rel() == file || PathRelativeToTop(f) == file { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 695 | return b.newTestingBuildParams(p), nil |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 696 | } |
| 697 | searchedOutputs = append(searchedOutputs, f.Rel()) |
| 698 | } |
| 699 | } |
| 700 | return TestingBuildParams{}, searchedOutputs |
| 701 | } |
| 702 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 703 | func (b baseTestingComponent) buildParamsFromOutput(file string) TestingBuildParams { |
| 704 | p, searchedOutputs := b.maybeBuildParamsFromOutput(file) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 705 | if p.Rule == nil { |
Paul Duffin | 4e6e35c | 2021-03-22 11:34:57 +0000 | [diff] [blame] | 706 | panic(fmt.Errorf("couldn't find output %q.\nall outputs:\n %s\n", |
| 707 | file, strings.Join(searchedOutputs, "\n "))) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 708 | } |
| 709 | return p |
| 710 | } |
| 711 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 712 | func (b baseTestingComponent) allOutputs() []string { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 713 | var outputFullPaths []string |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 714 | for _, p := range b.provider.BuildParamsForTests() { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 715 | outputs := append(WritablePaths(nil), p.Outputs...) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 716 | outputs = append(outputs, p.ImplicitOutputs...) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 717 | if p.Output != nil { |
| 718 | outputs = append(outputs, p.Output) |
| 719 | } |
| 720 | outputFullPaths = append(outputFullPaths, outputs.Strings()...) |
| 721 | } |
| 722 | return outputFullPaths |
| 723 | } |
| 724 | |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 725 | // MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty |
| 726 | // BuildParams if no rule is found. |
| 727 | func (b baseTestingComponent) MaybeRule(rule string) TestingBuildParams { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 728 | r, _ := b.maybeBuildParamsFromRule(rule) |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 729 | return r |
| 730 | } |
| 731 | |
| 732 | // Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found. |
| 733 | func (b baseTestingComponent) Rule(rule string) TestingBuildParams { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 734 | return b.buildParamsFromRule(rule) |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | // MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty |
| 738 | // BuildParams if no rule is found. |
| 739 | func (b baseTestingComponent) MaybeDescription(desc string) TestingBuildParams { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 740 | return b.maybeBuildParamsFromDescription(desc) |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | // Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is |
| 744 | // found. |
| 745 | func (b baseTestingComponent) Description(desc string) TestingBuildParams { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 746 | return b.buildParamsFromDescription(desc) |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | // MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel() |
| 750 | // value matches the provided string. Returns an empty BuildParams if no rule is found. |
| 751 | func (b baseTestingComponent) MaybeOutput(file string) TestingBuildParams { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 752 | p, _ := b.maybeBuildParamsFromOutput(file) |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 753 | return p |
| 754 | } |
| 755 | |
| 756 | // Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel() |
| 757 | // value matches the provided string. Panics if no rule is found. |
| 758 | func (b baseTestingComponent) Output(file string) TestingBuildParams { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 759 | return b.buildParamsFromOutput(file) |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | // AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms. |
| 763 | func (b baseTestingComponent) AllOutputs() []string { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 764 | return b.allOutputs() |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 767 | // TestingModule is wrapper around an android.Module that provides methods to find information about individual |
| 768 | // ctx.Build parameters for verification in tests. |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 769 | type TestingModule struct { |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 770 | baseTestingComponent |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 771 | module Module |
| 772 | } |
| 773 | |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 774 | func newTestingModule(config Config, module Module) TestingModule { |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 775 | return TestingModule{ |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 776 | newBaseTestingComponent(config, module), |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 777 | module, |
| 778 | } |
| 779 | } |
| 780 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 781 | // Module returns the Module wrapped by the TestingModule. |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 782 | func (m TestingModule) Module() Module { |
| 783 | return m.module |
| 784 | } |
| 785 | |
Paul Duffin | 97d8b40 | 2021-03-22 16:04:50 +0000 | [diff] [blame] | 786 | // VariablesForTestsRelativeToTop returns a copy of the Module.VariablesForTests() with every value |
| 787 | // having any temporary build dir usages replaced with paths relative to a notional top. |
| 788 | func (m TestingModule) VariablesForTestsRelativeToTop() map[string]string { |
| 789 | return normalizeStringMapRelativeToTop(m.config, m.module.VariablesForTests()) |
| 790 | } |
| 791 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 792 | // TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual |
| 793 | // ctx.Build parameters for verification in tests. |
| 794 | type TestingSingleton struct { |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 795 | baseTestingComponent |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 796 | singleton Singleton |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | // Singleton returns the Singleton wrapped by the TestingSingleton. |
| 800 | func (s TestingSingleton) Singleton() Singleton { |
| 801 | return s.singleton |
| 802 | } |
| 803 | |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 804 | func FailIfErrored(t *testing.T, errs []error) { |
| 805 | t.Helper() |
| 806 | if len(errs) > 0 { |
| 807 | for _, err := range errs { |
| 808 | t.Error(err) |
| 809 | } |
| 810 | t.FailNow() |
| 811 | } |
| 812 | } |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 813 | |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 814 | // Fail if no errors that matched the regular expression were found. |
| 815 | // |
| 816 | // Returns true if a matching error was found, false otherwise. |
| 817 | func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) bool { |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 818 | t.Helper() |
| 819 | |
| 820 | matcher, err := regexp.Compile(pattern) |
| 821 | if err != nil { |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 822 | t.Fatalf("failed to compile regular expression %q because %s", pattern, err) |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | found := false |
| 826 | for _, err := range errs { |
| 827 | if matcher.FindStringIndex(err.Error()) != nil { |
| 828 | found = true |
| 829 | break |
| 830 | } |
| 831 | } |
| 832 | if !found { |
| 833 | t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs)) |
| 834 | for i, err := range errs { |
Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 835 | t.Errorf("errs[%d] = %q", i, err) |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 836 | } |
| 837 | } |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 838 | |
| 839 | return found |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 840 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 841 | |
Paul Duffin | 91e3819 | 2019-08-05 15:07:57 +0100 | [diff] [blame] | 842 | func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) { |
| 843 | t.Helper() |
| 844 | |
| 845 | if expectedErrorPatterns == nil { |
| 846 | FailIfErrored(t, errs) |
| 847 | } else { |
| 848 | for _, expectedError := range expectedErrorPatterns { |
| 849 | FailIfNoMatchingErrors(t, expectedError, errs) |
| 850 | } |
| 851 | if len(errs) > len(expectedErrorPatterns) { |
| 852 | t.Errorf("additional errors found, expected %d, found %d", |
| 853 | len(expectedErrorPatterns), len(errs)) |
| 854 | for i, expectedError := range expectedErrorPatterns { |
| 855 | t.Errorf("expectedErrors[%d] = %s", i, expectedError) |
| 856 | } |
| 857 | for i, err := range errs { |
| 858 | t.Errorf("errs[%d] = %s", i, err) |
| 859 | } |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 860 | t.FailNow() |
Paul Duffin | 91e3819 | 2019-08-05 15:07:57 +0100 | [diff] [blame] | 861 | } |
| 862 | } |
Paul Duffin | 91e3819 | 2019-08-05 15:07:57 +0100 | [diff] [blame] | 863 | } |
| 864 | |
Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 865 | func SetKatiEnabledForTests(config Config) { |
| 866 | config.katiEnabled = true |
Paul Duffin | 8c3fec4 | 2020-03-04 20:15:08 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 869 | func AndroidMkEntriesForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) []AndroidMkEntries { |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 870 | var p AndroidMkEntriesProvider |
| 871 | var ok bool |
| 872 | if p, ok = mod.(AndroidMkEntriesProvider); !ok { |
Roland Levillain | dfe75b3 | 2019-07-23 16:53:32 +0100 | [diff] [blame] | 873 | t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name()) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 874 | } |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 875 | |
| 876 | entriesList := p.AndroidMkEntries() |
| 877 | for i, _ := range entriesList { |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 878 | entriesList[i].fillInEntries(ctx, mod) |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 879 | } |
| 880 | return entriesList |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 881 | } |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 882 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 883 | func AndroidMkDataForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) AndroidMkData { |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 884 | var p AndroidMkDataProvider |
| 885 | var ok bool |
| 886 | if p, ok = mod.(AndroidMkDataProvider); !ok { |
Roland Levillain | dfe75b3 | 2019-07-23 16:53:32 +0100 | [diff] [blame] | 887 | t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name()) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 888 | } |
| 889 | data := p.AndroidMk() |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 890 | data.fillInData(ctx, mod) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 891 | return data |
| 892 | } |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 893 | |
| 894 | // Normalize the path for testing. |
| 895 | // |
| 896 | // If the path is relative to the build directory then return the relative path |
| 897 | // to avoid tests having to deal with the dynamically generated build directory. |
| 898 | // |
| 899 | // Otherwise, return the supplied path as it is almost certainly a source path |
| 900 | // that is relative to the root of the source tree. |
| 901 | // |
| 902 | // The build and source paths should be distinguishable based on their contents. |
Paul Duffin | 567465d | 2021-03-16 01:21:34 +0000 | [diff] [blame] | 903 | // |
| 904 | // deprecated: use PathRelativeToTop instead as it handles make install paths and differentiates |
| 905 | // between output and source properly. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 906 | func NormalizePathForTesting(path Path) string { |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 907 | if path == nil { |
| 908 | return "<nil path>" |
| 909 | } |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 910 | p := path.String() |
| 911 | if w, ok := path.(WritablePath); ok { |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 912 | rel, err := filepath.Rel(w.getBuildDir(), p) |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 913 | if err != nil { |
| 914 | panic(err) |
| 915 | } |
| 916 | return rel |
| 917 | } |
| 918 | return p |
| 919 | } |
| 920 | |
Paul Duffin | 567465d | 2021-03-16 01:21:34 +0000 | [diff] [blame] | 921 | // NormalizePathsForTesting creates a slice of strings where each string is the result of applying |
| 922 | // NormalizePathForTesting to the corresponding Path in the input slice. |
| 923 | // |
| 924 | // deprecated: use PathsRelativeToTop instead as it handles make install paths and differentiates |
| 925 | // between output and source properly. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 926 | func NormalizePathsForTesting(paths Paths) []string { |
| 927 | var result []string |
| 928 | for _, path := range paths { |
| 929 | relative := NormalizePathForTesting(path) |
| 930 | result = append(result, relative) |
| 931 | } |
| 932 | return result |
| 933 | } |
Paul Duffin | 567465d | 2021-03-16 01:21:34 +0000 | [diff] [blame] | 934 | |
| 935 | // PathRelativeToTop returns a string representation of the path relative to a notional top |
| 936 | // directory. |
| 937 | // |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 938 | // It return "<nil path>" if the supplied path is nil, otherwise it returns the result of calling |
| 939 | // Path.RelativeToTop to obtain a relative Path and then calling Path.String on that to get the |
| 940 | // string representation. |
Paul Duffin | 567465d | 2021-03-16 01:21:34 +0000 | [diff] [blame] | 941 | func PathRelativeToTop(path Path) string { |
| 942 | if path == nil { |
| 943 | return "<nil path>" |
| 944 | } |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 945 | return path.RelativeToTop().String() |
Paul Duffin | 567465d | 2021-03-16 01:21:34 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | // PathsRelativeToTop creates a slice of strings where each string is the result of applying |
| 949 | // PathRelativeToTop to the corresponding Path in the input slice. |
| 950 | func PathsRelativeToTop(paths Paths) []string { |
| 951 | var result []string |
| 952 | for _, path := range paths { |
| 953 | relative := PathRelativeToTop(path) |
| 954 | result = append(result, relative) |
| 955 | } |
| 956 | return result |
| 957 | } |
| 958 | |
| 959 | // StringPathRelativeToTop returns a string representation of the path relative to a notional top |
| 960 | // directory. |
| 961 | // |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 962 | // See Path.RelativeToTop for more details as to what `relative to top` means. |
Paul Duffin | 567465d | 2021-03-16 01:21:34 +0000 | [diff] [blame] | 963 | // |
| 964 | // This is provided for processing paths that have already been converted into a string, e.g. paths |
| 965 | // in AndroidMkEntries structures. As a result it needs to be supplied the soong output dir against |
| 966 | // which it can try and relativize paths. PathRelativeToTop must be used for process Path objects. |
| 967 | func StringPathRelativeToTop(soongOutDir string, path string) string { |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 968 | ensureTestOnly() |
Paul Duffin | 567465d | 2021-03-16 01:21:34 +0000 | [diff] [blame] | 969 | |
| 970 | // A relative path must be a source path so leave it as it is. |
| 971 | if !filepath.IsAbs(path) { |
| 972 | return path |
| 973 | } |
| 974 | |
| 975 | // Check to see if the path is relative to the soong out dir. |
| 976 | rel, isRel, err := maybeRelErr(soongOutDir, path) |
| 977 | if err != nil { |
| 978 | panic(err) |
| 979 | } |
| 980 | |
| 981 | if isRel { |
| 982 | // The path is in the soong out dir so indicate that in the relative path. |
| 983 | return filepath.Join("out/soong", rel) |
| 984 | } |
| 985 | |
| 986 | // Check to see if the path is relative to the top level out dir. |
| 987 | outDir := filepath.Dir(soongOutDir) |
| 988 | rel, isRel, err = maybeRelErr(outDir, path) |
| 989 | if err != nil { |
| 990 | panic(err) |
| 991 | } |
| 992 | |
| 993 | if isRel { |
| 994 | // The path is in the out dir so indicate that in the relative path. |
| 995 | return filepath.Join("out", rel) |
| 996 | } |
| 997 | |
| 998 | // This should never happen. |
| 999 | panic(fmt.Errorf("internal error: absolute path %s is not relative to the out dir %s", path, outDir)) |
| 1000 | } |
| 1001 | |
| 1002 | // StringPathsRelativeToTop creates a slice of strings where each string is the result of applying |
| 1003 | // StringPathRelativeToTop to the corresponding string path in the input slice. |
| 1004 | // |
| 1005 | // This is provided for processing paths that have already been converted into a string, e.g. paths |
| 1006 | // in AndroidMkEntries structures. As a result it needs to be supplied the soong output dir against |
| 1007 | // which it can try and relativize paths. PathsRelativeToTop must be used for process Paths objects. |
| 1008 | func StringPathsRelativeToTop(soongOutDir string, paths []string) []string { |
| 1009 | var result []string |
| 1010 | for _, path := range paths { |
| 1011 | relative := StringPathRelativeToTop(soongOutDir, path) |
| 1012 | result = append(result, relative) |
| 1013 | } |
| 1014 | return result |
| 1015 | } |