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