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