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