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 ( |
Martin Stjernholm | 1ebef5b | 2022-02-10 23:34:28 +0000 | [diff] [blame] | 18 | "bytes" |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 19 | "fmt" |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 20 | "path/filepath" |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 21 | "regexp" |
Martin Stjernholm | 4c02124 | 2020-05-13 01:13:50 +0100 | [diff] [blame] | 22 | "sort" |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 23 | "strings" |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 24 | "sync" |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 25 | "testing" |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 26 | |
Martin Stjernholm | 1ebef5b | 2022-02-10 23:34:28 +0000 | [diff] [blame] | 27 | mkparser "android/soong/androidmk/parser" |
| 28 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 29 | "github.com/google/blueprint" |
Paul Duffin | 25259e9 | 2021-03-07 15:45:56 +0000 | [diff] [blame] | 30 | "github.com/google/blueprint/proptools" |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 31 | ) |
| 32 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 33 | func NewTestContext(config Config) *TestContext { |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 34 | namespaceExportFilter := func(namespace *Namespace) bool { |
| 35 | return true |
| 36 | } |
Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 37 | |
| 38 | nameResolver := NewNameResolver(namespaceExportFilter) |
| 39 | ctx := &TestContext{ |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 40 | Context: &Context{blueprint.NewContext(), config}, |
Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 41 | NameResolver: nameResolver, |
| 42 | } |
| 43 | |
| 44 | ctx.SetNameInterface(nameResolver) |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 45 | |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 46 | ctx.postDeps = append(ctx.postDeps, registerPathDepsMutator) |
| 47 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 48 | ctx.SetFs(ctx.config.fs) |
| 49 | if ctx.config.mockBpList != "" { |
| 50 | ctx.SetModuleListFile(ctx.config.mockBpList) |
| 51 | } |
| 52 | |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 53 | return ctx |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Paul Duffin | a560d5a | 2021-02-28 01:38:51 +0000 | [diff] [blame] | 56 | var PrepareForTestWithArchMutator = GroupFixturePreparers( |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 57 | // Configure architecture targets in the fixture config. |
| 58 | FixtureModifyConfig(modifyTestConfigToSupportArchMutator), |
| 59 | |
| 60 | // Add the arch mutator to the context. |
| 61 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 62 | ctx.PreDepsMutators(registerArchMutator) |
| 63 | }), |
| 64 | ) |
| 65 | |
| 66 | var PrepareForTestWithDefaults = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 67 | ctx.PreArchMutators(RegisterDefaultsPreArchMutators) |
| 68 | }) |
| 69 | |
| 70 | var PrepareForTestWithComponentsMutator = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 71 | ctx.PreArchMutators(RegisterComponentsMutator) |
| 72 | }) |
| 73 | |
| 74 | var PrepareForTestWithPrebuilts = FixtureRegisterWithContext(RegisterPrebuiltMutators) |
| 75 | |
| 76 | var PrepareForTestWithOverrides = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 77 | ctx.PostDepsMutators(RegisterOverridePostDepsMutators) |
| 78 | }) |
| 79 | |
Paul Duffin | e96108d | 2021-05-06 16:39:27 +0100 | [diff] [blame] | 80 | var PrepareForTestWithLicenses = GroupFixturePreparers( |
| 81 | FixtureRegisterWithContext(RegisterLicenseKindBuildComponents), |
| 82 | FixtureRegisterWithContext(RegisterLicenseBuildComponents), |
| 83 | FixtureRegisterWithContext(registerLicenseMutators), |
| 84 | ) |
| 85 | |
| 86 | func registerLicenseMutators(ctx RegistrationContext) { |
| 87 | ctx.PreArchMutators(RegisterLicensesPackageMapper) |
| 88 | ctx.PreArchMutators(RegisterLicensesPropertyGatherer) |
| 89 | ctx.PostDepsMutators(RegisterLicensesDependencyChecker) |
| 90 | } |
| 91 | |
| 92 | var PrepareForTestWithLicenseDefaultModules = GroupFixturePreparers( |
| 93 | FixtureAddTextFile("build/soong/licenses/Android.bp", ` |
| 94 | license { |
| 95 | name: "Android-Apache-2.0", |
| 96 | package_name: "Android", |
| 97 | license_kinds: ["SPDX-license-identifier-Apache-2.0"], |
| 98 | copyright_notice: "Copyright (C) The Android Open Source Project", |
| 99 | license_text: ["LICENSE"], |
| 100 | } |
| 101 | |
| 102 | license_kind { |
| 103 | name: "SPDX-license-identifier-Apache-2.0", |
| 104 | conditions: ["notice"], |
| 105 | url: "https://spdx.org/licenses/Apache-2.0.html", |
| 106 | } |
| 107 | |
| 108 | license_kind { |
| 109 | name: "legacy_unencumbered", |
| 110 | conditions: ["unencumbered"], |
| 111 | } |
| 112 | `), |
| 113 | FixtureAddFile("build/soong/licenses/LICENSE", nil), |
| 114 | ) |
| 115 | |
Paul Duffin | 4fbfb59 | 2021-07-09 16:47:38 +0100 | [diff] [blame] | 116 | var PrepareForTestWithNamespace = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 117 | registerNamespaceBuildComponents(ctx) |
| 118 | ctx.PreArchMutators(RegisterNamespaceMutator) |
| 119 | }) |
| 120 | |
Martin Stjernholm | 1ebef5b | 2022-02-10 23:34:28 +0000 | [diff] [blame] | 121 | var PrepareForTestWithMakevars = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 122 | ctx.RegisterSingletonType("makevars", makeVarsSingletonFunc) |
| 123 | }) |
| 124 | |
Paul Duffin | ec3292b | 2021-03-09 01:01:31 +0000 | [diff] [blame] | 125 | // Test fixture preparer that will register most java build components. |
| 126 | // |
| 127 | // Singletons and mutators should only be added here if they are needed for a majority of java |
| 128 | // module types, otherwise they should be added under a separate preparer to allow them to be |
| 129 | // selected only when needed to reduce test execution time. |
| 130 | // |
| 131 | // Module types do not have much of an overhead unless they are used so this should include as many |
| 132 | // module types as possible. The exceptions are those module types that require mutators and/or |
| 133 | // singletons in order to function in which case they should be kept together in a separate |
| 134 | // preparer. |
| 135 | // |
| 136 | // The mutators in this group were chosen because they are needed by the vast majority of tests. |
| 137 | var PrepareForTestWithAndroidBuildComponents = GroupFixturePreparers( |
Paul Duffin | 530483c | 2021-03-07 13:20:38 +0000 | [diff] [blame] | 138 | // Sorted alphabetically as the actual order does not matter as tests automatically enforce the |
| 139 | // correct order. |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 140 | PrepareForTestWithArchMutator, |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 141 | PrepareForTestWithComponentsMutator, |
Paul Duffin | 530483c | 2021-03-07 13:20:38 +0000 | [diff] [blame] | 142 | PrepareForTestWithDefaults, |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 143 | PrepareForTestWithFilegroup, |
Paul Duffin | 530483c | 2021-03-07 13:20:38 +0000 | [diff] [blame] | 144 | PrepareForTestWithOverrides, |
| 145 | PrepareForTestWithPackageModule, |
| 146 | PrepareForTestWithPrebuilts, |
| 147 | PrepareForTestWithVisibility, |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 148 | ) |
| 149 | |
Paul Duffin | ec3292b | 2021-03-09 01:01:31 +0000 | [diff] [blame] | 150 | // Prepares an integration test with all build components from the android package. |
| 151 | // |
| 152 | // This should only be used by tests that want to run with as much of the build enabled as possible. |
| 153 | var PrepareForIntegrationTestWithAndroid = GroupFixturePreparers( |
| 154 | PrepareForTestWithAndroidBuildComponents, |
| 155 | ) |
| 156 | |
Paul Duffin | 25259e9 | 2021-03-07 15:45:56 +0000 | [diff] [blame] | 157 | // Prepares a test that may be missing dependencies by setting allow_missing_dependencies to |
| 158 | // true. |
| 159 | var PrepareForTestWithAllowMissingDependencies = GroupFixturePreparers( |
| 160 | FixtureModifyProductVariables(func(variables FixtureProductVariables) { |
| 161 | variables.Allow_missing_dependencies = proptools.BoolPtr(true) |
| 162 | }), |
| 163 | FixtureModifyContext(func(ctx *TestContext) { |
| 164 | ctx.SetAllowMissingDependencies(true) |
| 165 | }), |
| 166 | ) |
| 167 | |
Paul Duffin | 76e5c8a | 2021-03-20 14:19:46 +0000 | [diff] [blame] | 168 | // Prepares a test that disallows non-existent paths. |
| 169 | var PrepareForTestDisallowNonExistentPaths = FixtureModifyConfig(func(config Config) { |
| 170 | config.TestAllowNonExistentPaths = false |
| 171 | }) |
| 172 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 173 | func NewTestArchContext(config Config) *TestContext { |
| 174 | ctx := NewTestContext(config) |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 175 | ctx.preDeps = append(ctx.preDeps, registerArchMutator) |
| 176 | return ctx |
| 177 | } |
| 178 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 179 | type TestContext struct { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 180 | *Context |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 181 | preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc |
| 182 | bp2buildPreArch, bp2buildMutators []RegisterMutatorFunc |
| 183 | NameResolver *NameResolver |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 184 | |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 185 | // The list of pre-singletons and singletons registered for the test. |
| 186 | preSingletons, singletons sortableComponents |
| 187 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 188 | // The order in which the pre-singletons, mutators and singletons will be run in this test |
| 189 | // context; for debugging. |
| 190 | preSingletonOrder, mutatorOrder, singletonOrder []string |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) { |
| 194 | ctx.preArch = append(ctx.preArch, f) |
| 195 | } |
| 196 | |
Paul Duffin | a80ef84 | 2020-01-14 12:09:36 +0000 | [diff] [blame] | 197 | func (ctx *TestContext) HardCodedPreArchMutators(f RegisterMutatorFunc) { |
| 198 | // Register mutator function as normal for testing. |
| 199 | ctx.PreArchMutators(f) |
| 200 | } |
| 201 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 202 | func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) { |
| 203 | ctx.preDeps = append(ctx.preDeps, f) |
| 204 | } |
| 205 | |
| 206 | func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) { |
| 207 | ctx.postDeps = append(ctx.postDeps, f) |
| 208 | } |
| 209 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 210 | func (ctx *TestContext) FinalDepsMutators(f RegisterMutatorFunc) { |
| 211 | ctx.finalDeps = append(ctx.finalDeps, f) |
| 212 | } |
| 213 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 214 | func (ctx *TestContext) RegisterBp2BuildConfig(config bp2BuildConversionAllowlist) { |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 215 | ctx.config.bp2buildPackageConfig = config |
| 216 | } |
| 217 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 218 | // PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules |
| 219 | // into Bazel BUILD targets that should run prior to deps and conversion. |
| 220 | func (ctx *TestContext) PreArchBp2BuildMutators(f RegisterMutatorFunc) { |
| 221 | ctx.bp2buildPreArch = append(ctx.bp2buildPreArch, f) |
| 222 | } |
| 223 | |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 224 | // registeredComponentOrder defines the order in which a sortableComponent type is registered at |
| 225 | // runtime and provides support for reordering the components registered for a test in the same |
| 226 | // way. |
| 227 | type registeredComponentOrder struct { |
| 228 | // The name of the component type, used for error messages. |
| 229 | componentType string |
| 230 | |
| 231 | // The names of the registered components in the order in which they were registered. |
| 232 | namesInOrder []string |
| 233 | |
| 234 | // Maps from the component name to its position in the runtime ordering. |
| 235 | namesToIndex map[string]int |
| 236 | |
| 237 | // A function that defines the order between two named components that can be used to sort a slice |
| 238 | // of component names into the same order as they appear in namesInOrder. |
| 239 | less func(string, string) bool |
| 240 | } |
| 241 | |
| 242 | // registeredComponentOrderFromExistingOrder takes an existing slice of sortableComponents and |
| 243 | // creates a registeredComponentOrder that contains a less function that can be used to sort a |
| 244 | // subset of that list of names so it is in the same order as the original sortableComponents. |
| 245 | func registeredComponentOrderFromExistingOrder(componentType string, existingOrder sortableComponents) registeredComponentOrder { |
| 246 | // Only the names from the existing order are needed for this so create a list of component names |
| 247 | // in the correct order. |
| 248 | namesInOrder := componentsToNames(existingOrder) |
| 249 | |
| 250 | // Populate the map from name to position in the list. |
| 251 | nameToIndex := make(map[string]int) |
| 252 | for i, n := range namesInOrder { |
| 253 | nameToIndex[n] = i |
| 254 | } |
| 255 | |
| 256 | // A function to use to map from a name to an index in the original order. |
| 257 | indexOf := func(name string) int { |
| 258 | index, ok := nameToIndex[name] |
| 259 | if !ok { |
| 260 | // Should never happen as tests that use components that are not known at runtime do not sort |
| 261 | // so should never use this function. |
| 262 | panic(fmt.Errorf("internal error: unknown %s %q should be one of %s", componentType, name, strings.Join(namesInOrder, ", "))) |
| 263 | } |
| 264 | return index |
| 265 | } |
| 266 | |
| 267 | // The less function. |
| 268 | less := func(n1, n2 string) bool { |
| 269 | i1 := indexOf(n1) |
| 270 | i2 := indexOf(n2) |
| 271 | return i1 < i2 |
| 272 | } |
| 273 | |
| 274 | return registeredComponentOrder{ |
| 275 | componentType: componentType, |
| 276 | namesInOrder: namesInOrder, |
| 277 | namesToIndex: nameToIndex, |
| 278 | less: less, |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // componentsToNames maps from the slice of components to a slice of their names. |
| 283 | func componentsToNames(components sortableComponents) []string { |
| 284 | names := make([]string, len(components)) |
| 285 | for i, c := range components { |
| 286 | names[i] = c.componentName() |
| 287 | } |
| 288 | return names |
| 289 | } |
| 290 | |
| 291 | // enforceOrdering enforces the supplied components are in the same order as is defined in this |
| 292 | // object. |
| 293 | // |
| 294 | // If the supplied components contains any components that are not registered at runtime, i.e. test |
| 295 | // specific components, then it is impossible to sort them into an order that both matches the |
| 296 | // runtime and also preserves the implicit ordering defined in the test. In that case it will not |
| 297 | // sort the components, instead it will just check that the components are in the correct order. |
| 298 | // |
| 299 | // Otherwise, this will sort the supplied components in place. |
| 300 | func (o *registeredComponentOrder) enforceOrdering(components sortableComponents) { |
| 301 | // Check to see if the list of components contains any components that are |
| 302 | // not registered at runtime. |
| 303 | var unknownComponents []string |
| 304 | testOrder := componentsToNames(components) |
| 305 | for _, name := range testOrder { |
| 306 | if _, ok := o.namesToIndex[name]; !ok { |
| 307 | unknownComponents = append(unknownComponents, name) |
| 308 | break |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // If the slice contains some unknown components then it is not possible to |
| 313 | // sort them into an order that matches the runtime while also preserving the |
| 314 | // order expected from the test, so in that case don't sort just check that |
| 315 | // the order of the known mutators does match. |
| 316 | if len(unknownComponents) > 0 { |
| 317 | // Check order. |
| 318 | o.checkTestOrder(testOrder, unknownComponents) |
| 319 | } else { |
| 320 | // Sort the components. |
| 321 | sort.Slice(components, func(i, j int) bool { |
| 322 | n1 := components[i].componentName() |
| 323 | n2 := components[j].componentName() |
| 324 | return o.less(n1, n2) |
| 325 | }) |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // checkTestOrder checks that the supplied testOrder matches the one defined by this object, |
| 330 | // panicking if it does not. |
| 331 | func (o *registeredComponentOrder) checkTestOrder(testOrder []string, unknownComponents []string) { |
| 332 | lastMatchingTest := -1 |
| 333 | matchCount := 0 |
| 334 | // Take a copy of the runtime order as it is modified during the comparison. |
| 335 | runtimeOrder := append([]string(nil), o.namesInOrder...) |
| 336 | componentType := o.componentType |
| 337 | for i, j := 0, 0; i < len(testOrder) && j < len(runtimeOrder); { |
| 338 | test := testOrder[i] |
| 339 | runtime := runtimeOrder[j] |
| 340 | |
| 341 | if test == runtime { |
| 342 | testOrder[i] = test + fmt.Sprintf(" <-- matched with runtime %s %d", componentType, j) |
| 343 | runtimeOrder[j] = runtime + fmt.Sprintf(" <-- matched with test %s %d", componentType, i) |
| 344 | lastMatchingTest = i |
| 345 | i += 1 |
| 346 | j += 1 |
| 347 | matchCount += 1 |
| 348 | } else if _, ok := o.namesToIndex[test]; !ok { |
| 349 | // The test component is not registered globally so assume it is the correct place, treat it |
| 350 | // as having matched and skip it. |
| 351 | i += 1 |
| 352 | matchCount += 1 |
| 353 | } else { |
| 354 | // Assume that the test list is in the same order as the runtime list but the runtime list |
| 355 | // contains some components that are not present in the tests. So, skip the runtime component |
| 356 | // to try and find the next one that matches the current test component. |
| 357 | j += 1 |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // If every item in the test order was either test specific or matched one in the runtime then |
| 362 | // it is in the correct order. Otherwise, it was not so fail. |
| 363 | if matchCount != len(testOrder) { |
| 364 | // The test component names were not all matched with a runtime component name so there must |
| 365 | // either be a component present in the test that is not present in the runtime or they must be |
| 366 | // in the wrong order. |
| 367 | testOrder[lastMatchingTest+1] = testOrder[lastMatchingTest+1] + " <--- unmatched" |
| 368 | panic(fmt.Errorf("the tests uses test specific components %q and so cannot be automatically sorted."+ |
| 369 | " Unfortunately it uses %s components in the wrong order.\n"+ |
| 370 | "test order:\n %s\n"+ |
| 371 | "runtime order\n %s\n", |
| 372 | SortedUniqueStrings(unknownComponents), |
| 373 | componentType, |
| 374 | strings.Join(testOrder, "\n "), |
| 375 | strings.Join(runtimeOrder, "\n "))) |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // registrationSorter encapsulates the information needed to ensure that the test mutators are |
| 380 | // registered, and thereby executed, in the same order as they are at runtime. |
| 381 | // |
| 382 | // It MUST be populated lazily AFTER all package initialization has been done otherwise it will |
| 383 | // only define the order for a subset of all the registered build components that are available for |
| 384 | // the packages being tested. |
| 385 | // |
| 386 | // e.g if this is initialized during say the cc package initialization then any tests run in the |
| 387 | // java package will not sort build components registered by the java package's init() functions. |
| 388 | type registrationSorter struct { |
| 389 | // Used to ensure that this is only created once. |
| 390 | once sync.Once |
| 391 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 392 | // The order of pre-singletons |
| 393 | preSingletonOrder registeredComponentOrder |
| 394 | |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 395 | // The order of mutators |
| 396 | mutatorOrder registeredComponentOrder |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 397 | |
| 398 | // The order of singletons |
| 399 | singletonOrder registeredComponentOrder |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // populate initializes this structure from globally registered build components. |
| 403 | // |
| 404 | // Only the first call has any effect. |
| 405 | func (s *registrationSorter) populate() { |
| 406 | s.once.Do(func() { |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 407 | // Create an ordering from the globally registered pre-singletons. |
| 408 | s.preSingletonOrder = registeredComponentOrderFromExistingOrder("pre-singleton", preSingletons) |
| 409 | |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 410 | // Created an ordering from the globally registered mutators. |
| 411 | globallyRegisteredMutators := collateGloballyRegisteredMutators() |
| 412 | s.mutatorOrder = registeredComponentOrderFromExistingOrder("mutator", globallyRegisteredMutators) |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 413 | |
| 414 | // Create an ordering from the globally registered singletons. |
| 415 | globallyRegisteredSingletons := collateGloballyRegisteredSingletons() |
| 416 | s.singletonOrder = registeredComponentOrderFromExistingOrder("singleton", globallyRegisteredSingletons) |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 417 | }) |
| 418 | } |
| 419 | |
| 420 | // Provides support for enforcing the same order in which build components are registered globally |
| 421 | // to the order in which they are registered during tests. |
| 422 | // |
| 423 | // MUST only be accessed via the globallyRegisteredComponentsOrder func. |
| 424 | var globalRegistrationSorter registrationSorter |
| 425 | |
| 426 | // globallyRegisteredComponentsOrder returns the globalRegistrationSorter after ensuring it is |
| 427 | // correctly populated. |
| 428 | func globallyRegisteredComponentsOrder() *registrationSorter { |
| 429 | globalRegistrationSorter.populate() |
| 430 | return &globalRegistrationSorter |
| 431 | } |
| 432 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 433 | func (ctx *TestContext) Register() { |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 434 | globalOrder := globallyRegisteredComponentsOrder() |
| 435 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 436 | // Ensure that the pre-singletons used in the test are in the same order as they are used at |
| 437 | // runtime. |
| 438 | globalOrder.preSingletonOrder.enforceOrdering(ctx.preSingletons) |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 439 | ctx.preSingletons.registerAll(ctx.Context) |
| 440 | |
Paul Duffin | c05b034 | 2021-03-06 13:28:13 +0000 | [diff] [blame] | 441 | mutators := collateRegisteredMutators(ctx.preArch, ctx.preDeps, ctx.postDeps, ctx.finalDeps) |
Paul Duffin | 281deb2 | 2021-03-06 20:29:19 +0000 | [diff] [blame] | 442 | // Ensure that the mutators used in the test are in the same order as they are used at runtime. |
| 443 | globalOrder.mutatorOrder.enforceOrdering(mutators) |
Paul Duffin | c05b034 | 2021-03-06 13:28:13 +0000 | [diff] [blame] | 444 | mutators.registerAll(ctx.Context) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 445 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 446 | // Ensure that the singletons used in the test are in the same order as they are used at runtime. |
| 447 | globalOrder.singletonOrder.enforceOrdering(ctx.singletons) |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 448 | ctx.singletons.registerAll(ctx.Context) |
| 449 | |
Paul Duffin | 41d77c7 | 2021-03-07 12:23:48 +0000 | [diff] [blame] | 450 | // 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] | 451 | ctx.preSingletonOrder = componentsToNames(preSingletons) |
| 452 | ctx.mutatorOrder = componentsToNames(mutators) |
| 453 | ctx.singletonOrder = componentsToNames(singletons) |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 456 | // RegisterForBazelConversion prepares a test context for bp2build conversion. |
| 457 | func (ctx *TestContext) RegisterForBazelConversion() { |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 458 | ctx.SetRunningAsBp2build() |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 459 | RegisterMutatorsForBazelConversion(ctx.Context, ctx.bp2buildPreArch) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 460 | } |
| 461 | |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 462 | func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) { |
| 463 | // This function adapts the old style ParseFileList calls that are spread throughout the tests |
| 464 | // to the new style that takes a config. |
| 465 | return ctx.Context.ParseFileList(rootDir, filePaths, ctx.config) |
| 466 | } |
| 467 | |
| 468 | func (ctx *TestContext) ParseBlueprintsFiles(rootDir string) (deps []string, errs []error) { |
| 469 | // This function adapts the old style ParseBlueprintsFiles calls that are spread throughout the |
| 470 | // tests to the new style that takes a config. |
| 471 | return ctx.Context.ParseBlueprintsFiles(rootDir, ctx.config) |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) { |
| 475 | ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory)) |
| 476 | } |
| 477 | |
Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 478 | func (ctx *TestContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) { |
| 479 | s, m := SingletonModuleFactoryAdaptor(name, factory) |
| 480 | ctx.RegisterSingletonType(name, s) |
| 481 | ctx.RegisterModuleType(name, m) |
| 482 | } |
| 483 | |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 484 | func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) { |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 485 | ctx.singletons = append(ctx.singletons, newSingleton(name, factory)) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Paul Duffin | eafc16b | 2021-02-24 01:43:18 +0000 | [diff] [blame] | 488 | func (ctx *TestContext) RegisterPreSingletonType(name string, factory SingletonFactory) { |
Paul Duffin | d182fb3 | 2021-03-07 12:24:44 +0000 | [diff] [blame] | 489 | ctx.preSingletons = append(ctx.preSingletons, newPreSingleton(name, factory)) |
Paul Duffin | eafc16b | 2021-02-24 01:43:18 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Martin Stjernholm | 14cdd71 | 2021-09-10 22:39:59 +0100 | [diff] [blame] | 492 | // ModuleVariantForTests selects a specific variant of the module with the given |
| 493 | // name by matching the variations map against the variations of each module |
| 494 | // variant. A module variant matches the map if every variation that exists in |
| 495 | // both have the same value. Both the module and the map are allowed to have |
| 496 | // extra variations that the other doesn't have. Panics if not exactly one |
| 497 | // module variant matches. |
| 498 | func (ctx *TestContext) ModuleVariantForTests(name string, matchVariations map[string]string) TestingModule { |
| 499 | modules := []Module{} |
| 500 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 501 | if ctx.ModuleName(m) == name { |
| 502 | am := m.(Module) |
| 503 | amMut := am.base().commonProperties.DebugMutators |
| 504 | amVar := am.base().commonProperties.DebugVariations |
| 505 | matched := true |
| 506 | for i, mut := range amMut { |
| 507 | if wantedVar, found := matchVariations[mut]; found && amVar[i] != wantedVar { |
| 508 | matched = false |
| 509 | break |
| 510 | } |
| 511 | } |
| 512 | if matched { |
| 513 | modules = append(modules, am) |
| 514 | } |
| 515 | } |
| 516 | }) |
| 517 | |
| 518 | if len(modules) == 0 { |
| 519 | // Show all the modules or module variants that do exist. |
| 520 | var allModuleNames []string |
| 521 | var allVariants []string |
| 522 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 523 | allModuleNames = append(allModuleNames, ctx.ModuleName(m)) |
| 524 | if ctx.ModuleName(m) == name { |
| 525 | allVariants = append(allVariants, m.(Module).String()) |
| 526 | } |
| 527 | }) |
| 528 | |
| 529 | if len(allVariants) == 0 { |
| 530 | panic(fmt.Errorf("failed to find module %q. All modules:\n %s", |
| 531 | name, strings.Join(SortedUniqueStrings(allModuleNames), "\n "))) |
| 532 | } else { |
| 533 | sort.Strings(allVariants) |
| 534 | panic(fmt.Errorf("failed to find module %q matching %v. All variants:\n %s", |
| 535 | name, matchVariations, strings.Join(allVariants, "\n "))) |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | if len(modules) > 1 { |
| 540 | moduleStrings := []string{} |
| 541 | for _, m := range modules { |
| 542 | moduleStrings = append(moduleStrings, m.String()) |
| 543 | } |
| 544 | sort.Strings(moduleStrings) |
| 545 | panic(fmt.Errorf("module %q has more than one variant that match %v:\n %s", |
| 546 | name, matchVariations, strings.Join(moduleStrings, "\n "))) |
| 547 | } |
| 548 | |
| 549 | return newTestingModule(ctx.config, modules[0]) |
| 550 | } |
| 551 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 552 | func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule { |
| 553 | var module Module |
| 554 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 555 | if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant { |
| 556 | module = m.(Module) |
| 557 | } |
| 558 | }) |
| 559 | |
| 560 | if module == nil { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 561 | // find all the modules that do exist |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 562 | var allModuleNames []string |
| 563 | var allVariants []string |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 564 | ctx.VisitAllModules(func(m blueprint.Module) { |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 565 | allModuleNames = append(allModuleNames, ctx.ModuleName(m)) |
| 566 | if ctx.ModuleName(m) == name { |
| 567 | allVariants = append(allVariants, ctx.ModuleSubDir(m)) |
| 568 | } |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 569 | }) |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 570 | sort.Strings(allVariants) |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 571 | |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 572 | if len(allVariants) == 0 { |
| 573 | panic(fmt.Errorf("failed to find module %q. All modules:\n %s", |
Martin Stjernholm | 98e0d88 | 2021-09-09 21:34:02 +0100 | [diff] [blame] | 574 | name, strings.Join(SortedUniqueStrings(allModuleNames), "\n "))) |
Colin Cross | beae6ec | 2020-08-11 12:02:11 -0700 | [diff] [blame] | 575 | } else { |
| 576 | panic(fmt.Errorf("failed to find module %q variant %q. All variants:\n %s", |
| 577 | name, variant, strings.Join(allVariants, "\n "))) |
| 578 | } |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 581 | return newTestingModule(ctx.config, module) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Jiyong Park | 37b2520 | 2018-07-11 10:49:27 +0900 | [diff] [blame] | 584 | func (ctx *TestContext) ModuleVariantsForTests(name string) []string { |
| 585 | var variants []string |
| 586 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 587 | if ctx.ModuleName(m) == name { |
| 588 | variants = append(variants, ctx.ModuleSubDir(m)) |
| 589 | } |
| 590 | }) |
| 591 | return variants |
| 592 | } |
| 593 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 594 | // SingletonForTests returns a TestingSingleton for the singleton registered with the given name. |
| 595 | func (ctx *TestContext) SingletonForTests(name string) TestingSingleton { |
| 596 | allSingletonNames := []string{} |
| 597 | for _, s := range ctx.Singletons() { |
| 598 | n := ctx.SingletonName(s) |
| 599 | if n == name { |
| 600 | return TestingSingleton{ |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 601 | baseTestingComponent: newBaseTestingComponent(ctx.config, s.(testBuildProvider)), |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 602 | singleton: s.(*singletonAdaptor).Singleton, |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | allSingletonNames = append(allSingletonNames, n) |
| 606 | } |
| 607 | |
| 608 | panic(fmt.Errorf("failed to find singleton %q."+ |
| 609 | "\nall singletons: %v", name, allSingletonNames)) |
| 610 | } |
| 611 | |
Martin Stjernholm | 1ebef5b | 2022-02-10 23:34:28 +0000 | [diff] [blame] | 612 | type InstallMakeRule struct { |
| 613 | Target string |
| 614 | Deps []string |
| 615 | OrderOnlyDeps []string |
| 616 | } |
| 617 | |
| 618 | func parseMkRules(t *testing.T, config Config, nodes []mkparser.Node) []InstallMakeRule { |
| 619 | var rules []InstallMakeRule |
| 620 | for _, node := range nodes { |
| 621 | if mkParserRule, ok := node.(*mkparser.Rule); ok { |
| 622 | var rule InstallMakeRule |
| 623 | |
| 624 | if targets := mkParserRule.Target.Words(); len(targets) == 0 { |
| 625 | t.Fatalf("no targets for rule %s", mkParserRule.Dump()) |
| 626 | } else if len(targets) > 1 { |
| 627 | t.Fatalf("unsupported multiple targets for rule %s", mkParserRule.Dump()) |
| 628 | } else if !targets[0].Const() { |
| 629 | t.Fatalf("unsupported non-const target for rule %s", mkParserRule.Dump()) |
| 630 | } else { |
| 631 | rule.Target = normalizeStringRelativeToTop(config, targets[0].Value(nil)) |
| 632 | } |
| 633 | |
| 634 | prereqList := &rule.Deps |
| 635 | for _, prereq := range mkParserRule.Prerequisites.Words() { |
| 636 | if !prereq.Const() { |
| 637 | t.Fatalf("unsupported non-const prerequisite for rule %s", mkParserRule.Dump()) |
| 638 | } |
| 639 | |
| 640 | if prereq.Value(nil) == "|" { |
| 641 | prereqList = &rule.OrderOnlyDeps |
| 642 | continue |
| 643 | } |
| 644 | |
| 645 | *prereqList = append(*prereqList, normalizeStringRelativeToTop(config, prereq.Value(nil))) |
| 646 | } |
| 647 | |
| 648 | rules = append(rules, rule) |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | return rules |
| 653 | } |
| 654 | |
| 655 | func (ctx *TestContext) InstallMakeRulesForTesting(t *testing.T) []InstallMakeRule { |
| 656 | installs := ctx.SingletonForTests("makevars").Singleton().(*makeVarsSingleton).installsForTesting |
| 657 | buf := bytes.NewBuffer(append([]byte(nil), installs...)) |
| 658 | parser := mkparser.NewParser("makevars", buf) |
| 659 | |
| 660 | nodes, errs := parser.Parse() |
| 661 | if len(errs) > 0 { |
| 662 | t.Fatalf("error parsing install rules: %s", errs[0]) |
| 663 | } |
| 664 | |
| 665 | return parseMkRules(t, ctx.config, nodes) |
| 666 | } |
| 667 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 668 | func (ctx *TestContext) Config() Config { |
| 669 | return ctx.config |
| 670 | } |
| 671 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 672 | type testBuildProvider interface { |
| 673 | BuildParamsForTests() []BuildParams |
| 674 | RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams |
| 675 | } |
| 676 | |
| 677 | type TestingBuildParams struct { |
| 678 | BuildParams |
| 679 | RuleParams blueprint.RuleParams |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 680 | |
| 681 | config Config |
| 682 | } |
| 683 | |
| 684 | // RelativeToTop creates a new instance of this which has had any usages of the current test's |
| 685 | // temporary and test specific build directory replaced with a path relative to the notional top. |
| 686 | // |
| 687 | // The parts of this structure which are changed are: |
| 688 | // * BuildParams |
| 689 | // * Args |
Paul Duffin | bbb0f8f | 2021-03-24 10:34:52 +0000 | [diff] [blame] | 690 | // * All Path, Paths, WritablePath and WritablePaths fields. |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 691 | // |
| 692 | // * RuleParams |
| 693 | // * Command |
| 694 | // * Depfile |
| 695 | // * Rspfile |
| 696 | // * RspfileContent |
| 697 | // * SymlinkOutputs |
| 698 | // * CommandDeps |
| 699 | // * CommandOrderOnly |
| 700 | // |
| 701 | // See PathRelativeToTop for more details. |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 702 | // |
| 703 | // deprecated: this is no longer needed as TestingBuildParams are created in this form. |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 704 | func (p TestingBuildParams) RelativeToTop() TestingBuildParams { |
| 705 | // If this is not a valid params then just return it back. That will make it easy to use with the |
| 706 | // Maybe...() methods. |
| 707 | if p.Rule == nil { |
| 708 | return p |
| 709 | } |
| 710 | if p.config.config == nil { |
Paul Duffin | e8366da | 2021-03-24 10:40:38 +0000 | [diff] [blame] | 711 | return p |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 712 | } |
| 713 | // Take a copy of the build params and replace any args that contains test specific temporary |
| 714 | // paths with paths relative to the top. |
| 715 | bparams := p.BuildParams |
Paul Duffin | bbb0f8f | 2021-03-24 10:34:52 +0000 | [diff] [blame] | 716 | bparams.Depfile = normalizeWritablePathRelativeToTop(bparams.Depfile) |
| 717 | bparams.Output = normalizeWritablePathRelativeToTop(bparams.Output) |
| 718 | bparams.Outputs = bparams.Outputs.RelativeToTop() |
| 719 | bparams.SymlinkOutput = normalizeWritablePathRelativeToTop(bparams.SymlinkOutput) |
| 720 | bparams.SymlinkOutputs = bparams.SymlinkOutputs.RelativeToTop() |
| 721 | bparams.ImplicitOutput = normalizeWritablePathRelativeToTop(bparams.ImplicitOutput) |
| 722 | bparams.ImplicitOutputs = bparams.ImplicitOutputs.RelativeToTop() |
| 723 | bparams.Input = normalizePathRelativeToTop(bparams.Input) |
| 724 | bparams.Inputs = bparams.Inputs.RelativeToTop() |
| 725 | bparams.Implicit = normalizePathRelativeToTop(bparams.Implicit) |
| 726 | bparams.Implicits = bparams.Implicits.RelativeToTop() |
| 727 | bparams.OrderOnly = bparams.OrderOnly.RelativeToTop() |
| 728 | bparams.Validation = normalizePathRelativeToTop(bparams.Validation) |
| 729 | bparams.Validations = bparams.Validations.RelativeToTop() |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 730 | bparams.Args = normalizeStringMapRelativeToTop(p.config, bparams.Args) |
| 731 | |
| 732 | // Ditto for any fields in the RuleParams. |
| 733 | rparams := p.RuleParams |
| 734 | rparams.Command = normalizeStringRelativeToTop(p.config, rparams.Command) |
| 735 | rparams.Depfile = normalizeStringRelativeToTop(p.config, rparams.Depfile) |
| 736 | rparams.Rspfile = normalizeStringRelativeToTop(p.config, rparams.Rspfile) |
| 737 | rparams.RspfileContent = normalizeStringRelativeToTop(p.config, rparams.RspfileContent) |
| 738 | rparams.SymlinkOutputs = normalizeStringArrayRelativeToTop(p.config, rparams.SymlinkOutputs) |
| 739 | rparams.CommandDeps = normalizeStringArrayRelativeToTop(p.config, rparams.CommandDeps) |
| 740 | rparams.CommandOrderOnly = normalizeStringArrayRelativeToTop(p.config, rparams.CommandOrderOnly) |
| 741 | |
| 742 | return TestingBuildParams{ |
| 743 | BuildParams: bparams, |
| 744 | RuleParams: rparams, |
| 745 | } |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Paul Duffin | bbb0f8f | 2021-03-24 10:34:52 +0000 | [diff] [blame] | 748 | func normalizeWritablePathRelativeToTop(path WritablePath) WritablePath { |
| 749 | if path == nil { |
| 750 | return nil |
| 751 | } |
| 752 | return path.RelativeToTop().(WritablePath) |
| 753 | } |
| 754 | |
| 755 | func normalizePathRelativeToTop(path Path) Path { |
| 756 | if path == nil { |
| 757 | return nil |
| 758 | } |
| 759 | return path.RelativeToTop() |
| 760 | } |
| 761 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 762 | // baseTestingComponent provides functionality common to both TestingModule and TestingSingleton. |
| 763 | type baseTestingComponent struct { |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 764 | config Config |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 765 | provider testBuildProvider |
| 766 | } |
| 767 | |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 768 | func newBaseTestingComponent(config Config, provider testBuildProvider) baseTestingComponent { |
| 769 | return baseTestingComponent{config, provider} |
| 770 | } |
| 771 | |
| 772 | // A function that will normalize a string containing paths, e.g. ninja command, by replacing |
| 773 | // any references to the test specific temporary build directory that changes with each run to a |
| 774 | // fixed path relative to a notional top directory. |
| 775 | // |
| 776 | // This is similar to StringPathRelativeToTop except that assumes the string is a single path |
| 777 | // containing at most one instance of the temporary build directory at the start of the path while |
| 778 | // this assumes that there can be any number at any position. |
| 779 | func normalizeStringRelativeToTop(config Config, s string) string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 780 | // The soongOutDir usually looks something like: /tmp/testFoo2345/001 |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 781 | // |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 782 | // Replace any usage of the soongOutDir with out/soong, e.g. replace "/tmp/testFoo2345/001" with |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 783 | // "out/soong". |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 784 | outSoongDir := filepath.Clean(config.soongOutDir) |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 785 | re := regexp.MustCompile(`\Q` + outSoongDir + `\E\b`) |
| 786 | s = re.ReplaceAllString(s, "out/soong") |
| 787 | |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 788 | // Replace any usage of the soongOutDir/.. with out, e.g. replace "/tmp/testFoo2345" with |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 789 | // "out". This must come after the previous replacement otherwise this would replace |
| 790 | // "/tmp/testFoo2345/001" with "out/001" instead of "out/soong". |
| 791 | outDir := filepath.Dir(outSoongDir) |
| 792 | re = regexp.MustCompile(`\Q` + outDir + `\E\b`) |
| 793 | s = re.ReplaceAllString(s, "out") |
| 794 | |
| 795 | return s |
| 796 | } |
| 797 | |
| 798 | // normalizeStringArrayRelativeToTop creates a new slice constructed by applying |
| 799 | // normalizeStringRelativeToTop to each item in the slice. |
| 800 | func normalizeStringArrayRelativeToTop(config Config, slice []string) []string { |
| 801 | newSlice := make([]string, len(slice)) |
| 802 | for i, s := range slice { |
| 803 | newSlice[i] = normalizeStringRelativeToTop(config, s) |
| 804 | } |
| 805 | return newSlice |
| 806 | } |
| 807 | |
| 808 | // normalizeStringMapRelativeToTop creates a new map constructed by applying |
| 809 | // normalizeStringRelativeToTop to each value in the map. |
| 810 | func normalizeStringMapRelativeToTop(config Config, m map[string]string) map[string]string { |
| 811 | newMap := map[string]string{} |
| 812 | for k, v := range m { |
| 813 | newMap[k] = normalizeStringRelativeToTop(config, v) |
| 814 | } |
| 815 | return newMap |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | func (b baseTestingComponent) newTestingBuildParams(bparams BuildParams) TestingBuildParams { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 819 | return TestingBuildParams{ |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 820 | config: b.config, |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 821 | BuildParams: bparams, |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 822 | RuleParams: b.provider.RuleParamsForTests()[bparams.Rule], |
Paul Duffin | e8366da | 2021-03-24 10:40:38 +0000 | [diff] [blame] | 823 | }.RelativeToTop() |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 824 | } |
| 825 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 826 | func (b baseTestingComponent) maybeBuildParamsFromRule(rule string) (TestingBuildParams, []string) { |
Thiébaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 827 | var searchedRules []string |
Paul Duffin | 4dbf6cf | 2021-06-08 10:06:37 +0100 | [diff] [blame] | 828 | buildParams := b.provider.BuildParamsForTests() |
| 829 | for _, p := range buildParams { |
| 830 | ruleAsString := p.Rule.String() |
| 831 | searchedRules = append(searchedRules, ruleAsString) |
| 832 | if strings.Contains(ruleAsString, rule) { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 833 | return b.newTestingBuildParams(p), searchedRules |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 834 | } |
| 835 | } |
Thiébaud Weksteen | 3600b80 | 2020-08-27 15:50:24 +0200 | [diff] [blame] | 836 | return TestingBuildParams{}, searchedRules |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 837 | } |
| 838 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 839 | func (b baseTestingComponent) buildParamsFromRule(rule string) TestingBuildParams { |
| 840 | p, searchRules := b.maybeBuildParamsFromRule(rule) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 841 | if p.Rule == nil { |
Paul Duffin | 4dbf6cf | 2021-06-08 10:06:37 +0100 | [diff] [blame] | 842 | panic(fmt.Errorf("couldn't find rule %q.\nall rules:\n%s", rule, strings.Join(searchRules, "\n"))) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 843 | } |
| 844 | return p |
| 845 | } |
| 846 | |
Martin Stjernholm | 827ba62 | 2022-02-03 00:20:11 +0000 | [diff] [blame] | 847 | func (b baseTestingComponent) maybeBuildParamsFromDescription(desc string) (TestingBuildParams, []string) { |
| 848 | var searchedDescriptions []string |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 849 | for _, p := range b.provider.BuildParamsForTests() { |
Martin Stjernholm | 827ba62 | 2022-02-03 00:20:11 +0000 | [diff] [blame] | 850 | searchedDescriptions = append(searchedDescriptions, p.Description) |
Colin Cross | b88b3c5 | 2019-06-10 15:15:17 -0700 | [diff] [blame] | 851 | if strings.Contains(p.Description, desc) { |
Martin Stjernholm | 827ba62 | 2022-02-03 00:20:11 +0000 | [diff] [blame] | 852 | return b.newTestingBuildParams(p), searchedDescriptions |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 853 | } |
| 854 | } |
Martin Stjernholm | 827ba62 | 2022-02-03 00:20:11 +0000 | [diff] [blame] | 855 | return TestingBuildParams{}, searchedDescriptions |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 856 | } |
| 857 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 858 | func (b baseTestingComponent) buildParamsFromDescription(desc string) TestingBuildParams { |
Martin Stjernholm | 827ba62 | 2022-02-03 00:20:11 +0000 | [diff] [blame] | 859 | p, searchedDescriptions := b.maybeBuildParamsFromDescription(desc) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 860 | if p.Rule == nil { |
Martin Stjernholm | 827ba62 | 2022-02-03 00:20:11 +0000 | [diff] [blame] | 861 | panic(fmt.Errorf("couldn't find description %q\nall descriptions:\n%s", desc, strings.Join(searchedDescriptions, "\n"))) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 862 | } |
| 863 | return p |
| 864 | } |
| 865 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 866 | func (b baseTestingComponent) maybeBuildParamsFromOutput(file string) (TestingBuildParams, []string) { |
Martin Stjernholm | a4aaa47 | 2021-09-17 02:51:48 +0100 | [diff] [blame] | 867 | searchedOutputs := WritablePaths(nil) |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 868 | for _, p := range b.provider.BuildParamsForTests() { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 869 | outputs := append(WritablePaths(nil), p.Outputs...) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 870 | outputs = append(outputs, p.ImplicitOutputs...) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 871 | if p.Output != nil { |
| 872 | outputs = append(outputs, p.Output) |
| 873 | } |
| 874 | for _, f := range outputs { |
Paul Duffin | 4e6e35c | 2021-03-22 11:34:57 +0000 | [diff] [blame] | 875 | if f.String() == file || f.Rel() == file || PathRelativeToTop(f) == file { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 876 | return b.newTestingBuildParams(p), nil |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 877 | } |
Martin Stjernholm | a4aaa47 | 2021-09-17 02:51:48 +0100 | [diff] [blame] | 878 | searchedOutputs = append(searchedOutputs, f) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 879 | } |
| 880 | } |
Martin Stjernholm | a4aaa47 | 2021-09-17 02:51:48 +0100 | [diff] [blame] | 881 | |
| 882 | formattedOutputs := []string{} |
| 883 | for _, f := range searchedOutputs { |
| 884 | formattedOutputs = append(formattedOutputs, |
| 885 | fmt.Sprintf("%s (rel=%s)", PathRelativeToTop(f), f.Rel())) |
| 886 | } |
| 887 | |
| 888 | return TestingBuildParams{}, formattedOutputs |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 889 | } |
| 890 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 891 | func (b baseTestingComponent) buildParamsFromOutput(file string) TestingBuildParams { |
| 892 | p, searchedOutputs := b.maybeBuildParamsFromOutput(file) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 893 | if p.Rule == nil { |
Paul Duffin | 4e6e35c | 2021-03-22 11:34:57 +0000 | [diff] [blame] | 894 | panic(fmt.Errorf("couldn't find output %q.\nall outputs:\n %s\n", |
| 895 | file, strings.Join(searchedOutputs, "\n "))) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 896 | } |
| 897 | return p |
| 898 | } |
| 899 | |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 900 | func (b baseTestingComponent) allOutputs() []string { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 901 | var outputFullPaths []string |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 902 | for _, p := range b.provider.BuildParamsForTests() { |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 903 | outputs := append(WritablePaths(nil), p.Outputs...) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 904 | outputs = append(outputs, p.ImplicitOutputs...) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 905 | if p.Output != nil { |
| 906 | outputs = append(outputs, p.Output) |
| 907 | } |
| 908 | outputFullPaths = append(outputFullPaths, outputs.Strings()...) |
| 909 | } |
| 910 | return outputFullPaths |
| 911 | } |
| 912 | |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 913 | // MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty |
| 914 | // BuildParams if no rule is found. |
| 915 | func (b baseTestingComponent) MaybeRule(rule string) TestingBuildParams { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 916 | r, _ := b.maybeBuildParamsFromRule(rule) |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 917 | return r |
| 918 | } |
| 919 | |
| 920 | // Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found. |
| 921 | func (b baseTestingComponent) Rule(rule string) TestingBuildParams { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 922 | return b.buildParamsFromRule(rule) |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | // MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty |
| 926 | // BuildParams if no rule is found. |
| 927 | func (b baseTestingComponent) MaybeDescription(desc string) TestingBuildParams { |
Martin Stjernholm | 827ba62 | 2022-02-03 00:20:11 +0000 | [diff] [blame] | 928 | p, _ := b.maybeBuildParamsFromDescription(desc) |
| 929 | return p |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | // Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is |
| 933 | // found. |
| 934 | func (b baseTestingComponent) Description(desc string) TestingBuildParams { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 935 | return b.buildParamsFromDescription(desc) |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | // MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel() |
| 939 | // value matches the provided string. Returns an empty BuildParams if no rule is found. |
| 940 | func (b baseTestingComponent) MaybeOutput(file string) TestingBuildParams { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 941 | p, _ := b.maybeBuildParamsFromOutput(file) |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 942 | return p |
| 943 | } |
| 944 | |
| 945 | // Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel() |
| 946 | // value matches the provided string. Panics if no rule is found. |
| 947 | func (b baseTestingComponent) Output(file string) TestingBuildParams { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 948 | return b.buildParamsFromOutput(file) |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | // AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms. |
| 952 | func (b baseTestingComponent) AllOutputs() []string { |
Paul Duffin | 0eda26b9 | 2021-03-22 09:34:29 +0000 | [diff] [blame] | 953 | return b.allOutputs() |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 956 | // TestingModule is wrapper around an android.Module that provides methods to find information about individual |
| 957 | // ctx.Build parameters for verification in tests. |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 958 | type TestingModule struct { |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 959 | baseTestingComponent |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 960 | module Module |
| 961 | } |
| 962 | |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 963 | func newTestingModule(config Config, module Module) TestingModule { |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 964 | return TestingModule{ |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 965 | newBaseTestingComponent(config, module), |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 966 | module, |
| 967 | } |
| 968 | } |
| 969 | |
Colin Cross | b77ffc4 | 2019-01-05 22:09:19 -0800 | [diff] [blame] | 970 | // Module returns the Module wrapped by the TestingModule. |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 971 | func (m TestingModule) Module() Module { |
| 972 | return m.module |
| 973 | } |
| 974 | |
Paul Duffin | 97d8b40 | 2021-03-22 16:04:50 +0000 | [diff] [blame] | 975 | // VariablesForTestsRelativeToTop returns a copy of the Module.VariablesForTests() with every value |
| 976 | // having any temporary build dir usages replaced with paths relative to a notional top. |
| 977 | func (m TestingModule) VariablesForTestsRelativeToTop() map[string]string { |
| 978 | return normalizeStringMapRelativeToTop(m.config, m.module.VariablesForTests()) |
| 979 | } |
| 980 | |
Paul Duffin | 962783a | 2021-03-29 00:00:17 +0100 | [diff] [blame] | 981 | // OutputFiles calls OutputFileProducer.OutputFiles on the encapsulated module, exits the test |
| 982 | // immediately if there is an error and otherwise returns the result of calling Paths.RelativeToTop |
| 983 | // on the returned Paths. |
| 984 | func (m TestingModule) OutputFiles(t *testing.T, tag string) Paths { |
| 985 | producer, ok := m.module.(OutputFileProducer) |
| 986 | if !ok { |
| 987 | t.Fatalf("%q must implement OutputFileProducer\n", m.module.Name()) |
| 988 | } |
| 989 | paths, err := producer.OutputFiles(tag) |
| 990 | if err != nil { |
| 991 | t.Fatal(err) |
| 992 | } |
| 993 | |
| 994 | return paths.RelativeToTop() |
| 995 | } |
| 996 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 997 | // TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual |
| 998 | // ctx.Build parameters for verification in tests. |
| 999 | type TestingSingleton struct { |
Paul Duffin | 31a2288 | 2021-03-22 09:29:00 +0000 | [diff] [blame] | 1000 | baseTestingComponent |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 1001 | singleton Singleton |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | // Singleton returns the Singleton wrapped by the TestingSingleton. |
| 1005 | func (s TestingSingleton) Singleton() Singleton { |
| 1006 | return s.singleton |
| 1007 | } |
| 1008 | |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 1009 | func FailIfErrored(t *testing.T, errs []error) { |
| 1010 | t.Helper() |
| 1011 | if len(errs) > 0 { |
| 1012 | for _, err := range errs { |
| 1013 | t.Error(err) |
| 1014 | } |
| 1015 | t.FailNow() |
| 1016 | } |
| 1017 | } |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 1018 | |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 1019 | // Fail if no errors that matched the regular expression were found. |
| 1020 | // |
| 1021 | // Returns true if a matching error was found, false otherwise. |
| 1022 | func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) bool { |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 1023 | t.Helper() |
| 1024 | |
| 1025 | matcher, err := regexp.Compile(pattern) |
| 1026 | if err != nil { |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 1027 | t.Fatalf("failed to compile regular expression %q because %s", pattern, err) |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | found := false |
| 1031 | for _, err := range errs { |
| 1032 | if matcher.FindStringIndex(err.Error()) != nil { |
| 1033 | found = true |
| 1034 | break |
| 1035 | } |
| 1036 | } |
| 1037 | if !found { |
| 1038 | t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs)) |
| 1039 | for i, err := range errs { |
Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 1040 | t.Errorf("errs[%d] = %q", i, err) |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 1041 | } |
| 1042 | } |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 1043 | |
| 1044 | return found |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 1045 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 1046 | |
Paul Duffin | 91e3819 | 2019-08-05 15:07:57 +0100 | [diff] [blame] | 1047 | func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) { |
| 1048 | t.Helper() |
| 1049 | |
| 1050 | if expectedErrorPatterns == nil { |
| 1051 | FailIfErrored(t, errs) |
| 1052 | } else { |
| 1053 | for _, expectedError := range expectedErrorPatterns { |
| 1054 | FailIfNoMatchingErrors(t, expectedError, errs) |
| 1055 | } |
| 1056 | if len(errs) > len(expectedErrorPatterns) { |
| 1057 | t.Errorf("additional errors found, expected %d, found %d", |
| 1058 | len(expectedErrorPatterns), len(errs)) |
| 1059 | for i, expectedError := range expectedErrorPatterns { |
| 1060 | t.Errorf("expectedErrors[%d] = %s", i, expectedError) |
| 1061 | } |
| 1062 | for i, err := range errs { |
| 1063 | t.Errorf("errs[%d] = %s", i, err) |
| 1064 | } |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 1065 | t.FailNow() |
Paul Duffin | 91e3819 | 2019-08-05 15:07:57 +0100 | [diff] [blame] | 1066 | } |
| 1067 | } |
Paul Duffin | 91e3819 | 2019-08-05 15:07:57 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 1070 | func SetKatiEnabledForTests(config Config) { |
| 1071 | config.katiEnabled = true |
Paul Duffin | 8c3fec4 | 2020-03-04 20:15:08 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 1074 | func AndroidMkEntriesForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) []AndroidMkEntries { |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 1075 | var p AndroidMkEntriesProvider |
| 1076 | var ok bool |
| 1077 | if p, ok = mod.(AndroidMkEntriesProvider); !ok { |
Roland Levillain | dfe75b3 | 2019-07-23 16:53:32 +0100 | [diff] [blame] | 1078 | t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name()) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 1079 | } |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 1080 | |
| 1081 | entriesList := p.AndroidMkEntries() |
| 1082 | for i, _ := range entriesList { |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 1083 | entriesList[i].fillInEntries(ctx, mod) |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 1084 | } |
| 1085 | return entriesList |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 1086 | } |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 1087 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 1088 | func AndroidMkDataForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) AndroidMkData { |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 1089 | var p AndroidMkDataProvider |
| 1090 | var ok bool |
| 1091 | if p, ok = mod.(AndroidMkDataProvider); !ok { |
Roland Levillain | dfe75b3 | 2019-07-23 16:53:32 +0100 | [diff] [blame] | 1092 | t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name()) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 1093 | } |
| 1094 | data := p.AndroidMk() |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 1095 | data.fillInData(ctx, mod) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 1096 | return data |
| 1097 | } |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1098 | |
| 1099 | // Normalize the path for testing. |
| 1100 | // |
| 1101 | // If the path is relative to the build directory then return the relative path |
| 1102 | // to avoid tests having to deal with the dynamically generated build directory. |
| 1103 | // |
| 1104 | // Otherwise, return the supplied path as it is almost certainly a source path |
| 1105 | // that is relative to the root of the source tree. |
| 1106 | // |
| 1107 | // The build and source paths should be distinguishable based on their contents. |
Paul Duffin | 567465d | 2021-03-16 01:21:34 +0000 | [diff] [blame] | 1108 | // |
| 1109 | // deprecated: use PathRelativeToTop instead as it handles make install paths and differentiates |
| 1110 | // between output and source properly. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1111 | func NormalizePathForTesting(path Path) string { |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 1112 | if path == nil { |
| 1113 | return "<nil path>" |
| 1114 | } |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1115 | p := path.String() |
| 1116 | if w, ok := path.(WritablePath); ok { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1117 | rel, err := filepath.Rel(w.getSoongOutDir(), p) |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1118 | if err != nil { |
| 1119 | panic(err) |
| 1120 | } |
| 1121 | return rel |
| 1122 | } |
| 1123 | return p |
| 1124 | } |
| 1125 | |
Paul Duffin | 567465d | 2021-03-16 01:21:34 +0000 | [diff] [blame] | 1126 | // NormalizePathsForTesting creates a slice of strings where each string is the result of applying |
| 1127 | // NormalizePathForTesting to the corresponding Path in the input slice. |
| 1128 | // |
| 1129 | // deprecated: use PathsRelativeToTop instead as it handles make install paths and differentiates |
| 1130 | // between output and source properly. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1131 | func NormalizePathsForTesting(paths Paths) []string { |
| 1132 | var result []string |
| 1133 | for _, path := range paths { |
| 1134 | relative := NormalizePathForTesting(path) |
| 1135 | result = append(result, relative) |
| 1136 | } |
| 1137 | return result |
| 1138 | } |
Paul Duffin | 567465d | 2021-03-16 01:21:34 +0000 | [diff] [blame] | 1139 | |
| 1140 | // PathRelativeToTop returns a string representation of the path relative to a notional top |
| 1141 | // directory. |
| 1142 | // |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1143 | // It return "<nil path>" if the supplied path is nil, otherwise it returns the result of calling |
| 1144 | // Path.RelativeToTop to obtain a relative Path and then calling Path.String on that to get the |
| 1145 | // string representation. |
Paul Duffin | 567465d | 2021-03-16 01:21:34 +0000 | [diff] [blame] | 1146 | func PathRelativeToTop(path Path) string { |
| 1147 | if path == nil { |
| 1148 | return "<nil path>" |
| 1149 | } |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1150 | return path.RelativeToTop().String() |
Paul Duffin | 567465d | 2021-03-16 01:21:34 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | // PathsRelativeToTop creates a slice of strings where each string is the result of applying |
| 1154 | // PathRelativeToTop to the corresponding Path in the input slice. |
| 1155 | func PathsRelativeToTop(paths Paths) []string { |
| 1156 | var result []string |
| 1157 | for _, path := range paths { |
| 1158 | relative := PathRelativeToTop(path) |
| 1159 | result = append(result, relative) |
| 1160 | } |
| 1161 | return result |
| 1162 | } |
| 1163 | |
| 1164 | // StringPathRelativeToTop returns a string representation of the path relative to a notional top |
| 1165 | // directory. |
| 1166 | // |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1167 | // 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] | 1168 | // |
| 1169 | // This is provided for processing paths that have already been converted into a string, e.g. paths |
| 1170 | // in AndroidMkEntries structures. As a result it needs to be supplied the soong output dir against |
| 1171 | // which it can try and relativize paths. PathRelativeToTop must be used for process Path objects. |
| 1172 | func StringPathRelativeToTop(soongOutDir string, path string) string { |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1173 | ensureTestOnly() |
Paul Duffin | 567465d | 2021-03-16 01:21:34 +0000 | [diff] [blame] | 1174 | |
| 1175 | // A relative path must be a source path so leave it as it is. |
| 1176 | if !filepath.IsAbs(path) { |
| 1177 | return path |
| 1178 | } |
| 1179 | |
| 1180 | // Check to see if the path is relative to the soong out dir. |
| 1181 | rel, isRel, err := maybeRelErr(soongOutDir, path) |
| 1182 | if err != nil { |
| 1183 | panic(err) |
| 1184 | } |
| 1185 | |
| 1186 | if isRel { |
| 1187 | // The path is in the soong out dir so indicate that in the relative path. |
| 1188 | return filepath.Join("out/soong", rel) |
| 1189 | } |
| 1190 | |
| 1191 | // Check to see if the path is relative to the top level out dir. |
| 1192 | outDir := filepath.Dir(soongOutDir) |
| 1193 | rel, isRel, err = maybeRelErr(outDir, path) |
| 1194 | if err != nil { |
| 1195 | panic(err) |
| 1196 | } |
| 1197 | |
| 1198 | if isRel { |
| 1199 | // The path is in the out dir so indicate that in the relative path. |
| 1200 | return filepath.Join("out", rel) |
| 1201 | } |
| 1202 | |
| 1203 | // This should never happen. |
| 1204 | panic(fmt.Errorf("internal error: absolute path %s is not relative to the out dir %s", path, outDir)) |
| 1205 | } |
| 1206 | |
| 1207 | // StringPathsRelativeToTop creates a slice of strings where each string is the result of applying |
| 1208 | // StringPathRelativeToTop to the corresponding string path in the input slice. |
| 1209 | // |
| 1210 | // This is provided for processing paths that have already been converted into a string, e.g. paths |
| 1211 | // in AndroidMkEntries structures. As a result it needs to be supplied the soong output dir against |
| 1212 | // which it can try and relativize paths. PathsRelativeToTop must be used for process Paths objects. |
| 1213 | func StringPathsRelativeToTop(soongOutDir string, paths []string) []string { |
| 1214 | var result []string |
| 1215 | for _, path := range paths { |
| 1216 | relative := StringPathRelativeToTop(soongOutDir, path) |
| 1217 | result = append(result, relative) |
| 1218 | } |
| 1219 | return result |
| 1220 | } |
Paul Duffin | f53555d | 2021-03-29 00:21:00 +0100 | [diff] [blame] | 1221 | |
| 1222 | // StringRelativeToTop will normalize a string containing paths, e.g. ninja command, by replacing |
| 1223 | // any references to the test specific temporary build directory that changes with each run to a |
| 1224 | // fixed path relative to a notional top directory. |
| 1225 | // |
| 1226 | // This is similar to StringPathRelativeToTop except that assumes the string is a single path |
| 1227 | // containing at most one instance of the temporary build directory at the start of the path while |
| 1228 | // this assumes that there can be any number at any position. |
| 1229 | func StringRelativeToTop(config Config, command string) string { |
| 1230 | return normalizeStringRelativeToTop(config, command) |
| 1231 | } |
Paul Duffin | 0aafcbf | 2021-03-29 00:56:32 +0100 | [diff] [blame] | 1232 | |
| 1233 | // StringsRelativeToTop will return a new slice such that each item in the new slice is the result |
| 1234 | // of calling StringRelativeToTop on the corresponding item in the input slice. |
| 1235 | func StringsRelativeToTop(config Config, command []string) []string { |
| 1236 | return normalizeStringArrayRelativeToTop(config, command) |
| 1237 | } |