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